diff options
author | QuentinAndre <quentin.andre@imt-atlantique.net> | 2021-08-25 15:32:23 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-10-25 12:06:03 +0200 |
commit | b53b5377417866e0bdaa8b861e4d20fc5f6b3109 (patch) | |
tree | 42e9e2a57d06bbd26811e54ea9bb0e7b28b2d3b5 /ishtar_common/tests.py | |
parent | b65cb1e0617989a74c0552a73242d311ad442e28 (diff) | |
download | Ishtar-b53b5377417866e0bdaa8b861e4d20fc5f6b3109.tar.bz2 Ishtar-b53b5377417866e0bdaa8b861e4d20fc5f6b3109.zip |
clean version of get_geo_items with ui integration and tests
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r-- | ishtar_common/tests.py | 360 |
1 files changed, 214 insertions, 146 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 1c757dd2a..2077a3781 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -84,6 +84,9 @@ from ishtar_common.utils import ( from ishtar_common.tasks import launch_export from ishtar_common import utils_secretary +from django.contrib.staticfiles.testing import StaticLiveServerTestCase +from selenium.webdriver.firefox.webdriver import WebDriver + COMMON_FIXTURES = [ settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", @@ -797,7 +800,7 @@ class SerializationTest(GenericSerializationTest, TestCase): AlterationCauseType, TreatmentEmergencyType, CommunicabilityType, - FunctionalArea + FunctionalArea, ) from archaeological_operations.models import CulturalAttributionType @@ -2735,18 +2738,24 @@ class GeomaticTest(TestCase): from ishtar_common.models_common import SpatialReferenceSystem wgs84 = SpatialReferenceSystem.objects.get(srid=4326) - poly_ope = ("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," - "((6 3,9 2,9 4,6 3)))") + poly_ope = ( + "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," + "((6 3,9 2,9 4,6 3)))" + ) point_ope = GEOSGeometry("POINT({} {} {})".format(3.8, 3, 1), srid=4326) point_2d_ope = GEOSGeometry("POINT({} {})".format(3.8, 3), srid=4326) xy_ope = 3.8, 3 - poly_cr = ("MULTIPOLYGON(((2 1,5 1,5 5,1 5,2 1),(2 2,2 3,3 3,3 2,2 2))," - "((6 3,9 2,9 4,6 3)))") + poly_cr = ( + "MULTIPOLYGON(((2 1,5 1,5 5,1 5,2 1),(2 2,2 3,3 3,3 2,2 2))," + "((6 3,9 2,9 4,6 3)))" + ) point_cr = GEOSGeometry("POINT({} {} {})".format(3.9, 3, 1), srid=4326) point_2d_cr = GEOSGeometry("POINT({} {})".format(3.9, 3), srid=4326) xy_cr = 3.9, 3 - poly_bf = ("MULTIPOLYGON(((2 1,4 1,4 4,1 4,2 1),(2 2,2 3,3 3,3 2,2 2))," - "((6 3,9 2,9 4,6 3)))") + poly_bf = ( + "MULTIPOLYGON(((2 1,4 1,4 4,1 4,2 1),(2 2,2 3,3 3,3 2,2 2))," + "((6 3,9 2,9 4,6 3)))" + ) point_bf = GEOSGeometry("POINT({} {} {})".format(3.9, 2.9, 1), srid=4326) point_2d_bf = GEOSGeometry("POINT({} {})".format(3.9, 2.9), srid=4326) xy_bf = 3.9, 2.9 @@ -2808,7 +2817,7 @@ class GeomaticTest(TestCase): bf.save() bf = BaseFind.objects.get(pk=bf.pk) bfs.append(bf) - pks[default['label']] = bf.pk + pks[default["label"]] = bf.pk find = Find.objects.create() find.base_finds.add(bf) @@ -2821,30 +2830,61 @@ class GeomaticTest(TestCase): ope1 = create_operation(user) ope2 = create_operation(user) - pks = {'Pt': ope1.pk, 'Poly': ope2.pk} + pks = {"Pt": ope1.pk, "Poly": ope2.pk} - pks.update(GeomaticTest.create_cr_with_bfs(ope1, "Point", "Point", ["Polygon", "Point"], "CR Pt Pt")) - pks.update(GeomaticTest.create_cr_with_bfs(ope1, "Point", "Polygon", ["Polygon", "Point"], "CR Pt Poly")) - pks.update(GeomaticTest.create_cr_with_bfs(ope2, "Polygon", "Point", ["Polygon", "Point"], "CR Poly Pt")) - pks.update(GeomaticTest.create_cr_with_bfs(ope2, "Polygon", "Polygon", ["Polygon", "Point"], "CR Poly Poly")) + pks.update( + GeomaticTest.create_cr_with_bfs( + ope1, "Point", "Point", ["Polygon", "Point"], "CR Pt Pt" + ) + ) + pks.update( + GeomaticTest.create_cr_with_bfs( + ope1, "Point", "Polygon", ["Polygon", "Point"], "CR Pt Poly" + ) + ) + pks.update( + GeomaticTest.create_cr_with_bfs( + ope2, "Polygon", "Point", ["Polygon", "Point"], "CR Poly Pt" + ) + ) + pks.update( + GeomaticTest.create_cr_with_bfs( + ope2, "Polygon", "Polygon", ["Polygon", "Point"], "CR Poly Poly" + ) + ) geom_ope = [ - [[[[1, 1], [5, 1], [5, 5], [1, 5], [1, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], - [[[6, 3], [9, 2], [9, 4], [6, 3]]]], + [ + [ + [[1, 1], [5, 1], [5, 5], [1, 5], [1, 1]], + [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]], + ], + [[[6, 3], [9, 2], [9, 4], [6, 3]]], + ], [3.86111, 3.02778], - [3.8, 3] + [3.8, 3], ] geom_cr = [ - [[[[2, 1], [5, 1], [5, 5], [1, 5], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], - [[[6, 3], [9, 2], [9, 4], [6, 3]]]], + [ + [ + [[2, 1], [5, 1], [5, 5], [1, 5], [2, 1]], + [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]], + ], + [[[6, 3], [9, 2], [9, 4], [6, 3]]], + ], [4.17708, 3.11458], - [3.9, 3] + [3.9, 3], ] geom_bf = [ - [[[[2, 1], [4, 1], [4, 4], [1, 4], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], - [[[6, 3], [9, 2], [9, 4], [6, 3]]]], + [ + [ + [[2, 1], [4, 1], [4, 4], [1, 4], [2, 1]], + [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]], + ], + [[[6, 3], [9, 2], [9, 4], [6, 3]]], + ], [4.42105, 2.73684], - [3.9, 2.9] + [3.9, 2.9], ] return [geom_ope, geom_cr, geom_bf], pks @@ -2883,38 +2923,63 @@ class GeomaticTest(TestCase): def test_setUpDefaultGeoItems(self): from django.contrib.gis.geos import GEOSGeometry from archaeological_operations.models import Operation + username, password, user = create_superuser() base_geoms, pks = self.setUpDefaultGeoItems(user) geoms = [] for geom in base_geoms: - poly = GEOSGeometry(json.dumps({'type': 'MultiPolygon', 'coordinates': geom[0]})) - centr = GEOSGeometry(json.dumps({'type': 'Point', 'coordinates': geom[1]})) - pt = GEOSGeometry(json.dumps({'type': 'Point', 'coordinates': geom[2]})) + poly = GEOSGeometry( + json.dumps({"type": "MultiPolygon", "coordinates": geom[0]}) + ) + centr = GEOSGeometry(json.dumps({"type": "Point", "coordinates": geom[1]})) + pt = GEOSGeometry(json.dumps({"type": "Point", "coordinates": geom[2]})) geoms.append([poly, centr, pt]) - ope_pt = Operation.objects.get(pk=pks['Pt']) + ope_pt = Operation.objects.get(pk=pks["Pt"]) self.assertPt(ope_pt, geoms[0]) - ope_poly = Operation.objects.get(pk=pks['Poly']) - print(ope_poly) - print(ope_poly.point_2d) + ope_poly = Operation.objects.get(pk=pks["Poly"]) self.assertPoly(ope_poly, geoms[0]) - cr_poly_poly = ope_poly.context_record.get(pk=pks['CR Poly Poly']) + cr_poly_poly = ope_poly.context_record.get(pk=pks["CR Poly Poly"]) self.assertPoly(cr_poly_poly, geoms[1]) - cr_poly_pt = ope_poly.context_record.get(pk=pks['CR Poly Pt']) + cr_poly_pt = ope_poly.context_record.get(pk=pks["CR Poly Pt"]) self.assertPt(cr_poly_pt, geoms[1]) - cr_pt_poly = ope_pt.context_record.get(pk=pks['CR Pt Poly']) + cr_pt_poly = ope_pt.context_record.get(pk=pks["CR Pt Poly"]) self.assertPoly(cr_pt_poly, geoms[1], geoms[0][2]) - cr_pt_pt = ope_pt.context_record.get(pk=pks['CR Pt Pt']) + cr_pt_pt = ope_pt.context_record.get(pk=pks["CR Pt Pt"]) self.assertPt(cr_pt_pt, geoms[1]) - self.assertPt(cr_pt_pt.base_finds.get(pk=pks['Find Point from CR Pt Pt']), geoms[2]) - self.assertPoly(cr_pt_pt.base_finds.get(pk=pks['Find Polygon from CR Pt Pt']), geoms[2], geoms[1][2]) - self.assertPt(cr_poly_pt.base_finds.get(pk=pks['Find Point from CR Poly Pt']), geoms[2]) - self.assertPoly(cr_poly_pt.base_finds.get(pk=pks['Find Polygon from CR Poly Pt']), geoms[2], geoms[1][2]) - self.assertPt(cr_pt_poly.base_finds.get(pk=pks['Find Point from CR Pt Poly']), geoms[2]) - self.assertPoly(cr_pt_poly.base_finds.get(pk=pks['Find Polygon from CR Pt Poly']), geoms[2], geoms[0][2]) - self.assertPt(cr_poly_poly.base_finds.get(pk=pks['Find Point from CR Poly Poly']), geoms[2]) - self.assertPoly(cr_poly_poly.base_finds.get(pk=pks['Find Polygon from CR Poly Poly']), geoms[2]) + self.assertPt( + cr_pt_pt.base_finds.get(pk=pks["Find Point from CR Pt Pt"]), geoms[2] + ) + self.assertPoly( + cr_pt_pt.base_finds.get(pk=pks["Find Polygon from CR Pt Pt"]), + geoms[2], + geoms[1][2], + ) + self.assertPt( + cr_poly_pt.base_finds.get(pk=pks["Find Point from CR Poly Pt"]), geoms[2] + ) + self.assertPoly( + cr_poly_pt.base_finds.get(pk=pks["Find Polygon from CR Poly Pt"]), + geoms[2], + geoms[1][2], + ) + self.assertPt( + cr_pt_poly.base_finds.get(pk=pks["Find Point from CR Pt Poly"]), geoms[2] + ) + self.assertPoly( + cr_pt_poly.base_finds.get(pk=pks["Find Polygon from CR Pt Poly"]), + geoms[2], + geoms[0][2], + ) + self.assertPt( + cr_poly_poly.base_finds.get(pk=pks["Find Point from CR Poly Poly"]), + geoms[2], + ) + self.assertPoly( + cr_poly_poly.base_finds.get(pk=pks["Find Polygon from CR Poly Poly"]), + geoms[2], + ) @staticmethod def geojson_geo_items(geoms, pks, test_get_geo_items=False): @@ -2942,15 +3007,14 @@ class GeomaticTest(TestCase): if label[:4] == "Find": dic_t = { "type": "Feature", - "geometry": { - "type": "MultiPolygon", "coordinates": geoms[2][0] - }, + "geometry": {"type": "MultiPolygon", "coordinates": geoms[2][0]}, "properties": {"label": label}, } dic_f = { "type": "Feature", "geometry": { - "type": "Point", "coordinates": pt_coords, + "type": "Point", + "coordinates": pt_coords, }, "properties": {"label": label}, } @@ -2958,7 +3022,9 @@ class GeomaticTest(TestCase): cache_dics_f[label] = dic_f if test_get_geo_items: res_t = BaseFind.objects.get(pk=pk).get_geo_items(get_polygons=True) - res_f = BaseFind.objects.get(pk=pk).get_geo_items(get_polygons=False) + res_f = BaseFind.objects.get(pk=pk).get_geo_items( + get_polygons=False + ) get_pk = "?pk=" else: get_polys = dic_t @@ -2968,47 +3034,56 @@ class GeomaticTest(TestCase): cache_key = lambda x: "Find " + x + " from " + label dic_t = { "type": "Feature", - "geometry": { - "type": "MultiPolygon", "coordinates": geoms[1][0] - }, + "geometry": {"type": "MultiPolygon", "coordinates": geoms[1][0]}, "properties": { "label": label, "base-finds": { - 'type': 'FeatureCollection', - 'features': [ + "type": "FeatureCollection", + "features": [ cache_dics_t[cache_key("Polygon")], cache_dics_t[cache_key("Point")], - ] - } + ], + }, }, } dic_f = { "type": "Feature", "geometry": { - "type": "Point", "coordinates": pt_coords, + "type": "Point", + "coordinates": pt_coords, }, "properties": { "label": label, "base-finds": { - 'type': 'FeatureCollection', - 'features': [ + "type": "FeatureCollection", + "features": [ cache_dics_f[cache_key("Polygon")], cache_dics_f[cache_key("Point")], - ] - } + ], + }, }, } cache_dics_t[label] = dic_t cache_dics_f[label] = dic_f if test_get_geo_items: - res_t = ContextRecord.objects.get(pk=pk).get_geo_items(get_polygons=True) - res_f = ContextRecord.objects.get(pk=pk).get_geo_items(get_polygons=False) + res_t = ContextRecord.objects.get(pk=pk).get_geo_items( + get_polygons=True + ) + res_f = ContextRecord.objects.get(pk=pk).get_geo_items( + get_polygons=False + ) get_pk = "?context_record_pk=" else: get_polys = copy.deepcopy(dic_t) - get_polys['properties']['base-finds'] = [cache_key("Polygon"), cache_key("Point")] + get_polys["properties"]["base-finds"] = [ + cache_key("Polygon"), + cache_key("Point"), + ] get_pts = copy.deepcopy(dic_f) - get_pts['properties']['base-finds'] = [cache_key("Polygon"), cache_key("Point")] + get_pts["properties"]["base-finds"] = [ + cache_key("Polygon"), + cache_key("Point"), + ] else: real_label = "OA3" cache_key_prefix = "CR " + label + " " @@ -3016,78 +3091,86 @@ class GeomaticTest(TestCase): real_label = "OA2" dic_t = { "type": "Feature", - "geometry": { - "type": "MultiPolygon", "coordinates": geoms[0][0] - }, + "geometry": {"type": "MultiPolygon", "coordinates": geoms[0][0]}, "properties": { "label": real_label, "context-records": { - 'type': 'FeatureCollection', - 'features': [ + "type": "FeatureCollection", + "features": [ cache_dics_t[cache_key_prefix + "Poly"], cache_dics_t[cache_key_prefix + "Pt"], - ] - } + ], + }, }, } dic_f = { "type": "Feature", "geometry": { - "type": "Point", "coordinates": pt_coords, + "type": "Point", + "coordinates": pt_coords, }, "properties": { "label": real_label, "context-records": { - 'type': 'FeatureCollection', - 'features': [ + "type": "FeatureCollection", + "features": [ cache_dics_f[cache_key_prefix + "Poly"], cache_dics_f[cache_key_prefix + "Pt"], - ] - } + ], + }, }, } if test_get_geo_items: - res_t = Operation.objects.get(pk=pk).get_geo_items(get_polygons=True) - res_f = Operation.objects.get(pk=pk).get_geo_items(get_polygons=False) + res_t = Operation.objects.get(pk=pk).get_geo_items( + get_polygons=True + ) + res_f = Operation.objects.get(pk=pk).get_geo_items( + get_polygons=False + ) get_pk = "?operation_pk=" else: get_polys = copy.deepcopy(dic_t) bf_labels = [] - for cr in dic_t['properties']['context-records']['features']: - for bf in cr['properties']['base-finds']['features']: - bf_labels.append(bf['properties']['label']) - get_polys['properties'] = { - 'context-records': [ + for cr in dic_t["properties"]["context-records"]["features"]: + for bf in cr["properties"]["base-finds"]["features"]: + bf_labels.append(bf["properties"]["label"]) + get_polys["properties"] = { + "context-records": [ cache_key_prefix + "Poly", - cache_key_prefix + "Pt" + cache_key_prefix + "Pt", ], - 'base-finds': bf_labels + "base-finds": bf_labels, } get_pts = copy.deepcopy(dic_f) bf_labels = [] - for cr in dic_f['properties']['context-records']['features']: - for bf in cr['properties']['base-finds']['features']: - bf_labels.append(bf['properties']['label']) - get_pts['properties'] = { - 'context-records': [ + for cr in dic_f["properties"]["context-records"]["features"]: + for bf in cr["properties"]["base-finds"]["features"]: + bf_labels.append(bf["properties"]["label"]) + get_pts["properties"] = { + "context-records": [ cache_key_prefix + "Poly", - cache_key_prefix + "Pt" + cache_key_prefix + "Pt", ], - 'base-finds': bf_labels + "base-finds": bf_labels, } if test_get_geo_items: - res[label] = {'get_polys': dic_t, - 'get_pts': dic_f, - 'res_polys': res_t, - 'res_pts': res_f, - 'get_pk': get_pk} + res[label] = { + "get_polys": dic_t, + "get_pts": dic_f, + "res_polys": res_t, + "res_pts": res_f, + "get_pk": get_pk, + } else: - res[label] = {'get_polys': get_polys, - 'get_pts': get_pts, - 'precise': precise} + res[label] = { + "get_polys": get_polys, + "get_pts": get_pts, + "precise": precise, + } return res + class NewItems(TestCase): fixtures = COMMON_FIXTURES @@ -3492,8 +3575,6 @@ class TemplateGenerationTest(TestCase): for key in expected_keys: self.assertIn(key, filtr) -from django.contrib.staticfiles.testing import StaticLiveServerTestCase -from selenium.webdriver.firefox.webdriver import WebDriver @tag("ui") class SeleniumTests(StaticLiveServerTestCase): @@ -3504,8 +3585,9 @@ class SeleniumTests(StaticLiveServerTestCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.waiting_time = 20 cls.selenium = WebDriver() - cls.selenium.implicitly_wait(20) + cls.selenium.implicitly_wait(cls.waiting_time) @classmethod def tearDownClass(cls): @@ -3524,14 +3606,17 @@ class SeleniumTests(StaticLiveServerTestCase): # login self.username, self.password, self.user = create_superuser() - self.selenium.get('%s%s' % (self.live_server_url, '/accounts/login/')) - self.WebDriverWait(self.selenium, 2).until( - lambda driver: driver.find_element_by_name("username")) + self.selenium.get("%s%s" % (self.live_server_url, "/accounts/login/")) + self.WebDriverWait(self.selenium, self.waiting_time).until( + lambda driver: driver.find_element_by_name("username") + ) username_input = self.selenium.find_element_by_name("username") username_input.send_keys(self.username) password_input = self.selenium.find_element_by_name("password") password_input.send_keys(self.password) - self.selenium.find_element_by_xpath('//button[@type="submit"][@class="btn btn-primary"]').click() + self.selenium.find_element_by_xpath( + '//button[@type="submit"][@class="btn btn-primary"]' + ).click() def setUpDefaultGeoItems(self): return GeomaticTest.setUpDefaultGeoItems(self.user) @@ -3546,67 +3631,50 @@ class SeleniumTests(StaticLiveServerTestCase): "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" + "var elementTop = arguments[0].getBoundingClientRect().top;" + "window.scrollBy(0, elementTop-(viewPortHeight/2));", - element) - - ''' - arguments: - i is the number of the row (starting at 1) - target and source are a tuple of french/english slugs corresponding respectively to the slug of the item of - which we want some details and the slug of the item from which we launched the search - ''' - def click_on_details(self, i, target, source=None): - table = '//table[contains(@id, ' + target[0] + ') or contains(@id, ' + target[1] + ')]' - if source: - table += '[contains(@id, ' + source[0] + ') or contains(@id, ' + source[1] + ')]' - details = table + '/tbody/' + '/tr[' + str(i) + ']/td/a[@class="display_details"]' - details = self.selenium.find_element_by_xpath(details) - self.scroll(details) - details.click() + element, + ) def assertNotInDOM(self, xpath): - self.selenium.implicitly_wait(3) + self.selenium.implicitly_wait(self.waiting_time / 5) try: - self.WebDriverWait(self.selenium, 2).until( + self.WebDriverWait(self.selenium, self.waiting_time / 5).until( self.EC.visibility_of_element_located((self.By.XPATH, xpath)) ) found = True except: found = False - self.selenium.implicitly_wait(20) + self.selenium.implicitly_wait(self.waiting_time) self.assertFalse(found) def assertMap(self, dic_base, slug_pk, features_collecs=None): - dic_base = {'type': 'FeatureCollection', 'features': [dic_base]} + dic_base = {"type": "FeatureCollection", "features": [dic_base]} mapid = None - timeout = 20 t = time() - while not mapid and time()-t < timeout: + while not mapid and time() - t < self.waiting_time: mapdivs = self.selenium.find_elements_by_class_name("window-map") for mapdiv in mapdivs: if slug_pk in mapdiv.get_attribute("id"): mapid = mapdiv.get_attribute("id") break - id = int(re.search(r'\d+$', mapid).group()) + id = int(re.search(r"\d+$", mapid).group()) # TODO: use the map and not the map_features in the js function if features_collecs: - self.WebDriverWait(self.selenium, 2).until( - self.EC.visibility_of_element_located((self.By.ID, "http-geo-items-ready-"+slug_pk)) + self.WebDriverWait(self.selenium, self.waiting_time).until( + self.EC.visibility_of_element_located( + (self.By.ID, "http-geo-items-ready-" + slug_pk) + ) ) - print('id : ', id) - print('mapid : ', mapid) - print('slug_pk : ', slug_pk) - print(self.selenium.execute_script('return base_features_type(arguments[0])', mapid)) - base_features, geo_items_feats = self.selenium.execute_script('return get_features_by_id(arguments[0])', mapid) - base_features['features'][0]['properties'].pop('name') - dic_base['features'][0]['properties'] = {'id': id} - print('feat : ', base_features) - print('dic : ', dic_base) + base_features, geo_items_feats = self.selenium.execute_script( + "return get_features_by_id(arguments[0])", mapid + ) + base_features["features"][0]["properties"].pop("name") + base_features_type = self.selenium.execute_script( + "return base_features_type(arguments[0])", mapid + ) + dic_base["features"][0]["properties"] = {"id": id} + self.assertEqual( + base_features_type, dic_base["features"][0]["geometry"]["type"] + ) self.assertEqual(base_features, dic_base) if features_collecs: - print('feat : ', geo_items_feats) - print('dic : ', features_collecs) - try: - self.assertEqual(geo_items_feats, features_collecs) - except: - self.selenium.implicitly_wait(10000) - self.selenium.find_element_by_xpath('doesnet') + self.assertEqual(geo_items_feats, features_collecs) |