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

Fixup test data #272

Merged
merged 2 commits into from
Mar 19, 2024
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
21 changes: 20 additions & 1 deletion usaon_benefit_tool/util/db/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

TODO: Break this up in to multiple modules!
"""
import sqlalchemy
from flask import current_app
from loguru import logger
from sqlalchemy import MetaData
Expand All @@ -23,8 +24,9 @@
SocietalBenefitArea,
SocietalBenefitKeyObjective,
SocietalBenefitSubArea,
User,
)
from usaon_benefit_tool.util.dev import DEV_USER
from usaon_benefit_tool.util.dev import DEV_USER, TEST_USER


def recreate_tables() -> None:
Expand Down Expand Up @@ -148,14 +150,18 @@ def _init_societal_benefit_areas(session: Session) -> None:


def _init_test_assessment(session: Session) -> None:
test_user = _get_or_create_test_user(session)

assessment = Assessment(
title="[TEST] This is testing data!",
description=(
"Created by running the relevant invoke task from the project source code."
),
created_by=test_user,
)

common_obj_fields = {
"created_by": test_user,
"organization": "-",
"funder": "-",
"funding_country": "-",
Expand Down Expand Up @@ -193,6 +199,7 @@ def _init_test_assessment(session: Session) -> None:
short_name="Test SBA",
societal_benefit_area_id=next(iter(IAOA_SBA_FRAMEWORK.keys())),
type=NodeType.SOCIETAL_BENEFIT_AREA,
created_by=test_user,
)

assessment_observing_system = AssessmentNode(
Expand Down Expand Up @@ -255,3 +262,15 @@ def _init_dev_user(session: Session) -> None:
logger.warning("Inserting dev user. This should not happen in production!")
session.add(DEV_USER)
session.commit()


def _get_or_create_test_user(session: Session) -> User:
"""Insert a test user if it doesn't already exist."""
try:
# Look up test user by email address with .one()
return User.query.filter_by(email=TEST_USER.email).one()
except sqlalchemy.orm.exc.NoResultFound:
logger.warning("Inserting test user.")
session.add(TEST_USER)
session.commit()
return TEST_USER
6 changes: 6 additions & 0 deletions usaon_benefit_tool/util/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# disabled at init time.
DEV_USER = User(
id=0,
email="dev_user@example.com",
name="Dev User",
orcid="junk",
role_id="admin",
)
TEST_USER = User(
email="test_user@example.com",
name="Test User",
orcid="junk",
Expand Down
Loading