def test_dict_depth_of_3_with_class_object(self, capsys, person_a): test_data = { 'key1': 1, 'key6': person_a, 'key2': { 'key3': 1, 'key4': { 'key5': 4, 'user': 5 } } } print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == ("key1 1\n" "key6 1\n" "first_name: 2\n" "last_name: 2\n" "father: 2\n" "key2 1\n" "key3 2\n" "key4 2\n" "key5 3\n" "user: 3\n") assert captured.err == ""
def test_dict_depth_of_3_with_7_items(self, capsys): test_data = { 'key1': { 'key2': 2, 'key3': 3 }, 'key4': 4, 'key5': { 'key6': { 'key7': 7 } } } print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == ("key1 1\n" "key2 2\n" "key3 2\n" "key4 1\n" "key5 1\n" "key6 2\n" "key7 3\n") assert captured.err == ""
def test_dict_depth_of_0_with_0_item(self, capsys): test_data = {} print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_dict_depth_of_1_with_3_items(self, capsys): test_data = {'key1': 1, 'key2': 2, 'key3': 3} print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == ("key1 1\n" "key2 1\n" "key3 1\n") assert captured.err == ""
def test_dict_depth_of_2_with_class_object(self, capsys, person_a): test_data = {'user': person_a} print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == ("user: 1\n" "first_name: 2\n" "last_name: 2\n" "father: 2\n") assert captured.err == ""
def test_dict_depth_of_3_with_5_items(self, capsys): test_data = {'key1': 1, 'key2': {'key3': 1, 'key4': {'key5': 4}}} print_depth(test_data) sys.stderr.write("") captured = capsys.readouterr() assert captured.out == ("key1 1\n" "key2 1\n" "key3 2\n" "key4 2\n" "key5 3\n") assert captured.err == ""
def test_solution_2(self): person_a = solution_2.Person("User", "1", None) person_b = solution_2.Person("User", "2", person_a) a = { "key1": 1, "key2": { "key3": 1, "key4": { "key5": 4, "user": person_b } } } expected_output = 'key1 1\nkey2 1\nkey3 2\nkey4 2\nkey5 3\nuser: 3\nfirst_name: 4\nlast_name: 4\nfather: 4\nfirst_name: 5\nlast_name: 5\nfather: 5\n' with patch('sys.stdout', new=io.StringIO()) as captured_std: solution_2.print_depth(a) self.assertEqual(captured_std.getvalue(), expected_output) with patch('sys.stdout', new=io.StringIO()) as captured_std: solution_2.print_depth([1, 2, 'foobar']) self.assertEqual(captured_std.getvalue(), '')