def test_field_matching(self): """ test that requests can specify field matches """ code, results = post_discovery_request({"org": "OrgA"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 1) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_1", result_ids) code, results = post_discovery_request({"org": "OrgB"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 1) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_2", result_ids)
def test_pagination(self): """ test that paging attributes are correctly applied """ code, results = post_discovery_request({"search_string": "Find this one"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 3) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 1}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_1", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 1, "page_index": 0}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_1", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 1, "page_index": 1}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_2", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 1, "page_index": 2}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_3", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 2}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 2) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_1", result_ids) self.assertIn(DemoCourse.DEMO_COURSE_ID + "_2", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 2, "page_index": 0}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 2) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_1", result_ids) self.assertIn(DemoCourse.DEMO_COURSE_ID + "_2", result_ids) code, results = post_discovery_request({"search_string": "Find this one", "page_size": 2, "page_index": 1}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) self.assertEqual(len(results["results"]), 1) result_ids = [r["data"]["id"] for r in results["results"]] self.assertIn(DemoCourse.DEMO_COURSE_ID + "_3", result_ids)
def test_search_from_url(self): """ test searching using the url """ code, results = post_discovery_request({}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3) code, results = post_discovery_request({"search_string": "right"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 1) code, results = post_discovery_request({"search_string": "parameter"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 2) code, results = post_discovery_request({"search_string": "Find this one"}) self.assertTrue(code < 300 and code > 199) self.assertEqual(results["total"], 3)
def test_page_size_too_large(self): """ test searching with too-large page_size """ code, results = post_discovery_request({ "search_string": "Find this one", "page_size": 101 }) self.assertEqual(code, 500) self.assertIn("error", results)
def test_malformed_query_handling(self): # root code, results = post_request({"search_string": "\"missing quote"}) self.assertGreater(code, 499) self.assertEqual(results["error"], 'Your query seems malformed. Check for unmatched quotes.') # course ID code, results = post_request({"search_string": "\"missing quote"}, "ABC/DEF/GHI") self.assertGreater(code, 499) self.assertEqual(results["error"], 'Your query seems malformed. Check for unmatched quotes.') # course discovery code, results = post_discovery_request({"search_string": "\"missing quote"}) self.assertGreater(code, 499) self.assertEqual(results["error"], 'Your query seems malformed. Check for unmatched quotes.')
def test_bad_engine(self): """ test in place to see how this module behaves when search engine is not available for some reason """ code, results = post_discovery_request({"search_string": "sun"}) self.assertGreater(code, 499) self.assertEqual(results["error"], 'An error occurred when searching for "sun"')
def test_page_size_too_large(self): """ test searching with too-large page_size """ code, results = post_discovery_request({"search_string": "Find this one", "page_size": 101}) self.assertEqual(code, 500) self.assertIn("error", results)