Skip to content

Commit fd5c6fd

Browse files
committed
Add test github workflow
Add test in github action Add local test stack Add 2 TU
1 parent a8be01c commit fd5c6fd

File tree

18 files changed

+502
-0
lines changed

18 files changed

+502
-0
lines changed

.github/workflows/pytest.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: pytest
2+
env:
3+
MODULE_DIR: /home/runner/work/gn_module_ZH/gn_module_ZH/ #${{ github.workspace }}/extra/${{ github.event.repository.name }}
4+
on:
5+
push:
6+
branches:
7+
- main
8+
- develop
9+
- add_pytest
10+
pull_request:
11+
12+
13+
jobs:
14+
build:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
geonature_ref: [
19+
"2.16.0",
20+
"master",
21+
"develop"
22+
] # Mettre version compatible
23+
uses: pnx-si/geonature/.github/workflows/gn-module-pytest.yml@develop
24+
with:
25+
geonature_ref: ${{ matrix.geonature_ref }}
26+
upload_coverage: true
27+
28+
29+
secrets:
30+
codecov_token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ htmlcov/
4242
.coverage
4343
.coverage.*
4444
.cache
45+
cache
4546
nosetests.xml
4647
coverage.xml
4748
*.cover
@@ -106,6 +107,7 @@ venv.bak/
106107

107108
# vscode
108109
.vscode/
110+
.idea
109111
# config module
110112
frontend/app/module.config.ts
111113

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "GeoNature"]
2+
path = GeoNature
3+
url = [email protected]:PnX-SI/GeoNature.git

GeoNature

Submodule GeoNature added at f23db17

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
###########################
2+
# colors #
3+
###########################
4+
PRINT_COLOR = printf
5+
COLOR_SUCCESS = \033[1;32m
6+
COLOR_DEBUG = \033[36m
7+
COLOR_RESET = \033[0m
8+
9+
###########################
10+
# Variables #
11+
###########################
12+
13+
DIR_BIN = .venv/bin/
14+
API_ENDPOINT = http://localhost:8000/api
15+
16+
-include Makefile.perso.mk
17+
18+
###########################
19+
# Environment #
20+
###########################
21+
22+
.PHONY: create_venv
23+
create_venv:
24+
$(call display_cmd, Create venv)
25+
python3 -m venv .venv
26+
27+
.PHONY: install_geonature
28+
install_geonature: create_venv
29+
$(call display_cmd, Install Geonature)
30+
ifeq "$(wildcard .venv/lib/python3.12/site-packages/geonature)" ""
31+
.venv/bin/pip install "geonature[tests] @ git+https://github.com/PnX-SI/GeoNature.git@develop"
32+
endif
33+
34+
###########################
35+
# Tests #
36+
###########################
37+
38+
.PHONY: tests
39+
tests: install_geonature
40+
$(call display_cmd, Launch tests)
41+
.venv/bin/pip install -e .
42+
. .venv/bin/activate && GEONATURE_CONFIG_FILE="config/test_config.toml" ./install_db_test/03b_populate_db.sh
43+
GEONATURE_CONFIG_FILE="config/test_config.toml" .venv/bin/geonature upgrade-modules-db
44+
GEONATURE_CONFIG_FILE="config/test_config.toml" .venv/bin/pytest -vs --cov --cov-report xml
45+
46+
define display_cmd
47+
@$(PRINT_COLOR) "\n$(COLOR_SUCCESS) ########################## $(COLOR_RESET)\n"
48+
@$(PRINT_COLOR) "$(COLOR_SUCCESS) ### $(1) $(COLOR_RESET)\n"
49+
@$(PRINT_COLOR) "$(COLOR_SUCCESS) ########################## $(COLOR_RESET)\n\n"
50+
endef

Makefile.perso.mk

Whitespace-only changes.

backend/gn_module_zh/tests/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from geonature.tests.fixtures import *
2+
from geonature.tests.fixtures import _session, app, users, _app
3+
from geonature.tests.test_permissions import g_permissions
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import datetime
2+
import pytest
3+
from geonature.utils.env import db
4+
5+
from shapely import Polygon
6+
import geoalchemy2
7+
8+
import uuid
9+
10+
def create_zh(
11+
main_name,
12+
code,
13+
id_org,
14+
id_role,
15+
zh_date,
16+
uuid_id_lim_list,
17+
id_sdage,
18+
polygon,
19+
zh_area,
20+
**kwargs,
21+
):
22+
# Import here because TZH class need to be imported after "app instanced"
23+
from gn_module_zh.model.zh_schema import TZH
24+
zh = TZH(
25+
main_name=main_name,
26+
code=code,
27+
id_org=id_org,
28+
create_author=id_role,
29+
update_author=id_role,
30+
create_date=zh_date,
31+
update_date=zh_date,
32+
id_lim_list=uuid_id_lim_list,
33+
id_sdage=id_sdage,
34+
geom=polygon,
35+
area=zh_area,
36+
)
37+
return zh
38+
39+
@pytest.fixture(scope="function")
40+
def zh_data(users):
41+
coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
42+
polygon = Polygon(coords)
43+
date = datetime.datetime(2024, 10, 2, 11, 22, 33)
44+
id_sdage = 967
45+
user = users["self_user"]
46+
data = {}
47+
with db.session.begin_nested():
48+
for (
49+
main_name,
50+
code,
51+
id_org,
52+
id_role,
53+
zh_date,
54+
uuid_id_lim_list,
55+
id_sdage,
56+
geom,
57+
zh_area,
58+
) in [
59+
(
60+
"zh1",
61+
"05CEN0189",
62+
2, # id_org = 2 (not dynamic) because not same table bib_organismes used with the users
63+
user.id_role,
64+
date,
65+
uuid.uuid4(),
66+
id_sdage,
67+
geoalchemy2.shape.from_shape(polygon),
68+
polygon.area,
69+
),
70+
]:
71+
kwargs = {}
72+
s = create_zh(
73+
main_name,
74+
code,
75+
id_org,
76+
id_role,
77+
zh_date,
78+
uuid_id_lim_list,
79+
id_sdage,
80+
geom,
81+
zh_area,
82+
**kwargs,
83+
)
84+
db.session.add(s)
85+
data[main_name] = s
86+
return data
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pypnusershub.tests.utils import set_logged_user
2+
3+
from flask import url_for
4+
5+
from geonature.tests.fixtures import *
6+
from .fixtures import *
7+
8+
@pytest.mark.usefixtures("client_class")
9+
class TestBdd:
10+
def test_no_zh(self, users):
11+
set_logged_user(self.client, users["self_user"])
12+
response = self.client.get(url_for("pr_zh.get_zh"))
13+
assert response.status_code == 200
14+
data = response.get_json()
15+
assert len(data["items"]["features"]) == 0
16+
17+
18+
def test_add_zh(self, users, zh_data):
19+
set_logged_user(self.client, users["self_user"])
20+
response = self.client.get(url_for("pr_zh.get_zh"))
21+
assert response.status_code == 200
22+
data = response.get_json()
23+
assert len(data["items"]["features"]) > 0

0 commit comments

Comments
 (0)