Skip to content

Commit

Permalink
update cli & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MJedr committed Oct 6, 2023
1 parent 0a25e38 commit d68487b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 46 deletions.
32 changes: 31 additions & 1 deletion matomo-api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions matomo-api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
25 changes: 1 addition & 24 deletions matomo-api/src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
4 changes: 2 additions & 2 deletions matomo-api/src/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date, datetime, timedelta
from datetime import datetime, timedelta

import click
from api import MatomoAPI
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions matomo-api/tests/cassettes/test_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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:
Expand All @@ -37,7 +37,7 @@ interactions:
vary:
- Origin
x-matomo-request-id:
- 317a5
- 698dd
x-powered-by:
- PHP/8.1.23
status:
Expand All @@ -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}'
Expand All @@ -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:
Expand All @@ -81,7 +81,7 @@ interactions:
vary:
- Origin
x-matomo-request-id:
- 7f2e7
- a78fb
x-powered-by:
- PHP/8.1.23
status:
Expand Down
20 changes: 10 additions & 10 deletions matomo-api/tests/cassettes/test_cli_default_arguments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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:
Expand All @@ -37,7 +37,7 @@ interactions:
vary:
- Origin
x-matomo-request-id:
- d4116
- 290ed
x-powered-by:
- PHP/8.1.23
status:
Expand All @@ -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'
Expand All @@ -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:
Expand All @@ -81,7 +81,7 @@ interactions:
vary:
- Origin
x-matomo-request-id:
- 8fa2e
- f0413
x-powered-by:
- PHP/8.1.23
status:
Expand Down
2 changes: 1 addition & 1 deletion matomo-api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion matomo-api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest


TEST_DAY = day = datetime.strptime("2023-10-02", "%Y-%m-%d")


Expand Down
4 changes: 3 additions & 1 deletion matomo-api/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
assert result.exit_code == 0

0 comments on commit d68487b

Please sign in to comment.