Skip to content

Commit

Permalink
test: unit tests for sort dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
pbullhove committed Oct 5, 2023
1 parent 91c774b commit 8ba2d59
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/tests/unit/common/utils/test_sort_dtos_by_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest

from common.utils.sort_entities_by_attribute import ( # replace 'your_module' with the actual module name
sort_dtos_by_attribute,
)


class TestSortDTOsByAttribute(unittest.TestCase):
def test_sort_by_single_attribute(self):
data = [{"age": 21}, {"age": 40}, {"age": 18}]
sorted_data = sort_dtos_by_attribute(data, "age")
self.assertEqual(sorted_data, [{"age": 18}, {"age": 21}, {"age": 40}])

def test_sort_by_nested_attribute(self):
data = [{"info": {"age": 21}}, {"info": {"age": 40}}, {"info": {"age": 18}}]
sorted_data = sort_dtos_by_attribute(data, "info.age")
self.assertEqual(sorted_data, [{"info": {"age": 18}}, {"info": {"age": 21}}, {"info": {"age": 40}}])

def test_sort_by_parent_attribute_raises_TypeError(self):
data = [{"info": {"age": 21}}, {"info": {"age": 40}}, {"info": {"age": 18}}]
with self.assertRaises(TypeError):
# Can't compare dict with dict
sorted_data = sort_dtos_by_attribute(data, "info")

def test_empty_list(self):
data = []
sorted_data = sort_dtos_by_attribute(data, "age")
self.assertEqual(sorted_data, [])

def test_invalid_attribute_raises_KeyError(self):
data = [{"age": 21}, {"age": 40}, {"age": 18}]
with self.assertRaises(KeyError):
sort_dtos_by_attribute(data, "invalid_attribute")

def test_incomparable_data_types_raises_TypeError(self):
data = [{"age": "21"}, {"age": 40}, {"age": 18}]
with self.assertRaises(TypeError):
# raises TypeError because can't compare "str" and "int"
sort_dtos_by_attribute(data, "age")

# Add more test cases as you find necessary


if __name__ == "__main__":
unittest.main()

0 comments on commit 8ba2d59

Please sign in to comment.