From 73ee524b2a51f335669426d0c652ca1848fdcd3c Mon Sep 17 00:00:00 2001 From: Nicola Dalpasso Date: Wed, 22 Nov 2023 11:24:30 +0100 Subject: [PATCH] tests: add tests for free text search of collections --- .../ext_product_types_free_text_search.json | 59 ++++++++++ tests/units/test_core.py | 63 +++++++++++ tests/units/test_http_server.py | 101 +----------------- 3 files changed, 126 insertions(+), 97 deletions(-) create mode 100644 tests/resources/ext_product_types_free_text_search.json diff --git a/tests/resources/ext_product_types_free_text_search.json b/tests/resources/ext_product_types_free_text_search.json new file mode 100644 index 000000000..498bf4423 --- /dev/null +++ b/tests/resources/ext_product_types_free_text_search.json @@ -0,0 +1,59 @@ +{ + "astraea_eod": { + "providers_config": { + "foo": { + "productType": "foo", + "metadata_mapping": { + "cloudCover": "$.null" + } + }, + "bar": { + "productType": "bar", + "metadata_mapping": { + "cloudCover": "$.null" + } + }, + "foobar": { + "productType": "foobar", + "metadata_mapping": { + "cloudCover": "$.null" + } + } + }, + "product_types_config": { + "foo": { + "abstract": "abstractFOO - This is FOO. FooAndBar", + "instrument": "Not Available", + "platform": "Not Available", + "platformSerialIdentifier": "Not Available", + "processingLevel": "Not Available", + "keywords": "suspendisse", + "license": "WTFPL", + "title": "titleFOO - Lorem FOO collection", + "missionStartDate": "2012-12-12T00:00:00.000Z" + }, + "bar": { + "abstract": "abstractBAR - This is BAR", + "instrument": "Not Available", + "platform": "Not Available", + "platformSerialIdentifier": "Not Available", + "processingLevel": "Not Available", + "keywords": "lectus,lectus_bar_key", + "license": "WTFPL", + "title": "titleBAR - Lorem BAR collection (FooAndBar)", + "missionStartDate": "2012-12-12T00:00:00.000Z" + }, + "foobar": { + "abstract": "abstract FOOBAR - This is FOOBAR", + "instrument": "Not Available", + "platform": "Not Available", + "platformSerialIdentifier": "Not Available", + "processingLevel": "Not Available", + "keywords": "tortor", + "license": "WTFPL", + "title": "titleFOOBAR - Lorem FOOBAR collection", + "missionStartDate": "2012-12-12T00:00:00.000Z" + } + } + } +} diff --git a/tests/units/test_core.py b/tests/units/test_core.py index 1fa2ac359..26e7aa136 100644 --- a/tests/units/test_core.py +++ b/tests/units/test_core.py @@ -421,6 +421,69 @@ def test_list_product_types_fetch_providers(self, mock_fetch_product_types_list) self.dag.list_product_types(provider="peps", fetch_providers=True) mock_fetch_product_types_list.assert_called_once_with(self.dag, provider="peps") + def test_list_product_types_with_free_text_filter_ok(self): + """Core api must correctly return the list of supported product types""" + + product_types = self.dag.list_product_types( + fetch_providers=False, filter="ABSTRACTFOO" + ) + self.assertIsInstance(product_types, list) + for product_type in product_types: + self.assertListProductTypesRightStructure(product_type) + # There should be no repeated product type in the output + self.assertEqual(len(product_types), len(set(pt["ID"] for pt in product_types))) + + def test_list_product_types_with_free_text_filter(self): + """Testing the search terms""" + + with open( + os.path.join(TEST_RESOURCES_PATH, "ext_product_types_free_text_search.json") + ) as f: + ext_product_types_conf = json.load(f) + self.dag.update_product_types_list(ext_product_types_conf) + + # match in the abstract + product_types = self.dag.list_product_types( + fetch_providers=False, filter="ABSTRACTFOO" + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["foo"]) + + # passing the provider + product_types = self.dag.list_product_types( + provider="astraea_eod", fetch_providers=False, filter="ABSTRACTFOO" + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["foo"]) + + # match in the abstract + product_types = self.dag.list_product_types( + fetch_providers=False, filter=" FOO THIS IS " + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["foo"]) + + # match in the keywords + product_types = self.dag.list_product_types( + fetch_providers=False, filter="LECTUS_BAR_KEY" + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["bar"]) + + # match in the title + product_types = self.dag.list_product_types( + fetch_providers=False, filter="COLLECTION FOOBAR" + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["foobar"]) + + # multiple terms + product_types = self.dag.list_product_types( + fetch_providers=False, filter="FOOANDBAR,FOOBAR" + ) + product_types_ids = [r["ID"] for r in product_types or []] + self.assertListEqual(product_types_ids, ["bar", "foo", "foobar"]) + def test_update_product_types_list(self): """Core api.update_product_types_list must update eodag product types list""" with open(os.path.join(TEST_RESOURCES_PATH, "ext_product_types.json")) as f: diff --git a/tests/units/test_http_server.py b/tests/units/test_http_server.py index 81e48cab0..9e9ef8193 100644 --- a/tests/units/test_http_server.py +++ b/tests/units/test_http_server.py @@ -1048,104 +1048,11 @@ def test_product_type_queryables_with_provider(self): check_links=False, ) - @mock.patch( - "eodag.rest.utils.eodag_api.guess_product_type", autospec=True, return_value=[] - ) - @mock.patch( - "eodag.rest.utils.eodag_api.list_product_types", - autospec=True, - return_value=[ - { - "ID": "S2_MSI_L1C", - "abstract": "The Level-1C product is composed of 100x100 km2 tiles " - "(ortho-images in UTM/WGS84 projection). [...]", - "instrument": "MSI", - "platform": "SENTINEL2", - "platformSerialIdentifier": ["S2A", "S2B"], - "processingLevel": "L1", - "sensorType": "OPTICAL", - "title": "SENTINEL2 Level - 1C", - }, - { - "ID": "S2_MSI_L2A", - "abstract": "The Level-2A product provides Bottom Of Atmosphere (BOA) " - "reflectance images derived from the associated Level-1C " - "products. [...]", - "instrument": "MSI", - "platform": "SENTINEL2", - "platformSerialIdentifier": ["S2A", "S2B"], - "processingLevel": "L2", - "sensorType": "OPTICAL", - "title": "SENTINEL2 Level-2A", - }, - { - "ID": "L57_REFLECTANCE", - "abstract": "Landsat 5,7,8 L2A data (old format) distributed by Theia " - "(2014 to 2017-03-20) using MUSCATE prototype, Lamber 93 " - "projection.", - "instrument": ["OLI", "TIRS"], - "platform": "LANDSAT", - "platformSerialIdentifier": ["L5", "L7", "L8"], - "processingLevel": "L2A", - "sensorType": "OPTICAL", - "title": "Landsat 5,7,8 Level-2A", - }, - ], - ) - def test_collection_free_text_search(self, list_pt, guess_pt): + @mock.patch("eodag.rest.utils.eodag_api.list_product_types", autospec=True) + def test_collection_free_text_search(self, list_pt): """Test STAC Collection free-text search""" - url = "/collections?q=NOT FOUND" - r = self.app.get(url) - self.assertTrue(guess_pt.called) - self.assertTrue(list_pt.called) - self.assertEqual(200, r.status_code) - self.assertFalse( - [ - it["title"] - for it in json.loads(r.content.decode("utf-8")).get("links", []) - if it["rel"] == "child" - ], - ) - - url = "/collections?q= LEVEL - 1C " + url = "/collections?q=TERM1,TERM2" r = self.app.get(url) - self.assertTrue(guess_pt.called) - self.assertTrue(list_pt.called) + list_pt.assert_called_once_with(provider=None, filter="TERM1,TERM2") self.assertEqual(200, r.status_code) - self.assertListEqual( - ["S2_MSI_L1C"], - [ - it["title"] - for it in json.loads(r.content.decode("utf-8")).get("links", []) - if it["rel"] == "child" - ], - ) - - url = "/collections?q=projection,atmosphere" - r = self.app.get(url) - self.assertTrue(guess_pt.called) - self.assertTrue(list_pt.called) - self.assertEqual(200, r.status_code) - self.assertListEqual( - ["S2_MSI_L1C", "S2_MSI_L2A", "L57_REFLECTANCE"], - [ - it["title"] - for it in json.loads(r.content.decode("utf-8")).get("links", []) - if it["rel"] == "child" - ], - ) - - url = "/collections?q=l7" - r = self.app.get(url) - self.assertTrue(guess_pt.called) - self.assertTrue(list_pt.called) - self.assertEqual(200, r.status_code) - self.assertListEqual( - ["L57_REFLECTANCE"], - [ - it["title"] - for it in json.loads(r.content.decode("utf-8")).get("links", []) - if it["rel"] == "child" - ], - )