diff --git a/matomo-api/poetry.lock b/matomo-api/poetry.lock index 7dbfa5d..be8c31b 100644 --- a/matomo-api/poetry.lock +++ b/matomo-api/poetry.lock @@ -44,6 +44,17 @@ python-versions = ">=3.7" [package.extras] test = ["pytest (>=6)"] +[[package]] +name = "freezegun" +version = "1.2.2" +description = "Let your Python tests travel through time" +category = "main" +optional = false +python-versions = ">=3.6" + +[package.dependencies] +python-dateutil = ">=2.7" + [[package]] name = "greenlet" version = "2.0.2" @@ -139,6 +150,17 @@ python-versions = "*" pytest = ">=3.6.0" vcrpy = "*" +[[package]] +name = "python-dateutil" +version = "2.8.2" +description = "Extensions to the standard Python datetime module" +category = "main" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" + +[package.dependencies] +six = ">=1.5" + [[package]] name = "pyyaml" version = "6.0.1" @@ -275,7 +297,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "d442dbd6e64245ee00797e619b06a79729f65ea001ddecf173a307d19b55b2e8" +content-hash = "4ec38968c1b5eaf92067783bb4f5ad4c4c2657479f287cad195e9dab6672d7a7" [metadata.files] certifi = [ @@ -371,6 +393,10 @@ exceptiongroup = [ {file = "exceptiongroup-1.1.3-py3-none-any.whl", hash = "sha256:343280667a4585d195ca1cf9cef84a4e178c4b6cf2274caef9859782b567d5e3"}, {file = "exceptiongroup-1.1.3.tar.gz", hash = "sha256:097acd85d473d75af5bb98e41b61ff7fe35efe6675e4f9370ec6ec5126d160e9"}, ] +freezegun = [ + {file = "freezegun-1.2.2-py3-none-any.whl", hash = "sha256:ea1b963b993cb9ea195adbd893a48d573fda951b0da64f60883d7e988b606c9f"}, + {file = "freezegun-1.2.2.tar.gz", hash = "sha256:cd22d1ba06941384410cd967d8a99d5ae2442f57dfafeff2fda5de8dc5c05446"}, +] greenlet = [ {file = "greenlet-2.0.2-cp27-cp27m-macosx_10_14_x86_64.whl", hash = "sha256:bdfea8c661e80d3c1c99ad7c3ff74e6e87184895bbaca6ee8cc61209f8b9b85d"}, {file = "greenlet-2.0.2-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:9d14b83fab60d5e8abe587d51c75b252bcc21683f24699ada8fb275d7712f5a9"}, @@ -550,6 +576,10 @@ pytest-vcr = [ {file = "pytest-vcr-1.0.2.tar.gz", hash = "sha256:23ee51b75abbcc43d926272773aae4f39f93aceb75ed56852d0bf618f92e1896"}, {file = "pytest_vcr-1.0.2-py2.py3-none-any.whl", hash = "sha256:2f316e0539399bea0296e8b8401145c62b6f85e9066af7e57b6151481b0d6d9c"}, ] +python-dateutil = [ + {file = "python-dateutil-2.8.2.tar.gz", hash = "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86"}, + {file = "python_dateutil-2.8.2-py2.py3-none-any.whl", hash = "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9"}, +] pyyaml = [ {file = "PyYAML-6.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d858aa552c999bc8a8d57426ed01e40bef403cd8ccdd0fc5f6f04a00414cac2a"}, {file = "PyYAML-6.0.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fd66fc5d0da6d9815ba2cebeb4205f95818ff4b79c3ebe268e75d961704af52f"}, diff --git a/matomo-api/pyproject.toml b/matomo-api/pyproject.toml index d5ef456..d7745d9 100644 --- a/matomo-api/pyproject.toml +++ b/matomo-api/pyproject.toml @@ -16,6 +16,7 @@ pytest = "^7.4.2" pytest-vcr = "^1.0.2" psycopg2 = "^2.9.8" urllib3 = "1.26.7" +freezegun = "^1.2.2" [tool.poetry.dev-dependencies] diff --git a/matomo-api/src/api.py b/matomo-api/src/api.py index 7795505..6ba3017 100644 --- a/matomo-api/src/api.py +++ b/matomo-api/src/api.py @@ -17,7 +17,7 @@ def __init__( db_password: str = None, db_host: str = None, db_name: str = None, - db_port: int = None + db_port: int = None, ) -> None: self.base_url = base_url or os.environ.get("MATOMO_BASE_URL") self.auth_token = auth_token or os.environ.get("MATOMO_AUTH_TOKEN") @@ -101,26 +101,3 @@ def fetch_inspire_statistics(self, date: date) -> None: session.add(data_entry) session.commit() session.close() - -from sqlalchemy import create_engine -from sqlalchemy.exc import OperationalError - -def test_connection(db_name, db_password, db_host): - try: - # Construct the database URL - database_url = f"postgresql://{db_name}:{db_password}@{db_host}:5432" - - # Create the engine - engine = create_engine(database_url) - - # Try to connect to the database - engine.connect() - - # Connection successful - return True - except OperationalError as e: - # Connection failed - print(f"Connection error: {e}") - return False - -# Example usage: diff --git a/matomo-api/src/cli.py b/matomo-api/src/cli.py index 41ade64..e6258ab 100644 --- a/matomo-api/src/cli.py +++ b/matomo-api/src/cli.py @@ -1,4 +1,4 @@ -from datetime import date, datetime, timedelta +from datetime import datetime, timedelta import click from api import MatomoAPI @@ -17,7 +17,7 @@ def fetch_matomo_inspire_data(date: str): click.echo(f"Fetching data for date {date}") if isinstance(date, str): try: - date = datetime.strptime(date, "%Y-%M-%d") + date = datetime.strptime(date, "%Y-%m-%d") except ValueError: click.echo("Wrong date format! Aborting.") return diff --git a/matomo-api/tests/cassettes/test_cli.yaml b/matomo-api/tests/cassettes/test_cli.yaml index 32fadde..f6539c9 100644 --- a/matomo-api/tests/cassettes/test_cli.yaml +++ b/matomo-api/tests/cassettes/test_cli.yaml @@ -11,7 +11,7 @@ interactions: User-Agent: - python-requests/2.31.0 method: GET - uri: https://webanalytics.web.cern.ch/index.php?date=2022-01-02+00%3A01%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day + uri: https://webanalytics.web.cern.ch/index.php?date=2022-01-02+00%3A00%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day response: body: string: '{"value":0}' @@ -28,7 +28,7 @@ interactions: content-type: - application/json; charset=utf-8 date: - - Mon, 02 Oct 2023 09:05:46 GMT + - Fri, 06 Oct 2023 11:57:46 GMT server: - Apache/2.4.57 (Debian) set-cookie: @@ -37,7 +37,7 @@ interactions: vary: - Origin x-matomo-request-id: - - 317a5 + - 698dd x-powered-by: - PHP/8.1.23 status: @@ -55,7 +55,7 @@ interactions: User-Agent: - python-requests/2.31.0 method: GET - uri: https://webanalytics.web.cern.ch/index.php?date=2022-01-02+00%3A01%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day + uri: https://webanalytics.web.cern.ch/index.php?date=2022-01-02+00%3A00%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day response: body: string: '{"value":0}' @@ -72,7 +72,7 @@ interactions: content-type: - application/json; charset=utf-8 date: - - Mon, 02 Oct 2023 09:05:47 GMT + - Fri, 06 Oct 2023 11:57:47 GMT server: - Apache/2.4.57 (Debian) set-cookie: @@ -81,7 +81,7 @@ interactions: vary: - Origin x-matomo-request-id: - - 7f2e7 + - a78fb x-powered-by: - PHP/8.1.23 status: diff --git a/matomo-api/tests/cassettes/test_cli_default_arguments.yaml b/matomo-api/tests/cassettes/test_cli_default_arguments.yaml index 51da986..ca17d70 100644 --- a/matomo-api/tests/cassettes/test_cli_default_arguments.yaml +++ b/matomo-api/tests/cassettes/test_cli_default_arguments.yaml @@ -11,10 +11,10 @@ interactions: User-Agent: - python-requests/2.31.0 method: GET - uri: https://webanalytics.web.cern.ch/index.php?date=2023-01-04+00%3A10%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day + uri: https://webanalytics.web.cern.ch/index.php?date=2023-10-05+00%3A00%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day response: body: - string: '{"value":24}' + string: '{"value":5}' headers: access-control-allow-credentials: - 'false' @@ -24,11 +24,11 @@ interactions: - must-revalidate - private content-length: - - '12' + - '11' content-type: - application/json; charset=utf-8 date: - - Thu, 05 Oct 2023 13:49:49 GMT + - Fri, 06 Oct 2023 11:57:19 GMT server: - Apache/2.4.57 (Debian) set-cookie: @@ -37,7 +37,7 @@ interactions: vary: - Origin x-matomo-request-id: - - d4116 + - 290ed x-powered-by: - PHP/8.1.23 status: @@ -55,10 +55,10 @@ interactions: User-Agent: - python-requests/2.31.0 method: GET - uri: https://webanalytics.web.cern.ch/index.php?date=2023-01-04+00%3A10%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day + uri: https://webanalytics.web.cern.ch/index.php?date=2023-10-05+00%3A00%3A00&format=json&method=VisitsSummary.getVisits&module=API&period=day response: body: - string: '{"value":24}' + string: '{"value":5}' headers: access-control-allow-credentials: - 'false' @@ -68,11 +68,11 @@ interactions: - must-revalidate - private content-length: - - '12' + - '11' content-type: - application/json; charset=utf-8 date: - - Thu, 05 Oct 2023 13:49:49 GMT + - Fri, 06 Oct 2023 11:57:20 GMT server: - Apache/2.4.57 (Debian) set-cookie: @@ -81,7 +81,7 @@ interactions: vary: - Origin x-matomo-request-id: - - 8fa2e + - f0413 x-powered-by: - PHP/8.1.23 status: diff --git a/matomo-api/tests/conftest.py b/matomo-api/tests/conftest.py index 10be242..792e55d 100644 --- a/matomo-api/tests/conftest.py +++ b/matomo-api/tests/conftest.py @@ -43,7 +43,7 @@ def updated_env(monkeypatch): DB_NAME="matomo", DB_PASSWORD="matomo", DB_USER="matomo", - DB_PORT=5432 + DB_PORT=5432, ) for env_name, env_value in matomo_api_kwargs.items(): monkeypatch.setenv(env_name, env_value) diff --git a/matomo-api/tests/test_api.py b/matomo-api/tests/test_api.py index 49938fa..24f0adc 100644 --- a/matomo-api/tests/test_api.py +++ b/matomo-api/tests/test_api.py @@ -2,7 +2,6 @@ import pytest - TEST_DAY = day = datetime.strptime("2023-10-02", "%Y-%m-%d") diff --git a/matomo-api/tests/test_cli.py b/matomo-api/tests/test_cli.py index 726d443..5834773 100644 --- a/matomo-api/tests/test_cli.py +++ b/matomo-api/tests/test_cli.py @@ -1,6 +1,7 @@ import pytest from cli import fetch_matomo_inspire_data from click.testing import CliRunner +from freezegun import freeze_time @pytest.mark.vcr( @@ -17,7 +18,8 @@ def test_cli(db): filter_headers=["authorization", "Set-Cookie"], filter_query_parameters=["token_auth", "idSite"], ) +@freeze_time("2023-10-05") def test_cli_default_arguments(db): runner = CliRunner() result = runner.invoke(fetch_matomo_inspire_data, []) - assert result.exit_code == 0 \ No newline at end of file + assert result.exit_code == 0