diff --git a/eligibility_server/db/models.py b/eligibility_server/db/models.py index bd5caab4..13976f47 100644 --- a/eligibility_server/db/models.py +++ b/eligibility_server/db/models.py @@ -1,6 +1,16 @@ from eligibility_server.db import db +class Metadata(db.Model): + id = db.Column(db.Integer, primary_key=True) + timestamp = db.Column(db.String, unique=True, nullable=False) + users = db.Column(db.Integer, nullable=False) + eligibility = db.Column(db.PickleType, nullable=False) + + def __repr__(self): + return f" 1 else ''}>" # noqa: E501 + + user_eligibility = db.Table( "user_eligibility", db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True), diff --git a/eligibility_server/db/setup.py b/eligibility_server/db/setup.py index 18c0aa68..a585ce3e 100644 --- a/eligibility_server/db/setup.py +++ b/eligibility_server/db/setup.py @@ -1,13 +1,14 @@ import csv +from datetime import datetime, timezone from tempfile import NamedTemporaryFile import click from flask import current_app -from sqlalchemy import inspect +from sqlalchemy import column, inspect import requests from eligibility_server.db import db -from eligibility_server.db.models import User, Eligibility +from eligibility_server.db.models import User, Eligibility, Metadata from eligibility_server.settings import Configuration @@ -29,9 +30,10 @@ def init_db_command(): click.echo("Creating table...") db.create_all() click.echo("Table created.") - import_users() + update_metadata() + def import_users(): """ @@ -135,3 +137,17 @@ def drop_db_command(): click.echo("Database dropped.") else: click.echo("Database does not exist.") + + +def update_metadata(): + Metadata.query.delete() + + ts = datetime.now(tz=timezone.utc).isoformat(timespec="seconds") + users = User.query.count() + eligibility = [e.name for e in Eligibility.query.add_column(column("name"))] + + metadata = Metadata(timestamp=ts, users=users, eligibility=eligibility) + db.session.add(metadata) + db.session.commit() + + click.echo("Database metadata updated.") diff --git a/tests/db/test_setup.py b/tests/db/test_setup.py index 87337a38..eb7a85b0 100644 --- a/tests/db/test_setup.py +++ b/tests/db/test_setup.py @@ -1,8 +1,10 @@ +from datetime import datetime, timedelta, timezone + import pytest from sqlalchemy import inspect from eligibility_server.db import db -from eligibility_server.db.models import Eligibility, User +from eligibility_server.db.models import Eligibility, Metadata, User @pytest.mark.usefixtures("flask") @@ -10,12 +12,19 @@ def test_init_db_command(runner): """Assumes that IMPORT_FILE_PATH is data/server.csv.""" runner.invoke(args="drop-db") + start_time = datetime.now(tz=timezone.utc) result = runner.invoke(args="init-db") assert result.exit_code == 0 assert User.query.count() == 26 assert Eligibility.query.count() == 1 + assert Metadata.query.count() == 1 + + metadata = Metadata.query.first() + assert metadata.users == User.query.count() + assert datetime.fromisoformat(metadata.timestamp) - start_time < timedelta(seconds=1) + assert metadata.eligibility == ["agency_card"] user_with_one_eligibility = User.query.filter_by(sub="32587", name="Gonzales").first() agency_card_type = Eligibility.query.filter_by(name="agency_card").first()