Skip to content

Commit

Permalink
Merge pull request #602 from biocore/csymons_sbi_prelaunch
Browse files Browse the repository at this point in the history
SBI Pre-launch Adjustments
  • Loading branch information
cassidysymons authored Feb 20, 2025
2 parents d9653bc + d0a0f35 commit 5b60918
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
49 changes: 31 additions & 18 deletions microsetta_private_api/repo/sample_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,26 +612,39 @@ def _validate_barcode_meta(self, sample_site, barcode_meta):
bc_valid = True
for fn, fv in barcode_meta.items():
if fn == "sample_site_last_washed_date":
try:
ret_val = datetime.datetime.strptime(
fv,
self.SAMPLE_SITE_LAST_WASHED_DATE_FORMAT
)
ret_dict[fn] = ret_val
except ValueError:
bc_valid = False
if fv is None or len(fv) == 0:
# Null value is acceptable, bypass format validation
ret_dict[fn] = None
else:
try:
ret_val = datetime.datetime.strptime(
fv,
self.SAMPLE_SITE_LAST_WASHED_DATE_FORMAT
)
ret_dict[fn] = ret_val
except ValueError:
bc_valid = False
elif fn == "sample_site_last_washed_time":
try:
ret_val = datetime.datetime.strptime(
fv,
self.SAMPLE_SITE_LAST_WASHED_TIME_FORMAT_STRPTIME
)
ret_dict[fn] = ret_val
except ValueError:
bc_valid = False
if fv is None or len(fv) == 0:
# Null value is acceptable, bypass format validation
ret_dict[fn] = None
else:
try:
ret_val = datetime.datetime.strptime(
fv,
self.SAMPLE_SITE_LAST_WASHED_TIME_FORMAT_STRPTIME
)
ret_dict[fn] = ret_val
except ValueError:
bc_valid = False
elif fn == "sample_site_last_washed_product":
# The ENUM type in the database will validate the value
ret_dict[fn] = fv
if fv is None or len(fv) == 0:
# Ensure that blank value - None/Null or "" - is passed on
# as None
ret_dict[fn] = None
else:
# The ENUM type in the database will validate the value
ret_dict[fn] = fv
else:
bc_valid = False

Expand Down
61 changes: 61 additions & 0 deletions microsetta_private_api/repo/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,49 @@ def test_validate_barcode_meta_pass(self):
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertNotEqual(bc_valid, False)

# Test each scenario where only one field is present to confirm
# that any other field can be nullable
bc_meta = {
"sample_site_last_washed_date": "01/10/2025",
"sample_site_last_washed_time": "",
"sample_site_last_washed_product": ""
}
exp = {
"sample_site_last_washed_date": datetime.datetime(
2025, 1, 10),
"sample_site_last_washed_time": None,
"sample_site_last_washed_product": None
}
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertEqual(bc_valid, exp)

bc_meta = {
"sample_site_last_washed_date": "",
"sample_site_last_washed_time": "9:30 AM",
"sample_site_last_washed_product": ""
}
exp = {
"sample_site_last_washed_date": None,
"sample_site_last_washed_time": datetime.datetime(
1900, 1, 1, 9, 30),
"sample_site_last_washed_product": None
}
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertEqual(bc_valid, exp)

bc_meta = {
"sample_site_last_washed_date": "",
"sample_site_last_washed_time": "",
"sample_site_last_washed_product": "Face cleanser"
}
exp = {
"sample_site_last_washed_date": None,
"sample_site_last_washed_time": None,
"sample_site_last_washed_product": "Face cleanser"
}
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertEqual(bc_valid, exp)

# Confirm that empty dicts pass, regardless of site
bc_meta = {}
bc_valid = sample_repo._validate_barcode_meta("Stool", bc_meta)
Expand All @@ -206,6 +249,24 @@ def test_validate_barcode_meta_fail(self):
bc_valid = sample_repo._validate_barcode_meta("Stool", bc_meta)
self.assertFalse(bc_valid)

# Try using an invalid value for the date
bc_meta = {
"sample_site_last_washed_date": "Cookie monster",
"sample_site_last_washed_time": "",
"sample_site_last_washed_product": "Face cleanser"
}
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertEqual(bc_valid, False)

# Try using an invalid value for the time
bc_meta = {
"sample_site_last_washed_date": "",
"sample_site_last_washed_time": "Rosemary focaccia",
"sample_site_last_washed_product": "Face cleanser"
}
bc_valid = sample_repo._validate_barcode_meta("Cheek", bc_meta)
self.assertEqual(bc_valid, False)

def test_update_barcode_meta_via_update_info(self):
# We're going to use a stable sample and override_locked to test
# the barcode meta update via update_info()
Expand Down

0 comments on commit 5b60918

Please sign in to comment.