Ejemplo n.º 1
0
def test_validate_strict_with_context_data():
    class Player(Model):
        id = IntType()

    try:
        validate(Player, {'id': 4}, strict=True, context={'name': 'Arthur'})
    except ValidationError as e:
        assert 'name' in e.messages
Ejemplo n.º 2
0
def test_validate_strict_with_context_data():
    class Player(Model):
        id = IntType()

    try:
        validate(Player, {'id': 4}, strict=True, context={'name': 'Arthur'})
    except ValidationError as e:
        assert 'name' in e.messages
Ejemplo n.º 3
0
    def test_validate_ignore_extra_context_data(self):
        class Player(Model):
            id = IntType()

        data = validate(Player, {"id": 4}, context={"name": "Arthur"})

        self.assertEqual(data, {"id": 4, "name": "Arthur"})
Ejemplo n.º 4
0
def test_validate_ignore_extra_context_data():
    class Player(Model):
        id = IntType()

    data = validate(Player, {'id': 4}, context={'name': 'Arthur'})

    assert data == {'id': 4, 'name': 'Arthur'}
Ejemplo n.º 5
0
    def test_validate_ignore_extra_context_data(self):
        class Player(Model):
            id = IntType()

        data = validate(Player, {'id': 4}, context={'name': 'Arthur'})

        self.assertEqual(data, {'id': 4, 'name': 'Arthur'})
Ejemplo n.º 6
0
def test_validate_with_instance_level_validators():
    class Player(Model):
        id = IntType()

        def validate_id(self, data, value, context):
            if self.id:
                raise ValidationError('Cannot change id')

    p1 = Player(trusted_data={'id': 4})

    try:
        validate(Player, p1, {'id': 3})
    except DataError as e:
        assert 'id' in e.messages
        assert 'Cannot change id' in e.messages['id']
        assert p1.id == 4
Ejemplo n.º 7
0
    def test_validate_ignore_extra_context_data(self):
        class Player(Model):
            id = IntType()

        data = validate(Player, {'id': 4}, context={'name': 'Arthur'})

        self.assertEqual(data, {'id': 4, 'name': 'Arthur'})
Ejemplo n.º 8
0
def test_functional_schema(player_schema, player_data):
    schema = player_schema
    input_data = player_data

    data = input_data  # state = 'RAW'

    expected = {'id': 42, 'full_name': 'Arthur Dent'}
    data = convert(schema, data, partial=True)
    assert data == expected  # state = 'CONVERTED'

    expected = {
        'id': 42,
        'first_name': 'Arthur',
        'last_name': 'Dent',
        'full_name': 'Arthur Dent'
    }
    data = validate(schema, data, convert=False, partial=False)
    assert data == expected  # state = 'VALIDATED'

    expected = {
        'id': 42,
        'first_name': 'Arthur',
        'last_name': 'Dent',
        'full_name': 'Arthur Dent'
    }
    data = to_primitive(schema, data)
    assert data == expected  # state = 'SERIALIZED'
Ejemplo n.º 9
0
def test_validate_ignore_extra_context_data():
    class Player(Model):
        id = IntType()

    data = validate(Player, {'id': 4}, context={'name': 'Arthur'})

    assert data == {'id': 4, 'name': 'Arthur'}
Ejemplo n.º 10
0
def test_validate_with_instance_level_validators():
    class Player(Model):
        id = IntType()

        def validate_id(self, data, value, context):
            if self.id:
                raise ValidationError('Cannot change id')

    p1 = Player(trusted_data={'id': 4})

    try:
        validate(Player, p1, {'id': 3})
    except DataError as e:
        assert 'id' in e.messages
        assert 'Cannot change id' in e.messages['id']
        assert p1.id == 4
Ejemplo n.º 11
0
    def test_validate_strict_with_context_data(self):
        class Player(Model):
            id = IntType()

        try:
            data = validate(Player, {"id": 4}, strict=True, context={"name": "Arthur"})
        except ValidationError as e:
            self.assertIn("name", e.messages)
Ejemplo n.º 12
0
    def test_validate_partial_with_context_data(self):
        class Player(Model):
            id = IntType()
            name = StringType(required=True)

        data = validate(Player, {'id': 4}, partial=False, context={'name': 'Arthur'})

        self.assertEqual(data, {'id': 4, 'name': 'Arthur'})
Ejemplo n.º 13
0
def test_validate_override_context_data():
    class Player(Model):
        id = IntType()

    p1 = Player({'id': 4})
    data = validate(Player, {'id': 3}, context=p1._data)

    data == {'id': 3}
Ejemplo n.º 14
0
def test_validate_partial_with_context_data():
    class Player(Model):
        id = IntType()
        name = StringType(required=True)

    data = validate(Player, {'id': 4}, partial=False, context={'name': 'Arthur'})

    assert data == {'id': 4, 'name': 'Arthur'}
