This repository has been archived by the owner on May 1, 2023. It is now read-only.
generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add scans model * fix: add organisation fk
- Loading branch information
1 parent
8c3f020
commit 2e8ec27
Showing
11 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
api/db_migrations/versions/eb58f2b364a3_create_scans_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""create scans table | ||
Revision ID: eb58f2b364a3 | ||
Revises: db98eafe1333 | ||
Create Date: 2021-08-24 18:35:42.224567 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "eb58f2b364a3" | ||
down_revision = "db98eafe1333" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"scans", | ||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True), | ||
sa.Column("organisation_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("template_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("scan_type_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("created_at", sa.DateTime, default=sa.func.utc_timestamp()), | ||
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()), | ||
sa.ForeignKeyConstraint( | ||
["organisation_id"], | ||
["organisations.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["template_id"], | ||
["templates.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["scan_type_id"], | ||
["scan_types.id"], | ||
), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("scans") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import datetime | ||
import uuid | ||
|
||
from sqlalchemy import DateTime, Column, ForeignKey | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from sqlalchemy.orm import relationship | ||
|
||
from models import Base | ||
from models.Organisation import Organisation | ||
from models.ScanType import ScanType | ||
from models.Template import Template | ||
|
||
|
||
class Scan(Base): | ||
__tablename__ = "scans" | ||
|
||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) | ||
created_at = Column( | ||
DateTime, | ||
index=False, | ||
unique=False, | ||
nullable=False, | ||
default=datetime.datetime.utcnow, | ||
) | ||
updated_at = Column( | ||
DateTime, | ||
index=False, | ||
unique=False, | ||
nullable=True, | ||
onupdate=datetime.datetime.utcnow, | ||
) | ||
organisation_id = Column( | ||
UUID(as_uuid=True), ForeignKey(Organisation.id), index=True, nullable=False | ||
) | ||
organisation = relationship("Organisation", back_populates="scans") | ||
template_id = Column( | ||
UUID(as_uuid=True), ForeignKey(Template.id), index=True, nullable=False | ||
) | ||
template = relationship("Template", back_populates="scans") | ||
scan_type_id = Column( | ||
UUID(as_uuid=True), ForeignKey(ScanType.id), index=True, nullable=False | ||
) | ||
scan_type = relationship("ScanType", back_populates="scans") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pytest | ||
|
||
from sqlalchemy.exc import IntegrityError | ||
|
||
from models.Scan import Scan | ||
|
||
|
||
def test_scan_belongs_to_a_template_a_scan_type_and_an_organisation( | ||
scan_type_fixture, template_fixture, organisation_fixture, session | ||
): | ||
scan = Scan( | ||
organisation=organisation_fixture, | ||
scan_type=scan_type_fixture, | ||
template=template_fixture, | ||
) | ||
session.add(scan) | ||
session.commit() | ||
assert organisation_fixture.scans[-1].id == scan.id | ||
assert scan_type_fixture.scans[-1].id == scan.id | ||
assert template_fixture.scans[-1].id == scan.id | ||
session.delete(scan) | ||
session.commit() | ||
|
||
|
||
def test_scan_model(scan_type_fixture, template_fixture, organisation_fixture): | ||
scan = Scan( | ||
organisation=organisation_fixture, | ||
scan_type=scan_type_fixture, | ||
template=template_fixture, | ||
) | ||
assert scan.scan_type is not None | ||
assert scan.template is not None | ||
|
||
|
||
def test_scan_model_saved( | ||
assert_new_model_saved, | ||
scan_type_fixture, | ||
template_fixture, | ||
organisation_fixture, | ||
session, | ||
): | ||
scan = Scan( | ||
organisation=organisation_fixture, | ||
scan_type=scan_type_fixture, | ||
template=template_fixture, | ||
) | ||
session.add(scan) | ||
session.commit() | ||
assert_new_model_saved(scan) | ||
session.delete(scan) | ||
session.commit() | ||
|
||
|
||
def test_scan_empty_template_fails(scan_type_fixture, organisation_fixture, session): | ||
scan = Scan( | ||
organisation=organisation_fixture, | ||
scan_type=scan_type_fixture, | ||
) | ||
session.add(scan) | ||
with pytest.raises(IntegrityError): | ||
session.commit() | ||
session.rollback() | ||
|
||
|
||
def test_scan_empty_scan_type_fails(template_fixture, organisation_fixture, session): | ||
scan = Scan( | ||
organisation=organisation_fixture, | ||
template=template_fixture, | ||
) | ||
session.add(scan) | ||
with pytest.raises(IntegrityError): | ||
session.commit() | ||
session.rollback() | ||
|
||
|
||
def test_scan_empty_organisation_fails(template_fixture, scan_type_fixture, session): | ||
scan = Scan( | ||
scan_type=scan_type_fixture, | ||
template=template_fixture, | ||
) | ||
session.add(scan) | ||
with pytest.raises(IntegrityError): | ||
session.commit() | ||
session.rollback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters