def test_it_validates_items_schema(self):
        self.assertValidationPasses(["banana", "cucumber"], schema.array_of(schema.string))
        self.assertValidationPasses([1, 2, 3], schema.array_of(schema.integer))
        self.assertValidationPasses([None], schema.array_of(schema.null))

        self.assertValidationFails(["banana", "cucumber"], schema.array_of(schema.integer))
        self.assertValidationFails([1, 2, 3], schema.array_of(schema.string))
    def test_it_validates_nullable(self):
        self.assertValidationPasses(None, schema.array_of(schema.object).nullable)

        self.assertValidationFails(False, schema.array_of(schema.object).nullable)
        self.assertValidationFails(0, schema.array_of(schema.object).nullable)
        self.assertValidationFails("[]", schema.array_of(schema.object).nullable)
        self.assertValidationFails({}, schema.array_of(schema.object).nullable)
예제 #3
0
  def test_array_of_type_generator(self):
    # type
    data = fake(schema.array_of(schema.integer))
    self.assertIsInstance(data, list)
    self.assertTrue(all(isinstance(x, int) for x in data))

    # overriding
    data = fake(schema.array_of(schema.integer), 0)
    self.assertEqual(data, 0)

    # example
    example = ['banana', 'cucumber']
    data = fake(schema.array_of(schema.string).example(example))
    self.assertEqual(data, example)

    # items_schema
    data = fake(schema.array_of(schema.string('banana')))
    self.assertTrue(all(x == 'banana' for x in data))

    # unique
    data = fake(schema.array_of(schema.integer.between(0, 1)).length(2).unique)
    self.assertEqual([0, 1], sorted(data))
    
    # length
    data = fake(schema.array_of(schema.array).length(5))
    self.assertEqual(len(data), 5)

    data = fake(schema.array_of(schema.array).length(0, 2))
    self.assertTrue(0 <= len(data) <= 2)

    data = fake(schema.array_of(schema.string).min_length(1))
    self.assertGreaterEqual(len(data), 1)

    data = fake(schema.array_of(schema.string).max_length(1))
    self.assertLessEqual(len(data), 1)
    def test_it_validates_uniqueness(self):
        self.assertValidationPasses([0, 1], schema.array_of(schema.integer).unique)

        self.assertValidationPasses(
            [{"id": 1}, {"id": 2}],
            schema.array_of(schema.object({"id": schema.integer})).unique(lambda a, b: a["id"] != b["id"]),
        )

        self.assertValidationFails([42, 42], schema.array_of(schema.integer).unique)
        self.assertValidationFails(
            [{"id": 1}, {"id": 1}],
            schema.array_of(schema.object({"id": schema.integer})).unique(lambda a, b: a["id"] != b["id"]),
        )
 def test_it_validates_type(self):
     self.assertValidationFails(None, schema.array_of(schema.integer))
     self.assertValidationFails(False, schema.array_of(schema.integer))
     self.assertValidationFails(0, schema.array_of(schema.integer))
     self.assertValidationFails("[]", schema.array_of(schema.integer))
     self.assertValidationFails({}, schema.array_of(schema.integer))
    def test_it_validates_length(self):
        self.assertValidationPasses([], schema.array_of(schema.string).empty)
        self.assertValidationPasses(["banana"], schema.array_of(schema.string).non_empty)
        self.assertValidationPasses(["banana"], schema.array_of(schema.string).length(1))
        self.assertValidationPasses([0], schema.array_of(schema.integer).length(1, 2))
        self.assertValidationPasses([0, 1], schema.array_of(schema.integer).length(1, 2))
        self.assertValidationPasses(["banana"], schema.array_of(schema.string).min_length(1))
        self.assertValidationPasses(["banana"], schema.array_of(schema.string).max_length(1))

        self.assertValidationFails(["banana"], schema.array_of(schema.string).empty)
        self.assertValidationFails([], schema.array_of(schema.string).non_empty)
        self.assertValidationFails([1], schema.array_of(schema.integer).length(2))
        self.assertValidationFails([1, 2, 3], schema.array_of(schema.integer).length(2))
        self.assertValidationFails([0, 1], schema.array_of(schema.integer).length(0, 1))
        self.assertValidationFails([0, 1], schema.array_of(schema.integer).length(3, 5))
        self.assertValidationFails([], schema.array_of(schema.integer).min_length(1))
        self.assertValidationFails([0, 1], schema.array_of(schema.integer).max_length(1))
 def test_array_of_type_substitution(self):
   self.assertSchemaCloned(schema.array_of(schema.integer), [1, 2, 3])
   self.assertIsInstance(schema.array_of(schema.integer) % [1, 2, 3], schema.types.Array)
  def test_array_of_type_representation(self):
    self.assertRepr(schema.array_of(schema.number),
                   'schema.array_of(schema.number)')

    self.assertRepr(schema.array_of(schema.boolean).nullable,
                   'schema.array_of(schema.boolean).nullable')

    self.assertRepr(schema.array_of(schema.number).empty,
                   'schema.array_of(schema.number).empty')

    self.assertRepr(schema.array_of(schema.number).non_empty,
                   'schema.array_of(schema.number).non_empty')

    self.assertRepr(schema.array_of(schema.number).length(2),
                   'schema.array_of(schema.number).length(2)')

    self.assertRepr(schema.array_of(schema.number).length(1, 10),
                   'schema.array_of(schema.number).length(1, 10)')

    self.assertRepr(schema.array_of(schema.number).min_length(1),
                   'schema.array_of(schema.number).min_length(1)')

    self.assertRepr(schema.array_of(schema.number).max_length(10),
                   'schema.array_of(schema.number).max_length(10)')

    self.assertRepr(schema.array_of(schema.string).unique,
                   'schema.array_of(schema.string).unique')

    self.assertRepr(schema.array_of(schema.string).unique(lambda a, b: a != b),
                   'schema.array_of(schema.string).unique(<predicate>)')

    self.assertRepr(
      schema.array_of(schema.object({
        'id': schema.string.numeric
      })),
      "schema.array_of(schema.object({" + "\n" +
      "  'id': schema.string.numeric" + "\n" +
      "}))"
    )