def test_parenthesized(self): searches.to_filter_args( 'user', Context(), 'email:"word" or (email:"test" and email:"example")') searches.to_filter_args('user', Context(), 'email:"test" or (email:"example")') searches.to_filter_args('user', Context(), '(email:"test")') searches.to_filter_args('user', Context(), '((email:"test"))')
def test_get_search(self): self.assertEqual(query_utils.get_search( Context(), {}, 'user'), []) self.assertEqual(query_utils.get_search(Context(), { 'search': 'email:"test"', }, 'user'), query_utils.searchutils.to_filter_args('user', Context(), 'email:"test"')) self.assertEqual(query_utils.get_search(Context(), { 'tsearch': 'email:"test"', }, 'user', param_name='tsearch'), query_utils.searchutils.to_filter_args('user', Context(), 'email:"test"'))
def _user_category_get(request, uuid_): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) user_category = None try: user_category = models.UserCategory.objects.get(uuid=uuid_, user=request.user) except models.UserCategory.DoesNotExist: return HttpResponseNotFound('user category not found') ret_obj = query_utils.generate_return_object(field_maps, user_category, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_entry_read_post(request, uuid_): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) read_feed_entry_user_mapping = None with transaction.atomic(): feed_entry = None try: feed_entry = models.FeedEntry.objects.get(uuid=uuid_) except models.FeedEntry.DoesNotExist: return HttpResponseNotFound('feed entry not found') try: read_feed_entry_user_mapping = models.ReadFeedEntryUserMapping.objects.get( feed_entry=feed_entry, user=request.user) except models.ReadFeedEntryUserMapping.DoesNotExist: read_feed_entry_user_mapping = models.ReadFeedEntryUserMapping.objects.create( feed_entry=feed_entry, user=request.user) ret_obj = context.format_datetime(read_feed_entry_user_mapping.read_at) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_get(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) url = request.GET.get('url') if not url: return HttpResponseBadRequest('\'url\' missing') url = url_normalize(url) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) feed = None try: feed = models.Feed.annotate_subscription_data( models.Feed.objects.all(), request.user).get(feed_url=url) except models.Feed.DoesNotExist: try: feed = _save_feed(url) except QueryException as e: return HttpResponse(e.message, status=e.httpcode) ret_obj = query_utils.generate_return_object(field_maps, feed, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def test_run(self): self.assertEqual(len(AllFieldsTestCase.TRIALS), len(fields._field_configs)) for key, generator in AllFieldsTestCase.TRIALS.items(): with self.subTest(key=key): db_objs = generator() fields_dict = fields._field_configs[key] for db_obj in db_objs: for field_config in fields_dict.values(): context = Context() context.request = AllFieldsTestCase.MockRequest() field_config.accessor(context, db_obj)
def _user_category_post(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover if 'text' not in json_: return HttpResponseBadRequest('\'text\' missing') if type(json_['text']) is not str: return HttpResponseBadRequest('\'text\' must be string') user_category = models.UserCategory(user=request.user, text=json_['text']) try: user_category.save() except IntegrityError: return HttpResponse('user category already exists', status=409) ret_obj = query_utils.generate_return_object(field_maps, user_category, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def test_generate_return_object(self): field_maps = [ { 'field_name': 'uuid', 'accessor': lambda context, db_obj: db_obj.uuid, } ] class MockObject: pass db_obj = MockObject() db_obj.uuid = 'test string' context = Context() self.assertEqual(query_utils.generate_return_object(field_maps, db_obj, context), { 'uuid': 'test string', })
def _feed_entries_query_stable_create_post(request): cache = caches['stable_query'] context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover sort = None try: sort = query_utils.get_sort(json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) search = None try: search = query_utils.get_search(context, json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) token = f'feedentry-{uuid.uuid4().int}' cache.set( token, list( models.FeedEntry.annotate_search_vectors( models.FeedEntry.objects.all()).filter(*search).order_by( *sort).values_list('uuid', flat=True))) content, content_type = query_utils.serialize_content(token) return HttpResponse(content, content_type)
def _user_get(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) user = request.user field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) ret_obj = query_utils.generate_return_object(field_maps, user, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def test_context(self): context = Context() query_dict = QueryDict( '_dtformat=%Y-%m-%d %H:%M:%S&_dformat=%Y-%m-%d&_tformat=%H:%M:%S') context.parse_query_dict(query_dict) utcnow = datetime.datetime.utcnow() self.assertEqual(context.format_datetime(utcnow), utcnow.strftime('%Y-%m-%d %H:%M:%S')) self.assertEqual(context.format_date(utcnow.date()), utcnow.date().strftime('%Y-%m-%d')) self.assertEqual(context.format_time(utcnow.time()), utcnow.time().strftime('%H:%M:%S')) d = Decimal('0.5') self.assertAlmostEqual(context.format_decimal(d), float(d))
def test_run(self): self.assertEqual(len(AllSearchesTestCase.TRIALS), len(searches._search_fns)) for key, trial_dict in AllSearchesTestCase.TRIALS.items(): with self.subTest(key=key): search_fns_dict = searches._search_fns[key] trial_searches = trial_dict['searches'] self.assertEqual(len(trial_searches), len(search_fns_dict)) queryset = trial_dict['get_queryset']() for field, test_values in trial_searches.items(): for test_value in test_values: with self.subTest(field=field, test_value=test_value): context = Context() context.request = AllSearchesTestCase.MockRequest() q = search_fns_dict[field](context, test_value) result = list(queryset.filter(q)) self.assertIsNotNone(result)
def test_feedentry_isRead(self): context = Context() user = models.User.objects.create( email='*****@*****.**') class MockRequest: def __init__(self): self.user = user context.request = MockRequest() feed = models.Feed.objects.create( feed_url='http://example.com/rss.xml', title='Sample Feed', home_url='http://example.com', published_at=datetime.datetime.utcnow(), updated_at=None, db_updated_at=None) feed_entry1 = models.FeedEntry.objects.create( feed=feed, url='http://example.com/entry1.html', content='<b>Some HTML Content</b>', author_name='John Doe') feed_entry2 = models.FeedEntry.objects.create( feed=feed, url='http://example.com/entry2.html', content='<b>Some HTML Content</b>', author_name='John Doe') models.ReadFeedEntryUserMapping.objects.create( feed_entry=feed_entry2, user=user) self.assertIsNone(fields._feedentry_readAt(context, feed_entry1)) self.assertIsNotNone(fields._feedentry_readAt(context, feed_entry2))
def get_context(self, request: HttpRequest, response: Any) -> Context: return Context(request=request, response=response)
def test_exclude(self): searches.to_filter_args( 'feed', Context(), 'uuid:!"99d63124-59e2-4204-ba61-be294dcb4d22,c54a1f76-f350-4336-b7c4-33ec8f5e81a3"' )
def test_or(self): searches.to_filter_args('user', Context(), 'email:"test" and email:"example"') searches.to_filter_args('user', Context(), 'email:"test" AND email:"example"')
def test_and(self): searches.to_filter_args('user', Context(), 'email:"test" or email:"example"') searches.to_filter_args('user', Context(), 'email:"test" OR email:"example"')
def test_malformed_value(self): with self.assertRaises(QueryException): searches.to_filter_args('feed', Context(), 'uuid:"bad uuid"')
def test_unknown_field(self): with self.assertRaises(QueryException): searches.to_filter_args('feed', Context(), 'email:"test"')
def test_malformed(self): with self.assertRaises(QueryException): searches.to_filter_args('user', Context(), '') with self.assertRaises(QueryException): searches.to_filter_args('user', Context(), '((email:"test")')
def _feed_entries_query_stable_post(request): cache = caches['stable_query'] context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover token = None try: token = json_['token'] except KeyError: return HttpResponseBadRequest('\'token\' missing') if type(token) is not str: return HttpResponseBadRequest('\'token\' must be string') if re.search(r'^feedentry-\d+$', token) is None: return HttpResponseBadRequest('\'token\' malformed') count = None try: count = query_utils.get_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) skip = None try: skip = query_utils.get_skip(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) field_maps = None try: fields = query_utils.get_fields__json(json_) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_objects = None try: return_objects = query_utils.get_return_objects(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_total_count = None try: return_total_count = query_utils.get_return_total_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) cache.touch(token) uuids = cache.get(token, []) ret_obj = {} if return_objects: current_uuids = uuids[skip:skip + count] feed_entries = { feed_entry.uuid: feed_entry for feed_entry in models.FeedEntry.objects.filter( uuid__in=current_uuids) } objs = [] if len(current_uuids) == len(feed_entries): for uuid_ in current_uuids: feed_entry = feed_entries[uuid_] obj = query_utils.generate_return_object( field_maps, feed_entry, context) objs.append(obj) ret_obj['objects'] = objs if return_total_count: ret_obj['totalCount'] = len(uuids) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def get_context(self, request: HttpRequest) -> Context: return Context(request=request)
def _feed_entries_query_post(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover count = None try: count = query_utils.get_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) skip = None try: skip = query_utils.get_skip(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) sort = None try: sort = query_utils.get_sort(json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) search = None try: search = query_utils.get_search(context, json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) field_maps = None try: fields = query_utils.get_fields__json(json_) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_objects = None try: return_objects = query_utils.get_return_objects(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_total_count = None try: return_total_count = query_utils.get_return_total_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) feed_entries = models.FeedEntry.annotate_search_vectors( models.FeedEntry.objects.all()).filter(*search) ret_obj = {} if return_objects: objs = [] for feed_entry in feed_entries.order_by(*sort)[skip:skip + count]: obj = query_utils.generate_return_object(field_maps, feed_entry, context) objs.append(obj) ret_obj['objects'] = objs if return_total_count: ret_obj['totalCount'] = feed_entries.count() content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)