def test_search_exact_matches(self): """Test that the search method returns accurate search results for exact matches. """ role = TestModelFactory.create_role("Event Organizer") user = TestModelFactory.create_user(password="******", email="*****@*****.**", company="ABC Corp") user.role = role venue = TestModelFactory.create_venue() event = TestModelFactory.create_event("Eric's Party", "live", id=1) event.venue = venue event.user = user db.session.add_all([user, event]) db.session.commit() # need to wait 2 seconds before sending the GET request to the Elasticsearch API # or else the resource won't be there time.sleep(2) match_query1 = MatchQuery("title", "Eric's Party") response, pagination = self.search_middleware.search( Event, match_query1) self.assertEqual(pagination.items.first(), event) self.assertEqual(response.total, 1) # lowercase should work too match_query2 = MatchQuery("title", "Eeric's Party") response, pagination = self.search_middleware.search( Event, match_query2) self.assertEqual(pagination.items.first(), event) self.assertEqual(response.total, 1)
def test_delete_after_commit(self): """Test to confirm that events deleted from the database. are reflected in Elasticsearch after each commit. """ role = TestModelFactory.create_role("Event Organizer") user = TestModelFactory.create_user(password="******", email="*****@*****.**", company="ABC Corp") user.role = role venue = TestModelFactory.create_venue() event = TestModelFactory.create_event("Foobar", "live", id=1) event.user = user event.venue = venue db.session.add_all([user, event]) db.session.commit() time.sleep(2) match_query1 = MatchQuery("title", "Foobar") response, pagination = self.search_middleware.search( Event, match_query1) self.assertEqual(pagination.items.first(), event) self.assertEqual(response.total, 1) db.session.delete(event) db.session.commit() time.sleep(2) match_query2 = MatchQuery("title", "Foobar") response, pagination = self.search_middleware.search( Event, match_query2) self.assertIsNone(pagination.items.first()) self.assertEqual(response.total, 0)
def test_event_search_invalid_parameters(self): """Test that invalid parameters to the search method throw the approriate exceptions. """ role = TestModelFactory.create_role("Event Organizer") user = TestModelFactory.create_user(password="******", email="*****@*****.**", company="ABC Corp") user.role = role venue = TestModelFactory.create_venue() event = TestModelFactory.create_event("Eric's Foobar", "live", id=1) event.user = user event.venue = venue db.session.add_all([user, event]) db.session.commit() time.sleep(2) # searching using a list with self.assertRaises(RequestError): match_query = MatchQuery("title", ["Eric's", "Foobar"]) response, pagination = self.search_middleware.search( Event, match_query) # searching using a model with self.assertRaises(SerializationError): match_query = MatchQuery("title", event) response, pagination = self.search_middleware.search( Event, match_query)
def test_search_partial_matches(self): """Test that the search method returns the appropriate reulsts for partial matches. """ role = TestModelFactory.create_role("Event Organizer") user = TestModelFactory.create_user(password="******", email="*****@*****.**", company="ABC Corp") user.role = role venue_one = TestModelFactory.create_venue(address="123 Main St.") venue_two = TestModelFactory.create_venue(address="456 Main St.") event_one = TestModelFactory.create_event("Eric's Foobar", "live", id=1) event_two = TestModelFactory.create_event("Eric's Party", "live", event_type="Convention", event_category="Sports", id=2) event_one.user = user event_two.user = user event_one.venue = venue_one event_two.venue = venue_two db.session.add_all([user, event_one, event_two]) db.session.commit() time.sleep(2) match_query1 = MatchQuery("title", "Foo") response, pagination = self.search_middleware.search( Event, match_query1) self.assertIsNone(pagination.items.first()) self.assertEqual(response.total, 0) match_query2 = MatchQuery("title", "Part") response, pagination = self.search_middleware.search( Event, match_query2) self.assertIsNone(pagination.items.first()) self.assertEqual(response.total, 0) match_query3 = MatchQuery("title", "Eric's Fooba") response, pagination = self.search_middleware.search( Event, match_query3) self.assertEqual(pagination.items.all(), [event_one, event_two]) self.assertEqual(response.total, 2) match_query4 = MatchQuery("title", "Party") response, pagination = self.search_middleware.search( Event, match_query4) self.assertEqual(pagination.items.first(), event_two) self.assertNotEqual(pagination.items.first(), event_one) self.assertEqual(response.total, 1)
def test_no_search_results(self): """Test that the search method returns no search results when the event title doesn't exist in Elasticsearch. """ role = TestModelFactory.create_role("Event Organizer") user = TestModelFactory.create_user(password="******", email="*****@*****.**", company="ABC Corp") user.role = role venue = TestModelFactory.create_venue() event = TestModelFactory.create_event("Eric's Party", "live", id=1) event.user = user event.venue = venue db.session.add_all([user, event]) db.session.commit() time.sleep(2) # search should return nothing match_query1 = MatchQuery("title", "Philly") response, pagination = self.search_middleware.search( Event, match_query1) self.assertIsNone(pagination.items.first()) self.assertEqual(response.total, 0)
def search_events_by_title(): """Return search results for searching for events by title. """ search_endpoint = "main.search_events_by_title" page = request.args.get("page", 1, type=int) match_query = MatchQuery("title", g.search_form.query.data, from_=page - 1) fragment = f"query={g.search_form.query.data}&" search_response, pagination = current_app.sqlalchemy_search_middleware.search( Event, match_query) events = [(event.main_image(), event) for event in pagination.items] return render_template("main/search.html", events=events, endpoint=search_endpoint, pagination=pagination, fragment=fragment)