def test_it_validates_one_occurrence(self):
        self.assertValidationPasses([42], schema.array.contains_one(schema.integer(42)))
        self.assertValidationPasses([0, 1, 2], schema.array.contains_one(schema.integer(1)))

        self.assertValidationFails([], schema.array.contains_one(schema.integer(42)))
        self.assertValidationFails([42, 42], schema.array.contains_one(schema.integer(42)))
        self.assertValidationFails([0, 1], schema.array.contains_one(schema.integer(42)))
    def test_it_validates_any_occurrences(self):
        self.assertValidationPasses([42], schema.array.contains(schema.integer(42)))
        self.assertValidationPasses([42, 42], schema.array.contains(schema.integer(42)))
        self.assertValidationPasses([1, 2, 42, 3], schema.array.contains(schema.integer(42)))

        self.assertValidationFails([], schema.array.contains(schema.integer(42)))
        self.assertValidationFails([0, 1], schema.array.contains(schema.integer(42)))
    def test_it_validates_any_occurrences(self):
        self.assertValidationPasses([42],
                                    schema.array.contains(schema.integer(42)))
        self.assertValidationPasses([42, 42],
                                    schema.array.contains(schema.integer(42)))
        self.assertValidationPasses([1, 2, 42, 3],
                                    schema.array.contains(schema.integer(42)))

        self.assertValidationFails([],
                                   schema.array.contains(schema.integer(42)))
        self.assertValidationFails([0, 1],
                                   schema.array.contains(schema.integer(42)))
    def test_it_validates_nullable(self):
        self.assertValidationPasses(None, schema.integer.nullable)
        self.assertValidationPasses(None, schema.integer(42).nullable)

        self.assertValidationFails(False, schema.integer.nullable)
        self.assertValidationFails("", schema.integer.nullable)
        self.assertValidationFails([], schema.integer.nullable)
        self.assertValidationFails({}, schema.integer.nullable)
    def test_it_validates_one_occurrence(self):
        self.assertValidationPasses([42],
                                    schema.array.contains_one(
                                        schema.integer(42)))
        self.assertValidationPasses([0, 1, 2],
                                    schema.array.contains_one(
                                        schema.integer(1)))

        self.assertValidationFails([],
                                   schema.array.contains_one(
                                       schema.integer(42)))
        self.assertValidationFails([42, 42],
                                   schema.array.contains_one(
                                       schema.integer(42)))
        self.assertValidationFails([0, 1],
                                   schema.array.contains_one(
                                       schema.integer(42)))
  def test_it_validates_optional_keys(self):
    self.assertValidationPasses(
      {
        'id': 1234
      },
      schema.object({
        'id':     schema.integer(1234),
        'title?': schema.string('banana')
      })
    )

    self.assertValidationFails(
      {
        'id': 1234,
        'title': 'not banana'
      },
      schema.object({
        'id':     schema.integer(1234),
        'title?': schema.string('banana')
      })
    )
    def test_it_validates_value(self):
        self.assertValidationPasses(42, schema.integer(42))
        self.assertValidationPasses(-42, schema.integer(-42))
        self.assertValidationPasses(0, schema.integer.zero)

        self.assertValidationFails(None, schema.integer(0))
        self.assertValidationFails(42, schema.integer(-42))
        self.assertValidationFails(-42, schema.integer(42))
        self.assertValidationFails(3.14, schema.integer(3))
        self.assertValidationFails("0", schema.integer.zero)
  def test_number_type_representation(self):
    self.assertRepr(schema.number,               'schema.number')
    self.assertRepr(schema.number(42),           'schema.number(42)')
    self.assertRepr(schema.number(3.14),         'schema.number(3.14)')
    self.assertRepr(schema.number.min(0),        'schema.number.min(0)')
    self.assertRepr(schema.number.max(1),        'schema.number.max(1)')
    self.assertRepr(schema.number.between(0, 1), 'schema.number.between(0, 1)')
    self.assertRepr(schema.number.positive,      'schema.number.positive')
    self.assertRepr(schema.number.non_positive,  'schema.number.non_positive')
    self.assertRepr(schema.number.negative,      'schema.number.negative')
    self.assertRepr(schema.number.non_negative,  'schema.number.non_negative')
    self.assertRepr(schema.number.unsigned,      'schema.number.unsigned')
    self.assertRepr(schema.number.zero,          'schema.number.zero')
    self.assertRepr(schema.number.multiple(5),   'schema.number.multiple(5)')
    self.assertRepr(schema.number.nullable,      'schema.number.nullable')

    self.assertRepr(schema.integer,               'schema.integer')
    self.assertRepr(schema.integer(42),           'schema.integer(42)')
    self.assertRepr(schema.integer.min(0),        'schema.integer.min(0)')
    self.assertRepr(schema.integer.max(1),        'schema.integer.max(1)')
    self.assertRepr(schema.integer.between(0, 1), 'schema.integer.between(0, 1)')
    self.assertRepr(schema.integer.positive,      'schema.integer.positive')
    self.assertRepr(schema.integer.non_positive,  'schema.integer.non_positive')
    self.assertRepr(schema.integer.negative,      'schema.integer.negative')
    self.assertRepr(schema.integer.non_negative,  'schema.integer.non_negative')
    self.assertRepr(schema.integer.zero,          'schema.integer.zero')
    self.assertRepr(schema.integer.multiple(5),   'schema.integer.multiple(5)')
    self.assertRepr(schema.integer.nullable,      'schema.integer.nullable')

    self.assertRepr(schema.float,                   'schema.float')
    self.assertRepr(schema.float(3.14),             'schema.float(3.14)')
    self.assertRepr(schema.float.min(0.0),          'schema.float.min(0.0)')
    self.assertRepr(schema.float.max(1.0),          'schema.float.max(1.0)')
    self.assertRepr(schema.float.between(0.0, 1.0), 'schema.float.between(0.0, 1.0)')
    self.assertRepr(schema.float.positive,          'schema.float.positive')
    self.assertRepr(schema.float.non_positive,      'schema.float.non_positive')
    self.assertRepr(schema.float.negative,          'schema.float.negative')
    self.assertRepr(schema.float.non_negative,      'schema.float.non_negative')
    self.assertRepr(schema.float.zero,              'schema.float.zero')
    self.assertRepr(schema.float.nullable,          'schema.float.nullable')
    def test_it_validates_items(self):
        self.assertValidationPasses([], schema.array([]))
        self.assertValidationPasses([42], schema.array([schema.integer(42)]))
        self.assertValidationPasses([0, 1], schema.array([schema.integer(0), schema.integer(1)]))

        self.assertValidationFails([], schema.array([schema.integer(42)]))
        self.assertValidationFails([42], schema.array([]))
        self.assertValidationFails(42, schema.array([schema.integer(42)]))
        self.assertValidationFails(["42"], schema.array([schema.integer(42)]))

        self.assertValidationFails(
            ["banana", "cucumber", "carrot"], schema.array([schema.string("banana"), schema.string("cucumber")])
        )

        self.assertValidationFails(
            ["banana", "cucumber"],
            schema.array([schema.string("banana"), schema.string("cucumber"), schema.string("carrot")]),
        )
    def test_it_validates_items(self):
        self.assertValidationPasses([], schema.array([]))
        self.assertValidationPasses([42], schema.array([schema.integer(42)]))
        self.assertValidationPasses([0, 1],
                                    schema.array(
                                        [schema.integer(0),
                                         schema.integer(1)]))

        self.assertValidationFails([], schema.array([schema.integer(42)]))
        self.assertValidationFails([42], schema.array([]))
        self.assertValidationFails(42, schema.array([schema.integer(42)]))
        self.assertValidationFails(['42'], schema.array([schema.integer(42)]))

        self.assertValidationFails(
            ['banana', 'cucumber', 'carrot'],
            schema.array([schema.string('banana'),
                          schema.string('cucumber')]))

        self.assertValidationFails(['banana', 'cucumber'],
                                   schema.array([
                                       schema.string('banana'),
                                       schema.string('cucumber'),
                                       schema.string('carrot')
                                   ]))
  def test_one_of_type_representation(self):
    self.assertRepr(schema.one_of(schema.integer, schema.string.numeric),
                   'schema.one_of(schema.integer, schema.string.numeric)')

    self.assertRepr(schema.one_of(schema.integer(0), schema.integer(1)).nullable,
                   'schema.one_of(schema.integer(0), schema.integer(1)).nullable')
  def test_array_type_representation(self):
    self.assertRepr(schema.array,                'schema.array')
    self.assertRepr(schema.array.nullable,       'schema.array.nullable')
    self.assertRepr(schema.array([]),            'schema.array([])')
    self.assertRepr(schema.array.length(10),     'schema.array.length(10)')
    self.assertRepr(schema.array.length(1, 2),   'schema.array.length(1, 2)')
    self.assertRepr(schema.array.min_length(1),  'schema.array.min_length(1)')
    self.assertRepr(schema.array.max_length(10), 'schema.array.max_length(10)')
    self.assertRepr(schema.array.empty,          'schema.array.empty')
    self.assertRepr(schema.array.non_empty,      'schema.array.non_empty')
    
    self.assertRepr(schema.array.unique,
                   'schema.array.unique')
    self.assertRepr(schema.array.unique(lambda a, b: a != b),
                   'schema.array.unique(<predicate>)')

    self.assertRepr(schema.array([schema.integer(0), schema.integer(1)]),
                   'schema.array([schema.integer(0), schema.integer(1)])')

    self.assertRepr(schema.array.contains(schema.integer(42)),
                   'schema.array.contains(schema.integer(42))')
    self.assertRepr(schema.array.contains_one(schema.boolean),
                   'schema.array.contains_one(schema.boolean)')
    self.assertRepr(schema.array.contains_many(schema.string('banana')),
                   "schema.array.contains_many(schema.string('banana'))")

    self.assertRepr(
      schema.array.contains(schema.object({
        'id': schema.integer(1)
      })),
      "schema.array.contains(schema.object({" + "\n" +
      "  'id': schema.integer(1)" + "\n" +
      "}))"
    )

    self.assertRepr(
      schema.array([
        schema.integer(1),
        schema.integer(2),
        schema.integer(3)
      ]),
      "schema.array([" + "\n" +
      "  schema.integer(1)," + "\n" +
      "  schema.integer(2)," + "\n" +
      "  schema.integer(3)" + "\n" +
      "])"
    )
    
    self.assertRepr(
      schema.array([
        schema.integer(1),
        schema.integer(2),
        schema.object({
          'id': schema.string.numeric
        })
      ]),
      "schema.array([" + "\n" +
      "  schema.integer(1)," + "\n" +
      "  schema.integer(2)," + "\n" +
      "  schema.object({" + "\n" +
      "    'id': schema.string.numeric" + "\n" +
      "  })" + "\n" +
      "])"
    )

    self.assertRepr(
      schema.object({
        'items': schema.array([schema.object({
          'id': schema.string.numeric
        })])
      }),
      "schema.object({" + "\n" +
      "  'items': schema.array([schema.object({" + "\n" +
      "    'id': schema.string.numeric" + "\n" +
      "  })])" + "\n" +
      "})"
    )