Ejemplo n.º 15
0
    def test_validate_partial_with_context_data(self):
        class Player(Model):
            id = IntType()
            name = StringType(required=True)

        data = validate(Player, {"id": 4}, partial=False, context={"name": "Arthur"})

        self.assertEqual(data, {"id": 4, "name": "Arthur"})
Ejemplo n.º 16
0
def test_validate_override_trusted_data():
    class Player(Model):
        id = IntType()

    p1 = Player({'id': 4})
    data = validate(Player, {'id': 3}, trusted_data=p1._data)

    assert data == {'id': 3}
Ejemplo n.º 17
0
    def test_validate_override_context_data(self):
        class Player(Model):
            id = IntType()

        p1 = Player({'id': 4})
        data = validate(Player, {'id': 3}, context=p1._data)

        self.assertEqual(data, {'id': 3})
Ejemplo n.º 18
0
    def test_validate_override_context_data(self):
        class Player(Model):
            id = IntType()

        p1 = Player({'id': 4})
        data = validate(Player, {'id': 3}, context=p1._data)

        self.assertEqual(data, {'id': 3})
Ejemplo n.º 19
0
def test_validate_override_context_data():
    class Player(Model):
        id = IntType()

    p1 = Player({'id': 4})
    data = validate(Player, {'id': 3}, context=p1._data)

    assert data == {'id': 3}
Ejemplo n.º 20
0
def test_validate_with_instance_level_validators():
    class Player(Model):
        id = IntType()

        def validate_id(self, context, value):
            if p1._initial['id'] != value:
                p1._data['id'] = p1._initial['id']
                raise ValidationError('Cannot change id')

    p1 = Player({'id': 4})
    p1.id = 3

    try:
        validate(Player, p1)
    except ValidationError as e:
        assert 'id' in e.messages
        assert 'Cannot change id' in e.messages['id']
        assert p1.id == 4
    def test_request(self):
        response = self.api.ping()

        self.assertEqual(response.status, 200)
        self.assertIsInstance(response.data, PingResponse)
        self.assertTrue(validate(PingResponse, response.data))

        self.assertEqual(response.data.server_status, 'Live')
        self.assertEqual(response.data.db_status, 'Live')
Ejemplo n.º 22
0
def test_validate_with_instance_level_validators():
    class Player(Model):
        id = IntType()

        def validate_id(self, context, value):
            if p1._initial['id'] != value:
                p1._data['id'] = p1._initial['id']
                raise ValidationError('Cannot change id')

    p1 = Player({'id': 4})
    p1.id = 3

    try:
        validate(Player, p1)
    except ValidationError as e:
        assert 'id' in e.messages
        assert 'Cannot change id' in e.messages['id']
        assert p1.id == 4
Ejemplo n.º 23
0
def test_validate_keep_context_data():
    class Player(Model):
        id = IntType()
        name = StringType()

    p1 = Player({'id': 4})
    data = validate(Player, {'name': 'Arthur'}, context=p1._data)

    assert data == {'id': 4, 'name': 'Arthur'}
    assert data != p1._data
Ejemplo n.º 24
0
    def test_validate_keep_context_data(self):
        class Player(Model):
            id = IntType()
            name = StringType()

        p1 = Player({'id': 4})
        data = validate(Player, {'name': 'Arthur'}, context=p1._data)

        self.assertEqual(data, {'id': 4, 'name': 'Arthur'})
        self.assertNotEqual(data, p1._data)
Ejemplo n.º 25
0
def test_validate_keep_context_data():
    class Player(Model):
        id = IntType()
        name = StringType()

    p1 = Player({'id': 4})
    data = validate(Player, {'name': 'Arthur'}, context=p1._data)

    assert data == {'id': 4, 'name': 'Arthur'}
    assert data != p1._data
Ejemplo n.º 26
0
    def test_validate_keep_context_data(self):
        class Player(Model):
            id = IntType()
            name = StringType()

        p1 = Player({'id': 4})
        data = validate(Player, {'name': 'Arthur'}, context=p1._data)

        self.assertEqual(data, {'id': 4, 'name': 'Arthur'})
        self.assertNotEqual(data, p1._data)
Ejemplo n.º 27
0
    def test_validate_keep_context_data(self):
        class Player(Model):
            id = IntType()
            name = StringType()

        p1 = Player({"id": 4})
        data = validate(Player, {"name": "Arthur"}, context=p1._data)

        self.assertEqual(data, {"id": 4, "name": "Arthur"})
        self.assertNotEqual(data, p1._data)
Ejemplo n.º 28
0
def test_validate_partial_with_trusted_data():
    class Player(Model):
        id = IntType()
        name = StringType(required=True)

    data = validate(Player, {'id': 4},
                    partial=False,
                    trusted_data={'name': 'Arthur'})

    assert data == {'id': 4, 'name': 'Arthur'}
