Skip to content

Commit

Permalink
feat(metadata): define and load model
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman committed Dec 18, 2024
1 parent c215e8c commit dcc8e43
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 10 additions & 0 deletions eligibility_server/db/models.py
Original file line number Diff line number Diff line change
@@ -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"<Metadata [{self.timestamp}]: {self.users} users, {len(self.eligibility)} type{'s' if len(self.eligibility) > 1 else ''}>" # noqa: E501


user_eligibility = db.Table(
"user_eligibility",
db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True),
Expand Down
22 changes: 19 additions & 3 deletions eligibility_server/db/setup.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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():
"""
Expand Down Expand Up @@ -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.")
11 changes: 10 additions & 1 deletion tests/db/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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")
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()
Expand Down

0 comments on commit dcc8e43

Please sign in to comment.