Skip to content

Commit

Permalink
Add test_to_csv()
Browse files Browse the repository at this point in the history
  • Loading branch information
davitmamrikishvili committed Jul 3, 2024
1 parent 058daf6 commit 569ea3d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,5 @@ Benchmark.
Other converters also have some not big speed improvements.
9. [TH2-5213] Extend cache_files_reading_speed with csv support.
9. [TH2-5213] Extend cache_files_reading_speed with csv support. Added `Data.to_json` method, that converts
data to valid csv.
3 changes: 3 additions & 0 deletions tests/test_files/file_to_test_to_csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a,b,c,d
1,2,,
,,3,4
3 changes: 3 additions & 0 deletions tests/test_files/file_to_test_to_csv_expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a,b,c,d
1,2,,
,,3,4
12 changes: 12 additions & 0 deletions tests/tests_unit/test_data/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ def test_to_json():
assert l1 == l2


def test_to_csv():
data = Data([{"a": 1, "b": 2}, {"c": 3, "d": 4}])
path_result = "tests/test_files/file_to_test_to_csv.csv"
path_expected = "tests/test_files/file_to_test_to_csv_expected.csv"

data.to_csv(path_result, overwrite=True)

with open(path_result, encoding="utf-8") as f1, open(path_expected, encoding="utf-8") as f2:
for l1, l2 in zip(f1, f2):
assert l1 == l2


class TestDataObjectJoining:
@classmethod
def setup_class(cls):
Expand Down
4 changes: 2 additions & 2 deletions th2_data_services/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ def to_csv(
"""Converts Data to csv.
Args:
filename (str): Output JSON filename.
filename (str): Output CSV filename.
overwrite (bool, optional): Overwrite if filename exists. Defaults to False.
NOTE:
Expand All @@ -1173,7 +1173,7 @@ def to_csv(
writer.writerows(self)
else:
writer = csv.DictWriter(
file, fieldnames=list(set().union(*[d.keys() for d in self]))
file, fieldnames=sorted(set().union(*[d.keys() for d in self]))
)
writer.writeheader()
for row_dict in self:
Expand Down

0 comments on commit 569ea3d

Please sign in to comment.