-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from prajjwalyd/url-shortener-test
added test for url-shortener
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.pytest_cache/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |