Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ResourceLinks model #101

Merged
merged 3 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
update_or_create_criterion,
update_or_create_link,
)
from models import Category, Subcategory, Criterion, Link, Score, State # noqa: E402
from models import ( # noqa: E402, F401
Category,
Subcategory,
Criterion,
Link,
ResourceLink,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this import being used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope :) I'll get rid of it.

Score,
State
)


@app.errorhandler(400)
Expand Down
38 changes: 38 additions & 0 deletions migrations/versions/52f7f51d21b1_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""empty message

Revision ID: 52f7f51d21b1
Revises: ea276fcdfa35
Create Date: 2021-01-14 12:35:50.498441

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

# revision identifiers, used by Alembic.
revision = '52f7f51d21b1'
down_revision = 'ea276fcdfa35'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('links', sa.Column('type', sa.String(length=20), nullable=True))
op.drop_index('honorable_mention_state_subcategory_active', table_name='links')
op.drop_index('innovative_idea_state_subcategory_active', table_name='links')
op.drop_column('links', 'created_at')
op.add_column('states', sa.Column('honorable_mention', sa.String(), nullable=True))
op.add_column('states', sa.Column('innovative_idea', sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('states', 'innovative_idea')
op.drop_column('states', 'honorable_mention')
op.add_column('links', sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
op.create_index('innovative_idea_state_subcategory_active', 'links', ['state', 'subcategory_id', 'active'], unique=False)
op.create_index('honorable_mention_state_subcategory_active', 'links', ['state', 'subcategory_id', 'active'], unique=False)
op.drop_column('links', 'type')
# ### end Alembic commands ###
18 changes: 15 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class State(BaseMixin, db.Model):
honorable_mention = db.Column(db.String())
grades = db.relationship('StateGrade', order_by='desc(StateGrade.created_at)', lazy=True)
scores = db.relationship('Score', lazy=True)
links = db.relationship('Link', lazy=True)
resource_links = db.relationship('ResourceLink', lazy=True)

def __init__(self, code, name=None, innovative_idea=None, honorable_mention=None):
self.code = code
Expand All @@ -55,7 +55,7 @@ def __repr__(self):
return '<id {}>'.format(self.code)

def serialize(self):
links = [link.serialize() for link in self.links]
resource_links = [resource_link.serialize() for resource_link in self.resource_links]

grade = self.grades[0].serialize() if self.grades else None
category_grades = []
Expand Down Expand Up @@ -87,7 +87,7 @@ def serialize(self):
'grade': grade,
'category_grades': category_grades,
'criterion_scores': criterion_scores,
'links': links,
'resource_links': resource_links,
}


Expand Down Expand Up @@ -345,6 +345,12 @@ class Link(BaseMixin, Deactivatable, db.Model):
state = db.Column(db.String(2), db.ForeignKey('states.code'), nullable=False)
text = db.Column(db.String())
url = db.Column(db.String())
type = db.Column(db.String(20))

__mapper_args__ = {
'polymorphic_on': type,
'polymorphic_identity': 'link'
}

def __init__(self, subcategory_id, state, text=None, url=None):
self.subcategory_id = subcategory_id
Expand Down Expand Up @@ -380,4 +386,10 @@ def serialize(self):
}


class ResourceLink(Link):
__mapper_args__ = {
'polymorphic_identity': 'resource_link'
}


db.Index('state_subcategory', Link.state, Link.subcategory_id)
1 change: 1 addition & 0 deletions tests/models/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_init(self):
self.assertEqual(self.link.state, self.state_code)
self.assertEqual(self.link.text, 'Section 20 of Statute 39-B')
self.assertEqual(self.link.url, 'ny.gov/link/to/statute')
self.assertEqual(self.link.type, 'link')
self.assertTrue(self.category.active)

def test_init_invalid_category(self):
Expand Down
73 changes: 73 additions & 0 deletions tests/models/test_resource_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import unittest
import datetime

from app import db
from models import ResourceLink
from strings import subcategory_not_found, invalid_state
from tests.test_utils import clear_database, create_state, create_category, create_subcategory


class ResourceLinkTestCase(unittest.TestCase):
def setUp(self):
self.state_code = 'NY'
create_state(code=self.state_code)
self.category = create_category()
self.subcategory = create_subcategory(self.category.id)
self.resource_link = ResourceLink(
subcategory_id=self.subcategory.id,
state=self.state_code,
text='Section 20 of Statute 39-B',
url='ny.gov/link/to/statute',
).save()

def tearDown(self):
clear_database(db)

def test_init(self):
self.assertEqual(self.resource_link.subcategory_id, self.subcategory.id)
self.assertEqual(self.resource_link.state, self.state_code)
self.assertEqual(self.resource_link.text, 'Section 20 of Statute 39-B')
self.assertEqual(self.resource_link.url, 'ny.gov/link/to/statute')
self.assertEqual(self.resource_link.type, 'resource_link')
self.assertTrue(self.category.active)

def test_init_invalid_category(self):
with self.assertRaises(ValueError) as e:
ResourceLink(
subcategory_id=0,
state=self.state_code,
text='Section 20 of Statute 39-B',
url='ny.gov/link/to/statute',
)
self.assertEqual(str(e.exception), subcategory_not_found)

def test_init_invalid_state_code(self):
with self.assertRaises(ValueError) as e:
ResourceLink(
subcategory_id=self.subcategory.id,
state='fake-state-code',
text='Section 20 of Statute 39-B',
url='ny.gov/link/to/statute',
)
self.assertEqual(str(e.exception), invalid_state)

def test_serialize(self):
self.assertEqual(
{
'id': self.resource_link.id,
'subcategory_id': self.subcategory.id,
'state': self.state_code,
'text': 'Section 20 of Statute 39-B',
'url': 'ny.gov/link/to/statute',
'active': True,
'deactivated_at': None,
},
self.resource_link.serialize()
)

def test_deactivate(self):
self.resource_link.deactivate()

self.assertFalse(self.resource_link.active)
self.assertTrue(isinstance(self.resource_link.deactivated_at, datetime.datetime))
self.assertTrue(self.resource_link.deactivated_at < datetime.datetime.utcnow())
10 changes: 5 additions & 5 deletions tests/models/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
create_criterion,
create_state_grade,
create_state_category_grade,
create_link,
create_resource_link,
)


Expand All @@ -33,8 +33,8 @@ def setUp(self):
criterion1 = create_criterion(subcategory.id)
criterion2 = create_criterion(subcategory.id)

self.link1 = create_link(subcategory.id, self.state.code)
self.link2 = create_link(subcategory.id, self.state.code)
self.link1 = create_resource_link(subcategory.id, self.state.code)
self.link2 = create_resource_link(subcategory.id, self.state.code)

self.state_grade1 = create_state_grade(self.state.code)
self.state_grade2 = create_state_grade(self.state.code)
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_serialize(self):
self.score1.serialize(),
self.score2.serialize(),
],
'links': [
'resource_links': [
self.link1.serialize(),
self.link2.serialize(),
],
Expand All @@ -121,7 +121,7 @@ def test_serialize_no_grades(self):
'grade': None,
'category_grades': [],
'criterion_scores': [],
'links': [],
'resource_links': [],
},
state.serialize()
)
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ def create_link(subcategory_id, state):
return models.Link(subcategory_id=subcategory_id, state=state).save()


def create_resource_link(subcategory_id, state):
return models.ResourceLink(subcategory_id=subcategory_id, state=state).save()


def auth_headers():
return {'Authorization': 'Bearer fake token'}