Skip to content

Commit

Permalink
Merge c8af43a into 03410c1
Browse files Browse the repository at this point in the history
  • Loading branch information
AmandaBirmingham committed Oct 27, 2020
2 parents 03410c1 + c8af43a commit 1595399
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 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 @@ -144,14 +144,14 @@ def create_kits(body, token_info):
number_of_kits = body['number_of_kits']
number_of_samples = body['number_of_samples']
kit_prefix = body.get('kit_id_prefix', None)
projects = body['projects']
project_ids = body['project_ids']

with Transaction() as t:
admin_repo = AdminRepo(t)

try:
kits = admin_repo.create_kits(number_of_kits, number_of_samples,
kit_prefix, projects)
kit_prefix, project_ids)
except KeyError:
return jsonify(code=422, message="Unable to create kits"), 422
else:
Expand Down
6 changes: 3 additions & 3 deletions microsetta_private_api/admin/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,13 @@ def test_create_kits_fail_nonexistent_project(self):
admin_repo = AdminRepo(t)

with self.assertRaisesRegex(KeyError, "does not exist"):
admin_repo.create_kits(5, 3, '', ['foo', 'bar'])
admin_repo.create_kits(5, 3, '', [10000, 10001])

def test_create_kits_success_not_microsetta(self):
with Transaction() as t:
admin_repo = AdminRepo(t)
non_tmi = admin_repo.create_kits(5, 3, '',
['Project - lm3eáqç(>?'])
[33])
self.assertEqual(['created', ], list(non_tmi.keys()))
self.assertEqual(len(non_tmi['created']), 5)
for obj in non_tmi['created']:
Expand All @@ -406,7 +406,7 @@ def test_create_kits_success_is_microsetta(self):
with Transaction() as t:
admin_repo = AdminRepo(t)
tmi = admin_repo.create_kits(4, 2, 'foo',
['American Gut Project'])
[1])
self.assertEqual(['created', ], list(tmi.keys()))
self.assertEqual(len(tmi['created']), 4)
for obj in tmi['created']:
Expand Down
4 changes: 2 additions & 2 deletions microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,14 @@ paths:
type: integer
kit_id_prefix:
type: string
projects:
project_ids:
type: array
items:
type: string
required:
- number_of_kits
- number_of_samples
- projects
- project_ids
responses:
'201':
description: Kit identifiers and associated samples were successfully created
Expand Down
28 changes: 16 additions & 12 deletions microsetta_private_api/repo/admin_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ def _generate_random_kit_name(self, name_length, prefix):
return prefix + '_' + rand_name

def create_kits(self, number_of_kits, number_of_samples, kit_prefix,
projects):
project_ids):
"""Create kits each with the same number of samples
Parameters
Expand All @@ -658,24 +658,29 @@ def create_kits(self, number_of_kits, number_of_samples, kit_prefix,
Number of samples that each kit will contain
kit_prefix : str or None
A prefix to put on to the kit IDs, this is optional.
projects : list of str
Project names the samples are to be associated with
project_ids : list of int
Project ids the samples are to be associated with
"""
# int ids come in as strings ...
project_ids = [int(x) for x in project_ids]

with self._transaction.cursor() as cur:
# get existing projects
query = f"""
SELECT {p.DB_PROJ_NAME_KEY}, project_id,
SELECT project_id,
{p.IS_MICROSETTA_KEY}
FROM barcodes.project;"""

cur.execute(query)
known_projects = {prj: (id_, tmi)
for prj, id_, tmi in cur.fetchall()}
projects_tmi = {id_: bool(tmi) for id_, tmi in cur.fetchall()}
is_tmi = False
for name in projects:
if name not in known_projects:
raise KeyError("%s does not exist" % name)
if known_projects[name][1]:
for input_proj_id in project_ids:
if input_proj_id not in projects_tmi:
raise KeyError("Project id %s does not exist" %
input_proj_id)
# if *any* of the projects the kits will be associate with are
# microsetta projects, set is_tmi to true
if projects_tmi[input_proj_id]:
is_tmi = True

# get existing kits to test for conflicts
Expand Down Expand Up @@ -717,8 +722,7 @@ def create_kits(self, number_of_kits, number_of_samples, kit_prefix,
# create barcode project associations
barcode_projects = []
for barcode in new_barcodes:
for project in projects:
prj_id = known_projects[project][0]
for prj_id in project_ids:
barcode_projects.append((barcode, prj_id))

# create shipping IDs
Expand Down

0 comments on commit 1595399

Please sign in to comment.