Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_dict_sorter.py: 100%

1 from mixinforge.dict_sorter import sort_dict_by_keys

2

3 import pytest

4

5

6 def test_sort_dict_by_keys_basic():

7 """

8 Test that the function correctly sorts a dictionary

9 whose keys are in an arbitrary order.

10 """

11 unsorted_dict = {"c": 3, "a": 1, "b": 2}

12 expected = {"a": 1, "b": 2, "c": 3}

13 assert sort_dict_by_keys(unsorted_dict) == expected

14

15 def test_sort_dict_by_keys_already_sorted():

16 """

17 Test that the function returns the same dictionary

18 if the keys are already sorted.

19 """

20 sorted_dict = {"a": 1, "b": 2, "c": 3}

21 assert sort_dict_by_keys(sorted_dict) == sorted_dict

22

23 def test_sort_dict_by_keys_empty():

24 """

25 Test that the function handles an empty dictionary.

26 """

27 assert sort_dict_by_keys({}) == {}

28

29 def test_sort_dict_by_keys_single_key():

30 """

31 Test that the function works with a dictionary that has only one key.

32 """

33 single_key_dict = {"a": 1}

34 assert sort_dict_by_keys(single_key_dict) == single_key_dict

35

36 def test_sort_dict_by_keys_case_sensitivity():

37 """

38 Test how the function handles case-sensitive sorting of keys.

39 In ASCII, uppercase letters come before lowercase letters.

40 """

41 mixed_case_dict = {"b": 2, "A": 1, "C": 3}

42 # Sorted order by key (ASCII-based): "A" < "C" < "b"

43 expected = {"A": 1, "C": 3, "b": 2}

44 assert sort_dict_by_keys(mixed_case_dict) == expected

45

46 def test_sort_dict_by_keys_numeric_strings():

47 """

48 Test that numeric strings as keys are also sorted correctly

49 (lexicographically).

50 """

51 numeric_str_keys = {"10": "ten", "2": "two", "1": "one"}

52 # Lexicographic sort: "1", "10", "2"

53 expected = {"1": "one", "10": "ten", "2": "two"}

54 assert sort_dict_by_keys(numeric_str_keys) == expected

55

56 def test_sort_dict_by_keys_assert_on_non_dict():

57 """

58 Test that the function raises an AssertionError

59 when a non-dict is passed.

60 """

61 with pytest.raises(TypeError):

62 sort_dict_by_keys(["not", "a", "dict"])