mirror of
https://github.com/sartography/protocol-builder-mock.git
synced 2025-01-12 08:44:46 +00:00
Prevents duplicate studies and requirements from being created on study update
This commit is contained in:
parent
c5861166d9
commit
a5a4733058
20
app.py
20
app.py
@ -40,7 +40,6 @@ def get_form(id, requirement_code):
|
||||
return
|
||||
|
||||
|
||||
|
||||
conn = connexion.App('Protocol Builder', specification_dir='./')
|
||||
conn.add_api('api.yml')
|
||||
|
||||
@ -56,7 +55,6 @@ else:
|
||||
app.config.root_path = app.instance_path
|
||||
app.config.from_pyfile('config.py', silent=True)
|
||||
|
||||
|
||||
db = SQLAlchemy(app)
|
||||
migrate = Migrate(app, db)
|
||||
ma = Marshmallow(app)
|
||||
@ -83,6 +81,7 @@ def has_no_empty_params(rule):
|
||||
arguments = rule.arguments if rule.arguments is not None else ()
|
||||
return len(defaults) >= len(arguments)
|
||||
|
||||
|
||||
@app.route("/site_map")
|
||||
def site_map():
|
||||
links = []
|
||||
@ -95,9 +94,6 @@ def site_map():
|
||||
return json.dumps({"links": links})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# **************************
|
||||
# WEB FORMS
|
||||
# **************************
|
||||
@ -131,6 +127,7 @@ def new_study():
|
||||
title=title,
|
||||
description_map=description_map)
|
||||
|
||||
|
||||
@app.route('/study/<study_id>', methods=['GET', 'POST'])
|
||||
def edit_study(study_id):
|
||||
study = db.session.query(Study).filter(Study.STUDYID == study_id).first()
|
||||
@ -190,11 +187,15 @@ def del_study(study_id):
|
||||
|
||||
|
||||
def _update_study(study, form):
|
||||
# quick hack to get auto-increment without creating a bunch of hassle, this is not
|
||||
# production code by any stretch of the imagination, but this is a throw away library.
|
||||
max_id = db.session.query(func.max(Study.STUDYID)).scalar() or 1
|
||||
if study.STUDYID is None:
|
||||
# quick hack to get auto-increment without creating a bunch of hassle, this is not
|
||||
# production code by any stretch of the imagination, but this is a throw away library.
|
||||
max_id = db.session.query(func.max(Study.STUDYID)).scalar() or 1
|
||||
|
||||
study.STUDYID = max_id + 1
|
||||
else:
|
||||
db.session.query(RequiredDocument).filter(RequiredDocument.STUDYID == study.STUDYID).delete()
|
||||
|
||||
study.STUDYID = max_id + 1
|
||||
study.TITLE = form.TITLE.data
|
||||
study.NETBADGEID = form.NETBADGEID.data
|
||||
study.DATE_MODIFIED = datetime.datetime.now()
|
||||
@ -209,6 +210,7 @@ def _update_study(study, form):
|
||||
db.session.add(study)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@app.route('/study_details/<study_id>', methods=['GET', 'POST'])
|
||||
def study_details(study_id):
|
||||
study_details = db.session.query(StudyDetails).filter(StudyDetails.STUDYID == study_id).first()
|
||||
|
@ -1,15 +1,14 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
os.environ["TESTING"] = "true"
|
||||
|
||||
import unittest
|
||||
from app import app, db
|
||||
from forms import StudyForm
|
||||
from models import Study
|
||||
from models import Study, RequiredDocument
|
||||
|
||||
|
||||
class Sanity_Check_Test(unittest.TestCase):
|
||||
|
||||
auths = {}
|
||||
|
||||
@classmethod
|
||||
@ -31,13 +30,38 @@ class Sanity_Check_Test(unittest.TestCase):
|
||||
self.ctx.pop()
|
||||
self.auths = {}
|
||||
|
||||
def test_add_study_post(self):
|
||||
"""Does add study post a new study?"""
|
||||
def test_add_and_edit_study(self):
|
||||
"""Add and edit a study"""
|
||||
study = Study(TITLE="My Test Document", NETBADGEID="dhf8r")
|
||||
form = StudyForm(formdata=None, obj=study)
|
||||
num_reqs = len(form.requirements.choices)
|
||||
self.assertGreater(num_reqs, 0)
|
||||
|
||||
for r in form.requirements:
|
||||
form.data['requirements'].append(r.data)
|
||||
|
||||
r = self.app.post('/new_study', data=form.data, follow_redirects=True)
|
||||
assert r.status_code == 200
|
||||
added_study = Study.query.filter(
|
||||
Study.TITLE == "My Test Document").first()
|
||||
added_study = Study.query.filter(Study.TITLE == "My Test Document").first()
|
||||
assert added_study
|
||||
num_studies_before = Study.query.count()
|
||||
|
||||
num_docs_before = RequiredDocument.query.filter(Study.STUDYID == added_study.STUDYID).count()
|
||||
self.assertEqual(num_reqs, num_docs_before)
|
||||
|
||||
"""Edit an existing study"""
|
||||
added_study.title = "New Title"
|
||||
form_2 = StudyForm(formdata=None, obj=added_study)
|
||||
|
||||
for r in form_2.requirements:
|
||||
form_2.data['requirements'].append(r.data)
|
||||
|
||||
r_2 = self.app.post('/study/%i' % added_study.STUDYID, data=form_2.data, follow_redirects=True)
|
||||
assert r_2.status_code == 200
|
||||
num_studies_after = Study.query.count()
|
||||
edited_study = Study.query.filter(Study.STUDYID == added_study.STUDYID).first()
|
||||
assert edited_study
|
||||
|
||||
num_docs_after = RequiredDocument.query.filter(Study.STUDYID == edited_study.STUDYID).count()
|
||||
self.assertEqual(num_docs_before, num_docs_after)
|
||||
self.assertEqual(num_studies_before, num_studies_after)
|
||||
|
Loading…
x
Reference in New Issue
Block a user