コード例 #1
0
    def test_blankable(self):
        v = StringField(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
            generated = v.generate()
            self.assertTrue(1 <= len(generated) <= 10)
            self.assertTrue(all([s in (string.ascii_letters + string.digits) for s in generated]))

        v = StringField(blank=True, blank_prob=0.5)
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0
            generated = v.generate()
            self.assertEqual(generated, "")
コード例 #2
0
    def test_min_max_length(self):
        v = StringField(min_length=5, max_length=10)
        generated = v.generate()
        self.assertTrue(5 <= len(generated) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits)
                             for s in generated]))

        v = StringField(min_length=0, max_length=0)
        generated = v.generate()
        self.assertEqual(generated, '')

        v = StringField(min_length=10, max_length=5)
        with self.assertRaises(ValueError):
            generated = v.generate()
コード例 #3
0
    def test_nullable_blankable_choices(self):
        choices = ["a", "b", "c"]
        v = StringField(null=True, null_prob=0.3, blank=True, blank_prob=0.3, choices=choices)

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

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

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            self.assertIn(v.generate(), choices)
コード例 #4
0
    def test_nullable_blankable_choices(self):
        choices = ['a', 'b', 'c']
        v = StringField(null=True, null_prob=0.3, blank=True, blank_prob=0.3,
                        choices=choices)

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

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

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 1
            mocked_random.choice = random.choice
            self.assertIn(v.generate(), choices)
コード例 #5
0
    def test_nullable_blankable(self):
        v = StringField(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
            generated = v.generate()
            self.assertIsNone(generated)

        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.random.return_value = 0.5
            generated = v.generate()
            self.assertEqual(generated, '')

        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
            generated = v.generate()
            self.assertTrue(1 <= len(generated) <= 10)
            self.assertTrue(all([s in (string.ascii_letters + string.digits)
                                 for s in generated]))
コード例 #6
0
class SimpleTestSchema(BaseSchema):

    schema = {
        'array': ArrayField(IntegerField()),
        'nest-1': {
            'nest-2': {
                'string': StringField()
            },
            'integer': IntegerField()
        },
        'nest_3': {
            'double': DoubleField()
        }
    }
コード例 #7
0
    def test_not_required(self):
        v = StringField(required=False, not_present_prob=0)
        generated = v.generate()
        self.assertTrue(1 <= len(generated) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits) for s in generated]))

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

        v = StringField(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 = StringField(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
            generated = v.generate()
            self.assertTrue(1 <= len(generated) <= 10)
            self.assertTrue(all([s in (string.ascii_letters + string.digits) for s in generated]))
コード例 #8
0
    def test_min_max_length(self):
        v = StringField(min_length=5, max_length=10)
        generated = v.generate()
        self.assertTrue(5 <= len(generated) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits) for s in generated]))

        v = StringField(min_length=0, max_length=0)
        generated = v.generate()
        self.assertEqual(generated, "")

        v = StringField(min_length=10, max_length=5)
        with self.assertRaises(ValueError):
            generated = v.generate()
コード例 #9
0
    def test_default(self):
        v = ArrayField(IntegerField())
        generated = v.generate()
        self.assertTrue(1 <= len(generated) <= 10)
        self.assertTrue(all([isinstance(i, int) for i in generated]))

        int_field = mock.Mock(wraps=IntegerField())
        int_field.generate = lambda: 0
        str_field = mock.Mock(wraps=StringField())
        str_field.generate = lambda: 'string'
        v = ArrayField([int_field, str_field])
        with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random:
            mocked_random.randint.return_value = 2

            mocked_random.choice.side_effect = [int_field, str_field]
            generated = v.generate()

            self.assertEqual(len(generated), 2)
            self.assertEqual(generated[0], 0)
            self.assertTrue(generated[1], 'string')
コード例 #10
0
 def test_default(self):
     v = StringField()
     generated = v.generate()
     self.assertTrue(1 <= len(generated) <= 10)
     self.assertTrue(all([s in (string.ascii_letters + string.digits) for s in generated]))
コード例 #11
0
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())
コード例 #12
0
 def test_charset(self):
     charset = "!@#$%^&*()_"
     v = StringField(charset=charset)
     generated = v.generate()
     self.assertTrue(1 <= len(generated) <= 10)
     self.assertTrue(all([s in charset for s in generated]))
コード例 #13
0
 def test_choices(self):
     choices = ['a', 'b', 'c']
     v = StringField(choices=choices)
     self.assertIn(v.generate(), choices)
コード例 #14
0
    def test_not_required(self):
        v = StringField(required=False, not_present_prob=0)
        generated = v.generate()
        self.assertTrue(1 <= len(generated) <= 10)
        self.assertTrue(all([s in (string.ascii_letters + string.digits)
                             for s in generated]))

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

        v = StringField(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 = StringField(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
            generated = v.generate()
            self.assertTrue(1 <= len(generated) <= 10)
            self.assertTrue(all([s in (string.ascii_letters + string.digits)
                                 for s in generated]))
コード例 #15
0
 def test_charset(self):
     charset = '!@#$%^&*()_'
     v = StringField(charset=charset)
     generated = v.generate()
     self.assertTrue(1 <= len(generated) <= 10)
     self.assertTrue(all([s in charset for s in generated]))
コード例 #16
0
 def test_default(self):
     v = StringField()
     generated = v.generate()
     self.assertTrue(1 <= len(generated) <= 10)
     self.assertTrue(all([s in (string.ascii_letters + string.digits)
                          for s in generated]))
コード例 #17
0
 def test_choices(self):
     choices = ["a", "b", "c"]
     v = StringField(choices=choices)
     self.assertIn(v.generate(), choices)