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