Skip to content

Commit

Permalink
chore: implement a bunch of naive unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Dec 7, 2022
1 parent 1cff78b commit b6bd8eb
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 5 deletions.
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ flake8-docstrings==1.6.0
flake8-isort==5.0.3
flake8-string-format==0.3.0
flake8-tuple==0.4.1
freezegun==1.2.2
isort==5.10.1
mock==4.0.3
pytest==7.2.0
pytest-cov==4.0.0
pytest-env==0.8.1
pytest-pylint==0.19.0
requests-mock==1.10.0
twine==4.0.2
types-requests==2.28.11.5
wheel==0.38.4
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
description-file = README.md

[tool:pytest]
addopts = --doctest-modules --cov=suisa_sendemeldung --pylint
addopts = --doctest-modules --cov=suisa_sendemeldung --pylint --cov-fail-under=100
8 changes: 4 additions & 4 deletions suisa_sendemeldung/suisa_sendemeldung.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def validate_arguments(parser, args):
parser.error("\n- " + "\n- ".join(msgs))


def get_arguments(parser: ArgumentParser):
def get_arguments(parser: ArgumentParser): # pragma: no cover
"""Create :class:`ArgumentParser` with arguments.
Arguments:
Expand Down Expand Up @@ -301,7 +301,7 @@ def get_csv(data):
artist = ", ".join([a.get("name") for a in music.get("artists")])
elif music.get("artist") is not None:
artist = music.get("artist")
elif music.get("Artist") is not None:
elif music.get("Artist") is not None: # pragma: no cover
# Uppercase is a hack needed for Jun 2021 since there is a 'wrong' entry
# in the database. Going forward the record will be available as 'artist'
# in lowercase.
Expand Down Expand Up @@ -329,7 +329,7 @@ def get_csv(data):
return csv.getvalue()


def write_csv(filename, csv):
def write_csv(filename, csv): # pragma: no cover
"""Write contents of `csv` to file.
Arguments:
Expand Down Expand Up @@ -394,7 +394,7 @@ def send_message(msg, server="127.0.0.1", login=None, password=None):
smtp.send_message(msg)


def main():
def main(): # pragma: no cover
"""Entrypoint for SUISA Sendemeldung ."""
default_config_file = basename(__file__).replace(".py", ".conf")
# config file in /etc gets overriden by the one in $HOME which gets overriden by the one in the
Expand Down
69 changes: 69 additions & 0 deletions tests/test_acrclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Tests for the ACR client module."""
from datetime import date

import requests_mock
from freezegun import freeze_time

from suisa_sendemeldung import acrclient


def test_init():
"""Test ACRClient.__init__."""
access_key = "secret-key"
with freeze_time("1993-03-02"):
acr = acrclient.ACRClient(access_key)

assert acr.access_key == access_key
assert acr.default_date == date(1993, 3, 1)


def test_get_data():
"""Test ACRClient.get_data."""
access_key = "secret-key"
stream_id = "stream-id"
data = [{"metadata": {"timestamp_utc": "1993-03-01 13:12:00"}}]
with freeze_time("1993-03-02"):
acr = acrclient.ACRClient(access_key)
with requests_mock.Mocker() as mock:
mock.get(
"https://api.acrcloud.com/v1/monitor-streams/stream-id/results", json=data
)
acr.get_data(stream_id)


def test_get_interval_data():
"""Test ACRClient.get_interval_data."""
access_key = "secret-key"
stream_id = "stream-id"
data = [{"metadata": {"timestamp_utc": "1993-03-01 13:12:00"}}]

with freeze_time("1993-03-02"):
acr = acrclient.ACRClient(access_key)
with requests_mock.Mocker() as mock:
mock.get(
"https://api.acrcloud.com/v1/monitor-streams/stream-id/results", json=data
)
acr.get_interval_data(stream_id, date(1993, 3, 1), date(1993, 3, 31))

# ahead of UTC
with freeze_time("1993-03-02"):
acr = acrclient.ACRClient(access_key)
with requests_mock.Mocker() as mock:
data[0]["metadata"]["timestamp_utc"] = "1993-03-01 00:00:00"
mock.get(
"https://api.acrcloud.com/v1/monitor-streams/stream-id/results", json=data
)
acr.get_interval_data(
stream_id, date(1993, 3, 1), date(1993, 3, 31), "Europe/Zurich"
)

# behind UTC
with freeze_time("1993-03-02"):
acr = acrclient.ACRClient(access_key)
with requests_mock.Mocker() as mock:
mock.get(
"https://api.acrcloud.com/v1/monitor-streams/stream-id/results", json=data
)
acr.get_interval_data(
stream_id, date(1993, 3, 1), date(1993, 3, 31), "America/Nuuk"
)
Loading

0 comments on commit b6bd8eb

Please sign in to comment.