예제 #13
0
  def test_array_type_generator(self):
    # type
    data = fake(schema.array)
    self.assertIsInstance(data, list)
    self.assertTrue(all(type(x) in self.primitive_types for x in data))

    # overriding
    array = [0, 1]
    data = fake(schema.array([schema.integer, schema.integer]), array)
    self.assertEqual(data, array)

    # example
    examples = (['true', 'false'], ['false', 'true'])
    data = fake(schema.array.examples(*examples))
    self.assertIn(data, examples)

    # items
    data = fake(schema.array([schema.number(0), schema.number(1)]))
    self.assertEqual(data, [0, 1])

    # contains
    data = fake(schema.array.contains(schema.integer(42)))
    self.assertGreaterEqual(data.count(42), 1)

    data = fake(schema.array.contains(schema.string('banana')).length(1))
    self.assertEqual(data, ['banana'])

    # contains_one
    data = fake(schema.array.contains_one(schema.integer(42)))
    self.assertIn(42, data)

    data = fake(schema.array.contains_one(schema.string('banana')).length(1))
    self.assertEqual(data, ['banana'])
    
    # contains_many
    data = fake(schema.array.contains_many(schema.integer(42)))
    self.assertGreaterEqual(data.count(42), 2)

    data = fake(schema.array.contains_many(schema.string('banana')).length(2))
    self.assertEqual(data, ['banana', 'banana'])

    # unique
    data = fake(schema.array([schema.boolean, schema.boolean]).unique)
    self.assertIn(True, data)
    self.assertIn(False, data)

    data = fake(schema.array.unique)
    self.assertEqual(len(set(data)), len(data))

    # length
    data = fake(schema.array.length(1))
    self.assertEqual(len(data), 1)

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

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

    data = fake(schema.array.max_length(1))
    self.assertLessEqual(len(data), 1)

    # empty
    data = fake(schema.array.empty)
    self.assertEqual(data, [])

    data = fake(schema.array.non_empty)
    self.assertGreaterEqual(len(data), 1)