mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Added model and dao for Jobs.
This commit is contained in:
19
app/dao/jobs_dao.py
Normal file
19
app/dao/jobs_dao.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from app import db
|
||||||
|
from app.models import Job
|
||||||
|
|
||||||
|
|
||||||
|
def save_job(job):
|
||||||
|
db.session.add(job)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_job_by_id(id):
|
||||||
|
return Job.query.get(id)
|
||||||
|
|
||||||
|
|
||||||
|
def get_jobs_by_service(service_id):
|
||||||
|
return Job.query.filter_by(service_id=service_id).all()
|
||||||
|
|
||||||
|
|
||||||
|
def get_jobs():
|
||||||
|
return Job.query.all()
|
||||||
0
app/job/__init__.py
Normal file
0
app/job/__init__.py
Normal file
@@ -1,6 +1,8 @@
|
|||||||
from . import db
|
from . import db
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
|
||||||
|
|
||||||
def filter_null_value_fields(obj):
|
def filter_null_value_fields(obj):
|
||||||
return dict(
|
return dict(
|
||||||
@@ -95,3 +97,26 @@ class Template(db.Model):
|
|||||||
content = db.Column(db.Text, index=False, unique=False, nullable=False)
|
content = db.Column(db.Text, index=False, unique=False, nullable=False)
|
||||||
service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=False)
|
service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=False)
|
||||||
service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic'))
|
service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic'))
|
||||||
|
|
||||||
|
|
||||||
|
class Job(db.Model):
|
||||||
|
|
||||||
|
__tablename__ = 'jobs'
|
||||||
|
|
||||||
|
id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||||
|
original_file_name = db.Column(db.String, nullable=False)
|
||||||
|
service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=False)
|
||||||
|
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
|
||||||
|
template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False)
|
||||||
|
created_at = db.Column(
|
||||||
|
db.DateTime,
|
||||||
|
index=False,
|
||||||
|
unique=False,
|
||||||
|
nullable=False,
|
||||||
|
default=datetime.datetime.now)
|
||||||
|
updated_at = db.Column(
|
||||||
|
db.DateTime,
|
||||||
|
index=False,
|
||||||
|
unique=False,
|
||||||
|
nullable=True,
|
||||||
|
onupdate=datetime.datetime.now)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class UserSchema(ma.ModelSchema):
|
|||||||
class ServiceSchema(ma.ModelSchema):
|
class ServiceSchema(ma.ModelSchema):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Service
|
model = models.Service
|
||||||
exclude = ("updated_at", "created_at", "tokens", "templates")
|
exclude = ("updated_at", "created_at", "tokens", "templates", "jobs")
|
||||||
|
|
||||||
|
|
||||||
class TemplateSchema(ma.ModelSchema):
|
class TemplateSchema(ma.ModelSchema):
|
||||||
|
|||||||
40
migrations/versions/0004_create_jobs.py
Normal file
40
migrations/versions/0004_create_jobs.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: c929d81b9e4a
|
||||||
|
Revises: 0003_create_tokens
|
||||||
|
Create Date: 2016-01-15 10:12:02.381160
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '0004_create_jobs'
|
||||||
|
down_revision = '0003_create_tokens'
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('jobs',
|
||||||
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
sa.Column('original_file_name', sa.String(), nullable=False),
|
||||||
|
sa.Column('service_id', sa.BigInteger(), nullable=True),
|
||||||
|
sa.Column('template_id', sa.BigInteger(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_jobs_template_id'), 'jobs', ['template_id'], unique=False)
|
||||||
|
### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index(op.f('ix_jobs_template_id'), table_name='jobs')
|
||||||
|
op.drop_index(op.f('ix_jobs_service_id'), table_name='jobs')
|
||||||
|
op.drop_table('jobs')
|
||||||
|
### end Alembic commands ###
|
||||||
117
tests/app/dao/test_jobs_dao.py
Normal file
117
tests/app/dao/test_jobs_dao.py
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
|
from app.dao.jobs_dao import (
|
||||||
|
save_job,
|
||||||
|
get_job_by_id,
|
||||||
|
get_jobs_by_service,
|
||||||
|
get_jobs
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
assert Job.query.count() == 0
|
||||||
|
job_id = uuid.uuid4()
|
||||||
|
data = {
|
||||||
|
'id': job_id,
|
||||||
|
'service_id': service.id,
|
||||||
|
'template_id': template.id,
|
||||||
|
'original_file_name': 'some.csv'
|
||||||
|
}
|
||||||
|
|
||||||
|
job = Job(**data)
|
||||||
|
|
||||||
|
save_job(job)
|
||||||
|
|
||||||
|
assert Job.query.count() == 1
|
||||||
|
job_from_db = Job.query.get(job_id)
|
||||||
|
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)
|
||||||
|
|
||||||
|
assert Job.query.count() == 0
|
||||||
|
job_id = uuid.uuid4()
|
||||||
|
data = {
|
||||||
|
'id': job_id,
|
||||||
|
'service_id': service.id,
|
||||||
|
'template_id': template.id,
|
||||||
|
'original_file_name': 'some.csv'
|
||||||
|
}
|
||||||
|
job = Job(**data)
|
||||||
|
save_job(job)
|
||||||
|
|
||||||
|
job_from_db = get_job_by_id(job_id)
|
||||||
|
|
||||||
|
assert job == job_from_db
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_jobs_for_service(notify_api, notify_db, notify_db_session,
|
||||||
|
sample_user):
|
||||||
|
|
||||||
|
service1, service2 = _do_services_setup(notify_db,
|
||||||
|
notify_db_session, sample_user)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user