Ejemplo n.º 29
0
    def test_validate_strict_with_context_data(self):
        class Player(Model):
            id = IntType()

        with self.assertRaises(ValidationError) as e:
            data = validate(Player, {'id': 4}, strict=True, context={'name': 'Arthur'})
        self.assertIn('name', e.exception.messages)

        with self.assertRaises(ValidationError) as e:
            Player({'id': 4, 'name': 'Arthur'}).validate(strict=True)
        self.assertIn('name', e.exception.messages)
Ejemplo n.º 30
0
    def test_validate_strict_with_context_data(self):
        class Player(Model):
            id = IntType()

        with self.assertRaises(ValidationError) as e:
            data = validate(Player, {'id': 4}, strict=True, context={'name': 'Arthur'})
        self.assertIn('name', e.exception.messages)

        with self.assertRaises(ModelConversionError) as e:
            Player({'id': 4, 'name': 'Arthur'}).validate(strict=True)
        self.assertIn('name', e.exception.messages)
Ejemplo n.º 31
0
def test_object_model_equivalence():
    # functional
    def get_full_name(data, *a, **kw):
        if not data:
            return
        return '{first_name} {last_name}'.format(**data)

    def set_full_name(data, value, *a, **kw):
        if not value:
            return
        data['first_name'], _, data['last_name'] = value.partition(' ')

    schema = Schema(
        'Player', Field('id', IntType()),
        Field('first_name', StringType(required=True)),
        Field('last_name', StringType(required=True)),
        Field(
            'full_name',
            calculated(type=StringType(),
                       fget=get_full_name,
                       fset=set_full_name)))

    # object
    class Player(Model):
        id = IntType()
        first_name = StringType(required=True)
        last_name = StringType(required=True)

        @serializable(type=StringType())
        def full_name(self):
            return get_full_name(self)

        @full_name.setter
        def full_name(self, value):
            set_full_name(self, value)

    input_data = {'id': '42', 'full_name': 'Arthur Dent', 'towel': True}

    data = input_data.copy()
    player = Player(input_data, strict=False, validate=False, init=False)

    data = convert(schema, data, partial=True)
    assert data == player._data

    data = validate(schema, data, convert=False, partial=False)
    player.validate()
    assert data == player._data

    data = to_primitive(schema, data)
    player_dict = player.serialize()
    assert data == player_dict
Ejemplo n.º 32
0
    def test_validate_with_instance_level_validators(self):
        class Player(Model):
            id = IntType()

            def validate_id(self, context, value):
                if self.id:
                    raise ValidationError("Cannot change id")

        p1 = Player({"id": 4})

        try:
            data = validate(p1, {"id": 3})
        except ValidationError as e:
            self.assertIn("id", e.messages)
            self.assertIn("Cannot change id", e.messages["id"])
            self.assertEqual(p1.id, 4)
Ejemplo n.º 33
0
    def test_validate_with_instance_level_validators(self):
        class Player(Model):
            id = IntType()

            def validate_id(self, context, value):
                if self.id:
                    raise ValidationError('Cannot change id')

        p1 = Player({'id': 4})

        try:
            data = validate(p1, {'id': 3})
        except ValidationError as e:
            self.assertIn('id', e.messages)
            self.assertIn('Cannot change id', e.messages['id'])
            self.assertEqual(p1.id, 4)
    def test_migration_status_exists(self):
        register_uri(
            POST,
            'http://%s/api/v2/migration/%s/success' % (api_dev_url, dummy_campaign_customer_uuid),
            body=b'{"success":true,"msg":"User already migrated"}',
            status=200,
            content_type='application/json'
        )

        response = self.api.send_migration_status(dummy_campaign_customer_uuid)

        self.assertEqual(response.status, 200)
        self.assertIsInstance(response.data, MigrationStatusResponse)
        self.assertTrue(validate(MigrationStatusResponse, response.data))

        self.assertTrue(response.data.success)
Ejemplo n.º 35
0
    def test_validate_with_instance_level_validators(self):
        class Player(Model):
            id = IntType()

            def validate_id(self, context, value):
                if p1._initial['id'] != value:
                    p1._data['id'] = p1._initial['id']
                    raise ValidationError('Cannot change id')

        p1 = Player({'id': 4})
        p1.id = 3

        try:
            data = validate(Player, p1)
        except ValidationError as e:
            self.assertIn('id', e.messages)
            self.assertIn('Cannot change id', e.messages['id'])
            self.assertEqual(p1.id, 4)
    def test_request(self):
        register_uri(
            GET,
            "http://%s/api/v2/banner/%s?email=%s" % (api_dev_url, dummy_campaign_uuid, self.dummy_email),
            body=bytearray(
                """
            {
                "bannerImageUrl": "https://buyexpressly.com/assets/banner/awesome-banner.jpg",
                "migrationLink": "https://www.myblog.com/expressly/api/3aff1880-b0f5-45bd-8f33-247f55981f2c"
            }""",
                "utf-8",
            ),
            status=200,
            content_type="application/json",
        )

        response = self.api.get_banner(dummy_campaign_uuid, self.dummy_email)

        self.assertEqual(response.status, 200)
        self.assertIsInstance(response.data, BannerResponse)
        self.assertTrue(validate(BannerResponse, response.data))
