Skip to content

Commit

Permalink
Merge pull request #1 from prajjwalyd/url-shortener-test
Browse files Browse the repository at this point in the history
added test for url-shortener
  • Loading branch information
prajjwalyd committed Jul 12, 2024
2 parents 6f123a4 + c094e99 commit 51fd33c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from app import app

@pytest.fixture
def client():
with app.test_client() as client:
yield client

def test_create_entry(client):
response = client.post('/create', json={'long_url': 'https://example.com'})
data = response.get_json()
assert response.status_code == 200
assert 'short_url' in data
assert data['long_url'] == 'https://example.com'

def test_redirect_url(client):
# Assuming there is a way to set up a known state or mock DB
client.post('/create', json={'long_url': 'https://example.com', 'custom_url': 'test'})
response = client.get('/test')
assert response.status_code == 302
assert response.location == 'https://example.com'

def test_qr_code_generation(client):
client.post('/create', json={'long_url': 'https://example.com', 'custom_url': 'test', 'generate_qr': True})
response = client.get('/qr/test')
assert response.status_code == 200
assert response.mimetype == 'image/png'

def test_get_analytics(client):
response = client.get('/test/analytics')
assert response.status_code == 200
2 changes: 2 additions & 0 deletions url-shortener/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.pytest_cache/
__pycache__/
5 changes: 4 additions & 1 deletion url-shortener/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, abort
import random
import string

Expand All @@ -11,6 +11,9 @@ def generate_short_url():
@app.route('/shorten', methods=['POST'])
def shorten_url():
data = request.get_json()
if not data or 'long_url' not in data:
abort(400, description="Invalid request: 'long_url' is required")

long_url = data['long_url']
custom_url = data.get('custom_url')

Expand Down
31 changes: 31 additions & 0 deletions url-shortener/test_url_shortener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
import json
from app import app

@pytest.fixture
def client():
app.testing = True
with app.test_client() as client:
yield client

def test_shorten_url(client):
# Test shortening a URL without a custom URL
response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com'}), content_type='application/json')
data = json.loads(response.data)
assert response.status_code == 200
assert 'short_url' in data
assert data['long_url'] == 'https://example.com'
assert len(data['short_url']) == 6

def test_shorten_url_with_custom(client):
# Test shortening a URL with a custom URL
response = client.post('/shorten', data=json.dumps({'long_url': 'https://example.com', 'custom_url': 'custom123'}), content_type='application/json')
data = json.loads(response.data)
assert response.status_code == 200
assert data['short_url'] == 'custom123'
assert data['long_url'] == 'https://example.com'

def test_invalid_request(client):
# Test handling of invalid requests
response = client.post('/shorten', data=json.dumps({'invalid_key': 'https://example.com'}), content_type='application/json')
assert response.status_code == 400

0 comments on commit 51fd33c

Please sign in to comment.