Skip to content

Commit

Permalink
do not fall back to data if repeat unregistered
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jan 8, 2025
1 parent 9ee0e51 commit 57a7682
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
22 changes: 4 additions & 18 deletions onadata/libs/tests/utils/test_csv_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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()
33 changes: 8 additions & 25 deletions onadata/libs/utils/csv_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 57a7682

Please sign in to comment.