Skip to content

Commit

Permalink
Merge pull request #266 from biocore/designate_active_projects
Browse files Browse the repository at this point in the history
Designate active projects
  • Loading branch information
wasade authored Sep 23, 2020
2 parents 0b36add + 7a06268 commit b4847e3
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 71 deletions.
4 changes: 2 additions & 2 deletions microsetta_private_api/admin/admin_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ def validate_admin_access(token_info):
raise Unauthorized()


def get_projects(token_info):
def get_projects(token_info, is_active=None):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
projects_list = admin_repo.get_projects()
projects_list = admin_repo.get_projects(is_active)
result = [x.to_api() for x in projects_list]
return jsonify(result), 200

Expand Down
161 changes: 101 additions & 60 deletions microsetta_private_api/admin/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ class AdminRepoTests(AdminTests):
p.SUBPROJECT_NAME_KEY: "IBL SIBL",
p.ALIAS_KEY: "Healthy Sitting",
p.SPONSOR_KEY: "Crowdfunded",
p.COORDINATION_KEY: "TMI"}
p.COORDINATION_KEY: "TMI",
p.IS_ACTIVE_KEY: True}

# TODO FIXME HACK: Need to build mock barcodes rather than using
# these fixed ones
Expand Down Expand Up @@ -526,69 +527,109 @@ def test_get_survey(self):
survey)
self.assertTrue(found)

def test_get_projects(self):
def _set_up_and_query_projects(self, t, is_active_val):
updated_dict = self._FULL_PROJECT_DICT.copy()
updated_dict[p.PROJ_NAME_KEY] = 'test_proj'
input = p.Project.from_dict(updated_dict)

admin_repo = AdminRepo(t)
# existing project 8 in test db
admin_repo.update_project(8, input)

set_up_sql = """
-- add some additional scans to project 8 so can test that
-- computed statistics based on latest scans are choosing all
-- (and only) the scans that they should:
-- add a scan w an earlier timestamp (though added into db
-- later) than existing one showing that this barcode USED TO
-- have a problem but no longer does.
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000007640', '2012-11-01', 'no-registered-account');
-- add a second *sample-is-valid* scan for the same barcode
-- so can ensure that sample status count are distinct
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000007640', '2012-12-01', 'sample-is-valid');
-- add two additional scans for a different barcode: a valid
-- scan followed by a problem scan, thus indicting this sample
-- currently has a problem.
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000070796', '2020-07-01', 'sample-is-valid');
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000070796', '2020-09-01', 'no-registered-account');
UPDATE barcodes.project SET is_active = FALSE
WHERE project_id = 2;
"""
with t.cursor() as cur:
cur.execute(set_up_sql)

output = admin_repo.get_projects(is_active_val)

updated_dict["project_id"] = 8
updated_dict[p.COMPUTED_STATS_KEY] = \
{p.NUM_FULLY_RETURNED_KITS_KEY: 1,
p.NUM_KITS_KEY: 5,
p.NUM_KITS_W_PROBLEMS_KEY: 1,
'num_no_associated_source': 0,
'num_no_collection_info': 0,
'num_no_registered_account': 1,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 4,
p.NUM_PARTIALLY_RETURNED_KITS_KEY: 2,
p.NUM_SAMPLES_KEY: 20,
p.NUM_SAMPLES_RECEIVED_KEY: 5,
p.NUM_UNIQUE_SOURCES_KEY: 4}

return updated_dict, output

def test_get_projects_all(self):
with Transaction() as t:
admin_repo = AdminRepo(t)
# existing project 8 in test db
admin_repo.update_project(8, input)
updated_dict, output = self._set_up_and_query_projects(
t, is_active_val=None)

# Test we have the correct number of total projects
self.assertEqual(len(output), 56)

# For one fully-characterized test project, ensure all the
# output values are what we expect (project 8)
self.assertEqual(updated_dict, output[7].to_api())

def test_get_projects_active(self):
with Transaction() as t:
updated_dict, output = self._set_up_and_query_projects(
t, is_active_val=True)

set_up_sql = """
-- add some additional scans to project 8 so can test that
-- computed statistics based on latest scans are choosing all
-- (and only) the scans that they should:
-- add a scan w an earlier timestamp (though added into db
-- later) than existing one showing that this barcode USED TO
-- have a problem but no longer does.
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000007640', '2012-11-01', 'no-registered-account');
-- add a second *sample-is-valid* scan for the same barcode
-- so can ensure that sample status count are distinct
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000007640', '2012-12-01', 'sample-is-valid');
-- add two additional scans for a different barcode: a valid
-- scan followed by a problem scan, thus indicting this sample
-- currently has a problem.
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000070796', '2020-07-01', 'sample-is-valid');
insert into barcodes.barcode_scans
(barcode, scan_timestamp, sample_status)
VALUES ('000070796', '2020-09-01', 'no-registered-account');
"""
# Test we have the correct number of active projects
self.assertEqual(len(output), 55)

# For one fully-characterized test project, ensure all the
# output values are what we expect. Project 8 is now 7th in
# list (note zero-based) bc project 2 is inactive, so not returned
self.assertEqual(updated_dict, output[6].to_api())

def test_get_projects_inactive(self):
with Transaction() as t:
with t.cursor() as cur:
cur.execute(set_up_sql)

output = admin_repo.get_projects()

updated_dict["project_id"] = 8
updated_dict[p.COMPUTED_STATS_KEY] = \
{p.NUM_FULLY_RETURNED_KITS_KEY: 1,
p.NUM_KITS_KEY: 5,
p.NUM_KITS_W_PROBLEMS_KEY: 1,
'num_no_associated_source': 0,
'num_no_collection_info': 0,
'num_no_registered_account': 1,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 4,
p.NUM_PARTIALLY_RETURNED_KITS_KEY: 2,
p.NUM_SAMPLES_KEY: 20,
p.NUM_SAMPLES_RECEIVED_KEY: 5,
p.NUM_UNIQUE_SOURCES_KEY: 4}

# Test we have the correct number of projects
self.assertEqual(len(output), 56)

# For one fully-characterized test project, ensure all the
# output values are what we expect
test_proj = output[7] # ordered by project id, so this is proj 8
test_proj_dict = test_proj.to_api()
self.assertEqual(updated_dict, test_proj_dict)
cur.execute("UPDATE barcodes.project"
" SET is_active = FALSE"
" WHERE project_id = 8;")

updated_dict, output = self._set_up_and_query_projects(
t, is_active_val=False)

updated_dict["is_active"] = False

# Test we have the correct number of inactive projects
self.assertEqual(len(output), 2)

# For one fully-characterized test project, ensure all the
# output values are what we expect. Project 8 is now inactive,
# and is 2nd in (zero-based) list, after project 2
self.assertEqual(updated_dict, output[1].to_api())
149 changes: 146 additions & 3 deletions microsetta_private_api/admin/tests/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def teardown_test_data():
admin_repo = AdminRepo(t)
acct_repo.delete_account(ACCT_ID_1)
admin_repo.delete_project_by_name(DUMMY_PROJ_NAME)
with t.cursor() as cur:
cur.execute("UPDATE barcodes.project"
" SET is_active = TRUE"
" WHERE project_id = 2")
t.commit()


Expand All @@ -56,6 +60,11 @@ def setup_test_data():
),
"fakekit")
acct_repo.create_account(acc)

with t.cursor() as cur:
cur.execute("UPDATE barcodes.project"
" SET is_active = FALSE"
" WHERE project_id = 2")
t.commit()


Expand Down Expand Up @@ -96,7 +105,8 @@ class AdminApiTests(TestCase):
"subproject_name": "IBL SIBL",
"alias": "Healthy Sitting",
"sponsor": "Crowdfunded",
"coordination": "TMI"}
"coordination": "TMI",
"is_active": True}

TEST_BARCODE = '000000001'

Expand Down Expand Up @@ -272,7 +282,7 @@ def test_update_project(self):
expected_result["project_id"] = proj_id
self.assertEqual(expected_result, stored_result)

