class CustomObjectKeyConstructor(DefaultKeyConstructor): """ Used to compute cache key for a single object. """ retrieve_sql = RetrieveSqlQueryKeyBit() updated_at = UpdatedAtKeyBit() user = UserKeyBit()
def test_should_use_view__get_queryset__and_filter_it_with__filter_queryset__and_filter_by__lookup_field(self): expected = (u'SELECT "unit_bittestmodel"."id", "unit_bittestmodel"."is_active" ' u'FROM "unit_bittestmodel" ' u'WHERE ("unit_bittestmodel"."is_active" = True {space}AND "unit_bittestmodel"."id" = 123{space})') space = '' expected = expected.format(space=space) response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, expected)
def test_should_use_view__get_queryset__and_filter_it_with__filter_queryset__and_filter_by__lookup_field( self): expected = ( u'SELECT "tests_app_bittestmodel"."id", "tests_app_bittestmodel"."is_active" ' u'FROM "tests_app_bittestmodel" ' u'WHERE ("tests_app_bittestmodel"."is_active" = True AND "tests_app_bittestmodel"."id" = 123 )' ) response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, expected)
def test_should_use_view__get_queryset__and_filter_it_with__filter_queryset__and_filter_by__lookup_field(self): expected = (u'SELECT "tests_app_bittestmodel"."id", "tests_app_bittestmodel"."is_active" ' u'FROM "tests_app_bittestmodel" ' u'WHERE ("tests_app_bittestmodel"."is_active" = True {space}AND "tests_app_bittestmodel"."id" = 123{space})') if get_django_features()['has_odd_space_in_sql_query']: space = ' ' else: space = '' expected = expected.format(space=space) response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, expected)
def test_should_use_view__get_queryset__and_filter_it_with__filter_queryset__and_filter_by__lookup_field( self): if django.VERSION >= (3, 1): expected = ( 'SELECT "unit_bittestmodel"."id", "unit_bittestmodel"."is_active" ' 'FROM "unit_bittestmodel" ' 'WHERE ("unit_bittestmodel"."is_active" AND "unit_bittestmodel"."id" = 123)' ) else: expected = ( 'SELECT "unit_bittestmodel"."id", "unit_bittestmodel"."is_active" ' 'FROM "unit_bittestmodel" ' 'WHERE ("unit_bittestmodel"."is_active" = True AND "unit_bittestmodel"."id" = 123)' ) response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, expected)
def test_should_return_none_if_empty_result_set_raised(self): self.kwargs['view_instance'].filter_queryset = lambda x: x.filter(pk__in=[]) response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, None)
def test_with_bad_lookup_value(self): self.kwargs['view_instance'].kwargs = {'id': "I'm ganna hack u are!"} response = RetrieveSqlQueryKeyBit().get_data(**self.kwargs) self.assertEqual(response, None)
class CustomObjectKeyConstructor(DefaultKeyConstructor): retrieve_sql = RetrieveSqlQueryKeyBit() updated_at = UpdatedAtKeyBit()
class PostObjectKeyConstructor(DefaultKeyConstructor): retrieve_sql = RetrieveSqlQueryKeyBit() updated_at = PostUpdatedAtKeyBit()