Skip to content

Commit

Permalink
Merge pull request #272 from nsidc/fixup-test-data
Browse files Browse the repository at this point in the history
Fixup test data
  • Loading branch information
mfisher87 authored Mar 19, 2024
2 parents 849ac80 + 667150f commit f5a2cef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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

0 comments on commit f5a2cef

Please sign in to comment.