diff --git a/onadata/libs/tests/utils/test_csv_builder.py b/onadata/libs/tests/utils/test_csv_builder.py index a78d27374d..e351959804 100644 --- a/onadata/libs/tests/utils/test_csv_builder.py +++ b/onadata/libs/tests/utils/test_csv_builder.py @@ -2288,13 +2288,8 @@ def test_registered_repeats(self): self.assertEqual(row, expected_row) csv_file.close() - @patch("onadata.libs.utils.csv_builder.register_xform_export_repeats_async.delay") - def test_registered_repeat_not_found(self, mock_register_repeats): - """Registered repeat not found in MetaData - - If a registered repeat is not found in MetaData, the columns - should be generated based on the data. - """ + def test_repeat_not_found_in_register(self): + """Repeat not found in register""" md_xform = """ | survey | | | type | name | label | @@ -2368,15 +2363,7 @@ def test_registered_repeat_not_found(self, mock_register_repeats): expected_header = [ "hospital_repeat[1]/hospital", "hospital_repeat[2]/hospital", - "hospital_repeat[1]/child_repeat[1]/name", - "hospital_repeat[1]/child_repeat[1]/birthweight", - "hospital_repeat[1]/child_repeat[1]/sex", - "hospital_repeat[1]/child_repeat[2]/name", - "hospital_repeat[1]/child_repeat[2]/birthweight", - "hospital_repeat[1]/child_repeat[2]/sex", - "hospital_repeat[2]/child_repeat[1]/name", - "hospital_repeat[2]/child_repeat[1]/birthweight", - "hospital_repeat[2]/child_repeat[1]/sex", + "meta/instanceID", "_id", "_uuid", "_submission_time", @@ -2391,7 +2378,7 @@ def test_registered_repeat_not_found(self, mock_register_repeats): "_media_all_received", ] self.assertCountEqual(header, expected_header) - # Missing registered parent repeat should not affect the export + # Parent repeat not registered metadata.extra_data = {"child_repeat": 2} metadata.save builder = CSVDataFrameBuilder( @@ -2406,4 +2393,3 @@ def test_registered_repeat_not_found(self, mock_register_repeats): header = next(csv_reader) self.assertCountEqual(header, expected_header) csv_file.close() - mock_register_repeats.assert_called() diff --git a/onadata/libs/utils/csv_builder.py b/onadata/libs/utils/csv_builder.py index 65f4e32ecf..166a13f283 100644 --- a/onadata/libs/utils/csv_builder.py +++ b/onadata/libs/utils/csv_builder.py @@ -799,17 +799,6 @@ def build_repeat_cols_from_data(): # Register repeat columns for future use register_xform_export_repeats_async.delay(self.xform.pk) - def fall_back_to_data(): - """Fall back to building repeat columns from data. - - Revert any changes made to the ordered columns. - """ - for column_xpath in self.ordered_columns: - if isinstance(value, list) and not self.ordered_columns[column_xpath]: - self.ordered_columns[column_xpath] = [] - - build_repeat_cols_from_data() - try: registered_repeats = MetaData.objects.get( content_type=content_type, @@ -847,28 +836,22 @@ def build_repeat_columns(repeat_xpath, num_repeats, parent_prefix=None): child_repeat_xpath = element.get_abbreviated_xpath() child_repeat_name = child_repeat_xpath.split("/")[-1] child_repeat_num = registered_repeats.extra_data.get( - child_repeat_name - ) - if child_repeat_num is None: - fall_back_to_data() - return - - build_repeat_columns( - child_repeat_xpath, child_repeat_num, prefix + child_repeat_name, 0 ) + if child_repeat_num: + build_repeat_columns( + child_repeat_xpath, child_repeat_num, prefix + ) for column_xpath, value in self.ordered_columns.items(): if isinstance(value, list) and not value: # Build repeat columns, start from parent and recurse into children # repeat columns repeat_name = column_xpath.split("/")[-1] - repeat_num = registered_repeats.extra_data.get(repeat_name) - - if repeat_num is None: - fall_back_to_data() - return + repeat_num = registered_repeats.extra_data.get(repeat_name, 0) - build_repeat_columns(column_xpath, repeat_num) + if repeat_num: + build_repeat_columns(column_xpath, repeat_num) def _format_for_dataframe(self, cursor): """