예제 #1
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()
예제 #2
0
    def test_to_internal_value_raises_validation_error_for_a_wrong_type(self):
        session = SESSION()
        queryset = session.query(self.TestPrimaryKeyRelatedFieldUserModel)
        instance = PrimaryKeyRelatedField(queryset=queryset)

        self.assertRaises(ValidationError, instance.to_internal_value, None)

        session.close()
예제 #3
0
    def test_is_saved_in_database_returs_true(self):
        instance = HyperlinkedRelatedField(view_name='user-detail')
        session = SESSION()

        user = session.query(self.TestHyperlinkedRelatedFieldUserModel) \
                      .filter_by(name='admin').first()
        self.assertTrue(instance.is_saved_in_database(user))

        session.close()
예제 #4
0
    def test_to_internal_value_raises_validation_exc_for_invalid_pk(self):
        session = SESSION()
        queryset = session.query(self.TestPrimaryKeyRelatedFieldUserModel)
        instance = PrimaryKeyRelatedField(queryset=queryset)

        self.assertRaises(ValidationError, instance.to_internal_value,
                          {'id': -1})

        session.close()
예제 #5
0
    def test_get_lookup_value_returns_tuple_for_objects_single_pk(self):
        instance = HyperlinkedRelatedField(view_name='user-detail')
        session = SESSION()

        user = session.query(self.TestHyperlinkedRelatedFieldUserModel) \
                      .filter_by(name='admin').first()
        self.assertEqual(instance.get_lookup_value(user), (user.id, ))

        session.close()
예제 #6
0
    def test_get_object_raises_improperly_configured_exception(self):
        session = SESSION()
        queryset = session.query(self.TestHyperlinkedRelatedFieldUserModel)
        instance = HyperlinkedRelatedField(view_name='user-detail',
                                           queryset=queryset)

        self.assertRaises(ImproperlyConfigured, instance.get_object,
                          'user-detail', [], {})

        session.close()
예제 #7
0
    def test_get_object_raises_validation_error_for_object_not_found(self):
        session = SESSION()
        queryset = session.query(self.TestHyperlinkedRelatedFieldUserModel)
        instance = HyperlinkedRelatedField(view_name='user-detail',
                                           queryset=queryset)

        self.assertRaises(ValidationError, instance.get_object, 'user-detail',
                          [], {'id': -1})

        session.close()
예제 #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)
     session.commit()
     session.close()
예제 #9
0
    def test_to_internal_value_returns_object(self):
        session = SESSION()
        user = session.query(self.TestPrimaryKeyRelatedFieldUserModel) \
                      .filter_by(name='admin').first()
        queryset = session.query(self.TestPrimaryKeyRelatedFieldUserModel)
        instance = PrimaryKeyRelatedField(queryset=queryset)

        relation_object = instance.to_internal_value({'id': user.id})
        self.assertIsInstance(relation_object,
                              self.TestPrimaryKeyRelatedFieldUserModel)
        self.assertEqual(relation_object.id, user.id)
예제 #10
0
    def test_get_attribute_returns_attribute(self):
        instance = RelatedField(read_only=True)
        instance.bind('user', self)
        session = SESSION()

        address = session.query(self.TestRelatedFieldAddressModel)\
                         .filter_by(email_address='*****@*****.**')\
                         .first()
        self.assertEqual(instance.get_attribute(address), address.user)

        session.close()
예제 #11
0
    def test_get_lookup_value_returns_tuple_for_objects_composite_pk(self):
        instance = HyperlinkedRelatedField(view_name='user-detail')
        session = SESSION()

        user = session\
            .query(self.TestHyperlinkedRelatedFieldCompositePkModel) \
            .filter_by(id=1, name='test_user').first()
        self.assertEqual(set(instance.get_lookup_value(user)),
                         {user.id, user.name})

        session.close()
예제 #12
0
    def test_get_attribute_returns_raises_attribute_error(self):
        instance = self.RelatedWithOptimization(read_only=True)
        instance.bind('user.pk', self)

        session = SESSION()

        address = session.query(self.TestRelatedFieldAddressModel) \
            .filter_by(email_address='*****@*****.**') \
            .first()
        self.assertRaises(AttributeError, instance.get_attribute, address)

        session.close()