def test_get_projects(self):
def test_get_projects_all(self):
# execute projects get
response = self.client.get(
"/api/admin/projects",
Expand Down Expand Up @@ -333,10 +343,143 @@ def test_get_projects(self):
'project_name': 'Project - %u[zGmÅq=g',
'sponsor': None,
'start_date': None,
'subproject_name': None}
'subproject_name': None,
'is_active': True}
self.assertEqual(56, len(response_obj))
self.assertEqual(expected_record, response_obj[7])

def test_get_projects_active(self):
# execute projects get
response = self.client.get(
"/api/admin/projects?is_active=true",
headers=MOCK_HEADERS
)

# check response code
self.assertEqual(200, response.status_code)

# load the response body
response_obj = json.loads(response.data)

expected_record = {'additional_contact_name': None,
'alias': None,
'bank_samples': False,
'branding_associated_instructions': None,
'branding_status': None,
'collection': None,
'computed_stats': {
'num_fully_returned_kits': 1,
'num_kits': 5,
'num_kits_w_problems': 0,
'num_no_associated_source': 0,
'num_no_collection_info': 0,
'num_no_registered_account': 0,
'num_partially_returned_kits': 1,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 4,
'num_samples': 20,
'num_samples_received': 4,
'num_unique_sources': 4},
'contact_email': None,
'contact_name': None,
'coordination': None,
'deadlines': None,
'disposition_comments': None,
'do_16s': None,
'do_mass_spec': None,
'do_metatranscriptomics': None,
'do_other': None,
'do_rt_qpcr': None,
'do_serology': None,
'do_shallow_shotgun': None,
'do_shotgun': None,
'is_blood': None,
'is_fecal': None,
'is_microsetta': False,
'is_other': None,
'is_saliva': None,
'is_skin': None,
'mass_spec_comments': None,
'mass_spec_contact_email': None,
'mass_spec_contact_name': None,
'num_subjects': None,
'num_timepoints': None,
'plating_start_date': None,
'project_id': 8,
'project_name': 'Project - %u[zGmÅq=g',
'sponsor': None,
'start_date': None,
'subproject_name': None,
'is_active': True}
self.assertEqual(55, len(response_obj))
self.assertEqual(expected_record, response_obj[6])

def test_get_projects_inactive(self):
# execute projects get
response = self.client.get(
"/api/admin/projects?is_active=false",
headers=MOCK_HEADERS
)

# check response code
self.assertEqual(200, response.status_code)

# load the response body
response_obj = json.loads(response.data)

expected_record = {'additional_contact_name': None,
'alias': None,
'bank_samples': False,
'branding_associated_instructions': None,
'branding_status': None,
'collection': None,
'computed_stats':
{'num_fully_returned_kits': 0,
'num_kits': 0,
'num_kits_w_problems': 0,
'num_no_associated_source': 0,
'num_no_collection_info': 0,
'num_no_registered_account': 0,
'num_partially_returned_kits': 0,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 0,
'num_samples': 800,
'num_samples_received': 0,
'num_unique_sources': 0},
'contact_email': None,
'contact_name': None,
'coordination': None,
'deadlines': None,
'disposition_comments': None,
'do_16s': None,
'do_mass_spec': None,
'do_metatranscriptomics': None,
'do_other': None,
'do_rt_qpcr': None,
'do_serology': None,
'do_shallow_shotgun': None,
'do_shotgun': None,
'is_active': False,
'is_blood': None,
'is_fecal': None,
'is_microsetta': False,
'is_other': None,
'is_saliva': None,
'is_skin': None,
'mass_spec_comments': None,
'mass_spec_contact_email': None,
'mass_spec_contact_name': None,
'num_subjects': None,
'num_timepoints': None,
'plating_start_date': None,
'project_id': 2,
'project_name': 'Project - /J/xL_|Eãt',
'sponsor': None,
'start_date': None,
'subproject_name': None}
self.assertEqual(1, len(response_obj))
self.assertEqual(expected_record, response_obj[0])

def test_scan_barcode_success(self):
"""Store info on new scan for valid barcode"""

Expand Down
Loading

0 comments on commit b4847e3

Please sign in to comment.