|
17 | 17 | _fetch_observed_survey_templates,
|
18 | 18 | _construct_multiselect_map,
|
19 | 19 | _find_best_answers,
|
20 |
| - drop_private_columns) |
| 20 | + drop_private_columns, |
| 21 | + _get_freetext_fields, |
| 22 | + EBI_REMOVE) |
21 | 23 | from microsetta_private_api.repo.survey_template_repo import SurveyTemplateRepo
|
22 | 24 | from microsetta_private_api.model.account import Account
|
23 | 25 | from microsetta_private_api.model.address import Address
|
| 26 | +from microsetta_private_api.repo.transaction import Transaction |
24 | 27 |
|
25 | 28 |
|
26 | 29 | class MM:
|
@@ -329,6 +332,32 @@ def test_drop_private_columns(self):
|
329 | 332 | obs = drop_private_columns(df)
|
330 | 333 | pdt.assert_frame_equal(obs, exp)
|
331 | 334 |
|
| 335 | + def test_drop_private_columns_freetext(self): |
| 336 | + # This test specifically asserts that the new code to drop free-text |
| 337 | + # fields works, even if those fields are not represented in the |
| 338 | + # EBI_REMOVE list |
| 339 | + |
| 340 | + # First, assert that ALL_ROOMMATES is not in EBI_REMOVE |
| 341 | + self.assertFalse("ALL_ROOMMATES" in EBI_REMOVE) |
| 342 | + |
| 343 | + # Next, assert that ALL_ROOMMATES is a free-text field |
| 344 | + freetext_fields = _get_freetext_fields() |
| 345 | + self.assertTrue("ALL_ROOMMATES" in freetext_fields) |
| 346 | + |
| 347 | + # Now, set up a test dataframe, based on the existing |
| 348 | + # test_drop_private_columns df, but with the ALL_ROOMMATES field added |
| 349 | + df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], |
| 350 | + columns=[ |
| 351 | + 'pM_foo', |
| 352 | + 'okay', |
| 353 | + 'ABOUT_yourSELF_TEXT', |
| 354 | + 'ALL_ROOMMATES']) |
| 355 | + |
| 356 | + # We only expect the "okay" column to remain |
| 357 | + exp = pd.DataFrame([[2, ], [6, ]], columns=['okay']) |
| 358 | + obs = drop_private_columns(df) |
| 359 | + pdt.assert_frame_equal(obs, exp) |
| 360 | + |
332 | 361 | def test_build_col_name(self):
|
333 | 362 | tests_and_expected = [('foo', 'bar', 'foo_bar'),
|
334 | 363 | ('foo', 'bar baz', 'foo_bar_baz')]
|
@@ -512,6 +541,30 @@ def test_find_best_answers(self):
|
512 | 541 | with self.assertRaises(KeyError):
|
513 | 542 | _ = obs[0]['response']['111']
|
514 | 543 |
|
| 544 | + def test_get_freetext_fields(self): |
| 545 | + with Transaction() as t: |
| 546 | + with t.cursor() as cur: |
| 547 | + # Grab the count for the number of free-text fields that exist |
| 548 | + # in the database |
| 549 | + cur.execute( |
| 550 | + "SELECT COUNT(*) " |
| 551 | + "FROM ag.survey_question_response_type " |
| 552 | + "WHERE survey_response_type IN ('TEXT', 'STRING')" |
| 553 | + ) |
| 554 | + row = cur.fetchone() |
| 555 | + freetext_count = row[0] |
| 556 | + |
| 557 | + # Use the _get_freetext_fields() function to pull the actual list |
| 558 | + freetext_fields = _get_freetext_fields() |
| 559 | + |
| 560 | + # Assert that the field count matches |
| 561 | + self.assertEqual(len(freetext_fields), freetext_count) |
| 562 | + |
| 563 | + # Assert that a few known free-text fields exist in the list |
| 564 | + self.assertTrue("ABOUT_YOURSELF_TEXT" in freetext_fields) |
| 565 | + self.assertTrue("ALL_ROOMMATES" in freetext_fields) |
| 566 | + self.assertTrue("DIET_RESTRICTIONS" in freetext_fields) |
| 567 | + |
515 | 568 |
|
516 | 569 | if __name__ == '__main__':
|
517 | 570 | unittest.main()
|
0 commit comments