Beispiel #1
0
class TestQuerySetMiddleware(TestCase):

    def setUp(self):
        self.middleware = QuerySetMiddleware()
        self.request = Mock()
        self.request.session = {}

    def test_query(self):
        self.request.configure_mock(
            **{
                'GET': {
                    'MODEL_PATH': 'django.contrib.auth.models.User',
                    'ARGS': ''
                }
            }
        )

        john = User(username='******', email='*****@*****.**')
        john.save()
        response = self.middleware.process_request(self.request)
        self.assertEqual(200, response.status_code)
        self.assertEqual(
            'john',
            json.loads(response.content.decode('utf-8'))[0].get('username')
        )
        self.assertEqual(
            '*****@*****.**',
            json.loads(response.content.decode('utf-8'))[0].get('email')
        )

    def test_empty_query(self):
        self.request.configure_mock(
            **{
                'GET': {
                    'MODEL_PATH': 'django.contrib.auth.models.User',
                    'QUERY_ARGS': '{"username": "******"}'
                }
            }
        )

        john = User(username='******', email='*****@*****.**')
        john.save()
        response = self.middleware.process_request(self.request)
        self.assertEqual(200, response.status_code)
        self.assertEqual(
            0,
            len(json.loads(response.content.decode('utf-8'))),
            'Query result should be empty when querying for username "jane"'
        )
Beispiel #2
0
 def setUp(self):
     self.middleware = QuerySetMiddleware()
     self.request = Mock()
     self.request.session = {}