def test_blankable(self):
        v = ObjectField(self.simple_schema, blank=True, blank_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, blank=True, blank_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqual(v.generate(), {})
    def test_not_required(self):
        v = ObjectField(self.simple_schema, required=False, not_present_prob=0)
        self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, required=False, not_present_prob=1)
        with self.assertRaises(NotGeneratedException):
            v.generate()

        v = ObjectField(self.simple_schema, required=False, not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            with self.assertRaises(NotGeneratedException):
                v.generate()

        v = ObjectField(self.simple_schema, required=False, not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())
    def test_nullable_blankable(self):
        v = ObjectField(self.simple_schema, null=True, null_prob=0.3,
                        blank=True, blank_prob=0.3)

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertIsNone(v.generate())

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0.5
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqual(v.generate(), {})

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())
    def test_not_required(self):
        v = ObjectField(self.simple_schema, required=False, not_present_prob=0)
        self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, required=False, not_present_prob=1)
        with self.assertRaises(NotGeneratedException):
            v.generate()

        v = ObjectField(self.simple_schema, required=False,
                        not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            with self.assertRaises(NotGeneratedException):
                v.generate()

        v = ObjectField(self.simple_schema, required=False,
                        not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())
 def test_default_nested_schema(self):
     v = ObjectField(self.nested_schema)
     generated = v.generate()
     self.assertEqual(set(generated.keys()), set(self.nested_schema.keys()))
     self.assertEqualSimpleSchema(generated['simple'])
     self.assertEqualComplexSchema(generated['complex'])
 def test_default_complex_schema(self):
     v = ObjectField(self.complex_schema)
     self.assertEqualComplexSchema(v.generate())
 def test_default_simple_schema(self):
     v = ObjectField(self.simple_schema)
     self.assertEqualSimpleSchema(v.generate())
class ObjectFieldTestCase(unittest.TestCase):

    simple_schema = {
        'integer': IntegerField(),
        'string': StringField(),
        'boolean': BooleanField()
    }

    complex_schema = {
        'array': ArrayField(IntegerField()),
        'nest-1': {
            'nest-2': {
                'string': StringField()
            },
            'integer': IntegerField()
        }
    }

    nested_schema = {
        'simple': ObjectField(simple_schema),
        'complex': ObjectField(complex_schema)
    }

    def assertEqualSimpleSchema(self, value):
        self.assertEqual(set(value.keys()), set(self.simple_schema.keys()))
        self.assertTrue(0 <= value['integer'] <= 100)
        self.assertTrue(0 <= len(value['string']) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits)
                             for s in value['string']]))
        self.assertTrue(isinstance(value['boolean'], bool))

    def assertEqualComplexSchema(self, value):
        self.assertEqual(set(value.keys()), set(self.complex_schema.keys()))
        self.assertTrue(0 <= len(value['array']) <= 10)
        self.assertTrue(all([isinstance(i, int) for i in value['array']]))

        self.assertEqual(set(value['nest-1'].keys()),
                         set(self.complex_schema['nest-1'].keys()))
        self.assertTrue(0 <= value['nest-1']['integer'] <= 100)

        self.assertEqual(set(value['nest-1']['nest-2'].keys()),
                         set(self.complex_schema['nest-1']['nest-2'].keys()))
        self.assertTrue(0 <= len(value['nest-1']['nest-2']['string']) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits)
                             for s in value['nest-1']['nest-2']['string']]))

    def test_default_simple_schema(self):
        v = ObjectField(self.simple_schema)
        self.assertEqualSimpleSchema(v.generate())

    def test_default_complex_schema(self):
        v = ObjectField(self.complex_schema)
        self.assertEqualComplexSchema(v.generate())

    def test_default_nested_schema(self):
        v = ObjectField(self.nested_schema)
        generated = v.generate()
        self.assertEqual(set(generated.keys()), set(self.nested_schema.keys()))
        self.assertEqualSimpleSchema(generated['simple'])
        self.assertEqualComplexSchema(generated['complex'])

    def test_not_required(self):
        v = ObjectField(self.simple_schema, required=False, not_present_prob=0)
        self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, required=False, not_present_prob=1)
        with self.assertRaises(NotGeneratedException):
            v.generate()

        v = ObjectField(self.simple_schema, required=False,
                        not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            with self.assertRaises(NotGeneratedException):
                v.generate()

        v = ObjectField(self.simple_schema, required=False,
                        not_present_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())

    def test_nullable(self):
        v = ObjectField(self.simple_schema, null=True, null_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, null=True, null_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertIsNone(v.generate())

    def test_blankable(self):
        v = ObjectField(self.simple_schema, blank=True, blank_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())

        v = ObjectField(self.simple_schema, blank=True, blank_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqual(v.generate(), {})

    def test_nullable_blankable(self):
        v = ObjectField(self.simple_schema, null=True, null_prob=0.3,
                        blank=True, blank_prob=0.3)

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertIsNone(v.generate())

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0.5
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqual(v.generate(), {})

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            mocked_random.randint = random.randint
            self.assertEqualSimpleSchema(v.generate())
 def test_default_nested_schema(self):
     v = ObjectField(self.nested_schema)
     generated = v.generate()
     self.assertEqual(set(generated.keys()), set(self.nested_schema.keys()))
     self.assertEqualSimpleSchema(generated["simple"])
     self.assertEqualComplexSchema(generated["complex"])
Esempio n. 10
0
 def test_default_complex_schema(self):
     v = ObjectField(self.complex_schema)
     self.assertEqualComplexSchema(v.generate())
Esempio n. 11
0
 def test_default_simple_schema(self):
     v = ObjectField(self.simple_schema)
     self.assertEqualSimpleSchema(v.generate())