diff options
author | QuentinAndre <quentin.andre@imt-atlantique.net> | 2021-08-07 01:23:16 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-10-25 12:06:02 +0200 |
commit | 5124549dd86b3a4522cc953768a93e4dcce1a89c (patch) | |
tree | ba8aedfa4f07511f11ce49fb60f1b656104c7ccb | |
parent | 8eeb37c0648690e4ffca7234b710e44be4359b20 (diff) | |
download | Ishtar-5124549dd86b3a4522cc953768a93e4dcce1a89c.tar.bz2 Ishtar-5124549dd86b3a4522cc953768a93e4dcce1a89c.zip |
PR for peer review
-rw-r--r-- | archaeological_context_records/tests.py | 105 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 181 | ||||
-rw-r--r-- | archaeological_operations/templates/ishtar/sheet_operation.html | 1 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 225 | ||||
-rw-r--r-- | ishtar_common/static/js/ishtar-map.js | 143 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/sheet_geo_items.html | 7 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/sheet_simple_map.html | 4 | ||||
-rw-r--r-- | ishtar_common/tests.py | 112 |
8 files changed, 688 insertions, 90 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 0d1c84c7f..a71706e40 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -59,7 +59,7 @@ from ishtar_common.tests import ( GenericSerializationTest, COMMON_FIXTURES, WAREHOUSE_FIXTURES, - SearchText, + SearchText, SeleniumTests, ) from archaeological_operations.models import Operation @@ -1362,3 +1362,106 @@ class GraphGenerationTest(ContextRecordInit, TestCase): self.assertNotIn('"CR 4"', content) self.assertNotIn('"CR 2C"', content) self.assertNotIn('"CR 2D"', content) + + +class SeleniumTestsContextRecords(SeleniumTests): + fixtures = CONTEXT_RECORD_FIXTURES + + def test_geo_items(self): + def test_cr(i): + cr_label = self.selenium.find_element_by_xpath('//tbody/tr[' + str(i) + ']/td[2]').text + if cr_label[3:5] == "Pt": + dic_base = dic_point + else: + dic_base = dic_polygon + cr = self.selenium.find_element_by_xpath('//tbody/tr[' + str(i) + ']/td/a[@class="display_details"]') + self.scroll(cr) + cr.click() + self.WebDriverWait(self.selenium, 2).until( + lambda driver: driver.find_element_by_xpath( + '//dl[@class="col-12"]/dt[text()="Display geo items" or text()="Afficher les éléments"]')) + self.selenium.find_element_by_xpath( + '//dl[@class="col-12"]/dt[text()="Display geo items" or text()="Afficher les éléments"]') + dd = '//dl[@class="col-12"]/dd' + self.selenium.find_element_by_xpath(dd + '/label[text()="Display associated features: "]') + select = dd + '/select[@name="display-geo-items"]' + disp = self.selenium.find_element_by_xpath(select) + self.assertEqual(disp.get_attribute("value"), "") + self.assertMap(dic_base) + + # context record specific + + # click on "Base finds" + self.selenium.execute_script('return initialize_test_map()') + self.scroll(disp) + disp.click() + self.selenium.find_element_by_xpath(select + '/option[@value="basefinds"]').click() + cr_geoms = [ + {'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [[[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 1 from ' + cr_label}, + 'type': 'Feature'}, + {'geometry': {'coordinates': [], + 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 2 from ' + cr_label}, + 'type': 'Feature'}] + } + ] + self.assertMap(dic_base, cr_geoms) + + select_get_poly = dd + '/select[@name="get-polygons"]' + get_poly = self.selenium.find_element_by_xpath(select_get_poly) + self.selenium.execute_script('return initialize_test_map()') + get_poly.click() + self.selenium.find_element_by_xpath(select_get_poly + '/option[@value="points"]').click() + cr_geoms = [ + {'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [3.861, 3.028], + 'type': 'Point'}, + 'properties': {'label': 'Find 1 from ' + cr_label}, + 'type': 'Feature'}, + {'geometry': {'coordinates': [2, 43], + 'type': 'Point'}, + 'properties': {'label': 'Find 2 from ' + cr_label}, + 'type': 'Feature'}] + } + ] + self.assertMap(dic_base, cr_geoms) + + # Click on "None" + self.selenium.execute_script('return initialize_test_map()') + disp.click() + self.selenium.find_element_by_xpath(select + '/option[@value=""]').click() + self.assertMap(dic_base) + + self.setUpDefaultGeoItems() + + dic_polygon = { + 'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [[[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {}, + 'type': 'Feature'} + ]} + dic_point = { + 'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [2,43], + 'type': 'Point'}, + 'properties': {}, + 'type': 'Feature'} + ]} + for i in range(1, 5): + dropdown = self.selenium.find_element_by_xpath('//a[@class="nav-link dropdown-toggle"]') + self.scroll(dropdown) + dropdown.click() + self.selenium.find_element_by_xpath('//div[@class="dropdown-menu show"]/a[@href="/record_search/"]').click() + test_cr(i)
\ No newline at end of file diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index d6b2cc2ac..f02d0276e 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -90,7 +90,7 @@ from ishtar_common.tests import ( WAREHOUSE_FIXTURES, COMMON_FIXTURES, GenericSerializationTest, - SearchText, + SearchText, SeleniumTests, ) from archaeological_operations.tests import ImportTest, create_operation from archaeological_context_records.tests import ContextRecordInit @@ -2520,6 +2520,64 @@ class GeomaticTest(FindInit, TestCase): self.assertEqual(base_find.point_2d.ewkt, "SRID=4326;POINT (42 3)") self.assertEqual(base_find.point_source, "P") + @staticmethod + def create_cr_with_bfs(operat, geom_cr, list_geom_bf, label_cr, geom_params): + limit, geom, geom_2d, xy, wgs84 = geom_params + default = { + "label": label_cr, + "operation": operat, + } + cr = ContextRecord.objects.create(**default) + if geom_cr == "Point": + cr.point = geom + cr.multi_polygon = None + cr.multi_polygon_source = None + cr.multi_polygon_source_item = None + cr.x, cr.y = xy + cr.point_2d = geom_2d + cr.spatial_reference_system = wgs84 + else: + cr.point = None + cr.point_source = None + cr.point_2d = None + cr.point_source_item = None + cr.multi_polygon = "SRID=4326;" + limit + cr.multi_polygon_source = "P" + cr.multi_polygon_source_item = str(ContextRecord._meta.verbose_name) + cr.save() + cr = ContextRecord.objects.get(pk=cr.pk) + + bfs = [] + for i in range(len(list_geom_bf)): + default = { + "label": "Find " + str(i + 1) + " from " + label_cr, + "context_record": cr, + } + bf = models.BaseFind.objects.create(**default) + if list_geom_bf[i] == "Point": + bf.point = geom + bf.multi_polygon = None + bf.multi_polygon_source = None + bf.multi_polygon_source_item = None + bf.x, bf.y = xy + bf.point_2d = geom_2d + bf.spatial_reference_system = wgs84 + else: + bf.multi_polygon = "SRID=4326;" + limit + bf.point_source = None + bf.point_2d = None + bf.point = None + bf.save() + bf = models.BaseFind.objects.get(pk=bf.pk) + bfs.append(bf) + + '''data = {} + find = models.Find.objects.create(**data) + print(bf.point_2d) + find.base_finds.add(bf) + PROBLEM : point_2d is None''' + return cr, bfs + def test_get_geo_items(self): wgs84 = SpatialReferenceSystem.objects.get(srid=4326) profile = get_current_profile() @@ -2536,57 +2594,10 @@ class GeomaticTest(FindInit, TestCase): geom = GEOSGeometry("POINT({} {} {})".format(2, 43, 1), srid=4326) geom_2d = GEOSGeometry("POINT({} {})".format(2, 43), srid=4326) xy = 2, 43 - - def create_cr_with_bfs(operat, geom_cr, list_geom_bf, label_cr): - default = { - "label": label_cr, - "history_modifier": context_record.history_modifier, - "operation": operat, - } - cr = ContextRecord.objects.create(**default) - if geom_cr == "Point": - cr.point = geom - cr.multi_polygon = None - cr.multi_polygon_source = None - cr.multi_polygon_source_item = None - cr.x, cr.y = xy - cr.point_2d = geom_2d - cr.spatial_reference_system = wgs84 - else: - cr.multi_polygon = "SRID=4326;" + limit - cr.multi_polygon_source = "P" - cr.multi_polygon_source_item = str(ContextRecord._meta.verbose_name) - cr.save() - cr = ContextRecord.objects.get(pk=cr.pk) - - bfs = [] - for i in range(len(list_geom_bf)): - default = { - "label": "Find " + str(i + 1) + " from " + label_cr, - "history_modifier": base_find.history_modifier, - "context_record": cr, - } - bf = models.BaseFind.objects.create(**default) - if list_geom_bf[i] == "Point": - bf.point = geom - bf.multi_polygon = None - bf.multi_polygon_source = None - bf.multi_polygon_source_item = None - bf.x, bf.y = xy - bf.point_2d = geom_2d - bf.spatial_reference_system = wgs84 - else: - bf.multi_polygon = "SRID=4326;" + limit - bf.point_source = None - bf.point_2d = None - bf.point = None - bf.save() - bf = models.BaseFind.objects.get(pk=bf.pk) - bfs.append(bf) - return cr, bfs + geom_params = [limit, geom, geom_2d, xy, wgs84] # with point - cr_pt_pt, bfs = create_cr_with_bfs(operation, "Point", ["Point"], "CR Pt Pt") + cr_pt_pt, bfs = self.create_cr_with_bfs(operation, "Point", ["Point"], "CR Pt Pt", geom_params) base_find_pt = bfs[0] dic_pt_t = { @@ -2647,8 +2658,8 @@ class GeomaticTest(FindInit, TestCase): self.assertIn(json.dumps(dic_pt_pt_t).encode("utf-8"), response.content) # the context record is a multi-polygon - cr_poly_pt, bfs = create_cr_with_bfs( - operation, "Polygon", ["Point"], "CR Poly Pt" + cr_poly_pt, bfs = self.create_cr_with_bfs( + operation, "Polygon", ["Point"], "CR Poly Pt", geom_params ) base_find_pt = bfs[0] dic_poly_t = { @@ -2711,8 +2722,8 @@ class GeomaticTest(FindInit, TestCase): self.assertIn(json.dumps(dic_poly_pt_t).encode("utf-8"), response.content) # we add a base find which is a multi-polygon - cr_poly_poly_and_pt, bfs = create_cr_with_bfs( - ope, "Polygon", ["Polygon", "Point"], "CR Poly Poly Pt" + cr_poly_poly_and_pt, bfs = self.create_cr_with_bfs( + ope, "Polygon", ["Polygon", "Point"], "CR Poly Poly Pt", geom_params ) base_find_poly, base_find_pt = bfs @@ -2798,8 +2809,8 @@ class GeomaticTest(FindInit, TestCase): self.assertEqual(dic_poly_poly_and_pt_t, json.loads(response.content)) # context record is a point, base find 1 a polygon and base find 2 a point - cr_pt_poly_and_pt, bfs = create_cr_with_bfs( - ope, "Point", ["Polygon", "Point"], "CR Pt Poly Pt" + cr_pt_poly_and_pt, bfs = self.create_cr_with_bfs( + ope, "Point", ["Polygon", "Point"], "CR Pt Poly Pt", geom_params ) base_find_poly, base_find_pt = bfs @@ -3242,3 +3253,61 @@ class TemplateTest(FindInit, TestCase): for tpl in self.templates: if os.path.exists(tpl): os.remove(tpl) + +class SeleniumTestsBaseFinds(SeleniumTests): + fixtures = FIND_FIXTURES + + def test_geo_items(self): + def test_bf(i): + bf_label = self.selenium.find_element_by_xpath('//tbody/tr[' + str(i) + ']/td[2]').text + print("BF LABEL : ", bf_label) + if bf_label[3:5] == "Pt": + dic_base = dic_point + else: + dic_base = dic_polygon + cr = self.selenium.find_element_by_xpath('//tbody/tr[' + str(i) + ']/td/a[@class="display_details"]') + self.scroll(cr) + cr.click() + + self.assertMap(dic_base) + + title = self.selenium.find_elements_by_xpath( + '//dl[@class="col-12"]/dt[text()="Display geo items" or text()="Afficher les éléments"]') + dd = '//dl[@class="col-12"]/dd' + label = self.selenium.find_elements_by_xpath(dd + '/label[text()="Display associated features: "]') + disp = self.selenium.find_elements_by_xpath(dd + '/select[@name="display-geo-items"]') + + self.assertLess(len(title), 1) + self.assertLess(len(label), 1) + self.assertLess(len(disp), 1) + + self.setUpDefaultGeoItems() + + dic_polygon = { + 'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [ + [[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {}, + 'type': 'Feature'} + ]} + dic_point = { + 'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [2, 43], + 'type': 'Point'}, + 'properties': {}, + 'type': 'Feature'} + ]} + print(models.Find.objects.all()) + #TODO: create finds from BaseFinds in setUpDefaultGeoItems + for i in range(1, 9): + dropdown = self.selenium.find_element_by_xpath('//a[@class="nav-link dropdown-toggle"]') + self.scroll(dropdown) + dropdown.click() + self.selenium.find_element_by_xpath('//div[@class="dropdown-menu show"]/a[@href="/find_search/"]').click() + test_bf(i)
\ No newline at end of file diff --git a/archaeological_operations/templates/ishtar/sheet_operation.html b/archaeological_operations/templates/ishtar/sheet_operation.html index caf5f4234..f597c9598 100644 --- a/archaeological_operations/templates/ishtar/sheet_operation.html +++ b/archaeological_operations/templates/ishtar/sheet_operation.html @@ -273,6 +273,7 @@ {% endif %} <h3>{% trans "Localisation"%}</h3> + <div class="row"> {% with geo_item=item %} {% include "ishtar/blocks/sheet_simple_map.html"%} diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 39fa730f0..d2814a21d 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -19,6 +19,7 @@ import json import datetime +import re from subprocess import Popen, PIPE from io import StringIO, BytesIO import tempfile @@ -37,6 +38,7 @@ from django.utils.text import slugify from django.contrib.auth.models import User, Permission from django.utils.translation import ugettext_lazy as _, pgettext, pgettext_lazy + from . import models from ishtar_common.views import document_deletion_steps @@ -94,7 +96,7 @@ from ishtar_common.tests import ( COMMON_FIXTURES, GenericSerializationTest, WAREHOUSE_FIXTURES, - SearchText, + SearchText, SeleniumTests, ) from ishtar_common.serializers import restore_serialized @@ -193,8 +195,8 @@ class ImportTest(object): # target for this import target = ( TargetKey.objects.filter(target__target="operation_type") - .order_by("-pk") - .all()[0] + .order_by("-pk") + .all()[0] ) target.value = models.OperationType.objects.get(txt_idx="prog_excavation").pk target.is_set = True @@ -299,7 +301,7 @@ class ImportTest(object): mcc = ImporterType.objects.get(name="MCC - UE") mcc_file = open( settings.ROOT_PATH + "../archaeological_context_records/tests/" - "MCC-context-records-example.csv", + "MCC-context-records-example.csv", "rb", ) file_dict = { @@ -1577,12 +1579,12 @@ class ParcelTest(ImportTest, TestCase): self.assertTrue( (values["section"], values["parcel_number"]) in result[year], msg='Section - Parcel number: "%s - %s" is not ' - 'in "%s"' - % ( - values["section"], - values["parcel_number"], - str(result[year]), - ), + 'in "%s"' + % ( + values["section"], + values["parcel_number"], + str(result[year]), + ), ) parcels.pop(parcels.index(values)) result[year].pop( @@ -1593,14 +1595,14 @@ class ParcelTest(ImportTest, TestCase): parcels, [], msg='Parcel(s): "%s" haven\'t be ' - 'recognized in "%s"' % (str(parcels), value), + 'recognized in "%s"' % (str(parcels), value), ) not_imported = [data for data in result.values() if data] self.assertEqual( not_imported, [], msg='Parcel(s): "%s" haven\'t be ' - 'recognized in "%s"' % (str(not_imported), value), + 'recognized in "%s"' % (str(not_imported), value), ) @@ -1910,14 +1912,14 @@ class OperationTest(TestCase, OperationInitTest): operation.save() self.assertIsNotNone(operation.search_vector) for key in ( - "old", - "dirty", - "daisy", - "'2010'", - "zardoz", - "huiaaa5", - "{}42huiaaa5".format(profile.operation_prefix.lower()), - "42huiaaa5", + "old", + "dirty", + "daisy", + "'2010'", + "zardoz", + "huiaaa5", + "{}42huiaaa5".format(profile.operation_prefix.lower()), + "42huiaaa5", ): self.assertIn(key, operation.search_vector) @@ -2080,8 +2082,8 @@ class OperationTest(TestCase, OperationInitTest): f = BytesIO(response.content) filetype = ( Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE) - .communicate(f.read(1024))[0] - .strip() + .communicate(f.read(1024))[0] + .strip() ) self.assertTrue(filetype.startswith(b"application/pdf")) @@ -4181,3 +4183,182 @@ class DocumentQATest(OperationInitTest, TestCase): self.assertEqual( models.Document.objects.get(pk=document_1.pk).source_type, source_type ) + + +class SeleniumTestsOperations(SeleniumTests): + fixtures = OPERATION_FIXTURES + + def test_geo_items(self): + def test_operation_i(i): + #TODO: dic_base should be point or poly, here it's only poly + dic_base = { + 'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [ + [[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {}, + 'type': 'Feature'} + ]} + self.selenium.find_element_by_xpath('//tbody/tr[' + str(i) + ']/td/a[@class="display_details"]').click() + self.WebDriverWait(self.selenium, 2).until( + lambda driver: driver.find_element_by_xpath( + '//dl[@class="col-12"]/dt[text()="Display geo items" or text()="Afficher les éléments"]')) + self.selenium.find_element_by_xpath( + '//dl[@class="col-12"]/dt[text()="Display geo items" or text()="Afficher les éléments"]') + dd = '//dl[@class="col-12"]/dd' + self.selenium.find_element_by_xpath(dd + '/label[text()="Display associated features: "]') + select = dd + '/select[@name="display-geo-items"]' + disp = self.selenium.find_element_by_xpath(select) + self.assertEqual(disp.get_attribute("value"), "") + self.assertMap(dic_base) + + # operation specific + + # click on "Context records" + self.selenium.execute_script('return initialize_test_map()') + self.scroll(disp) + disp.click() + self.selenium.find_element_by_xpath(select + '/option[@value="contextrecords"]').click() + cr_geoms = [ + {'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [ + [[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': None, + 'type': 'Feature'}, + {'geometry': + {'coordinates': [], 'type': 'MultiPolygon'}, + 'properties': None, 'type': 'Feature'}]} + ] + self.assertMap(dic_base, cr_geoms) + + select_get_poly = dd + '/select[@name="get-polygons"]' + get_poly = self.selenium.find_element_by_xpath(select_get_poly) + get_poly.click() + self.selenium.find_element_by_xpath(select_get_poly + '/option[@value="points"]').click() + cr_geoms = [ + {'type': 'FeatureCollection', + 'features': [ + {'geometry': + {'coordinates': [3.861, 3.028], 'type': 'Point'}, + 'properties': None, + 'type': 'Feature'}, + {'geometry': + {'coordinates': [2, 43], 'type': 'Point'}, + 'properties': None, 'type': 'Feature'}]} + ] + self.assertMap(dic_base, cr_geoms) + + # Click on "Everything" + disp.click() + self.selenium.find_element_by_xpath(select + '/option[@value="basefinds"]').click() + self.selenium.execute_script('return initialize_test_map()') + get_poly.click() + self.selenium.find_element_by_xpath(select_get_poly + '/option[@value="polygons"]').click() + every_geoms = [ + {'type': 'FeatureCollection', + 'features': [{ + 'geometry': { + 'coordinates': [[[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 1 from CR Poly Poly Pt'}, + 'type': 'Feature'}, + {'geometry': { + 'coordinates': [], + 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 2 from CR Poly Poly Pt'}, + 'type': 'Feature'} + ]}, + {'type': 'FeatureCollection', + 'features': [ + {'geometry': { + 'coordinates': [[[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 1 from CR Pt Poly Pt'}, 'type': 'Feature'}, + {'geometry': { + 'coordinates': [], 'type': 'MultiPolygon'}, + 'properties': {'label': 'Find 2 from CR Pt Poly Pt'}, + 'type': 'Feature'} + ]}, + {'type': 'FeatureCollection', + 'features': [ + {'geometry': { + 'coordinates': [ + [[[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]]]], + 'type': 'MultiPolygon'}, + 'properties': None, + 'type': 'Feature'}, + {'geometry': { + 'coordinates': [], + 'type': 'MultiPolygon'}, + 'properties': None, + 'type': 'Feature'} + ]}] + self.assertMap(dic_base, every_geoms) + + self.selenium.execute_script('return initialize_test_map()') + get_poly.click() + self.selenium.find_element_by_xpath(select_get_poly + '/option[@value="points"]').click() + every_geoms = [ + {'type': 'FeatureCollection', + 'features': [{ + 'geometry': { + 'coordinates': [3.861, 3.028], + 'type': 'Point'}, + 'properties': {'label': 'Find 1 from CR Poly Poly Pt'}, + 'type': 'Feature'}, + {'geometry': { + 'coordinates': [2, 43], + 'type': 'Point'}, + 'properties': {'label': 'Find 2 from CR Poly Poly Pt'}, + 'type': 'Feature'} + ]}, + {'type': 'FeatureCollection', + 'features': [ + {'geometry': { + 'coordinates': [3.861, 3.028], + 'type': 'Point'}, + 'properties': {'label': 'Find 1 from CR Pt Poly Pt'}, 'type': 'Feature'}, + {'geometry': { + 'coordinates': [2, 43], 'type': 'Point'}, + 'properties': {'label': 'Find 2 from CR Pt Poly Pt'}, + 'type': 'Feature'} + ]}, + {'type': 'FeatureCollection', + 'features': [ + {'geometry': { + 'coordinates': [3.861, 3.028], + 'type': 'Point'}, + 'properties': None, + 'type': 'Feature'}, + {'geometry': { + 'coordinates': [2, 43], + 'type': 'Point'}, + 'properties': None, + 'type': 'Feature'} + ]}] + self.assertMap(dic_base, every_geoms) + + # Click on "None" + self.selenium.execute_script('return initialize_test_map()') + disp.click() + self.selenium.find_element_by_xpath(select + '/option[@value=""]').click() + self.assertMap(dic_base) + + self.setUpDefaultGeoItems() + + self.selenium.find_element_by_xpath('//a[@class="nav-link dropdown-toggle"]').click() + self.selenium.find_element_by_xpath('//div[@class="dropdown-menu show"]/a[@href="/operation_search/"]').click() + test_operation_i(1) # point based operation + dropdown = self.selenium.find_element_by_xpath('//a[@class="nav-link dropdown-toggle"]') + self.scroll(dropdown) + dropdown.click() + self.selenium.find_element_by_xpath('//div[@class="dropdown-menu show"]/a[@href="/operation_search/"]').click() + test_operation_i(2) # polygon based operation diff --git a/ishtar_common/static/js/ishtar-map.js b/ishtar_common/static/js/ishtar-map.js index c78da1098..d4eb7285d 100644 --- a/ishtar_common/static/js/ishtar-map.js +++ b/ishtar_common/static/js/ishtar-map.js @@ -550,6 +550,17 @@ var vector_source = {}; var vector_layer = {}; var vector_features = {}; +// for test +var geo_items_features = null; +var current_test = false; +var initialize_test_map = function () { + geo_items_features = {}; + current_test = true; + if ($("#http-geo-items-ready").length === 0) { + $("#display-geo-items").after('<div id="http-geo-items-ready">Ready!</div>'); + } + $("#http-geo-items-ready").hide(); +} var initialize_base_map = function(map_id, layers){ center = wkt_format.readGeometry(map_default_center).getCoordinates(); @@ -603,20 +614,31 @@ 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); map[map_id].on('click', manage_click_on_map(map_id)); map[map_id].on('pointermove', manage_hover(map_id)); + + if (current_test) { + geo_items_features[map_id] = []; // for test + } }; 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); @@ -662,19 +684,32 @@ var disp_geo_items = function(map_id, base_url, slug, pk, if (httpRequest.readyState === XMLHttpRequest.DONE) { if (httpRequest.status === 200) { geo_items = to_geo_items(JSON.parse(httpRequest.responseText), slug, display_both) + if (current_test) { + geo_items_features[map_id] = []; + } + var feat = null; for (geo_item of geo_items['base-finds']) { if (get_poly) { - display_associated_polys(map_id, geo_item, 'basefind'); + feat = display_associated_polys(map_id, geo_item, 'basefind'); } else { - display_associated_points(map_id, geo_item, 'basefind'); + feat = display_associated_points(map_id, geo_item, 'basefind'); + } + if (current_test) { + geo_items_features[map_id].push(feat); } } for (geo_item of geo_items['context-records']) { if (get_poly) { - display_associated_polys(map_id, geo_item, 'contextrecord'); + feat = display_associated_polys(map_id, geo_item, 'contextrecord'); } else { - display_associated_points(map_id, geo_item, 'contextrecord'); + feat = display_associated_points(map_id, geo_item, 'contextrecord'); } + if (current_test) { + geo_items_features[map_id].push(feat); + } + } + if (current_test) { + $("#http-geo-items-ready").show(); } } else { return; } } @@ -714,6 +749,7 @@ var display_associated_polys = function (map_id, polys, slug) { style: style }); map[map_id].addLayer(_vector_layer); + return _vector_features } var get_associated_base_find_style = function (feature) { @@ -742,17 +778,18 @@ var get_associated_context_record_style = function (feature) { }; var display_associated_points = function (map_id, points, slug) { + console.log('geo items points') console.log(points); - _point_features = geojson_format.readFeatures(points); - console.log(_point_features[0].getGeometry().getType()) + /*_point_features = geojson_format.readFeatures(points); _cluster_source = new ol.source.Cluster({ distance: 40, source: new ol.source.Vector() }); _cluster_source.getSource().addFeatures(_point_features); - style = get_associated_base_find_style; + // TODO: create own style + style = cluster_get_style; if (slug === 'contextrecord') { - style = get_associated_context_record_style; + style = style; } _cluster_layer = new ol.layer.Vector({ name: 'Cluster', @@ -760,5 +797,95 @@ var display_associated_points = function (map_id, points, slug) { style: style }); map[map_id].addLayer(_cluster_layer); + return _point_features;*/ +} + +// for test purpose +// to insert in sheet_simple_map.html when working with maps +// wait_for_map("map-{{window_id}}-{{geo_item.SLUG}}-{{geo_item.pk}}"); +var wait_for_map = function(map_id) { + map[map_id].on('rendercomplete', function(event) { + console.log("map fully loaded"); + }) + if (map_id in cluster_layer) { + console.log('cluster layer'); + cluster_layer[map_id].on('change', function(event) { + if (cluster_layer.getState === "ready") { + console.log("cluster fully loaded"); + } + }) + } + if (map_id in vector_layer) { + console.log('vector layer'); + vector_layer[map_id].on('change', function(event) { + if (vector_layer.getState === "ready") { + console.log("vector fully loaded"); + } + }) + } + if (map_id in cluster_source) { + console.log('cluster source'); + cluster_source[map_id].on('change', function(event) { + if (cluster_source.getState === "ready") { + console.log("cluster source fully loaded"); + } + }) + } + if (map_id in vector_source) { + console.log('vector source'); + vector_source[map_id].on('change', function(event) { + if (vector_source.getState === "ready") { + console.log("vector source fully loaded"); + } + }) + } +} + +var get_map_by_id_test = function (id) { +// problem here: async function because map and layers are not fully loaded directly + map[id].getLayers().getArray()[0].getSource().on( + 'change', function (evt) { + console.log('change') + var source = evt.target; + if (source.getState() === "ready") { + console.log('ready'); + console.log(map_layers[id][0].getSource().getKeys()) + } + } + ) + map[id].on( + 'change', function (evt) { + console.log('change') + var map = evt.target; + if (map.getState() === "ready") { + console.log('ready'); + console.log(map_layers[id][0].getSource().getKeys()) + } + } + ) + console.log("vector_features") + console.log(vector_features[id]) + console.log("layers") + console.log(map_layers[id]) + console.log("source") + console.log(map_layers[id][0].getSource()) +} + +var get_map_by_id = function (id) { + if (vector_features[id] == null) { + var base_features = geojson_format.writeFeaturesObject(point_features[id], {decimals: 3}); + } else { + var base_features = geojson_format.writeFeaturesObject(vector_features[id], {decimals: 3}); + } + 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})); + } + } + return [base_features, geo_items_feats]; +} +var print = 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 36fbabfcf..99fc7b866 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html @@ -24,9 +24,9 @@ </dl> <script> var hide_get_polygons = function() { - const disp = $("#display-geo-items"); - const get_poly = $("#get-polygons"); - const label = $("#label-get-polygons"); + var disp = $("#display-geo-items"); + var get_poly = $("#get-polygons"); + var label = $("#label-get-polygons"); get_poly.hide(); label.hide(); disp.change( function () { @@ -38,6 +38,7 @@ var hide_get_polygons = function() { }) } hide_get_polygons(); + </script> {% endif %} {% endif %}
\ No newline at end of file diff --git a/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html b/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html index b4e669a70..e8a33ae68 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html @@ -53,6 +53,10 @@ get_polygons.change( function () { disp_map(); display_geo_items(disp.val(), get_polygons.val()); }) +console.log('points with base points'); +display_associated_points("map-{{window_id}}-{{geo_item.SLUG}}-{{geo_item.pk}}", + {{geo_item.point_2d_geojson|safe}}, "{{geo_item.SLUG}}"); + {% endif %} </script> {% endif %} diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index e535eda4b..73e3dda88 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -16,6 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # See the file COPYING for details. +import re from bs4 import BeautifulSoup as Soup import csv @@ -3127,3 +3128,114 @@ class TemplateGenerationTest(TestCase): filtr = doc.get_filter(template, filter_re) for key in expected_keys: self.assertIn(key, filtr) + +from django.contrib.staticfiles.testing import StaticLiveServerTestCase +from selenium.webdriver.firefox.webdriver import WebDriver + +class SeleniumTests(StaticLiveServerTestCase): + from selenium.webdriver.support.wait import WebDriverWait + from selenium.webdriver.common.by import By + from selenium.webdriver.support import expected_conditions as EC + from archaeological_operations.models import Operation + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.selenium = WebDriver() + cls.selenium.implicitly_wait(20) + + @classmethod + def tearDownClass(cls): + cls.selenium.quit() + super().tearDownClass() + + def setUp(self): + # profile + profile = models.get_current_profile() + profile.mapping = True + profile.files = True + profile.context_record = True + profile.find = True + profile.warehouse = True + profile.save() + + # 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")) + 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() + + def setUpDefaultGeoItems(self): + from django.contrib.gis.geos import GEOSGeometry + from ishtar_common.models_common import SpatialReferenceSystem + from archaeological_finds.tests import GeomaticTest + from archaeological_operations.tests import create_operation + + ope1 = create_operation(self.user) + ope2 = create_operation(self.user) + + wgs84 = SpatialReferenceSystem.objects.get(srid=4326) + limit = ( + "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)))" + ) + geom = GEOSGeometry("POINT({} {} {})".format(2, 43, 1), srid=4326) + geom_2d = GEOSGeometry("POINT({} {})".format(2, 43), srid=4326) + xy = 2, 43 + geom_params = [limit, geom, geom_2d, xy, wgs84] + + ope1 = self.Operation.objects.get(pk=ope1.pk) + ope1.multi_polygon = limit + ope1.save() + ope1 = self.Operation.objects.get(pk=ope1.pk) + ope1.point = geom + ope1.x, ope2.y = xy + ope1.point_source = "P" + ope1.spatial_reference_system = wgs84 + ope1.point_source_item = "Operation" + ope1.save() + ope1 = self.Operation.objects.get(pk=ope1.pk) + ope2.multi_polygon = limit + ope2.save() + + cr_pt1, (bf_poly_pt1, bf_pt_pt1) = GeomaticTest.create_cr_with_bfs(ope1, "Point", ["Polygon", "Point"], + "CR Pt Poly Pt", geom_params) + cr_poly1, (bf_poly_poly1, bf_pt_poly1) = GeomaticTest.create_cr_with_bfs(ope1, "Polygon", ["Polygon", "Point"], + "CR Poly Poly Pt", geom_params) + cr_pt2, (bf_poly_pt2, bf_pt_pt2) = GeomaticTest.create_cr_with_bfs(ope2, "Point", ["Polygon", "Point"], + "CR Pt Poly Pt", geom_params) + cr_poly2, (bf_poly_poly2, bf_pt_poly2) = GeomaticTest.create_cr_with_bfs(ope2, "Polygon", ["Polygon", "Point"], + "CR Poly Poly Pt", geom_params) + def scroll(self, element): + self.selenium.execute_script( + "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" + + "var elementTop = arguments[0].getBoundingClientRect().top;" + + "window.scrollBy(0, elementTop-(viewPortHeight/2));", + element) + + def assertMap(self, dic_base, features_collecs=None): + mapdiv = self.selenium.find_element_by_class_name("window-map") + mapid = mapdiv.get_attribute("id") + 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")) + ) + print(id) + print(self.selenium.execute_script('return print(arguments[0])', mapid)) + base_features, geo_items_feats = self.selenium.execute_script('return get_map_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) + self.assertEqual(base_features, dic_base) + if features_collecs: + print('feat : ', geo_items_feats) + print('dic : ', features_collecs) + self.assertEqual(geo_items_feats, features_collecs) |