Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

feat: add Template model #97

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions api/db_migrations/versions/bf6d44121dce_create_templates_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""create templates table

Revision ID: bf6d44121dce
Revises: da6527f6cd76
Create Date: 2021-08-24 13:14:26.089149

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "bf6d44121dce"
down_revision = "da6527f6cd76"
branch_labels = None
depends_on = None


def upgrade():
op.create_table(
"templates",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("organisation_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("token", postgresql.UUID(as_uuid=True), nullable=False, unique=True),
sa.Column("name", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime, default=sa.func.now()),
sa.Column("updated_at", sa.DateTime, onupdate=sa.func.utc_timestamp()),
sa.ForeignKeyConstraint(
["organisation_id"],
["organisations.id"],
),
)


def downgrade():
op.drop_table("templates")
1 change: 1 addition & 0 deletions api/models/Organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Organisation(Base):
onupdate=datetime.datetime.utcnow,
)

templates = relationship("Template")
users = relationship("User")

@validates("name")
Expand Down
40 changes: 40 additions & 0 deletions api/models/Template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import datetime
import uuid

from sqlalchemy import DateTime, Column, ForeignKey, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship, validates

from models import Base
from models.Organisation import Organisation


class Template(Base):
__tablename__ = "templates"

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
token = Column(UUID(as_uuid=True), default=uuid.uuid4)
name = Column(String, nullable=False)
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="templates")

@validates("name")
def validate_name(self, _key, value):
assert value != ""
return value
9 changes: 9 additions & 0 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from alembic.config import Config
from alembic import command

from models.Organisation import Organisation

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

Expand All @@ -18,6 +20,13 @@ def f(model):
return f


@pytest.fixture(scope="session")
def organisation_fixture(session):
organisation = Organisation(name="name")
session.add(organisation)
return organisation


@pytest.fixture(scope="session")
def session():
db_engine = create_engine(os.environ.get("SQLALCHEMY_DATABASE_TEST_URI"))
Expand Down
60 changes: 60 additions & 0 deletions api/tests/models/test_Template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from sqlalchemy.exc import IntegrityError

from models.Template import Template


def test_template_belongs_to_an_organisation(organisation_fixture, session):
template = Template(
name="name",
organisation=organisation_fixture,
)
session.add(template)
session.commit()
assert organisation_fixture.templates[0].id == template.id
session.delete(template)
session.commit()


def test_template_model(organisation_fixture):
template = Template(
name="name",
organisation=organisation_fixture,
)
assert template.name == "name"
assert template.organisation is not None


def test_template_model_saved(assert_new_model_saved, organisation_fixture, session):
template = Template(
name="name",
organisation=organisation_fixture,
)
session.add(template)
session.commit()
assert template.name == "name"
assert_new_model_saved(template)
assert template.token is not None
session.delete(template)
session.commit()


def test_template_empty_name_fails(organisation_fixture, session):
template = Template(
organisation=organisation_fixture,
)
session.add(template)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()


def test_template_empty_organisation_fails(session):
template = Template(
name="name",
)
session.add(template)
with pytest.raises(IntegrityError):
session.commit()
session.rollback()
9 changes: 1 addition & 8 deletions api/tests/models/test_User.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

from sqlalchemy.exc import IntegrityError

from models.Organisation import Organisation
from models.User import User


@pytest.fixture(scope="session")
def organisation_fixture(session):
organisation = Organisation(name="name")
session.add(organisation)
return organisation
from models.User import User


def test_user_belongs_to_an_organisation(organisation_fixture, session):
Expand Down