From f1a831e37ee06b21f3d1d49f6c749b84e15cbb3a Mon Sep 17 00:00:00 2001 From: QuentinAndre Date: Tue, 24 Aug 2021 21:38:46 +0200 Subject: ui tests without base feature point working --- ishtar_common/static/js/ishtar-map.js | 34 ++- .../templates/ishtar/blocks/sheet_geo_items.html | 8 +- ishtar_common/tests.py | 227 +++++++++++++++++++-- 3 files changed, 228 insertions(+), 41 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/static/js/ishtar-map.js b/ishtar_common/static/js/ishtar-map.js index 00aadfd9b..ab8096b06 100644 --- a/ishtar_common/static/js/ishtar-map.js +++ b/ishtar_common/static/js/ishtar-map.js @@ -146,9 +146,9 @@ var TrackPositionControl = (function (Control) { if ( Control ) TrackPositionControl.__proto__ = Control; TrackPositionControl.prototype = Object.create( Control && Control.prototype ); - TrackPositionControl.prototype.constructor = TrackPositionControl; + TrackPositionControl.prototype.varructor = TrackPositionControl; - TrackPositionControl.prototype.handleTrackPosition = function handleTrackPosition () { + TrackPositionControl.prototype.handvarrackPosition = function handvarrackPosition () { if (!geolocation[this.map_id]){ set_geoloc_source(this.map_id); } else { @@ -452,7 +452,7 @@ var open_map_window = function(map_id){ }; }; -var complete_list_label = "complete list..."; +var complete_list_label = "compvare list..."; var _display_items = function(map_id, features, offset_x, offset_y){ // console.log("display_items"); @@ -553,13 +553,14 @@ var vector_features = {}; // for test var geo_items_features = null; var current_test = false; -var initialize_test_map = function () { +var initialize_test_map = function (slug_pk) { + var id = "http-geo-items-ready-" + slug_pk; geo_items_features = {}; current_test = true; - if ($("#http-geo-items-ready").length === 0) { - $("#display-geo-items").after('
Ready!
'); + if ($("#"+id).length === 0) { + $("#display-geo-items").after('
Ready!
'); } - $("#http-geo-items-ready").hide(); + $("#"+id).hide(); } var initialize_base_map = function(map_id, layers){ @@ -614,17 +615,12 @@ var display_map = function(map_id, points, lines_and_polys, layers){ link_template[map_id] = lines_and_polys['link_template']; } if (map[map_id]){ - redraw_map(map_id, layers); } else { initialize_base_map(map_id, layers); } - //console.log('map init') - //console.log(map[map_id]); display_points(map_id, points); display_lines_and_polys(map_id, lines_and_polys); - //console.log('map after points'); - //console.log(map[map_id]); init_popup(map_id); @@ -637,8 +633,6 @@ var display_map = function(map_id, points, lines_and_polys, layers){ }; var display_points = function(map_id, points){ - //console.log('points'); - //console.log(points); if (!points) return; point_features[map_id] = geojson_format.readFeatures(points); enable_clustering(map_id); @@ -701,7 +695,7 @@ var display_geo_items = function(map_id, base_url, slug, pk, if (current_test) { geo_items_features[map_id].push(bfs) geo_items_features[map_id].push(crs) - $("#http-geo-items-ready").show(); + $("#http-geo-items-ready-"+slug+"-"+pk).show(); } } else { return; } } @@ -872,19 +866,19 @@ var get_map_by_id_test = function (id) { var get_features_by_id = function (id) { if (vector_features[id] == null) { - var base_features = geojson_format.writeFeaturesObject(point_features[id], {decimals: 3}); + var base_features = geojson_format.writeFeaturesObject(point_features[id], {decimals: 5}); } else { - var base_features = geojson_format.writeFeaturesObject(vector_features[id], {decimals: 3}); + var base_features = geojson_format.writeFeaturesObject(vector_features[id], {decimals: 5}); } var geo_items_feats = []; if (geo_items_features && id in geo_items_features) { for (var features of geo_items_features[id]) { - geo_items_feats.push(geojson_format.writeFeaturesObject(features, {decimals: 3})); + geo_items_feats.push(geojson_format.writeFeaturesObject(features, {decimals: 5})); } } return [base_features, geo_items_feats]; } -var print = function (id) { +var base_features_type = function (id) { return "vector " + typeof vector_features[id] + ", cluster " + typeof point_features[id] ; -} \ No newline at end of file +} diff --git a/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html b/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html index 93b9a5b35..33b3dc5ed 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html @@ -1,20 +1,20 @@ {% load i18n window_field %}{% if PROFILE.mapping %} {% if geo_item.point_2d or geo_item.multi_polygon %}
-
{% trans "Display geo items" %}
+
{% trans "Display geo items" %}
{% if geo_item.SLUG == "operation" %} - + {% endif %} - +
- diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index e3314b58f..ca3b40303 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -16,9 +16,8 @@ # along with this program. If not, see . # See the file COPYING for details. -import re - from bs4 import BeautifulSoup as Soup +import re import csv import datetime import importlib @@ -2852,23 +2851,23 @@ class GeomaticTest(TestCase): @staticmethod def pt_coords_from_label(label, geoms): if label[:2] == "Pt": - return geoms[0][2] + return geoms[0][2], True if label[:4] == "Poly": - return geoms[0][1] + return geoms[0][1], False if label[:2] == "CR": if label[-2:] == "Pt": - return geoms[1][2] + return geoms[1][2], True if label[3:5] == "Pt": - return geoms[0][2] - return geoms[1][1] + return geoms[0][2], True + return geoms[1][1], False if label[5:10] == "Point": - return geoms[2][2] + return geoms[2][2], True # label has shape Find Polygon from CR xxx xxx if label[-2:] == "Pt": - return geoms[1][2] + return geoms[1][2], True if label[21:23] == "Pt": - return geoms[0][2] - return geoms[2][1] + return geoms[0][2], True + return geoms[2][1], False def assertPt(self, item, geom): self.assertEqual(item.multi_polygon.coords, geom[0].coords) @@ -2917,6 +2916,177 @@ class GeomaticTest(TestCase): 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): + from archaeological_operations.models import Operation + from archaeological_context_records.models import ContextRecord + from archaeological_finds.models import BaseFind + import copy + + cache_dics_t, cache_dics_f = {}, {} + res = {} + + def finds_first(x): + if x[:2] == "CR": + return 0 + if x[:4] == "Find": + return -1 + else: + return 1 + + labels = list(pks.keys()) + labels.sort(key=finds_first) + for label in labels: + pk = pks[label] + pt_coords, precise = GeomaticTest.pt_coords_from_label(label, geoms) + if label[:4] == "Find": + dic_t = { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", "coordinates": geoms[2][0] + }, + "properties": {"label": label}, + } + dic_f = { + "type": "Feature", + "geometry": { + "type": "Point", "coordinates": pt_coords, + }, + "properties": {"label": label}, + } + cache_dics_t[label] = dic_t + 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) + get_pk = "?pk=" + else: + get_polys = dic_t + get_pts = dic_f + + elif label[:2] == "CR": + cache_key = lambda x: "Find " + x + " from " + label + dic_t = { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", "coordinates": geoms[1][0] + }, + "properties": { + "label": label, + "base-finds": { + '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, + }, + "properties": { + "label": label, + "base-finds": { + '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) + get_pk = "?context_record_pk=" + else: + get_polys = copy.deepcopy(dic_t) + 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")] + else: + real_label = "OA3" + cache_key_prefix = "CR " + label + " " + if label == "Pt": + real_label = "OA2" + dic_t = { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", "coordinates": geoms[0][0] + }, + "properties": { + "label": real_label, + "context-records": { + '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, + }, + "properties": { + "label": real_label, + "context-records": { + '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) + 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': [ + cache_key_prefix + "Poly", + cache_key_prefix + "Pt" + ], + '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': [ + cache_key_prefix + "Poly", + cache_key_prefix + "Pt" + ], + '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} + else: + res[label] = {'get_polys': get_polys, + 'get_pts': get_pts, + 'precise': precise} + return res class NewItems(TestCase): fixtures = COMMON_FIXTURES @@ -3366,6 +3536,11 @@ class SeleniumTests(StaticLiveServerTestCase): def setUpDefaultGeoItems(self): return GeomaticTest.setUpDefaultGeoItems(self.user) + def pks_and_geojson(self): + geoms, pks = self.setUpDefaultGeoItems() + geojsons = GeomaticTest.geojson_geo_items(geoms, pks) + return pks, geojsons + def scroll(self, element): self.selenium.execute_script( "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" @@ -3373,20 +3548,38 @@ class SeleniumTests(StaticLiveServerTestCase): + "window.scrollBy(0, elementTop-(viewPortHeight/2));", element) - def assertMap(self, dic_base, features_collecs=None): - mapdiv = self.selenium.find_element_by_id("window-map") - mapid = mapdiv.get_attribute("id") + def assertNotInDOM(self, xpath): + self.selenium.implicitly_wait(5) + try: + self.WebDriverWait(self.selenium, 2).until( + self.EC.visibility_of_element_located((self.By.XPATH, xpath)) + ) + found = True + except: + found = False + self.selenium.implicitly_wait(20) + self.assertFalse(found) + + def assertMap(self, dic_base, slug_pk, features_collecs=None): + dic_base = {'type': 'FeatureCollection', 'features': [dic_base]} + mapdivs = self.selenium.find_elements_by_class_name("window-map") + mapid = None + 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()) # 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")) + self.EC.visibility_of_element_located((self.By.ID, "http-geo-items-ready-"+slug_pk)) ) print(id) - print(self.selenium.execute_script('return print(arguments[0])', mapid)) + print('mapid : ', mapid) + 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 + dic_base['features'][0]['properties'] = {'id': id} print('feat : ', base_features) print('dic : ', dic_base) self.assertEqual(base_features, dic_base) -- cgit v1.2.3