Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Oct 23, 2024
1 parent f54bb68 commit a563e71
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 4 additions & 1 deletion onadata/libs/tests/utils/test_csv_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,7 +2090,10 @@ def test_entity_list_dataset(self):
self.assertCountEqual(row, expected_row)

def test_extra_columns_dataview(self):
"""Extra columns are included in export for dataview"""
"""Extra columns are included in export for dataview
Only extra columns in the dataview are included in the export
"""
md_xform = """
| survey |
| | type | name | label |
Expand Down
36 changes: 29 additions & 7 deletions onadata/libs/tests/utils/test_export_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Tests Export Builder Functionality
"""

from __future__ import unicode_literals

import csv
Expand All @@ -23,6 +24,7 @@
from savReaderWriter import SavHeaderReader, SavReader

from onadata.apps.logger.import_tools import django_file
from onadata.apps.logger.models import DataView
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.viewer.models.data_dictionary import DataDictionary
from onadata.apps.viewer.models.parsed_instance import _encode_for_mongo, query_data
Expand Down Expand Up @@ -1622,10 +1624,10 @@ def test_to_xlsx_export_works(self):
"children_cartoons",
"children_cartoons_characters",
]
self.assertEqual(list(workbook.get_sheet_names()), expected_sheet_names)
self.assertEqual(list(workbook.sheetnames), expected_sheet_names)

# check header columns
main_sheet = workbook.get_sheet_by_name("childrens_survey")
main_sheet = workbook["childrens_survey"]
expected_column_headers = [
"name",
"age",
Expand Down Expand Up @@ -1654,7 +1656,7 @@ def test_to_xlsx_export_works(self):
sorted(list(column_headers)), sorted(expected_column_headers)
)

childrens_sheet = workbook.get_sheet_by_name("children")
childrens_sheet = workbook["children"]
expected_column_headers = [
"children/name",
"children/age",
Expand Down Expand Up @@ -1683,7 +1685,7 @@ def test_to_xlsx_export_works(self):
sorted(list(column_headers)), sorted(expected_column_headers)
)

cartoons_sheet = workbook.get_sheet_by_name("children_cartoons")
cartoons_sheet = workbook["children_cartoons"]
expected_column_headers = [
"children/cartoons/name",
"children/cartoons/why",
Expand All @@ -1704,9 +1706,7 @@ def test_to_xlsx_export_works(self):
sorted(list(column_headers)), sorted(expected_column_headers)
)

characters_sheet = workbook.get_sheet_by_name(
"children_cartoons_characters"
)
characters_sheet = workbook["children_cartoons_characters"]
expected_column_headers = [
"children/cartoons/characters/name",
"children/cartoons/characters/good_or_evil",
Expand Down Expand Up @@ -3668,3 +3668,25 @@ def test_sav_export_with_duplicate_metadata(self, mock_uuid):
rows[1] = list(map(_str_if_bytes, rows[1]))
self.assertEqual(expected_data, rows)
shutil.rmtree(temp_dir)

def test_extra_columns_dataview(self):
"""Extra columns are included in export for dataview
Only extra columns in the dataview are included in the export
"""
survey = self._create_childrens_survey()
self._publish_xls_file_and_set_xform(
_logger_fixture_path("childrens_survey.xlsx")
)
export_builder = ExportBuilder()
export_builder.set_survey(survey)
dataview = DataView.objects.create(
xform=self.xform,
name="test",
columns=["name", "_id"],
project=self.project,
)
fields = export_builder.get_fields(
dataview, export_builder.sections[0], "title"
)
self.assertEqual(fields, ["name", "_id"])
2 changes: 2 additions & 0 deletions onadata/libs/utils/export_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,8 @@ def get_fields(self, dataview, section, key):
if column in self.extra_columns:
columns.append(column)

return columns

return [
(
element.get("_label_xpath") or element[key]
Expand Down

0 comments on commit a563e71

Please sign in to comment.