Exemplo n.º 1
0
    def test_create_raises_type_error(self):

        class UserSerializer(ModelSerializer):
            addresses = serializers.PrimaryKeyRelatedField(
                queryset=Query(self.TestModelSerializerAddressModel),
                many=True
            )

            class Meta:
                model = self.TestModelSerializerUserModel

        session = SESSION()
        address = self.TestModelSerializerAddressModel(email='*****@*****.**')
        session.add(address)
        session.commit()
        data = {
            'name': 'admin_with_email',
            'addresses': [{'id': address.id}, ],
            'gender': 'male'

        }
        instance = UserSerializer(data=data)
        instance.is_valid(raise_exception=True)

        self.assertRaises(TypeError, instance.create, data)

        session.delete(address)
        session.commit()
        session.close()
Exemplo n.º 2
0
    def setUpClass(cls):
        super(TestHyperlinkedRelatedField, cls).setUpClass()
        session = SESSION()
        user = cls.TestHyperlinkedRelatedFieldUserModel(name='admin')
        address = cls.TestHyperlinkedRelatedFieldAddressModel(
            email_address='*****@*****.**')
        user_composite_pk = cls.TestHyperlinkedRelatedFieldCompositePkModel(
            id=1, name='test_user')
        user.addresses.append(address)
        session.add(user)
        session.add(address)
        session.add(user_composite_pk)
        session.commit()
        session.close()

        url_parser = URLParser()
        cls.data = {
            'urls': [
                url_parser.define_route('/user/{pk}/',
                                        FakeView, [
                                            'GET',
                                        ],
                                        name='user-detail'),
            ]
        }
        set_urlconf(cls.data)
Exemplo n.º 3
0
    def test_create(self):

        class UserSerializer(ModelSerializer):

            class Meta:
                model = self.TestModelSerializerUserModel
                exclude = ('addresses', )

        data = {'name': 'default_admin', 'gender': 'male'}
        instance = UserSerializer(data=data)
        instance.is_valid(raise_exception=True)
        instance.create(instance.data)
        session = SESSION()
        user = session\
            .query(self.TestModelSerializerUserModel)\
            .filter(self.TestModelSerializerUserModel.name == data['name'])\
            .first()

        self.assertIsNotNone(user)
        self.assertIsInstance(user, self.TestModelSerializerUserModel)
        self.assertEqual(user.name, data['name'])
        self.assertEqual(user.gender, data['gender'])

        session.delete(user)
        session.commit()
        session.close()
Exemplo n.º 4
0
 def setUpClass(cls):
     super(TestGetFieldKwargs, cls).setUpClass()
     session = SESSION()
     session.add_all([
         cls.TestGetFieldKwargsModel(
             username='******',
             address='UK, London',
             email='*****@*****.**',
             gender='male',
             salary=100.00
         ),
         cls.TestGetFieldKwargsModel(
             username='******',
             address='USA, CA, San Francisco',
             email='*****@*****.**',
             gender='male',
             salary=123.56
         ),
         cls.TestGetFieldKwargsModel(
             username='******',
             address=None,
             email='*****@*****.**',
             gender='female',
             salary=999.99
         ),
     ])
     session.commit()
Exemplo n.º 5
0
 def setUpClass(cls):
     super(TestPrimaryKeyRelatedField, cls).setUpClass()
     session = SESSION()
     user = cls.TestPrimaryKeyRelatedFieldUserModel(name='admin')
     address = cls.TestPrimaryKeyRelatedFieldAddressModel(
         email_address='*****@*****.**')
     user.addresses.append(address)
     session.add(user)
     session.add(address)
     session.commit()
     session.close()