Ejemplo n.º 37
0
def test_state_machine_equivalence(player_schema, player_data):
    schema = player_schema
    input_data = player_data

    data = input_data.copy()
    machine = Machine(input_data, schema)
    assert machine.state == 'raw'

    data = convert(schema, data, partial=True)
    machine.convert()
    assert machine.state == 'converted'
    assert data == machine.data

    data = validate(schema, data, convert=False, partial=False)
    machine.validate()
    assert machine.state == 'validated'
    assert data == machine.data

    data = to_primitive(schema, data)
    machine.serialize()
    assert machine.state == 'serialized'
    assert data == machine.data
Ejemplo n.º 38
0
def test_validate_simple_dict():
    class Player(Model):
        id = IntType()

    validate(Player, {'id': 4})
 def time_validate_dictionary(self):
     validate(Foo, data)
 def peakmem_validate_dictionary(self):
     for i in xrange(10000):
         validate(Foo, data)
Ejemplo n.º 41
0
def test_functional_schema_required(player_schema):
    with pytest.raises(DataError):
        validate(player_schema, {}, partial=False)
    def test_request(self):
        register_uri(
            GET,
            'http://%s/api/v2/migration/%s/user' % (api_dev_url, dummy_campaign_customer_uuid),
            body=bytearray("""
            {
                "meta": {
                    "locale": "UKR",
                    "sender": "https://expresslyapp.com/api/v2/migration/%s}"
                },
                "data": {
                    "email": "*****@*****.**",
                    "customerData": {
                        "firstName": "John",
                        "lastName": "Smith",
                        "gender": "M",
                        "billingAddress": 0,
                        "shippingAddress": 1,
                        "company": "Expressly",
                        "dob": "1987-08-07",
                        "taxNumber": "GB0249894821",
                        "onlinePresence": [
                            {
                                "field": "website",
                                "value": "http://www.myblog.com"
                            }
                        ],
                        "dateUpdated": "2015-07-10T11:42:00+01:00",
                        "emails": [
                            {
                                "email": "*****@*****.**",
                                "alias": "default"
                            },
                            {
                                "email": "*****@*****.**",
                                "alias": "work"
                            }
                        ],
                        "phones": [
                            {
                                "type": "M",
                                "number": "020734581250",
                                "countryCode": 44
                            },
                            {
                                "type": "L",
                                "number": "020731443250",
                                "countryCode": 44
                            }
                        ],
                        "addresses": [
                            {
                                "firstName": "John",
                                "lastName": "Smith",
                                "address1": "12 Piccadilly",
                                "address2": "Room 14",
                                "city": "London",
                                "companyName": "WorkHard Ltd",
                                "zip": "W1C 34U",
                                "phone": 1,
                                "alias": "Work address",
                                "stateProvince": "LND",
                                "country": "GBR"
                            },
                            {
                                "firstName": "John C.",
                                "lastName": "Smith",
                                "address1": "23 Sallsberry Ave",
                                "address2": "Flat 3",
                                "city": "London",
                                "companyName": "",
                                "zip": "NW3 4HG",
                                "phone": 0,
                                "alias": "Home address",
                                "stateProvince": "LND",
                                "country": "GBR"
                            }
                        ]
                    },
                    "cart": {
                        "productId": "491",
                        "couponCode": "20OFF"
                    }
                }
            }""" % dummy_campaign_customer_uuid, 'utf-8'),
            status=200,
            content_type='application/json'
        )

        response = self.api.get_migration_customer(dummy_campaign_customer_uuid)

        self.assertTrue(response.status, 200)
        self.assertIsInstance(response.data, MigrationCustomerResponse)
        self.assertTrue(validate(MigrationCustomerResponse, response.data))
Ejemplo n.º 43
0
def validate_test():
    song1 = Song()
    song1.artist = 'Fiona Apple'
    song1.url = 'http://www.youtube.com/watch?v=67KGSJVkix0'
    # song1.validate()
    validate(Song, song1)
Ejemplo n.º 44
0
def test_validate_simple_dict():
    class Player(Model):
        id = IntType()

    validate(Player, {'id': 4})
Ejemplo n.º 45
0
    def test_validate_simple_dict(self):
        class Player(Model):
            id = IntType()

        validate(Player, {"id": 4})