def test_validate_dict_passes_undefined_if_a_value_isnt_in_data(self):
     mocked = Mock(return_value={})
     with patch.object(Schema, "_validate", mocked):
         data = {"a": 1, "c": 2}
         schema = {"a": int, "b": int, "c": int, "d": int}
         Schema._validate_dict(schema, data, [])
         mocked.assert_has_calls([
             call(int, 1, ["a"]),
             call(int, Undefined, ["b"]),
             call(int, 2, ["c"]),
             call(int, Undefined, ["d"])
         ], any_order=True)
 def test_validate_dict_calls_validate_once_for_each_key(self):
     # TODO: Should we be checking this, or just checking the output?
     mocked = Mock(return_value={})
     with patch.object(Schema, "_validate", mocked):
         schema = {"a": unicode, "c": int, "e": unicode, "g": bool}
         data = {"a": "b", "c": 4, "e": "f", "g": True}
         Schema._validate_dict(schema, data, [])
         mocked.assert_has_calls([
             call(unicode, "b", ["a"]),
             call(int, 4, ["c"]),
             call(unicode, "f", ["e"]),
             call(bool, True, ["g"])
         ], any_order=True)
Beispiel #3
0
 def test_validate_dict_passes_undefined_if_a_value_isnt_in_data(self):
     mocked = Mock(return_value={})
     with patch.object(Schema, "_validate", mocked):
         data = {"a": 1, "c": 2}
         schema = {"a": int, "b": int, "c": int, "d": int}
         Schema._validate_dict(schema, data, [])
         mocked.assert_has_calls([
             call(int, 1, ["a"]),
             call(int, Undefined, ["b"]),
             call(int, 2, ["c"]),
             call(int, Undefined, ["d"])
         ],
                                 any_order=True)
Beispiel #4
0
 def test_validate_dict_calls_validate_once_for_each_key(self):
     # TODO: Should we be checking this, or just checking the output?
     mocked = Mock(return_value={})
     with patch.object(Schema, "_validate", mocked):
         schema = {"a": unicode, "c": int, "e": unicode, "g": bool}
         data = {"a": "b", "c": 4, "e": "f", "g": True}
         Schema._validate_dict(schema, data, [])
         mocked.assert_has_calls([
             call(unicode, "b", ["a"]),
             call(int, 4, ["c"]),
             call(unicode, "f", ["e"]),
             call(bool, True, ["g"])
         ],
                                 any_order=True)
 def test_validate_dict_returns_defined_values_from_validation(self):
     def mock_return(schema, data, path):
         return data
     mocked = Mock(side_effect=mock_return)
     with patch.object(Schema, "_validate", mocked):
         schema = {"a": int, "b": int, "c": int, "d": int}
         data = {"a": 1, "c": 2, "d": 3}
         self.assertEqual(Schema._validate_dict(schema, data, []), data)
Beispiel #6
0
 def test_validate_dict_returns_all_data_if_schema_is_empty(self):
     self.assertEqual(Schema._validate_dict({}, {
         "a": 1,
         "b": None
     }, []), {
         "a": 1,
         "b": None
     })
Beispiel #7
0
    def test_validate_dict_returns_defined_values_from_validation(self):
        def mock_return(schema, data, path):
            return data

        mocked = Mock(side_effect=mock_return)
        with patch.object(Schema, "_validate", mocked):
            schema = {"a": int, "b": int, "c": int, "d": int}
            data = {"a": 1, "c": 2, "d": 3}
            self.assertEqual(Schema._validate_dict(schema, data, []), data)
    def test_validate_dict_appends_the_key_to_the_current_path(self):
        # TODO: Should we be checking this, or just the output?
        # TODO: If we do decide to keep this, it should be combined with above
        mocked = Mock(return_value={})
        with patch.object(Schema, "_validate", mocked):
            schema = {"a": int, "b": int, "c": int}
            data = {"a": 1, "b": 2, "c": 3}
            Schema._validate_dict(schema, data, [])
            mocked.assert_has_calls([
                call(int, 1, ["a"]),
                call(int, 2, ["b"]),
                call(int, 3, ["c"])
            ], any_order=True)

            mocked.reset_mock()
            Schema._validate_dict(schema, data, ["f"])
            mocked.assert_has_calls([
                call(int, 1, ["f", "a"]),
                call(int, 2, ["f", "b"]),
                call(int, 3, ["f", "c"])
            ], any_order=True)

            mocked.reset_mock()
            Schema._validate_dict(schema, data, ["f", "g", 1])
            mocked.assert_has_calls([
                call(int, 1, ["f", "g", 1, "a"]),
                call(int, 2, ["f", "g", 1, "b"]),
                call(int, 3, ["f", "g", 1, "c"])
            ], any_order=True)
 def test_validate_dict_raises_invalid_group_for_invalid_data(self):
     def mock_return(schema, data, path):
         if data is Undefined:
             return Undefined
         if not isinstance(data, schema):
             raise Invalid("err", data, path)
         return data
     mocked = Mock(side_effect=mock_return)
     with patch.object(Schema, "_validate", mocked):
         with self.assertRaises(InvalidGroup) as cm:
             schema = {"a": int, "b": int, "c": int, "d": int}
             data = {"a": None, "b": 1, "c": 2}
             Schema._validate_dict(schema, data, [])
         self.assertEqual(map(str, cm.exception.errors), ["err @ data[a]"])
         self.assertEqual([e.data for e in cm.exception.errors], [None])
         with self.assertRaises(InvalidGroup) as cm:
             schema = {"a": int, "b": int, "c": int, "d": int}
             data = {"a": 1, "c": 1.1, "d": "abc"}
             Schema._validate_dict(schema, data, [])
         self.assertEqual(
             map(str, cm.exception.errors),
             ["err @ data[c]", "err @ data[d]"])
         self.assertEqual(
             [e.data for e in cm.exception.errors], [1.1, "abc"])
         with self.assertRaises(InvalidGroup) as cm:
             schema = {"a": int, "b": int, "c": int, "d": int}
             data = {"a": 1, "b": 2, "c": 2.0, "d": None}
             Schema._validate_dict(schema, data, ["x", 1])
         self.assertEqual(
             map(str, cm.exception.errors),
             ["err @ data[x][1][c]", "err @ data[x][1][d]"])
         self.assertEqual(
             [e.data for e in cm.exception.errors], [2.0, None])