Exemplo n.º 6
0
    def test_update_with_relations(self):

        class UserSerializer(ModelSerializer):
            addresses = serializers.PrimaryKeyRelatedField(
                queryset=Query(self.TestModelSerializerAddressModel),
                many=True
            )

            class Meta:
                model = self.TestModelSerializerUserModel

        session = SESSION()
        address = self.TestModelSerializerAddressModel(email='*****@*****.**')
        session.add(address)
        session.commit()
        data = {
            'name': 'admin_for_update_v1',
            'addresses': [],
            'gender': 'male'
        }
        instance = UserSerializer(data=data)
        instance.is_valid(raise_exception=True)
        instance.save()
        user = session \
            .query(self.TestModelSerializerUserModel) \
            .filter(self.TestModelSerializerUserModel.name == data['name']) \
            .first()

        self.assertIsNotNone(user)
        self.assertIsInstance(user, self.TestModelSerializerUserModel)
        self.assertEqual(user.name, data['name'])
        self.assertEqual(user.gender, data['gender'])

        data = {
            'name': 'admin_for_update_v2',
            'addresses': [{'id': address.id}, ],
            'gender': 'male'
        }
        instance = UserSerializer(user, data=data, partial=True)
        instance.is_valid(raise_exception=True)
        updated_user = instance.save()

        self.assertIsNotNone(updated_user)
        self.assertIsInstance(updated_user, self.TestModelSerializerUserModel)
        self.assertEqual(updated_user.name, data['name'])
        self.assertEqual(updated_user.gender, data['gender'])
        self.assertEqual(len(updated_user.addresses), 1)
        self.assertEqual(updated_user.addresses[0].id, address.id)

        session.delete(address)
        session.delete(user)
        session.commit()
        session.close()
Exemplo n.º 7
0
    def test_get_attribute_returns_relation(self):
        child_instance = self.FakeRelatedField(read_only=True)
        instance = ManyRelatedField(child_relation=child_instance)
        instance.bind('addresses', self)

        session = SESSION()

        user = self.TestManyRelatedFieldUserModel(name='admin')
        address = self.TestManyRelatedFieldAddressModel(
            email_address='*****@*****.**')
        user.addresses.append(address)
        session.add(user)
        session.add(address)
        session.commit()
        self.assertEqual(instance.get_attribute(user), [address])

        session.close()
Exemplo n.º 8
0
    def setUpClass(cls):
        super(TestPrimaryKeyRelatedField, cls).setUpClass()
        session = SESSION()
        user = cls.TestPrimaryKeyRelatedFieldUserModel(name='admin')
        address = cls.TestPrimaryKeyRelatedFieldAddressModel(
            email_address='*****@*****.**')
        user.addresses.append(address)
        session.add(user)
        session.add(address)

        user_custom = cls.TestPKRelatedFieldUserNoUselistModel(
            name='admin_no_uselist')
        email = cls.TestPrimaryKeyRelatedFieldEmailModel(
            email='*****@*****.**')
        user_custom.email = email
        session.add(user_custom)
        session.add(email)

        session.commit()
        session.close()
Exemplo n.º 9
0
 def setUpClass(cls):
     super(TestGetFieldKwargs, cls).setUpClass()
     session = SESSION()
     session.add_all([
         cls.TestGetFieldKwargsModel(username='******',
                                     address='UK, London',
                                     email='*****@*****.**',
                                     gender='male',
                                     salary=100.00),
         cls.TestGetFieldKwargsModel(username='******',
                                     address='USA, CA, San Francisco',
                                     email='*****@*****.**',
                                     gender='male',
                                     salary=123.56),
         cls.TestGetFieldKwargsModel(username='******',
                                     address=None,
                                     email='*****@*****.**',
                                     gender='female',
                                     salary=999.99),
     ])
     session.commit()
Exemplo n.º 10
0
    def test_update(self):

        class UserSerializer(ModelSerializer):

            class Meta:
                model = self.TestModelSerializerUserModel
                exclude = ('addresses', )

        data = {'name': 'admin_for_update_v1', 'gender': 'male'}
        instance = UserSerializer(data=data)
        instance.is_valid(raise_exception=True)
        instance.save()
        session = SESSION()
        user = session \
            .query(self.TestModelSerializerUserModel) \
            .filter(self.TestModelSerializerUserModel.name == data['name']) \
            .first()

        self.assertIsNotNone(user)
        self.assertIsInstance(user, self.TestModelSerializerUserModel)
        self.assertEqual(user.name, data['name'])
        self.assertEqual(user.gender, data['gender'])

        data = {'name': 'admin_for_update_v2', 'gender': 'female'}
        instance = UserSerializer(user, data=data, partial=True)
        instance.is_valid(raise_exception=True)
        updated_user = instance.save()

        self.assertIsNotNone(updated_user)
        self.assertIsInstance(updated_user, self.TestModelSerializerUserModel)
        self.assertEqual(updated_user.name, data['name'])
        self.assertEqual(updated_user.gender, data['gender'])

        session.delete(user)
        session.commit()
        session.close()