Skip to content

Commit

Permalink
Alter ConceptVisitLog concept_id to string. (#907)
Browse files Browse the repository at this point in the history
Issue #906
  • Loading branch information
Wim-De-Clercq authored Jul 30, 2024
1 parent 63a9633 commit c0f1e72
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""alter ConceptVisitLog concept_id to string
Revision ID: 29306f749043
Revises: b2a7d7614973
Create Date: 2024-07-30 09:12:37.521748
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '29306f749043'
down_revision = 'b2a7d7614973'


def upgrade():
with op.batch_alter_table('concept_visit_log') as batch_op:
batch_op.alter_column('concept_id', type_=sa.String, existing_type=sa.Integer)


def downgrade():
# Drop concept_visit_log which have non-integer concept_id
op.execute(
"DELETE FROM concept_visit_log "
"WHERE cast(cast(concept_id AS INTEGER) AS TEXT) != concept_id"
)
with op.batch_alter_table('concept_visit_log') as batch_op:
batch_op.alter_column('concept_id', type_=sa.Integer, existing_type=sa.String)
2 changes: 1 addition & 1 deletion atramhasis/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConceptschemeVisitLog(Base):
class ConceptVisitLog(Base):
__tablename__ = 'concept_visit_log'
id = Column(Integer, primary_key=True, autoincrement=True)
concept_id = Column(Integer, nullable=False)
concept_id = Column(String, nullable=False)
conceptscheme_id = Column(String(25), nullable=False)
visited_at = Column(DateTime, default=func.now(), nullable=False)
origin = Column(String(25), nullable=False)
Expand Down
36 changes: 17 additions & 19 deletions tests/test_datamanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,36 +178,34 @@ def test_get_first_day(self):
@patch('atramhasis.data.datamanagers.date', Mock(today=Mock(return_value=date(2015, 9, 15))))
def test_get_most_popular_concepts_for_conceptscheme(self):
self.session.add(
ConceptVisitLog(concept_id=1, conceptscheme_id='1', origin='REST',
ConceptVisitLog(concept_id='1', conceptscheme_id='1', origin='REST',
visited_at=datetime(2015, 8, 27, 10, 58, 3))
)
self.session.add(
ConceptVisitLog(concept_id=1, conceptscheme_id='1', origin='REST',
ConceptVisitLog(concept_id='1', conceptscheme_id='1', origin='REST',
visited_at=datetime(2015, 8, 27, 11, 58, 3))
)
self.session.add(
ConceptVisitLog(concept_id=2, conceptscheme_id='1', origin='REST',
ConceptVisitLog(concept_id='2', conceptscheme_id='1', origin='REST',
visited_at=datetime(2015, 8, 27, 10, 58, 3))
)
self.session.add(
ConceptVisitLog(concept_id=2, conceptscheme_id='2', origin='REST',
ConceptVisitLog(concept_id='2', conceptscheme_id='2', origin='REST',
visited_at=datetime(2015, 8, 27, 10, 58, 3))
)
self.assertListEqual(
[
{'concept_id': 1, 'scheme_id': 1},
{'concept_id': 2, 'scheme_id': 1}
],
self.audit_manager.get_most_popular_concepts_for_conceptscheme(
1, 5, 'last_month'
)
)
self.assertListEqual([{'concept_id': 2, 'scheme_id': 2}],
self.audit_manager.get_most_popular_concepts_for_conceptscheme(2, 5, 'last_month'))
self.assertListEqual([{'concept_id': 1, 'scheme_id': 1}],
self.audit_manager.get_most_popular_concepts_for_conceptscheme(1, 1, 'last_month'))
self.assertListEqual([],
self.audit_manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_day'))

manager = self.audit_manager
result = manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_month')
expected = [
{'concept_id': '1', 'scheme_id': 1}, {'concept_id': '2', 'scheme_id': 1}
]
self.assertListEqual(expected, result)
result = manager.get_most_popular_concepts_for_conceptscheme(2, 5, 'last_month')
self.assertListEqual([{'concept_id': '2', 'scheme_id': 2}], result)
result = manager.get_most_popular_concepts_for_conceptscheme(1, 1, 'last_month')
self.assertListEqual([{'concept_id': '1', 'scheme_id': 1}], result)
result = manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_day')
self.assertListEqual([], result)


class CountsManagerTest(DbTest):
Expand Down

0 comments on commit c0f1e72

Please sign in to comment.