Skip to content

Commit

Permalink
Merge pull request #21 from TheTedLab/unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
TheTedLab authored Mar 29, 2024
2 parents 792e2f3 + b5c541d commit 0e67904
Show file tree
Hide file tree
Showing 42 changed files with 11,800 additions and 109 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Python Unit Tests
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: true

jobs:
pytest:
name: pytest
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests with pytest
run: |
pytest -m unit
coverage-badge -o tests/docs/coverage.svg
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifacts
uses: actions/upload-pages-artifact@v1
with:
path: tests/docs
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
![spotistats_logo](https://github.com/TheTedLab/SpotiStats/assets/71270225/2fe17957-f07f-4e0b-80b6-2e27943d14d5)

[![unit tests workflow](https://github.com/TheTedLab/SpotiStats/actions/workflows/unit-tests.yaml/badge.svg)](https://github.com/TheTedLab/SpotiStats/actions/workflows/unit-tests.yaml)
[![coverage status](https://thetedlab.github.io/SpotiStats/coverage.svg)](https://thetedlab.github.io/SpotiStats/index.html)

## Table of contents
* [Introduction](https://github.com/TheTedLab/SpotiStats#introduction)
* [Contributors](https://github.com/TheTedLab/SpotiStats#contributors)
Expand Down
9 changes: 9 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[pytest]
testpaths = tests
addopts =
-rsxX -l --tb=auto --strict-markers
--cov-config=tests/.coveragerc --cov=src/aggregator --cov-report html:tests/docs --cov-report term
markers =
unit: Run the unit test functions
env_files =
tests/.env
51 changes: 51 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
astroid==2.15.8
certifi==2023.11.17
charset-normalizer==3.3.2
coverage==7.4.4
coverage-badge==1.1.0
dill==0.3.8
dodgy==0.2.1
flake8==5.0.4
flake8-polyfill==1.0.2
gitdb==4.0.11
GitPython==3.1.42
idna==3.6
iniconfig==2.0.0
isort==5.13.2
lazy-object-proxy==1.10.0
mccabe==0.7.0
mypy==1.8.0
mypy-extensions==1.0.0
packaging==23.2
pep8-naming==0.10.0
platformdirs==4.2.0
pluggy==1.4.0
prospector==1.10.3
pycodestyle==2.9.1
pydocstyle==6.3.0
pyflakes==2.5.0
pylint==2.17.7
pylint-celery==0.3
pylint-django==2.5.3
pylint-flask==0.6
pylint-plugin-utils==0.7
pytest==8.1.1
pytest-cov==4.1.0
pytest-dotenv==0.5.2
pytest-mock==3.12.0
python-dotenv==1.0.1
PyYAML==6.0.1
requests==2.31.0
requests-mock==1.11.0
requirements-detector==1.2.2
semver==3.0.2
setoptconf-tmp==0.3.1
six==1.16.0
smmap==5.0.1
snowballstemmer==2.2.0
toml==0.10.2
tomlkit==0.12.4
types-requests==2.31.0.20240218
typing_extensions==4.10.0
urllib3==2.1.0
wrapt==1.16.0
Empty file added src/__init__.py
Empty file.
37 changes: 10 additions & 27 deletions src/aggregator/artist_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"""Module for additional aggregator to collect related artist data from Spotify."""
import json
import requests
from auth_credentials import client_id, client_secret

token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
}
import requests


def get_related_artists(artists_ids, headers, stage=0):
Expand Down Expand Up @@ -41,24 +35,10 @@ def get_related_artists(artists_ids, headers, stage=0):
return artists_ids_list


def main():
def artist_aggregate_main(source_file_path, id_file_path, headers):
"""Main function for data aggregation."""
response = requests.post(
'https://accounts.spotify.com/api/token',
data=token_data,
timeout=10
)

access_token = response.json().get('access_token')
token_type = response.json().get('token_type')

headers = {
'Authorization': f'{token_type} {access_token}',
}

followed_file_name = 'spotify-followed-artists.json'
with open(
f'src/aggregator/resources/{followed_file_name}',
source_file_path,
mode='r',
encoding='utf-8'
) as file:
Expand Down Expand Up @@ -86,14 +66,17 @@ def main():
ensure_ascii=False
)

id_file_name = 'artists-ids-list.json'
with open(
f'src/aggregator/resources/{id_file_name}',
id_file_path,
mode='w',
encoding='utf-8'
) as file:
file.write(json_string)


if __name__ == '__main__':
main()
# if __name__ == '__main__':
# artist_aggregate_main(
# source_file_path='src/aggregator/resources/spotify-followed-artists.json',
# id_file_path='src/aggregator/resources/artists-ids-list.json',
# headers={'Authorization': f'{token_type} {access_token}'}
# )
2 changes: 2 additions & 0 deletions src/aggregator/classes/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def __init__(self, album_name, album_id, album_type, release_date, genres, label
self.genres = genres
self.label = label
self.popularity = popularity
if type(album_name) is not str:
raise TypeError("Album name must be string.")

def __str__(self):
return f'{self.album_name} {self.album_id} {self.album_type}' \
Expand Down
2 changes: 2 additions & 0 deletions src/aggregator/classes/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def __init__(self, artist_id, name, popularity, followers, genres, albums=None,
self.genres = genres
self.albums = albums
self.tracks = tracks
if type(name) is not str:
raise TypeError("Artist name must be string.")

def __str__(self):
return f'{self.artist_id} {self.name} {self.popularity}' \
Expand Down
2 changes: 2 additions & 0 deletions src/aggregator/classes/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, track_name, track_id, artists, albums, duration_ms, explicit,
self.tempo = tempo
self.time_signature = time_signature
self.valence = valence
if type(track_name) is not str:
raise TypeError("Track name must be string.")

def __str__(self):
return f'{self.track_name} {self.track_id} {self.artists} {self.albums}' \
Expand Down
Empty file.
156 changes: 156 additions & 0 deletions src/aggregator/resources/albums/album-1e0tqn3caRoNqZU6YosOLa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"data": {
"albumUnion": {
"tracks": {
"items": [
{
"track": {
"artists": {
"items": [
{
"profile": {
"name": "M|O|O|N"
},
"uri": "spotify:artist:0M2HHtY3OOQzIZxrHkbJLT"
}
]
},
"associations": {
"associatedVideos": {
"totalCount": 0
}
},
"contentRating": {
"label": "NONE"
},
"discNumber": 1,
"duration": {
"totalMilliseconds": 271200
},
"name": "Paris",
"playability": {
"playable": false
},
"playcount": "14682819",
"relinkingInformation": null,
"saved": false,
"trackNumber": 1,
"uri": "spotify:track:5uDzCEn4Tzv9qKWiYNyTaQ"
},
"uid": "26980fac60e866a354c1"
},
{
"track": {
"artists": {
"items": [
{
"profile": {
"name": "M|O|O|N"
},
"uri": "spotify:artist:0M2HHtY3OOQzIZxrHkbJLT"
}
]
},
"associations": {
"associatedVideos": {
"totalCount": 0
}
},
"contentRating": {
"label": "NONE"
},
"discNumber": 1,
"duration": {
"totalMilliseconds": 289714
},
"name": "Crystals",
"playability": {
"playable": false
},
"playcount": "16249642",
"relinkingInformation": null,
"saved": false,
"trackNumber": 2,
"uri": "spotify:track:3KwzRfEeKhK5ADT9iPFHe2"
},
"uid": "49bcfa252bde9bee0693"
},
{
"track": {
"artists": {
"items": [
{
"profile": {
"name": "M|O|O|N"
},
"uri": "spotify:artist:0M2HHtY3OOQzIZxrHkbJLT"
}
]
},
"associations": {
"associatedVideos": {
"totalCount": 0
}
},
"contentRating": {
"label": "NONE"
},
"discNumber": 1,
"duration": {
"totalMilliseconds": 289523
},
"name": "Hydrogen",
"playability": {
"playable": false
},
"playcount": "28560078",
"relinkingInformation": null,
"saved": false,
"trackNumber": 3,
"uri": "spotify:track:6vc7GNW3FxMkgKuProVOoB"
},
"uid": "412f7ff7ebee3d5b65fd"
},
{
"track": {
"artists": {
"items": [
{
"profile": {
"name": "M|O|O|N"
},
"uri": "spotify:artist:0M2HHtY3OOQzIZxrHkbJLT"
}
]
},
"associations": {
"associatedVideos": {
"totalCount": 0
}
},
"contentRating": {
"label": "NONE"
},
"discNumber": 1,
"duration": {
"totalMilliseconds": 362222
},
"name": "Release",
"playability": {
"playable": false
},
"playcount": "6102854",
"relinkingInformation": null,
"saved": false,
"trackNumber": 4,
"uri": "spotify:track:7IwoJJ2bmzQULYOE89POMU"
},
"uid": "3866198027f9fc814328"
}
],
"totalCount": 4
}
}
},
"extensions": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2014,17 +2014,17 @@
"country": "FR",
"region": "IDF"
},
{
"numberOfListeners": 4031,
"city": "Kyiv",
"country": "UA",
"region": "30"
},
{
"numberOfListeners": 3660,
"city": "London",
"country": "GB",
"region": "ENG"
},
{
"numberOfListeners": 3424,
"city": "Minsk",
"country": "BY",
"region": "HM"
}
]
}
Loading

0 comments on commit 0e67904

Please sign in to comment.