mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
test cleanup with fixtures
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from app.models import (User, Service, Template, Token)
|
from app.models import (User, Service, Template, Token, Job)
|
||||||
from app.dao.users_dao import (save_model_user)
|
from app.dao.users_dao import (save_model_user)
|
||||||
from app.dao.services_dao import save_model_service
|
from app.dao.services_dao import save_model_service
|
||||||
from app.dao.templates_dao import save_model_template
|
from app.dao.templates_dao import save_model_template
|
||||||
from app.dao.tokens_dao import save_model_token
|
from app.dao.tokens_dao import save_model_token
|
||||||
|
from app.dao.jobs_dao import save_job
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
@@ -57,10 +59,30 @@ def sample_template(notify_db,
|
|||||||
def sample_token(notify_db,
|
def sample_token(notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
service=None):
|
service=None):
|
||||||
import uuid
|
|
||||||
if service is None:
|
if service is None:
|
||||||
service = sample_service(notify_db, notify_db_session)
|
service = sample_service(notify_db, notify_db_session)
|
||||||
data = {'service_id': service.id}
|
data = {'service_id': service.id}
|
||||||
token = Token(**data)
|
token = Token(**data)
|
||||||
save_model_token(token)
|
save_model_token(token)
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def sample_job(notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service=None,
|
||||||
|
template=None):
|
||||||
|
if service is None:
|
||||||
|
service = sample_service(notify_db, notify_db_session)
|
||||||
|
if template is None:
|
||||||
|
template = sample_template(notify_db, notify_db_session,
|
||||||
|
service=service)
|
||||||
|
data = {
|
||||||
|
'id': uuid.uuid4(),
|
||||||
|
'service_id': service.id,
|
||||||
|
'template_id': template.id,
|
||||||
|
'original_file_name': 'some.csv'
|
||||||
|
}
|
||||||
|
job = Job(**data)
|
||||||
|
save_job(job)
|
||||||
|
return job
|
||||||
|
|||||||
@@ -9,27 +9,19 @@ from app.dao.jobs_dao import (
|
|||||||
|
|
||||||
from app.models import Job
|
from app.models import Job
|
||||||
|
|
||||||
from tests.app.conftest import (
|
|
||||||
sample_template as create_template,
|
|
||||||
sample_service as create_service
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def test_save_job(notify_db, notify_db_session, sample_template):
|
||||||
def test_save_job(notify_api, notify_db, notify_db_session, sample_user):
|
|
||||||
service = create_service(notify_db, notify_db_session, user=sample_user)
|
|
||||||
template = create_template(notify_db, notify_db_session, service=service)
|
|
||||||
|
|
||||||
assert Job.query.count() == 0
|
assert Job.query.count() == 0
|
||||||
job_id = uuid.uuid4()
|
job_id = uuid.uuid4()
|
||||||
data = {
|
data = {
|
||||||
'id': job_id,
|
'id': job_id,
|
||||||
'service_id': service.id,
|
'service_id': sample_template.service_id,
|
||||||
'template_id': template.id,
|
'template_id': sample_template.id,
|
||||||
'original_file_name': 'some.csv'
|
'original_file_name': 'some.csv'
|
||||||
}
|
}
|
||||||
|
|
||||||
job = Job(**data)
|
job = Job(**data)
|
||||||
|
|
||||||
save_job(job)
|
save_job(job)
|
||||||
|
|
||||||
assert Job.query.count() == 1
|
assert Job.query.count() == 1
|
||||||
@@ -37,16 +29,13 @@ def test_save_job(notify_api, notify_db, notify_db_session, sample_user):
|
|||||||
assert job == job_from_db
|
assert job == job_from_db
|
||||||
|
|
||||||
|
|
||||||
def test_get_job_by_id(notify_api, notify_db, notify_db_session, sample_user):
|
def test_get_job_by_id(notify_db, notify_db_session, sample_template):
|
||||||
service = create_service(notify_db, notify_db_session, user=sample_user)
|
|
||||||
template = create_template(notify_db, notify_db_session, service=service)
|
|
||||||
|
|
||||||
assert Job.query.count() == 0
|
assert Job.query.count() == 0
|
||||||
job_id = uuid.uuid4()
|
job_id = uuid.uuid4()
|
||||||
data = {
|
data = {
|
||||||
'id': job_id,
|
'id': job_id,
|
||||||
'service_id': service.id,
|
'service_id': sample_template.service_id,
|
||||||
'template_id': template.id,
|
'template_id': sample_template.id,
|
||||||
'original_file_name': 'some.csv'
|
'original_file_name': 'some.csv'
|
||||||
}
|
}
|
||||||
job = Job(**data)
|
job = Job(**data)
|
||||||
@@ -57,61 +46,21 @@ def test_get_job_by_id(notify_api, notify_db, notify_db_session, sample_user):
|
|||||||
assert job == job_from_db
|
assert job == job_from_db
|
||||||
|
|
||||||
|
|
||||||
def test_get_jobs_for_service(notify_api, notify_db, notify_db_session,
|
def test_get_jobs_for_service(notify_db, notify_db_session, sample_job):
|
||||||
sample_user):
|
|
||||||
|
|
||||||
service1, service2 = _do_services_setup(notify_db,
|
service_id = sample_job.service_id
|
||||||
notify_db_session, sample_user)
|
job_from_db = get_jobs_by_service(service_id)
|
||||||
|
|
||||||
jobs1 = [job for job in service1.jobs] # get jobs directly from service
|
assert len(job_from_db) == 1
|
||||||
jobs1_from_dao = get_jobs_by_service(service1.id) # get jobs via dao
|
assert sample_job == job_from_db[0]
|
||||||
|
|
||||||
assert len(jobs1_from_dao) == 5
|
|
||||||
assert jobs1 == jobs1_from_dao
|
|
||||||
|
|
||||||
jobs2 = [job for job in service2.jobs] # get jobs directly from service
|
|
||||||
jobs2_from_dao = get_jobs_by_service(service2.id) # get jobs via dao
|
|
||||||
|
|
||||||
assert len(jobs2_from_dao) == 2
|
|
||||||
assert jobs2 == jobs2_from_dao
|
|
||||||
|
|
||||||
assert jobs1_from_dao != jobs2_from_dao
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_all_jobs(notify_api, notify_db, notify_db_session,
|
def test_get_all_jobs(notify_db, notify_db_session, sample_template):
|
||||||
sample_user):
|
from tests.app.conftest import sample_job as create_job
|
||||||
|
|
||||||
_do_services_setup(notify_db, notify_db_session, sample_user)
|
|
||||||
|
|
||||||
jobs_from_db = get_jobs()
|
|
||||||
assert len(jobs_from_db) == 7
|
|
||||||
|
|
||||||
|
|
||||||
def _do_services_setup(notify_db, notify_db_session, sample_user):
|
|
||||||
service1 = create_service(notify_db, notify_db_session, user=sample_user)
|
|
||||||
template1 = create_template(notify_db, notify_db_session, service=service1)
|
|
||||||
|
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
job_id = uuid.uuid4()
|
create_job(notify_db,
|
||||||
data = {
|
notify_db_session,
|
||||||
'id': job_id,
|
sample_template.service,
|
||||||
'service_id': service1.id,
|
sample_template)
|
||||||
'template_id': template1.id,
|
jobs_from_db = get_jobs()
|
||||||
'original_file_name': 'some.csv'
|
assert len(jobs_from_db) == 5
|
||||||
}
|
|
||||||
save_job(Job(**data))
|
|
||||||
|
|
||||||
service2 = create_service(notify_db, notify_db_session, user=sample_user)
|
|
||||||
template2 = create_template(notify_db, notify_db_session, service=service2)
|
|
||||||
|
|
||||||
for i in range(2):
|
|
||||||
job_id = uuid.uuid4()
|
|
||||||
data = {
|
|
||||||
'id': job_id,
|
|
||||||
'service_id': service2.id,
|
|
||||||
'template_id': template2.id,
|
|
||||||
'original_file_name': 'some.csv'
|
|
||||||
}
|
|
||||||
save_job(Job(**data))
|
|
||||||
|
|
||||||
return service1, service2
|
|
||||||
|
|||||||
Reference in New Issue
Block a user