예제 #13
0
    def test_get_attribute_returns_pk_only_object(self):
        instance = self.RelatedWithOptimization(read_only=True)
        instance.bind('user.id', self)

        session = SESSION()

        address = session.query(self.TestRelatedFieldAddressModel) \
                         .filter_by(email_address='*****@*****.**') \
                         .first()
        relation_object = instance.get_attribute(address)
        self.assertIsInstance(relation_object, PKOnlyObject)
        self.assertEqual(relation_object.pk, address.user.id)

        session.close()
예제 #14
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()
예제 #15
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()
예제 #16
0
 def setUpClass(cls):
     cls._cls_overridden_context = override_settings(**cls.settings)
     cls._cls_overridden_context.enable()
     super(TestSQLAlchemyMixin, cls).setUpClass()
     cls.session = SESSION()
     cls.session.add_all([
         cls.TableForSQLAlchemyMixin(login='******'),
     ])
     cls.session.commit()
예제 #17
0
 def setUpClass(cls):
     super(TestUniqueORMValidator, cls).setUpClass()
     cls.session = SESSION()
     cls.session.add_all([
         cls.TableWithUniqueName(name='Adam'),
         cls.TableWithUniqueName(name='Bob'),
         cls.TableWithUniqueName(name='Eve'),
     ])
     cls.session.commit()
예제 #18
0
    def test_is_saved_in_database_returs_false_for_pending_object(self):
        instance = HyperlinkedRelatedField(view_name='user-detail')
        session = SESSION()

        user = self.TestHyperlinkedRelatedFieldUserModel(name='test_admin')
        session.add(user)
        self.assertFalse(instance.is_saved_in_database(user))

        session.close()
예제 #19
0
    def test_to_representation_returns_object_pk(self):
        session = SESSION()
        queryset = session.query(self.TestPrimaryKeyRelatedFieldUserModel)
        instance = PrimaryKeyRelatedField(queryset=queryset)

        user = session.query(self.TestPrimaryKeyRelatedFieldUserModel) \
                      .filter_by(name='admin').first()
        self.assertEqual(instance.to_representation(user), user.id)

        session.close()
예제 #20
0
    def test_get_attribute_returns_empty_list_for_pending_object(self):
        child_instance = self.FakeRelatedField(read_only=True)
        instance = ManyRelatedField(child_relation=child_instance)
        instance.bind('addresses', self)

        session = SESSION()

        pending_user = self.TestManyRelatedFieldUserModel(name='admin')
        session.add(pending_user)
        self.assertEqual(instance.get_attribute(pending_user), [])

        session.close()
예제 #21
0
    def test_get_object_returns_object_from_database(self):
        session = SESSION()
        queryset = session.query(self.TestHyperlinkedRelatedFieldUserModel)
        instance = HyperlinkedRelatedField(view_name='user-detail',
                                           queryset=queryset)

        user = session.query(self.TestHyperlinkedRelatedFieldUserModel) \
            .filter_by(name='admin').first()

        obj = instance.get_object('user-detail', [], {'id': user.id})
        self.assertIsInstance(obj, self.TestHyperlinkedRelatedFieldUserModel)
        self.assertEqual(obj.id, user.id)

        session.close()
예제 #22
0
    def test_to_representation_returns_remote_pk_from_pk_only_object(self):
        session = SESSION()
        queryset = session.query(self.TestPrimaryKeyRelatedFieldEmailModel)
        instance = PrimaryKeyRelatedField(queryset=queryset,
                                          many=False,
                                          required=False,
                                          pk_field=CharField())

        user = session.query(self.TestPKRelatedFieldUserNoUselistModel) \
            .filter_by(name='admin_no_uselist').first()

        email_pk = user.email.email
        pk_only_object = PKOnlyObject(pk=user.email)
        self.assertEqual(instance.to_representation(pk_only_object), email_pk)

        session.close()
예제 #23
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()
예제 #24
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()
예제 #25
0
 def setUpClass(cls):
     cls._cls_overridden_context = override_settings(**cls.settings)
     cls._cls_overridden_context.enable()
     super(TestORMSessionMixin, cls).setUpClass()
     cls.session = SESSION()
예제 #26
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()
예제 #27
0
    def test_create_with_relation_data(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)
        instance.create(instance.validated_data)
        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'])
        self.assertEqual(user.addresses[0].id, address.id)

        session.delete(user)
        session.delete(address)
        session.commit()
        session.close()
예제 #28
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()
예제 #29
0
    def test_update_raises_validate_error_for_object_does_not_exist(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)
        session.delete(user)
        session.commit()

        self.assertRaises(ValidationError, instance.save)

        session.delete(address)
        session.commit()
        session.close()
예제 #30
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)