Beispiel #10
0
    def test_validate_dict_appends_the_key_to_the_current_path(self):
        # TODO: Should we be checking this, or just the output?
        # TODO: If we do decide to keep this, it should be combined with above
        mocked = Mock(return_value={})
        with patch.object(Schema, "_validate", mocked):
            schema = {"a": int, "b": int, "c": int}
            data = {"a": 1, "b": 2, "c": 3}
            Schema._validate_dict(schema, data, [])
            mocked.assert_has_calls([
                call(int, 1, ["a"]),
                call(int, 2, ["b"]),
                call(int, 3, ["c"])
            ],
                                    any_order=True)

            mocked.reset_mock()
            Schema._validate_dict(schema, data, ["f"])
            mocked.assert_has_calls([
                call(int, 1, ["f", "a"]),
                call(int, 2, ["f", "b"]),
                call(int, 3, ["f", "c"])
            ],
                                    any_order=True)

            mocked.reset_mock()
            Schema._validate_dict(schema, data, ["f", "g", 1])
            mocked.assert_has_calls([
                call(int, 1, ["f", "g", 1, "a"]),
                call(int, 2, ["f", "g", 1, "b"]),
                call(int, 3, ["f", "g", 1, "c"])
            ],
                                    any_order=True)
Beispiel #11
0
    def test_validate_dict_raises_invalid_group_for_invalid_data(self):
        def mock_return(schema, data, path):
            if data is Undefined:
                return Undefined
            if not isinstance(data, schema):
                raise Invalid("err", data, path)
            return data

        mocked = Mock(side_effect=mock_return)
        with patch.object(Schema, "_validate", mocked):
            with self.assertRaises(InvalidGroup) as cm:
                schema = {"a": int, "b": int, "c": int, "d": int}
                data = {"a": None, "b": 1, "c": 2}
                Schema._validate_dict(schema, data, [])
            self.assertEqual(map(str, cm.exception.errors), ["err @ data[a]"])
            self.assertEqual([e.data for e in cm.exception.errors], [None])
            with self.assertRaises(InvalidGroup) as cm:
                schema = {"a": int, "b": int, "c": int, "d": int}
                data = {"a": 1, "c": 1.1, "d": "abc"}
                Schema._validate_dict(schema, data, [])
            self.assertEqual(map(str, cm.exception.errors),
                             ["err @ data[c]", "err @ data[d]"])
            self.assertEqual([e.data for e in cm.exception.errors],
                             [1.1, "abc"])
            with self.assertRaises(InvalidGroup) as cm:
                schema = {"a": int, "b": int, "c": int, "d": int}
                data = {"a": 1, "b": 2, "c": 2.0, "d": None}
                Schema._validate_dict(schema, data, ["x", 1])
            self.assertEqual(map(str, cm.exception.errors),
                             ["err @ data[x][1][c]", "err @ data[x][1][d]"])
            self.assertEqual([e.data for e in cm.exception.errors],
                             [2.0, None])
 def test_validate_dict_raises_invalid_if_not_given_a_dictionary(self):
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, [], [])
     self.assertEqual(str(cm.exception), "Expected an object")
     self.assertEqual(cm.exception.data, [])
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, Undefined, [0])
     self.assertEqual(str(cm.exception), "Expected an object @ data[0]")
     self.assertEqual(cm.exception.data, Undefined)
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, None, ["a", "b"])
     self.assertEqual(str(cm.exception), "Expected an object @ data[a][b]")
     self.assertEqual(cm.exception.data, None)
Beispiel #13
0
 def test_validate_dict_raises_invalid_if_not_given_a_dictionary(self):
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, [], [])
     self.assertEqual(str(cm.exception), "Expected an object")
     self.assertEqual(cm.exception.data, [])
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, Undefined, [0])
     self.assertEqual(str(cm.exception), "Expected an object @ data[0]")
     self.assertEqual(cm.exception.data, Undefined)
     with self.assertRaises(Invalid) as cm:
         Schema._validate_dict({}, None, ["a", "b"])
     self.assertEqual(str(cm.exception), "Expected an object @ data[a][b]")
     self.assertEqual(cm.exception.data, None)
 def test_validate_dict_returns_all_data_if_schema_is_empty(self):
     self.assertEqual(
         Schema._validate_dict({}, {"a": 1, "b": None}, []),
         {"a": 1, "b": None})