Skip to content

Commit

Permalink
Merge 11776da into 972a028
Browse files Browse the repository at this point in the history
  • Loading branch information
dhakim87 committed Mar 3, 2020
2 parents 972a028 + 11776da commit 1821c0d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 39 deletions.
10 changes: 3 additions & 7 deletions microsetta_private_api/api/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from microsetta_private_api.repo.sample_repo import SampleRepo

from microsetta_private_api.model.account import Account
from microsetta_private_api.model.source import Source
from microsetta_private_api.model.source import Source, info_from_api
from microsetta_private_api.model.source import human_info_from_api
from microsetta_private_api.LEGACY.locale_data import american_gut, british_gut

Expand Down Expand Up @@ -205,12 +205,8 @@ def update_source(account_id, source_id, body):
source_repo = SourceRepo(t)
source = source_repo.get_source(account_id, source_id)

# Uhhh, where do I get source_data from???
# source.source_data = something?
# TODO: Answer: source data is coming in in the request body,
# Fill it in!

source_repo.update_source_data(source)
source.source_data = info_from_api(body)
source_repo.update_source_data_api_fields(source)
# I wonder if there's some way to get the creation_time/update_time
# during the insert/update...
source = source_repo.get_source(account_id, source_id)
Expand Down
65 changes: 55 additions & 10 deletions microsetta_private_api/api/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,42 @@ def test_get_sources(self):
1,
"Expected 1 source named Planty")

def test_put_source(self):
resp = self.client.get(
'/api/accounts/%s/sources?language_tag=en-US' % ACCT_ID)

check_response(resp)
sources = json.loads(resp.data)
self.assertGreaterEqual(len(sources), 3)
to_edit = sources[2]
source_id = to_edit["source_id"]
fuzzy = fuzz(to_edit)
fuzzy["source_type"] = to_edit["source_type"]
fuzzy.pop("source_id")
resp = self.client.put(
'/api/accounts/%s/sources/%s?language_tag=en-US' %
(ACCT_ID, source_id),
content_type='application/json',
data=json.dumps(fuzzy)
)
check_response(resp)
fuzzy_resp = json.loads(resp.data)
self.assertEqual(fuzzy["source_name"], fuzzy_resp["source_name"])
self.assertEqual(fuzzy["source_description"],
fuzzy_resp["source_description"])
to_edit.pop("source_id")
resp = self.client.put(
'/api/accounts/%s/sources/%s?language_tag=en-US' %
(ACCT_ID, source_id),
content_type='application/json',
data=json.dumps(to_edit)
)
check_response(resp)
edit_resp = json.loads(resp.data)
self.assertEqual(to_edit["source_name"], edit_resp["source_name"])
self.assertEqual(to_edit["source_description"],
edit_resp["source_description"])

def test_surveys(self):
resp = self.client.get(
'/api/accounts/%s/sources?language_tag=en-US' % ACCT_ID)
Expand Down Expand Up @@ -323,10 +359,12 @@ def test_bobo_takes_a_survey(self):
t.commit()

def test_create_new_account(self):

# Had to change from janedoe@example.com after I ran api/ui to create
# a janedoe@example.com address in my test db.
FAKE_EMAIL = "zbkhasdahl4wlnas@asdjgakljesgnoqe.com"
# Clean up before the test in case we already have a janedoe
with Transaction() as t:
AccountRepo(t).delete_account_by_email("janedoe@example.com")
AccountRepo(t).delete_account_by_email(FAKE_EMAIL)
t.commit()

""" Test: Create a new account using a kit id """
Expand All @@ -339,7 +377,7 @@ def test_create_new_account(self):
"state": "CA",
"street": "123 Main St. E. Apt. 2"
},
"email": "janedoe@example.com",
"email": FAKE_EMAIL,
"first_name": "Jane",
"last_name": "Doe",
"kit_name": "jb_qhxqe"
Expand Down Expand Up @@ -377,7 +415,7 @@ def test_create_new_account(self):

# Clean up after this test so we don't leave the account around
with Transaction() as t:
AccountRepo(t).delete_account_by_email("janedoe@example.com")
AccountRepo(t).delete_account_by_email(FAKE_EMAIL)
t.commit()

