def test_query_on_static_file(self): url = URL(self.utils.service_url) url.path = "/tools/index.html?123" response = self.utils.try_till_response(str(url), data=b"") self.assertEqual(response.status_code, 200) self.assertEqual(response.all_content, OVERVIEW)
def test_leaves_w_array(self): url = URL(self.resources + "/test_ref_w_deep_parameters.json") url.query = { "value": { "one": { "two": [{ "test": 1 }, { "test": 2 }, "3"] } } } result = mo_json_config.get(url) expected = { "a": { "two": [{ "test": 1 }, { "test": 2 }, "3"] }, "b": [{ "test": 1 }, { "test": 2 }, "3"] } self.assertEqual(result, expected, "expecting proper parameter expansion")
def test_bad_file_request(self): url = URL(self.utils.service_url) url.path = "/tools/../../README.md" response = self.utils.try_till_response(str(url), data=b"") self.assertEqual(response.status_code, 404) self.assertEqual(response.all_content, "")
def test_rest_get(self): data = [ {"a": 0, "b": 0}, {"a": 0, "b": 1}, {"a": 1, "b": 0}, {"a": 1, "b": 1} ] test = wrap({ "data": data, "query": {"from": TEST_TABLE}, "expecting_list": {"data": data} }) self.utils.execute_tests(test) # LOAD, AND SET meta.testing=True url = URL(self.utils.testing.query) url.path = "json/" + test.query['from'] url.query = {"a": 1} response = self.utils.try_till_response(str(url), data=b"") self.assertEqual(response.status_code, 200) # ORDER DOES NOT MATTER, TEST EITHER expected1 = value2json([{"a": 1, "b": 0}, {"a": 1, "b": 1}], pretty=True).encode('utf8') expected2 = value2json([{"a": 1, "b": 1}, {"a": 1, "b": 0}], pretty=True).encode('utf8') try: self.assertEqual(response.all_content, expected1) except Exception: self.assertEqual(response.all_content, expected2)
def test_root_request(self): if self.utils.not_real_service(): return url = URL(self.utils.service_url) url.path = "" url = str(url) response = self.utils.try_till_response(url, data=b"") self.assertEqual(response.status_code, 200) self.assertEqual(response.all_content, OVERVIEW)
def test_bad_file_request(self): url = URL(self.utils.testing.query) url.path = "/tools/../../README.md" response = self.utils.try_till_response(str(url), data=b"") if response.status_code == 200: Log.note("Response is:\n{{response|indent}}", response=response.content) self.assertEqual(response.status_code, 404) self.assertEqual(response.content, b"")
def test_rest_get(self): settings = self.utils.fill_container({ "data": [{ "a": 0, "b": 0 }, { "a": 0, "b": 1 }, { "a": 1, "b": 0 }, { "a": 1, "b": 1 }], "query": { "from": "" } # DUMMY LINE }) url = URL(self.utils.service_url) url.path = "json/" + settings.index url.query = {"a": 1} response = self.utils.try_till_response(str(url), data=b"") self.assertEqual(response.status_code, 200) # ORDER DOES NOT MATTER, TEST EITHER expected1 = convert.unicode2utf8( convert.value2json([{ "a": 1, "b": 0 }, { "a": 1, "b": 1 }], pretty=True)) expected2 = convert.unicode2utf8( convert.value2json([{ "a": 1, "b": 1 }, { "a": 1, "b": 0 }], pretty=True)) try: self.assertEqual(response.all_content, expected1) except Exception: self.assertEqual(response.all_content, expected2)
def test_url_parameter_list(self): url = self.resources + "/test_ref_w_parameters.json?test1=a&test1=b&test2=c&test1=d" self.assertEqual( URL(url).query, { "test1": ["a", "b", "d"], "test2": "c" }, "expecting test1 to be an array")
def test_save_then_load(self): test = { "data": [{ "a": "b" }], "query": { "meta": { "save": True }, "from": TEST_TABLE, "select": "a" }, "expecting_list": { "meta": { "format": "list" }, "data": ["b"] } } settings = self.utils.fill_container(test) bytes = unicode2utf8( value2json({ "from": settings.index, "select": "a", "format": "list" })) expected_hash = convert.bytes2base64( hashlib.sha1(bytes).digest()[0:6]).replace("/", "_") wrap(test).expecting_list.meta.saved_as = expected_hash self.utils.send_queries(test) # ENSURE THE QUERY HAS BEEN INDEXED Log.note("Flush saved query (with hash {{hash}})", hash=expected_hash) container = elasticsearch.Index(index="saved_queries", type=save_query.DATA_TYPE, kwargs=settings) container.flush(forced=True) with Timer("wait for 5 seconds"): Till(seconds=5).wait() url = URL(self.utils.testing.query) response = self.utils.try_till_response(url.scheme + "://" + url.host + ":" + text_type(url.port) + "/find/" + expected_hash, data=b'') self.assertEqual(response.status_code, 200) self.assertEqual(response.all_content, bytes)
def test_recovery_of_empty_string(self): test = wrap({ "data": [ {"a": "bee"} ], "query": { "from": TEST_TABLE, "select": "a", "where": {"prefix": {"a": ""}}, "format": "list" }, "expecting_list": { "meta": { "format": "list" }, "data": ["bee"] } }) settings = self.utils.fill_container(test) bytes = value2json(test.query).encode('utf8') expected_hash = convert.bytes2base64(hashlib.sha1(bytes).digest()[0:6]).replace("/", "_") test.expecting_list.meta.saved_as = expected_hash test.query.meta = {"save": True} self.utils.send_queries(test) # ENSURE THE QUERY HAS BEEN INDEXED Log.note("Flush saved query") container = elasticsearch.Index(index="saved_queries", kwargs=settings) container.flush(forced=True) with Timer("wait for 5 seconds"): Till(seconds=5).wait() url = URL(self.utils.testing.query) response = self.utils.try_till_response(url.scheme + "://" + url.host + ":" + text(url.port) + "/find/" + expected_hash, data=b'') self.assertEqual(response.status_code, 200) self.assertEqual(response.all_content, bytes)
def test_json_parameter(self): url = URL(self.resources + "/test_ref_w_parameters.json") url.query = {"metadata": ["a", "b"]} result = mo_json_config.get(url) self.assertEqual(result, {"a": ["a", "b"]}, "expecting proper parameter expansion")
def test_empty_object_as_json_parameter(self): url = URL(self.resources + "/test_ref_w_parameters.json") url.query = {"metadata": Data()} result = mo_json_config.get(url) self.assertEqual(result, {}, "expecting proper parameter expansion")
def test_favicon(self): url = URL(self.utils.testing.query) url.path = "/favicon.ico" response = self.utils.try_till_response(str(url), data=b"") self.assertEqual(response.status_code, 200)