Skip to content

Commit

Permalink
Refactor tests to use fixtures (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelletran-codecov authored Oct 21, 2024
1 parent e7432b4 commit 2ae6c6d
Show file tree
Hide file tree
Showing 5 changed files with 564 additions and 574 deletions.
2 changes: 1 addition & 1 deletion database/tests/factories/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Meta:
).hexdigest()
)
ci_passed = True
pullid = 1
pullid = None
timestamp = datetime(2019, 2, 1, 17, 59, 47)
author = factory.SubFactory(OwnerFactory)
repository = factory.SubFactory(RepositoryFactory)
Expand Down
59 changes: 31 additions & 28 deletions tasks/tests/unit/test_brolly_stats_rollup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,42 @@
import uuid

import httpx
import pytest
import respx
from mock import Mock, patch

from database.models import Constants
from database.tests.factories import (
CommitFactory,
ConstantsFactory,
ReportFactory,
RepositoryFactory,
UploadFactory,
UserFactory,
)
from tasks.brolly_stats_rollup import DEFAULT_BROLLY_ENDPOINT, BrollyStatsRollupTask


@pytest.fixture
def version(dbsession) -> str:
version = dbsession.query(Constants).filter_by(key="version").scalar()
if version is None:
version = ConstantsFactory.create(key="version", value="hello")
dbsession.add(version)
dbsession.flush()
return version


@pytest.fixture
def install_id(dbsession) -> int:
install_id = dbsession.query(Constants).filter_by(key="install_id").scalar()
if install_id is None:
install_id = ConstantsFactory.create(key="install_id", value=str(uuid.uuid4()))
dbsession.add(install_id)
dbsession.flush()
return install_id


def _get_n_hours_ago(n):
return datetime.datetime.now() - datetime.timedelta(hours=n)

Expand All @@ -38,17 +60,15 @@ def test_get_min_seconds_interval_between_executions(self, dbsession):
)

@patch("tasks.brolly_stats_rollup.get_config", return_value=False)
def test_run_cron_task_while_disabled(self, mocker, dbsession):
task = BrollyStatsRollupTask()

def test_run_cron_task_while_disabled(self, dbsession):
result = BrollyStatsRollupTask().run_cron_task(dbsession)
assert result == {
"uploaded": False,
"reason": "telemetry disabled in codecov.yml",
}

@respx.mock
def test_run_cron_task_http_ok(self, mocker, dbsession):
def test_run_cron_task_http_ok(self, dbsession, install_id, version):
users = [UserFactory.create(name=name) for name in ("foo", "bar", "baz")]
for user in users:
dbsession.add(user)
Expand All @@ -73,8 +93,9 @@ def test_run_cron_task_http_ok(self, mocker, dbsession):
for commit in commits:
dbsession.add(commit)

report = ReportFactory.create(commit=commits[0])
uploads = [
UploadFactory.create(created_at=created_at, report=None)
UploadFactory.create(created_at=created_at, report=report)
for created_at in (
_get_n_hours_ago(5),
_get_n_hours_ago(16),
Expand All @@ -84,17 +105,12 @@ def test_run_cron_task_http_ok(self, mocker, dbsession):
for upload in uploads:
dbsession.add(upload)

install_id = ConstantsFactory.create(key="install_id", value=str(uuid.uuid4()))
version = ConstantsFactory.create(key="version", value="hello")
dbsession.add(install_id)
dbsession.add(version)
dbsession.flush()

install_id_val = dbsession.query(Constants).get("install_id").value
version_val = dbsession.query(Constants).get("version").value
print("mattmatt", install_id_val, version_val)

dbsession.flush()

mock_request = respx.post(DEFAULT_BROLLY_ENDPOINT).mock(
return_value=httpx.Response(200)
)
Expand All @@ -117,18 +133,10 @@ def test_run_cron_task_http_ok(self, mocker, dbsession):
}

@respx.mock
def test_run_cron_task_not_ok(self, mocker, dbsession):
def test_run_cron_task_not_ok(self, dbsession, install_id, version):
mock_request = respx.post(DEFAULT_BROLLY_ENDPOINT).mock(
return_value=httpx.Response(500)
)

install_id = ConstantsFactory.create(key="install_id", value=str(uuid.uuid4()))
version = ConstantsFactory.create(key="version", value="hello")
dbsession.add(install_id)
dbsession.add(version)

dbsession.flush()

task = BrollyStatsRollupTask()
result = task.run_cron_task(dbsession)
assert mock_request.called
Expand All @@ -146,7 +154,9 @@ def test_run_cron_task_not_ok(self, mocker, dbsession):
}

@respx.mock
def test_run_cron_task_include_admin_email_if_populated(self, mocker, dbsession):
def test_run_cron_task_include_admin_email_if_populated(
self, mocker, dbsession, install_id, version
):
mock_request = respx.post(DEFAULT_BROLLY_ENDPOINT).mock(
return_value=httpx.Response(200)
)
Expand All @@ -155,13 +165,6 @@ def test_run_cron_task_include_admin_email_if_populated(self, mocker, dbsession)
BrollyStatsRollupTask, "_get_admin_email", return_value="hello"
)

install_id = ConstantsFactory.create(key="install_id", value=str(uuid.uuid4()))
version = ConstantsFactory.create(key="version", value="hello")
dbsession.add(install_id)
dbsession.add(version)

dbsession.flush()

task = BrollyStatsRollupTask()
result = task.run_cron_task(dbsession)
assert mock_request.called
Expand Down
1 change: 1 addition & 0 deletions tasks/tests/unit/test_commit_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_update_commit(
repository__owner__service="github",
repository__owner__service_id="104562106",
repository__name="test_example",
pullid=1,
)
dbsession.add(commit)
dbsession.flush()
Expand Down
Loading

0 comments on commit 2ae6c6d

Please sign in to comment.