def test_edit_account_info(self):
Expand Down Expand Up @@ -529,14 +567,21 @@ def test_create_non_human_sources(self):
loc = resp.headers.get("Location")
url = werkzeug.urls.url_parse(loc)
source_id_from_loc = url.path.split('/')[-1]
new_source = json.loads(resp.data)
source_id_from_obj = new_source['source_id']
result_source = json.loads(resp.data)
source_id_from_obj = result_source['source_id']
self.assertIsNotNone(source_id_from_loc,
"Couldn't parse source id from location "
"header")
self.assertEqual(source_id_from_obj, source_id_from_obj,
self.assertEqual(source_id_from_loc, source_id_from_obj,
"Different source id from loc header and resp")

self.assertEqual(new_source["source_type"],
result_source["source_type"])
self.assertEqual(new_source["source_description"],
result_source["source_description"])
self.assertEqual(new_source["source_name"],
result_source["source_name"])

# TODO: It would be standard to make a test database and delete it
# or keep it entirely in memory. But the change scripts add in
# too much data to a default database to make this feasible for
Expand Down Expand Up @@ -672,7 +717,7 @@ def test_associate_sample_and_survey(self):
check_response(resp)
assoc_surveys = json.loads(resp.data)
self.assertFalse(any([survey_id == survey['survey_id']
for survey in assoc_surveys]),
for survey in assoc_surveys]),
"Deleted survey association was still around")

# Check that we can't assign a sample to a survey owned by a source
Expand Down Expand Up @@ -967,7 +1012,7 @@ def test_survey_localization(self):
'survey_template_id': BOBO_FAVORITE_SURVEY_TEMPLATE,
'survey_text': model_gb
})
)
)
check_response(resp, 400)

# Lastly, posting an answer that does translate but is wrong
Expand All @@ -983,7 +1028,7 @@ def test_survey_localization(self):
'survey_template_id': BOBO_FAVORITE_SURVEY_TEMPLATE,
'survey_text': model_gb
})
)
)
check_response(resp, 400)

with Transaction() as t:
Expand Down
28 changes: 24 additions & 4 deletions microsetta_private_api/model/source.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
from microsetta_private_api.model.model_base import ModelBase
from werkzeug.exceptions import BadRequest


def info_from_api(api_obj):
if api_obj["source_type"] == Source.SOURCE_TYPE_HUMAN:
# Note: consent_date and date_revoked aren't sent over the api
# so will be lost in translation
return human_info_from_api(api_obj,
consent_date=None,
date_revoked=None)
elif api_obj["source_type"] == Source.SOURCE_TYPE_ANIMAL:
return animal_decoder(api_obj)
elif api_obj["source_type"] == Source.SOURCE_TYPE_ENVIRONMENT:
return environment_decoder(api_obj)
else:
raise BadRequest("Unknown source_type: " + str(api_obj["source_type"]))


def human_info_from_api(human_source, consent_date, date_revoked):
Expand Down Expand Up @@ -104,10 +120,11 @@ def to_api(self):
consent = {
"participant_name": self.source_data.name,
"participant_email": self.source_data.email,
"parent_1_name": self.parent1_name,
"parent_2_name": self.parent2_name,
"deceased_parent": (self.parent1_deceased or
self.parent2_deceased),
"parent_1_name": self.source_data.parent1_name,
"parent_2_name": self.source_data.parent2_name,
"deceased_parent": (self.source_data.parent1_deceased
or
self.source_data.parent2_deceased),
"obtainer_name": self.source_data.assent_obtainer
}
else:
Expand All @@ -116,6 +133,9 @@ def to_api(self):
"participant_email": self.source_data.email
}
result["consent"] = consent
elif self.source_type in [Source.SOURCE_TYPE_ANIMAL,
Source.SOURCE_TYPE_ENVIRONMENT]:
result["source_description"] = self.source_data.description

return result

Expand Down
31 changes: 13 additions & 18 deletions microsetta_private_api/repo/source_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,28 @@ def get_source(self, account_id, source_id):
return None
return SourceRepo._row_to_source(r)

def update_source_data(self, source):
row = SourceRepo._source_to_row(source)

# Rotate id, account.id to end for the where clause
row_type_to_desc = row[2:]
row_id_and_acct = row[0:2]
final_row = row_type_to_desc + row_id_and_acct
def update_source_data_api_fields(self, source):
# Business Policy: For now I will let them edit only name and
# description. Anything else they have to recreate the source
# Everything else they send up, we currently ignore.
# TODO: Change yaml to remove extraneous fields?
# Raise exc in this layer?

with self._transaction.cursor() as cur:
cur.execute("UPDATE source "
"SET "
"source_type = %s, "
"source_name = %s, "
"participant_email = %s, "
"is_juvenile = %s, "
"parent_1_name = %s, "
"parent_2_name = %s, "
"deceased_parent = %s, "
"date_signed = %s, "
"date_revoked = %s, "
"assent_obtainer = %s, "
"age_range = %s, "
"description = %s "
"WHERE "
"source.id = %s AND "
"source.account_id = %s",
final_row)
(
getattr(source.source_data, 'name', None),
getattr(source.source_data, 'description', None),
source.id,
source.account_id
)
)
return cur.rowcount == 1

def create_source(self, source):
Expand Down

0 comments on commit 1821c0d

Please sign in to comment.