def test_it_validates_length(self):
    self.assertValidationPasses({},                           schema.object.empty)
    self.assertValidationPasses({'id': 42},                   schema.object.non_empty)
    self.assertValidationPasses({},                           schema.object.length(0))
    self.assertValidationPasses({'id': 42},                   schema.object.length(1))
    self.assertValidationPasses({'id': 42},                   schema.object.length(1, 2))
    self.assertValidationPasses({'id': 42, 'deleted': False}, schema.object.length(1, 2))
    self.assertValidationPasses({'id': 42},                   schema.object.min_length(1))
    self.assertValidationPasses({'id': 42},                   schema.object.max_length(1))
    self.assertValidationPasses(
      {
        'id': 42,
        'deleted': False
      },
      schema.object({
        'id': schema.integer
      }).length(2)
    )

    self.assertValidationFails({'id': 42},                    schema.object.empty)
    self.assertValidationFails({},                            schema.object.non_empty)
    self.assertValidationFails({'id': 42},                    schema.object.length(0))
    self.assertValidationFails({'id': 42},                    schema.object.length(2))
    self.assertValidationFails({'id': 42, 'deleted': False},  schema.object.length(0, 1))
    self.assertValidationFails({'id': 42, 'deleted': False},  schema.object.length(3, 5))
    self.assertValidationFails({},                            schema.object.min_length(1))
    self.assertValidationFails({'id': 42, 'deleted': False},  schema.object.max_length(1))
    self.assertValidationFails(
      {
        'id': 42
      },
      schema.object({
        'id': schema.integer
      }).length(2)
    )
  def test_it_validates_strict_keys(self):
    self.assertValidationPasses({}, schema.object({}).strict)
    self.assertValidationPasses(
      {
        'id': 1234,
        'title': 'banana'
      },
      schema.object({
        'id':    schema.integer,
        'title': schema.string
      }).strict
    )

    self.assertValidationFails({'id': '1'}, schema.object({}).strict)
    self.assertValidationFails(
      {
        'id': 1234,
        'title': 'banana',
        'extra_key': True
      },
      schema.object({
        'id':    schema.integer,
        'title': schema.string
      }).strict
    )
    def test_it_validates_options_with_nullable_type(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.null

        self.assertValidationPasses(None,
                                    schema.one_of(option1, option2, option3))
        self.assertValidationPasses({'id': 42},
                                    schema.one_of(option1, option2, option3))
        self.assertValidationPasses({'id': '42'},
                                    schema.one_of(option1, option2, option3))

        self.assertValidationPasses(
            None,
            schema.one_of(option1, option2, schema.null).nullable)

        self.assertValidationFails(False,
                                   schema.one_of(option1, option2, option3))
        self.assertValidationFails(0, schema.one_of(option1, option2, option3))
        self.assertValidationFails('', schema.one_of(option1, option2,
                                                     option3))
        self.assertValidationFails([], schema.one_of(option1, option2,
                                                     option3))
        self.assertValidationFails({}, schema.one_of(option1, option2,
                                                     option3))
    def test_it_validates_nullable(self):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')

            option1 = schema.object({'id': schema.integer})
            option2 = schema.object({'id': schema.string.numeric})

            self.assertValidationPasses(
                None,
                schema.one_of(option1, option2).nullable)
            self.assertValidationPasses({'id': 42},
                                        schema.one_of(option1,
                                                      option2).nullable)
            self.assertValidationPasses({'id': '42'},
                                        schema.one_of(option1,
                                                      option2).nullable)

            self.assertValidationFails(
                False,
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails(
                0,
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails(
                '',
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails([],
                                       schema.one_of(option1,
                                                     option2).nullable)
            self.assertValidationFails({},
                                       schema.one_of(option1,
                                                     option2).nullable)
Ejemplo n.º 5
0
    def test_it_validates_options_with_null_type(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.null

        self.assertValidationPasses(None,
                                    schema.any_of(option1, option2, option3))
        self.assertValidationPasses({'id': 42},
                                    schema.any_of(option1, option2, option3))
        self.assertValidationPasses({'id': '42'},
                                    schema.any_of(option1, option2, option3))
  def test_it_validates_options(self):
    option1 = schema.object({'id': schema.integer})
    option2 = schema.object({'id': schema.string.numeric})
    option3 = schema.object({'deleted': schema.boolean})

    self.assertValidationPasses({'id': 42},         schema.one_of(option1, option2))
    self.assertValidationPasses({'id': '42'},       schema.one_of(option1, option2))
    self.assertValidationPasses({'id': 42},         schema.one_of(option1, option3))
    self.assertValidationPasses({'deleted': False}, schema.one_of(option1, option3))

    self.assertValidationFails({'id': None},                 schema.one_of(option1, option2))
    self.assertValidationFails({'id': 42, 'deleted': False}, schema.one_of(option1, option3))
  def test_it_validates_nullable(self):
    with warnings.catch_warnings():
      warnings.simplefilter('ignore')

      self.assertValidationPasses(None,       schema.object.nullable)
      self.assertValidationPasses(None,       schema.object({'id': schema.integer}).nullable)
      self.assertValidationPasses({'id': 42}, schema.object({'id': schema.integer}).nullable)

      self.assertValidationFails(False, schema.object.nullable)
      self.assertValidationFails(0,     schema.object.nullable)
      self.assertValidationFails('',    schema.object.nullable)
      self.assertValidationFails([],    schema.object.nullable)
Ejemplo n.º 8
0
  def test_object_type_generator(self):
    # type
    data = fake(schema.object)
    self.assertIsInstance(data, dict)
    self.assertTrue(all(type(data[key]) in self.primitive_types for key in data))

    # overriding
    dictionary = {'id': 1}
    data = fake(schema.object({'id': schema.integer}), dictionary)
    self.assertEqual(data, dictionary)

    # example
    example = {'id': 1, 'title': 'Title', 'rating': 5.0}
    data = fake(schema.object.example(example))
    self.assertEqual(data, example)

    # keys
    data = fake(schema.object({
      'id':     schema.integer.positive,
      'title':  schema.string,
      'author': schema.object({
                  'id':   schema.integer.positive,
                  'name': schema.string
                })
    }))
    self.assertIsInstance(data, dict)
    self.assertIn('id', data)
    self.assertIsInstance(data['id'], int)
    self.assertIn('title', data)
    self.assertIsInstance(data['title'], str)
    self.assertIn('author', data)
    self.assertIsInstance(data['author'], dict)
    self.assertIn('id', data['author'])
    self.assertIsInstance(data['author']['id'], int)
    self.assertIn('name', data['author'])
    self.assertIsInstance(data['author']['name'], str)

    # length
    data = fake(schema.object({'id': schema.string.numeric}).length(5))
    self.assertEqual(len(data), 5)

    data = fake(schema.object.length(0))
    self.assertEqual(len(data), 0)

    data = fake(schema.object.length(0, 1))
    self.assertTrue(0 <= len(data) <= 1)

    # empty
    data = fake(schema.object.empty)
    self.assertEqual(data, {})

    data = fake(schema.object.non_empty)
    self.assertGreaterEqual(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_nullable(self):
    option1 = schema.object({'id': schema.integer})
    option2 = schema.object({'id': schema.string.numeric})

    self.assertValidationPasses(None,         schema.one_of(option1, option2).nullable)
    self.assertValidationPasses({'id': 42},   schema.one_of(option1, option2).nullable)
    self.assertValidationPasses({'id': '42'}, schema.one_of(option1, option2).nullable)

    self.assertValidationFails(False, schema.one_of(option1, option2).nullable)
    self.assertValidationFails(0,     schema.one_of(option1, option2).nullable)
    self.assertValidationFails('',    schema.one_of(option1, option2).nullable)
    self.assertValidationFails([],    schema.one_of(option1, option2).nullable)
    self.assertValidationFails({},    schema.one_of(option1, option2).nullable)
  def test_it_validates_keys(self):
    self.assertValidationPasses({}, schema.object({}))

    self.assertValidationPasses(
      {
        'string': 'banana',
        'number': 42,
        'array': [0, 1],
        'empty_object': {},
        'object': {
          'boolean': True,
          'object': {
            'nothing': None
          }
        }
      },
      schema.object({
        'string':       schema.string('banana'),
        'number':       schema.number(42),
        'array':        schema.array([schema.number(0), schema.number(1)]),
        'empty_object': schema.object({}),
        'object':       schema.object({
                          'boolean': schema.boolean(True),
                          'object': schema.object({
                            'nothing': schema.null
                          })
                        })
      })
    )

    self.assertValidationFails(None,        schema.object({}))
    self.assertValidationFails({},          schema.object({'id': schema.integer}))
    self.assertValidationFails({'id': '1'}, schema.object({'id': schema.integer}))
  def test_object_type_representation(self):
    self.assertRepr(schema.object,               'schema.object')
    self.assertRepr(schema.object({}),           'schema.object({})')
    self.assertRepr(schema.object.nullable,      'schema.object.nullable')
    self.assertRepr(schema.object.length(1),     'schema.object.length(1)')
    self.assertRepr(schema.object.length(0, 1),  'schema.object.length(0, 1)')
    self.assertRepr(schema.object.min_length(1), 'schema.object.min_length(1)')
    self.assertRepr(schema.object.max_length(1), 'schema.object.max_length(1)')
    self.assertRepr(schema.object.empty,         'schema.object.empty')
    self.assertRepr(schema.object.non_empty,     'schema.object.non_empty')
    
    self.assertRepr(
      schema.object({
        'id': schema.integer.positive
      }),
      "schema.object({" + "\n" +
      "  'id': schema.integer.positive" + "\n" +
      "})"
    )

    self.assertRepr(
      schema.object({
        'id': schema.integer.positive
      }).strict,
      "schema.object({" + "\n" +
      "  'id': schema.integer.positive" + "\n" +
      "}).strict"
    )

    self.assertRepr(
      schema.object({
        'attrs': schema.object({
          'height': schema.float,
          'width': schema.float
        }),
        'id': schema.integer.positive
      }),
      "schema.object({" + "\n" +
      "  'attrs': schema.object({" + "\n" +
      "    'height': schema.float," + "\n" +
      "    'width': schema.float" + "\n" +
      "  })," + "\n" +
      "  'id': schema.integer.positive" + "\n" +
      "})"
    )
  def test_it_validates_undefined_keys(self):
    self.assertValidationPasses(
      {
        'id': 42
      },
      schema.object({
        'id':    schema.integer,
        'title': schema.undefined
      })
    )

    self.assertValidationFails(
      {
        'id': 42
      },
      schema.object({
        'id': schema.undefined
      })
    )
    def test_it_validates_options(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.object({'deleted': schema.boolean})

        self.assertValidationPasses({'id': 42},
                                    schema.one_of(option1, option2))
        self.assertValidationPasses({'id': '42'},
                                    schema.one_of(option1, option2))
        self.assertValidationPasses({'id': 42},
                                    schema.one_of(option1, option3))
        self.assertValidationPasses({'deleted': False},
                                    schema.one_of(option1, option3))

        self.assertValidationFails({'id': None},
                                   schema.one_of(option1, option2))
        self.assertValidationFails({
            'id': 42,
            'deleted': False
        }, schema.one_of(option1, option3))
Ejemplo n.º 15
0
  def test_object_type_substitution(self):
    self.assertSchemaCloned(schema.object({'id': schema.integer}), {'id': 42})

    object_schema = schema.object({
      'id':         schema.integer,
      'title?':     schema.string,
      'is_deleted': schema.boolean,
      'created_at': schema.undefined
    })
    keys = {
      'id': 42,
      'title': 'Banana Title'
    }
    self.assertSchemaCloned(object_schema, keys)
    substituted = object_schema % keys
    self.assertSchemaHasValue(substituted['id'], keys['id'])
    self.assertIn('title', substituted)
    self.assertSchemaHasValue(substituted['title'], keys['title'])
    self.assertIn('is_deleted', substituted)
    self.assertIsInstance(substituted['created_at'], schema.types.Undefined)
  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_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_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" +
      "}))"
    )
  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" +
      "})"
    )