From 95bf685159766fabc4251ea9d7d1f3fc7c520e9a Mon Sep 17 00:00:00 2001 From: David Larlet Date: Thu, 27 Apr 2023 20:33:41 -0400 Subject: [PATCH 1/7] Setup to create integration tests with Playwright --- Makefile | 6 +++++- pyproject.toml | 3 +++ umap/tests/integration/__init__.py | 0 umap/tests/integration/test_basics.py | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 umap/tests/integration/__init__.py create mode 100644 umap/tests/integration/test_basics.py diff --git a/Makefile b/Makefile index 312803a8..8401f1bb 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ install: ## Install the dependencies .PHONY: develop develop: ## Install the test and dev dependencies python3 -m pip install -e .[test,dev] + playwright install .PHONY: pretty-templates pretty-templates: ## Prettify template files @@ -45,9 +46,12 @@ publish: ## Publish the Python package to Pypi @hatch publish make clean - test: py.test -xv umap/tests/ + +test-integration: + DJANGO_ALLOW_ASYNC_UNSAFE=1 py.test -xv umap/tests/integration/ + clean: rm -f dist/* rm -rf build/* diff --git a/pyproject.toml b/pyproject.toml index 69e9f7cb..e0191f43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,8 +52,11 @@ dev = [ ] test = [ "factory-boy==3.2.1", + "playwright==1.32.1", "pytest==6.2.5", "pytest-django==4.5.2", + "pytest-playwright==0.3.3", + ] docker = [ "uwsgi==2.0.21", diff --git a/umap/tests/integration/__init__.py b/umap/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/umap/tests/integration/test_basics.py b/umap/tests/integration/test_basics.py new file mode 100644 index 00000000..4bb545ee --- /dev/null +++ b/umap/tests/integration/test_basics.py @@ -0,0 +1,18 @@ +import pytest +from playwright.sync_api import expect + + +def test_page_title(page, live_server): + page.goto(live_server.url) + expect(page).to_have_title("uMap") + + +@pytest.mark.parametrize( + "lang,link_name,link_url", + [("fr", "Créer une carte", "/fr/map/new/"), ("en", "Create a map", "/en/map/new/")], +) +def test_create_map_link(page, live_server, lang, link_name, link_url): + page.goto(f"{live_server.url}/{lang}/") + create_map_button = page.locator("header nav a.button") + expect(create_map_button).to_have_text(link_name) + expect(create_map_button).to_have_attribute("href", link_url) From 29547d0bd60313823770bdd94a0dc4b6f0ac4ac9 Mon Sep 17 00:00:00 2001 From: David Larlet Date: Mon, 1 May 2023 14:14:06 -0400 Subject: [PATCH 2/7] Add a test to verify marker creation at position --- umap/tests/integration/test_basics.py | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/umap/tests/integration/test_basics.py b/umap/tests/integration/test_basics.py index 4bb545ee..9f9e6755 100644 --- a/umap/tests/integration/test_basics.py +++ b/umap/tests/integration/test_basics.py @@ -16,3 +16,32 @@ def test_create_map_link(page, live_server, lang, link_name, link_url): create_map_button = page.locator("header nav a.button") expect(create_map_button).to_have_text(link_name) expect(create_map_button).to_have_attribute("href", link_url) + + +def test_create_map_with_cursor(page, live_server): + page.goto(f"{live_server.url}/en/map/new/") + + # Click on the Draw a marker button on a new map. + create_marker_link = page.locator(".leaflet-control-toolbar ").get_by_title( + "Draw a marker" + ) + expect(create_marker_link).to_have_attribute("href", "#") + create_marker_link.click() + + # Check no marker is present by default. + marker_pane_children = page.locator(".leaflet-marker-pane > div") + expect(marker_pane_children).to_have_count(0) + + # Click on the map, it will place a marker at the given position. + map = page.locator("#map") + map.click(position={"x": 200, "y": 200}) + expect(marker_pane_children).to_have_count(1) + expect(marker_pane_children).to_have_attribute( + "style", + ( + "margin-left: -16px; " + "margin-top: -40px; " + "transform: translate3d(200px, 200px, 0px); " + "z-index: 200;" + ), + ) From 4c03f012bda31a90597a35a4c886a754516b7cc9 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Tue, 16 May 2023 15:14:54 +0200 Subject: [PATCH 3/7] Bump playwright --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e0191f43..4dce31de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,7 @@ dev = [ ] test = [ "factory-boy==3.2.1", - "playwright==1.32.1", + "playwright==1.33.0", "pytest==6.2.5", "pytest-django==4.5.2", "pytest-playwright==0.3.3", From c35c6ee78c7272a2ac4afa0808cfa1c6d6704134 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 4 Sep 2023 15:00:51 +0200 Subject: [PATCH 4/7] test: allow to run playwright tests directly with py.test cli --- Makefile | 2 +- umap/tests/integration/__init__.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8401f1bb..a66e462d 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ test: py.test -xv umap/tests/ test-integration: - DJANGO_ALLOW_ASYNC_UNSAFE=1 py.test -xv umap/tests/integration/ + py.test -xv umap/tests/integration/ clean: rm -f dist/* diff --git a/umap/tests/integration/__init__.py b/umap/tests/integration/__init__.py index e69de29b..ac695ec8 100644 --- a/umap/tests/integration/__init__.py +++ b/umap/tests/integration/__init__.py @@ -0,0 +1,3 @@ +import os + +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "1" From 1975b93ad8f5f043636a43f065fab5f69020c39a Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 4 Sep 2023 15:01:15 +0200 Subject: [PATCH 5/7] Bump playwright and pytest-playwright --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4dce31de..9c7177b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,10 +52,10 @@ dev = [ ] test = [ "factory-boy==3.2.1", - "playwright==1.33.0", + "playwright==1.37.0", "pytest==6.2.5", "pytest-django==4.5.2", - "pytest-playwright==0.3.3", + "pytest-playwright==0.4.2", ] docker = [ From 3a5174c0038b22d3f9d0f886efd172292799fa27 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 4 Sep 2023 15:04:00 +0200 Subject: [PATCH 6/7] Fix tests with playwright Basically: - when using playwright, we use live_server - live_server make django-pytest switch in "transactional db" mode - when in this mode, it will reset all data of all tables in db - we relied on data created during the migrations, so only available for first test run --- umap/tests/conftest.py | 14 +++++++++----- umap/tests/integration/test_basics.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/umap/tests/conftest.py b/umap/tests/conftest.py index 0f3bd6ce..a9a29726 100644 --- a/umap/tests/conftest.py +++ b/umap/tests/conftest.py @@ -5,8 +5,13 @@ import pytest from django.core.cache import cache from django.core.signing import get_cookie_signer -from .base import DataLayerFactory, MapFactory, UserFactory -from umap.models import Licence, TileLayer +from .base import ( + DataLayerFactory, + MapFactory, + UserFactory, + TileLayerFactory, + LicenceFactory, +) TMP_ROOT = tempfile.mkdtemp() @@ -37,8 +42,7 @@ def user2(): @pytest.fixture def licence(): - # Should be created by the migrations. - return Licence.objects.last() + return LicenceFactory() @pytest.fixture @@ -73,4 +77,4 @@ def datalayer(map): @pytest.fixture def tilelayer(): - return TileLayer.objects.last() + return TileLayerFactory() diff --git a/umap/tests/integration/test_basics.py b/umap/tests/integration/test_basics.py index 9f9e6755..206cc785 100644 --- a/umap/tests/integration/test_basics.py +++ b/umap/tests/integration/test_basics.py @@ -18,7 +18,7 @@ def test_create_map_link(page, live_server, lang, link_name, link_url): expect(create_map_button).to_have_attribute("href", link_url) -def test_create_map_with_cursor(page, live_server): +def test_create_map_with_cursor(page, live_server, tilelayer): page.goto(f"{live_server.url}/en/map/new/") # Click on the Draw a marker button on a new map. From 2f7ff46a3877285c1a39a32a57b926d8cee2cc66 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 4 Sep 2023 15:07:43 +0200 Subject: [PATCH 7/7] test: no need for mirations anymore --- pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest.ini b/pytest.ini index b4c5f0e5..d9fcbd49 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] DJANGO_SETTINGS_MODULE=umap.tests.settings -addopts = "--pdbcls=IPython.terminal.debugger:Pdb" +addopts = "--pdbcls=IPython.terminal.debugger:Pdb --no-migrations"