Add files via upload
This commit is contained in:
parent
8e53f0038d
commit
f216cf05c5
27 changed files with 29565 additions and 0 deletions
0
api/tests/unit/__init__.py
Normal file
0
api/tests/unit/__init__.py
Normal file
127
api/tests/unit/conftest.py
Normal file
127
api/tests/unit/conftest.py
Normal file
|
@ -0,0 +1,127 @@
|
|||
import pytest
|
||||
import faker
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
|
||||
from app.main import app as flask_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake():
|
||||
return faker.Faker()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
flask_app.config['TESTING'] = True
|
||||
|
||||
with flask_app.test_client() as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def lastfm_response():
|
||||
return {
|
||||
"recenttracks": {
|
||||
"@attr": {
|
||||
"page": "1",
|
||||
"perPage": "1",
|
||||
"total": "229279",
|
||||
"totalPages": "229279",
|
||||
"user": "biahll"
|
||||
},
|
||||
"track": [
|
||||
{
|
||||
"@attr": {
|
||||
"nowplaying": "true"
|
||||
},
|
||||
"album": {
|
||||
"#text": "My My My!",
|
||||
"mbid": ""
|
||||
},
|
||||
"artist": {
|
||||
"#text": "Mother's Daughter",
|
||||
"mbid": ""
|
||||
},
|
||||
"image": [
|
||||
{
|
||||
"#text": "https: //lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",
|
||||
"size": "small"
|
||||
},
|
||||
{
|
||||
"#text": "https: //lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",
|
||||
"size": "medium"
|
||||
},
|
||||
{
|
||||
"#text": "https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",
|
||||
"size": "large"
|
||||
},
|
||||
{
|
||||
"#text": "https: //lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",
|
||||
"size": "extralarge"
|
||||
}
|
||||
],
|
||||
"mbid": "",
|
||||
"name": "My My My!",
|
||||
"streamable": "0",
|
||||
"url": "https: //www.last.fm/music/Mother%27s+Daughter/_/My+My+My!"
|
||||
},
|
||||
{
|
||||
"album": {
|
||||
"#text": "Glory Days (Japan Edition)",
|
||||
"mbid": ""
|
||||
},
|
||||
"artist": {
|
||||
"#text": "Little Mix",
|
||||
"mbid": "38f59974-2f4d-4bfa-b2e3-d2696de1b675"
|
||||
},
|
||||
"date": {
|
||||
"#text": "16 Jul 2020, 22: 30",
|
||||
"uts": "1594938632"
|
||||
},
|
||||
"image": [
|
||||
{
|
||||
"#text": "https: //lastfm.freetls.fastly.net/i/u/34s/9e824c0b80bbf5c24807f22d081566c4.jpg",
|
||||
"size": "small"
|
||||
},
|
||||
{
|
||||
"#text": "https://lastfm.freetls.fastly.net/i/u/64s/9e824c0b80bbf5c24807f22d081566c4.jpg",
|
||||
"size": "medium"
|
||||
},
|
||||
{
|
||||
"#text": "https://lastfm.freetls.fastly.net/i/u/174s/9e824c0b80bbf5c24807f22d081566c4.jpg",
|
||||
"size": "large"
|
||||
},
|
||||
{
|
||||
"#text": "https: //lastfm.freetls.fastly.net/i/u/300x300/9e824c0b80bbf5c24807f22d081566c4.jpg",
|
||||
"size": "extralarge"
|
||||
}
|
||||
],
|
||||
"mbid": "02930d7b-3e67-41c5-9d6e-ad593228d441",
|
||||
"name": "Shout Out to My Ex",
|
||||
"streamable": "0",
|
||||
"url": "https://www.last.fm/music/Little+Mix/_/Shout+Out+to+My+Ex"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def lastfm_empty_response():
|
||||
return {
|
||||
"recenttracks": {
|
||||
"@attr": {
|
||||
"page": "1",
|
||||
"perPage": "1",
|
||||
"total": "1",
|
||||
"totalPages": "1",
|
||||
"user": "biahll"
|
||||
},
|
||||
"track": []
|
||||
}
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def lastfm_no_recent_tracks_response():
|
||||
return {}
|
0
api/tests/unit/healthcheck/__init__.py
Normal file
0
api/tests/unit/healthcheck/__init__.py
Normal file
8
api/tests/unit/healthcheck/test_healthcheck.py
Normal file
8
api/tests/unit/healthcheck/test_healthcheck.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
import os
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def test_get_healthcheck(client):
|
||||
rv = client.get('/')
|
||||
assert rv.status_code == 200
|
0
api/tests/unit/songs/__init__.py
Normal file
0
api/tests/unit/songs/__init__.py
Normal file
69
api/tests/unit/songs/test_latest_songs.py
Normal file
69
api/tests/unit/songs/test_latest_songs.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
import os
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": ''})
|
||||
def test_without_api_key(client):
|
||||
rv = client.get('/user/latest-song')
|
||||
assert rv.json['message'] == 'INTERNAL_ERROR'
|
||||
assert rv.status_code == 500
|
||||
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": 'something new'})
|
||||
@patch('requests.get', side_effect=[Exception()])
|
||||
def test_get_with_exception(requests, client):
|
||||
rv = client.get('/user/latest-song')
|
||||
assert rv.json['message'] == 'INTERNAL_ERROR'
|
||||
assert rv.status_code == 500
|
||||
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": 'something old'})
|
||||
@patch('requests.get')
|
||||
def test_get_with_success(mock_get, client, lastfm_response):
|
||||
mock_get.return_value.status_code = 500
|
||||
mock_get.return_value.json.return_value = lastfm_response
|
||||
rv = client.get('/user/latest-song')
|
||||
assert rv.json == {'track': lastfm_response['recenttracks']['track'][0]}
|
||||
assert rv.status_code == mock_get.return_value.status_code
|
||||
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": 'something old'})
|
||||
@patch('requests.get')
|
||||
def test_get_with_success_and_different_format(mock_get, client, lastfm_response):
|
||||
mock_get.return_value.status_code = 200
|
||||
mock_get.return_value.json.return_value = lastfm_response
|
||||
rv = client.get('/user/latest-song?format=shields.io')
|
||||
song = lastfm_response['recenttracks']['track'][0]['name']
|
||||
artist = lastfm_response['recenttracks']['track'][0]['artist']['#text']
|
||||
|
||||
assert rv.json == {
|
||||
'schemaVersion': 1,
|
||||
'label': 'Last.FM Last Played Song',
|
||||
'message': f"{song} - {artist}"
|
||||
}
|
||||
assert rv.status_code == mock_get.return_value.status_code
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": 'something old'})
|
||||
@patch('requests.get')
|
||||
def test_get_with_success_no_tracks(mock_get, client, lastfm_empty_response):
|
||||
mock_get.return_value.status_code = 200
|
||||
mock_get.return_value.json.return_value = lastfm_empty_response
|
||||
rv = client.get('/user/latest-song')
|
||||
|
||||
assert rv.status_code == 200
|
||||
assert rv.json == {
|
||||
'message': 'NO_TRACKS_FOUND'
|
||||
}
|
||||
|
||||
@patch.dict(os.environ, {"LASTFM_API_KEY": 'something old'})
|
||||
@patch('requests.get')
|
||||
def test_get_with_error_user_not_found_tracks(mock_get, client, lastfm_no_recent_tracks_response):
|
||||
mock_get.return_value.status_code = 200
|
||||
mock_get.return_value.json.return_value = lastfm_no_recent_tracks_response
|
||||
rv = client.get('/user/latest-song')
|
||||
|
||||
assert rv.status_code == 404
|
||||
assert rv.json == {
|
||||
'message': 'USER_LIKELY_DOESNT_EXIST'
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue