Beispiel #1
0
    def test_with_given_list_of_dictionaries_with_key_not_string_should_raise_exception(
            self):
        given = [{
            'name': 'Marto',
            27: 24
        }, {
            27: 25
        }, {
            'name': 'Sashko',
            27: 25
        }]
        key = 27

        exc = None

        # ACT
        try:
            result = my_sort(given, key=key)
        except Exception as err:
            exc = err

        # ASSERTS

        self.assertIsNotNone(exc)
        self.assertEqual(str(exc), 'Key {k}: not a string!'.format(k=key))
Beispiel #2
0
    def test_with_given_list_of_dictionaries_existing_key_and_ascending_order_should_sort_in_ascending_order(
            self):
        dictionary = [{
            'name': 'Marto',
            'age': 24
        }, {
            'name': 'Ivo',
            'age': 27
        }, {
            'name': 'Sashko',
            'age': 25
        }]
        key = 'age'
        ascending = True

        result = my_sort(dictionary, ascending, key)
        expected = [{
            'name': 'Marto',
            'age': 24
        }, {
            'name': 'Sashko',
            'age': 25
        }, {
            'name': 'Ivo',
            'age': 27
        }]

        self.assertEqual(result, expected)
Beispiel #3
0
    def test_with_given_tuple_of_dictionaries_existing_key_and_descending_order_should_sort_in_descending_order(
            self):
        dictionary = ({
            'name': 'Marto',
            'age': 24
        }, {
            'name': 'Ivo',
            'age': 27
        }, {
            'name': 'Sashko',
            'age': 25
        })
        key = 'age'
        ascending = False

        result = my_sort(dictionary, ascending, key)
        expected = ({
            'name': 'Ivo',
            'age': 27
        }, {
            'name': 'Sashko',
            'age': 25
        }, {
            'name': 'Marto',
            'age': 24
        })

        self.assertEqual(result, expected)
Beispiel #4
0
    def test_with_given_list_of_dicts_should_sort_them(self):
        to_sort = [{'h': 10}, {'h': 5}]
        expected = [{'h': 5}, {'h': 10}]

        result = my_sort(to_sort, key='h')

        self.assertEqual(result, expected)
Beispiel #5
0
    def test_with_given_touple_should_return_sorted_tuple(self):
        to_sort = (10, 8, 9, 10, 100)
        expected = (8, 9, 10, 10, 100)

        result = my_sort(to_sort)

        self.assertEqual(result, expected)
Beispiel #6
0
    def test_with_given_tuple_and_descending_order_should_sort_in_descending_order(
            self):
        given_tuple = (4, 2, 6, 20)

        result = my_sort(given_tuple, False)
        expected = (20, 6, 4, 2)

        self.assertEqual(result, expected)
Beispiel #7
0
    def test_with_given_list_and_descending_order_shpuld_send_in_descending_order(
            self):
        given_list = [4, 2, 6, 20]

        result = my_sort(given_list, False)
        expected = [20, 6, 4, 2]

        self.assertEqual(result, expected)
Beispiel #8
0
    def test_with_given_not_homogene_iterable_should_raise_exception(self):
        given = (1, 2, 'str')

        err = None
        try:
            result = my_sort(given, False)
        except Exception as e:
            err = e

        self.assertEqual(str(err), 'Iterable is not homogene!')
Beispiel #9
0
    def test_with_given_invalid_iterable_should_raise_exception(self):
        given_ivalid = {1: 'one'}

        exc = None

        # ACT
        try:
            result = my_sort(given_ivalid)
        except Exception as err:
            exc = err

        # ASSERTS
        self.assertIsNotNone(exc)
        self.assertEqual(str(exc), 'Invalid iterable given!')
Beispiel #10
0
    def test_with_given_tuple_of_dictionaries_with_default_key_should_return_same(
            self):
        given = ({
            'name': 'Marto',
            'age': 24
        }, {
            'name': 'Ivo',
            'age': 27
        }, {
            'name': 'Sashko',
            'age': 25
        })

        result = my_sort(given)

        self.assertEqual(given, result)
Beispiel #11
0
    def test_with_given_list_of_dictionaries_with_key_not_in_all_should_raise_exception(
            self):
        given = [{
            'name': 'Marto',
            'age': 24
        }, {}, {
            'name': 'Sashko',
            'age': 25
        }]
        key = 'age'

        exc = None

        # ACT
        try:
            result = my_sort(given, key=key)
        except Exception as err:
            exc = err

        # ASSERTS

        self.assertIsNotNone(exc)
        self.assertEqual(str(exc), 'Key age not in all dictionaries!')
Beispiel #12
0
 def test_with_no_iterable_and_given_key_should_return_empty_list(self):
     result = my_sort(key='age')
     expected = []
     self.assertEqual(result, expected)
Beispiel #13
0
 def test_with_no_iterable_and_ascending_false_should_return_empty_list(
         self):
     result = my_sort(ascending=False)
     expected = []
     self.assertEqual(result, expected)
Beispiel #14
0
 def test_with_no_arguments_should_return_empty_list(self):
     result = my_sort()
     expected = []
     self.assertEqual(result, expected)
Beispiel #15
0
    def test_with_given_nothing_should_return_empty_list(self):

        result = my_sort()

        self.assertEqual(result, [])