Example #1
0
 def test_optional_validation(self):
     """Fields are required by default but can be made optional"""
     fields = [
         IntegerField(required=False),
         UnicodeField(required=False),
         ObjectField(required=False),
         ArrayField(required=False),
         IsoDateString(required=False),
         # subschema tested elsewhere...
     ]
     for field in fields:
         assert field.validate(None)
Example #2
0
    def test_array_validation(self):
        field = ArrayField(int)

        assert field.validate([1, 2, 3])

        with self.assertRaises(Invalid):
            field.validate(["a", 1, 1.0])

        with self.assertRaises(Invalid):
            field.validate(None)

        with self.assertRaises(Invalid):
            field.validate({1: "A", 2: "B"})

        with self.assertRaises(Invalid):
            field.validate("hiya")

        field = ArrayField(int, required=False)

        assert field.validate([1, 2, 3])

        assert field.validate(None)

        with self.assertRaises(Invalid):
            field.validate(1)

        class SomeSchema(Schema):
            good = IntegerField()

        field = ArrayField(SomeSchema, required=False)

        assert field.validate([SomeSchema(good=1)])

        assert field.validate([{"good": 1}])

        assert field.validate(None)

        with self.assertRaises(Invalid):
            field.validate([SomeSchema(good="bad")])

        with self.assertRaises(Invalid):
            field.validate([{"good": "bad"}])

        with self.assertRaises(Invalid):
            field.validate([{"wat": "whatever"}])
Example #3
0
 class My2(Schema):
     stuff = ArrayField(Sub, required=True)
Example #4
0
 class My(Schema):
     stuff = ArrayField(Sub, required=False)
Example #5
0
 class Outer(Schema):
     many = ArrayField(Inner)
Example #6
0
 class SomeSchema(Schema):
     great = ArrayField(int, required=False)
Example #7
0
    def test_array_serialization(self):
        field = ArrayField(int)

        flat = [1, 2, 3]
        self.assertEqual(flat, field.serialize(flat))

        self.assertEqual(None, field.serialize(None))

        self.assertEqual([], field.serialize([]))

        self.assertEqual("wow", field.serialize("wow"))

        class SomeSchema(Schema):
            wat = IntegerField()

        field = ArrayField(Subschema)
        source = [SomeSchema(wat=1), SomeSchema(wat=2)]
        self.assertEqual([{"wat": 1}, {"wat": 2}], field.serialize(source))
        source = [SomeSchema(), SomeSchema()]
        self.assertEqual([], field.serialize(source))
        self.assertEqual([{"wat": None}, {"wat": None}],
                         field.serialize(source, implicit_nulls=False))
Example #8
0
 class SomeSchema(Schema):
     great = ArrayField(int)
Example #9
0
 class Myschema(Schema):
     maybe = ArrayField()
Example #10
0
 class Source(Schema):
     together = ArrayField(IntegerField())
     sep_1 = IntegerField()
     sep_2 = IntegerField()
Example #11
0
 class SecondSchema(Schema):
     those = ArrayField(ManySchema)
Example #12
0
class Conversation(Schema):
    numbers = ArrayField(IntegerField())
    conversants = ArrayField(Subschema(Person))
Example #13
0
 class Some(Schema):
     wow = ArrayField()