def test_place_filters(self): datetimes = (datetime.datetime(*d) for d in ((2100, 1, 2, 0, 0), (2100, 1, 1, 12, 00), (2100, 1, 1, 23, 00))) place = get(Place, point=F(geometry=geos.Point(x=0, y=0))) matched_events = { self.make_event_fixture(start_date=dt.date(), start_time=dt.time(), place=place) for dt in datetimes } self.make_event_fixture() # throw in a non-match query_params = dict(**self.auth_params) query_params.update( **{'place': PlaceFilterOptions()._uri_from_obj(place)}) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(matched_events)) json_resp = try_json_loads(resp.content) resource = api_v1._registry['eventsummary'] matched_events_sorted = sorted( matched_events, key=lambda e: e.occurrences.all()[0].start_datetime) for matched_event, event_summary_json in zip(matched_events_sorted, json_resp['objects']): event_summary = resource.get_via_uri( event_summary_json['resource_uri']) self.assertIn(event_summary.event, matched_events, 'Should not be part of search results') self.assertEqual(matched_event, event_summary.event, 'Unexpected order')
def test_detail_get_past(self): occ = get(Occurrence, place=F(point=F(geometry=geos.Point(y=0, x=0)))) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNone(resp_dict, "Returned unexpected existing occurrence") self.assertResponseCode(resp, 410)
def test_detail_get_past(self): occ = get(Occurrence, place=F(point=F(geometry=geos.Point(y=0, x=0)))) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNone(resp_dict, 'Returned unexpected existing occurrence') self.assertResponseCode(resp, 410)
def test_list_get(self): user = get(User) user_profile = user.get_profile() avatar = get(Avatar, user=user, primary=True) api_auth_params = dict(api_key=user_profile.user.api_key.key, **self.auth_params) resp = self.client.get(self.uri, data=api_auth_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) resp_dict = try_json_loads(resp.content) user_dict = resp_dict['objects'][0] user_uri = self.resource().get_resource_uri(user_profile) self.assertEquals(user_uri, user_dict['resource_uri'], 'Unexpected URI for user') self.assertEquals(user_profile.user.first_name, user_dict['first_name'], 'Unexpected first name for user') self.assertEquals(user_profile.user.last_name, user_dict['last_name'], 'Unexpected last name for user') self.assertEquals(user_profile.user.email, user_dict['email'], 'Unexpected email address for user') self.assertEquals(avatar.avatar.url, user_dict['avatar'], 'Unexpected avatar url for user')
def test_detail_get_future(self): occ = get(Occurrence, start_date=datetime.datetime.now().date() + datetime.timedelta(days=30), place=F(point=F(geometry=geos.Point(y=0, x=0)))) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') self.assertResponseCode(resp, 200) self.assertEqual(uri, resp_dict['resource_uri'], 'Unexpected resource uri in response') event_field = resp_dict.get('event') self.assertIsInstance( event_field, basestring, 'Did not find expected reference to associated event URI') place_field = resp_dict.get('place') self.assertIsInstance(place_field, dict, 'Full occurrence does not have place dict') point_field = place_field.get('point') self.assertIsInstance(point_field, dict, 'Full occurrence dict does not have point dict') city_field = point_field.get('city') self.assertIsInstance(city_field, dict, 'Full occurrence dict does not have city dict')
def test_date_next_seven_days_category_filters(self): now = datetime.datetime.now() matched_event = CategoryFilterMixin.mutate_fixture_matched( BaseEventSummaryTest.make_event_fixture()) unmatched_event = CategoryFilterMixin.mutate_fixture_unmatched( BaseEventSummaryTest.make_event_fixture()) DateFilterMixin.mutate_fixture_matched( matched_event, (now + relativedelta(days=4)).date()) DateFilterMixin.mutate_fixture_unmatched( unmatched_event, (now + datetime.timedelta(days=10)).date()) query_params = dict(self.category_filter_options.music, **self.auth_params) query_params.update(self.date_filter_options.next_seven_days) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp['objects'][0]['resource_uri'] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, 'Unexpected event in results for date (next 7 days) and category filter combination' )
def test_time_category_filters(self): matched_event = CategoryFilterMixin.mutate_fixture_matched( BaseEventSummaryTest.make_event_fixture()) unmatched_event = CategoryFilterMixin.mutate_fixture_unmatched( BaseEventSummaryTest.make_event_fixture()) TimeFilterMixin.mutate_fixture_matched(matched_event, datetime.time(19, 0)) TimeFilterMixin.mutate_fixture_unmatched(unmatched_event, datetime.time(22, 0)) query_params = dict(self.category_filter_options.music, **self.auth_params) query_params.update(self.time_filter_options.evening) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp['objects'][0]['resource_uri'] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, 'Unexpected event in results for time and category filter combination' )
def test_fts_title_description_ranking(self): keyword = "guitar" title_with_kw = "Title including %s" % keyword description_with_kw = "Description including %s" % keyword events = [] # create & save events in reverse order to expected search order for title, description in reversed( ((title_with_kw, description_with_kw), (title_with_kw, ""), ("", description_with_kw)) ): event = BaseEventSummaryTest.make_event_fixture() event.title = title event.description = description event.save() events.insert(0, event) BaseEventSummaryTest.make_event_fixture() query_params = dict(q=keyword, **self.auth_params) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(events)) json_resp = try_json_loads(resp.content) for ix, search_result in enumerate(json_resp["objects"]): search_result_uri = search_result["resource_uri"] expected_uri = self.resource().get_resource_uri(events[ix]) self.assertEqual( expected_uri, search_result_uri, """Unexpected search ranking for summary with title %s and description %s""" % (events[ix].title, events[ix].description), )
def assertResponseMetaList(self, resp, expected_count): resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, "Malformed response") self.assertIsNotNone(resp_dict.get("objects"), "Malformed objects field from response") self.assertIsNotNone(resp_dict.get("meta"), "Malformed meta field from response") meta_field = resp_dict["meta"] self.assertEqual( expected_count, meta_field["total_count"], "Incorrect count of objects in meta field of response" ) self.assertEqual(expected_count, len(resp_dict.get("objects")), "Incorrect count of objects in response")
def test_registration_response_user_does_not_exist(self): email = "*****@*****.**" encoded_auth_params = "?" + urllib.urlencode(self.auth_params) json_params = json.dumps({"email": email}) # test that registration with email address not yet associated with # account doesn't yield 'this email address is already in use' error resp = self.client.post(self.uri + encoded_auth_params, json_params, content_type="application/json") self.assertResponseCode(resp, 400) json_resp = try_json_loads(resp.content) resp_email_field = json_resp.get("email") self.assertIsNone(resp_email_field, "Account unexpectedly exists")
def test_detail_get(self): event = BaseEventSummaryTest.make_event_fixture() uri = self.resource().get_resource_uri(event) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, "Malformed response") self.assertResponseCode(resp, 200) self.assertEqual(uri, resp_dict["resource_uri"], "Unexpected resource uri in response") occurrences_field = resp_dict.get("occurrences") self.assertIsInstance(occurrences_field[0], dict, "Did not find expected occurrence child dictionary")
def assertResponseMetaList(self, resp, expected_count): resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') self.assertIsNotNone(resp_dict.get('objects'), 'Malformed objects field from response') self.assertIsNotNone(resp_dict.get('meta'), 'Malformed meta field from response') meta_field = resp_dict['meta'] self.assertEqual( expected_count, meta_field['total_count'], 'Incorrect count of objects in meta field of response') self.assertEqual(expected_count, len(resp_dict.get('objects')), 'Incorrect count of objects in response')
def test_registration_response_user_does_not_exist(self): email = '*****@*****.**' encoded_auth_params = '?' + urllib.urlencode(self.auth_params) json_params = json.dumps({'email': email}) # test that registration with email address not yet associated with # account doesn't yield 'this email address is already in use' error resp = self.client.post(self.uri + encoded_auth_params, json_params, content_type='application/json') self.assertResponseCode(resp, 400) json_resp = try_json_loads(resp.content) resp_email_field = json_resp.get('email') self.assertIsNone(resp_email_field, 'Account unexpectedly exists')
def test_list_get(self): new_user = User.objects.create_user(self.username, self.email, self.password) auth_header = "Basic %s" % base64.b64encode("%s:%s" % (self.email, self.password)) resp = self.client.get(self.uri, data=self.auth_params, HTTP_AUTHORIZATION=auth_header) self.assertResponseCode(resp, 200) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, "Malformed response") api_key_obj = resp_dict["objects"][0] self.assertEqual( new_user.api_key.key, api_key_obj["key"], "Unexpected key %s returned for user %s in response, expecting %s" % (api_key_obj["key"], new_user.username, new_user.api_key.key), )
def test_category_filters_music(self): matched_event = self.mutate_fixture_matched(BaseEventSummaryTest.make_event_fixture()) self.mutate_fixture_unmatched(BaseEventSummaryTest.make_event_fixture()) query_params = dict(**self.auth_params) query_params.update(**self.filter_options.music) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp["objects"][0]["resource_uri"] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual(expected_first_uri, first_search_result_uri, "Unexpected event in results for category filter")
def test_list_get(self): events = [] for ix in range(2): event = BaseEventSummaryTest.make_event_fixture() events.append(event) resp = self.client.get(self.uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, "Malformed response") self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(events)) event_titles = set(e.title for e in events) produced_event_titles = set(e["title"] for e in resp_dict["objects"]) self.assertEqual(event_titles, produced_event_titles, "Unexpected initial recommended events set")
def test_detail_get(self): event = BaseEventSummaryTest.make_event_fixture() uri = self.resource().get_resource_uri(event) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') self.assertResponseCode(resp, 200) self.assertEqual(uri, resp_dict['resource_uri'], 'Unexpected resource uri in response') occurrences_field = resp_dict.get('occurrences') self.assertIsInstance( occurrences_field[0], dict, 'Did not find expected occurrence child dictionary')
def test_list_get(self): new_user = User.objects.create_user(self.username, self.email, self.password) auth_header = 'Basic %s' % base64.b64encode( '%s:%s' % (self.email, self.password)) resp = self.client.get(self.uri, data=self.auth_params, HTTP_AUTHORIZATION=auth_header) self.assertResponseCode(resp, 200) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') api_key_obj = resp_dict['objects'][0] self.assertEqual( new_user.api_key.key, api_key_obj['key'], 'Unexpected key %s returned for user %s in response, expecting %s' % (api_key_obj['key'], new_user.username, new_user.api_key.key))
def test_list_get(self): events = [] for ix in range(2): event = BaseEventSummaryTest.make_event_fixture() events.append(event) resp = self.client.get(self.uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(events)) event_titles = set(e.title for e in events) produced_event_titles = set(e['title'] for e in resp_dict['objects']) self.assertEqual(event_titles, produced_event_titles, 'Unexpected initial recommended events set')
def test_price_filters_under_twenty(self): matched_event = self.mutate_fixture_matched( BaseEventSummaryTest.make_event_fixture()) self.mutate_fixture_unmatched( BaseEventSummaryTest.make_event_fixture()) query_params = dict(**self.auth_params) query_params.update(**self.filter_options.under_twenty) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp['objects'][0]['resource_uri'] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual(expected_first_uri, first_search_result_uri, 'Unexpected event in results for under price filter')
def test_list_get(self): user = get(User) user_profile = user.get_profile() avatar = get(Avatar, user=user, primary=True) api_auth_params = dict(api_key=user_profile.user.api_key.key, **self.auth_params) resp = self.client.get(self.uri, data=api_auth_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) resp_dict = try_json_loads(resp.content) user_dict = resp_dict["objects"][0] user_uri = self.resource().get_resource_uri(user_profile) self.assertEquals(user_uri, user_dict["resource_uri"], "Unexpected URI for user") self.assertEquals(user_profile.user.first_name, user_dict["first_name"], "Unexpected first name for user") self.assertEquals(user_profile.user.last_name, user_dict["last_name"], "Unexpected last name for user") self.assertEquals(user_profile.user.email, user_dict["email"], "Unexpected email address for user") self.assertEquals(avatar.avatar.url, user_dict["avatar"], "Unexpected avatar url for user")
def test_fts_description_simple(self): keyword = "guitar" description_with_kw = "Description including %s" % keyword matched_event = BaseEventSummaryTest.make_event_fixture() matched_event.description = description_with_kw matched_event.save() BaseEventSummaryTest.make_event_fixture() query_params = dict(q=keyword, **self.auth_params) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp["objects"][0]["resource_uri"] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual(expected_first_uri, first_search_result_uri, "Unexpected search result")
def test_detail_get_future(self): occ = get( Occurrence, start_date=datetime.datetime.now().date() + datetime.timedelta(days=30), place=F(point=F(geometry=geos.Point(y=0, x=0))), ) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, "Malformed response") self.assertResponseCode(resp, 200) self.assertEqual(uri, resp_dict["resource_uri"], "Unexpected resource uri in response") event_field = resp_dict.get("event") self.assertIsInstance( event_field, basestring, "Did not find expected reference to associated event URI in event field" ) place_field = resp_dict.get("place") self.assertIsInstance( place_field, basestring, "Did not find expected reference to associated place URI in place field" )
def test_fts_description_simple(self): keyword = 'guitar' description_with_kw = 'Description including %s' % keyword matched_event = BaseEventSummaryTest.make_event_fixture() matched_event.description = description_with_kw matched_event.save() BaseEventSummaryTest.make_event_fixture() query_params = dict(q=keyword, **self.auth_params) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp['objects'][0]['resource_uri'] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual(expected_first_uri, first_search_result_uri, 'Unexpected search result')
def test_time_category_filters(self): matched_event = CategoryFilterMixin.mutate_fixture_matched(BaseEventSummaryTest.make_event_fixture()) unmatched_event = CategoryFilterMixin.mutate_fixture_unmatched(BaseEventSummaryTest.make_event_fixture()) TimeFilterMixin.mutate_fixture_matched(matched_event, datetime.time(19, 0)) TimeFilterMixin.mutate_fixture_unmatched(unmatched_event, datetime.time(22, 0)) query_params = dict(self.category_filter_options.music, **self.auth_params) query_params.update(self.time_filter_options.evening) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp["objects"][0]["resource_uri"] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, "Unexpected event in results for time and category filter combination", )
def test_date_next_seven_days_category_filters(self): now = datetime.datetime.now() matched_event = CategoryFilterMixin.mutate_fixture_matched(BaseEventSummaryTest.make_event_fixture()) unmatched_event = CategoryFilterMixin.mutate_fixture_unmatched(BaseEventSummaryTest.make_event_fixture()) DateFilterMixin.mutate_fixture_matched(matched_event, (now + relativedelta(days=4)).date()) DateFilterMixin.mutate_fixture_unmatched(unmatched_event, (now + datetime.timedelta(days=10)).date()) query_params = dict(self.category_filter_options.music, **self.auth_params) query_params.update(self.date_filter_options.next_seven_days) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp["objects"][0]["resource_uri"] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, "Unexpected event in results for date (next 7 days) and category filter combination", )
def test_place_filters(self): datetimes = (datetime.datetime(*d) for d in ((2100, 1, 2, 0, 0), (2100, 1, 1, 12, 00), (2100, 1, 1, 23, 00))) place = get(Place, point=F(geometry=geos.Point(x=0, y=0))) matched_events = { self.make_event_fixture(start_date=dt.date(), start_time=dt.time(), place=place) for dt in datetimes } self.make_event_fixture() # throw in a non-match query_params = dict(**self.auth_params) query_params.update(**{"place": PlaceFilterOptions()._uri_from_obj(place)}) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(matched_events)) json_resp = try_json_loads(resp.content) resource = api_v1._registry["eventsummary"] matched_events_sorted = sorted(matched_events, key=lambda e: e.occurrences.all()[0].start_datetime) for matched_event, event_summary_json in zip(matched_events_sorted, json_resp["objects"]): event_summary = resource.get_via_uri(event_summary_json["resource_uri"]) self.assertIn(event_summary.event, matched_events, "Should not be part of search results") self.assertEqual(matched_event, event_summary.event, "Unexpected order")
def test_date_filters_this_weekend(self): now = datetime.datetime.now() start_date_weekend = now if now.weekday() == 6 else now + relativedelta(weekday=5) matched_event = self.mutate_fixture_matched( BaseEventSummaryTest.make_event_fixture(), start_date_weekend.date() ) self.mutate_fixture_unmatched( BaseEventSummaryTest.make_event_fixture(), (now + datetime.timedelta(days=30)).date() ) query_params = dict(**self.auth_params) query_params.update(**self.filter_options.this_weekend) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp["objects"][0]["resource_uri"] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, "Unexpected event in results for this weekend date filter" )
def test_fts_title_description_ranking(self): keyword = 'guitar' title_with_kw = 'Title including %s' % keyword description_with_kw = 'Description including %s' % keyword events = [] # create & save events in reverse order to expected search order for title, description in reversed(( ( title_with_kw, description_with_kw, ), (title_with_kw, ''), ('', description_with_kw), )): event = (BaseEventSummaryTest.make_event_fixture()) event.title = title event.description = description event.save() events.insert(0, event) BaseEventSummaryTest.make_event_fixture() query_params = dict(q=keyword, **self.auth_params) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(events)) json_resp = try_json_loads(resp.content) for ix, search_result in enumerate(json_resp['objects']): search_result_uri = search_result['resource_uri'] expected_uri = self.resource().get_resource_uri(events[ix]) self.assertEqual( expected_uri, search_result_uri, '''Unexpected search ranking for summary with title %s and description %s''' % (events[ix].title, events[ix].description))
def test_date_filters_this_weekend(self): now = datetime.datetime.now() start_date_weekend = now if now.weekday( ) == 6 else now + relativedelta(weekday=5) matched_event = self.mutate_fixture_matched( BaseEventSummaryTest.make_event_fixture(), start_date_weekend.date()) self.mutate_fixture_unmatched( BaseEventSummaryTest.make_event_fixture(), (now + datetime.timedelta(days=30)).date()) query_params = dict(**self.auth_params) query_params.update(**self.filter_options.this_weekend) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, 1) json_resp = try_json_loads(resp.content) first_search_result_uri = json_resp['objects'][0]['resource_uri'] expected_first_uri = self.resource().get_resource_uri(matched_event) self.assertEqual( expected_first_uri, first_search_result_uri, 'Unexpected event in results for this weekend date filter')