-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/tests/unit/common/utils/test_sort_dtos_by_attribute.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |