Skip to content

Commit

Permalink
Merge pull request #34 from TelesCoop/feat-test
Browse files Browse the repository at this point in the history
Add backend tests
  • Loading branch information
ludovicdmt authored Jan 30, 2025
2 parents c288d14 + 53f370b commit 1faecaa
Show file tree
Hide file tree
Showing 23 changed files with 206,841 additions and 249 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,41 @@ env:

jobs:
setup:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- name: Install python
uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: "pip"

- name: Install PostgreSQL, PostGIS, and GDAL
run: |
sudo apt-get update && sudo apt-get install -y \
postgresql-14 \
postgresql-14-postgis-3 \
postgresql-server-dev-14 \
binutils \
libproj-dev \
gdal-bin \
python3-psycopg2
- name: Install python dependencies
run: pip install -r back/requirements.txt
- name: Start PostgreSQL service
run: |
sudo service postgresql start
- name: Create PostgreSQL user and database
run: |
sudo -u postgres psql -c "CREATE USER travis WITH PASSWORD '42';"
sudo -u postgres psql -c "ALTER USER travis WITH SUPERUSER CREATEDB;"
sudo -u postgres psql -c "CREATE DATABASE iarbre OWNER travis;"
- name: Connect to the database and enable PostGIS
run: |
sudo -u postgres psql -U travis iarbre -c "CREATE EXTENSION postgis;"
- name: Install front js dependencies
run: npm install
working-directory: ./front
Expand Down Expand Up @@ -139,7 +164,7 @@ jobs:
- name: Migrate database
run: python back/manage.py migrate
- name: Load test database
run: python back/manage.py backup_db restore_db test_db
run: python back/manage.py backup_db.py recover_db_and_media --file_media 2025-01-23T16:35_media.zip --file 2025-01-23T16:35_postgres_backup.dump
- name: Run back-end server in background
run: python manage.py runserver localhost:8000 &
working-directory: ./back
Expand All @@ -164,4 +189,4 @@ jobs:
needs: [setup]
steps:
- name: Run documentation validation
run: make validate
run: mkdocs build
5 changes: 4 additions & 1 deletion .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
deploy-prod:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Deploy frontend in prod
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
build:
if: "!contains(github.event.head_commit.message, '[no doc]')"
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion back/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Suivez le [guide d'installation Django GIS](https://docs.djangoproject.com/en/5.
Vous pouvez également essayer d'installer les packages requis via `apt`, bien que cela puisse ne pas toujours suffire :

```bash
sudo apt install postgresql-x postgresql-x-postgis-3 postgresql-server-dev-x python3-psycopg
sudo apt install postgresql-x postgresql-x-postgis-3 postgresql-server-dev-x python3-psycopg2
sudo apt install binutils libproj-dev gdal-bin # Pour les requêtes géographiques
```

Expand Down
27 changes: 17 additions & 10 deletions back/api/utils/mvt_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from tqdm import tqdm
import gc

MVT_EXTENT = 4096


class MVTGenerator:
def __init__(
Expand Down Expand Up @@ -49,7 +51,6 @@ def generate_tiles(self):
"""Generate MVT tiles for the entire geometry queryset."""
# Get total bounds of the queryset
bounds = self._get_queryset_bounds()

for zoom in range(self.min_zoom, self.max_zoom + 1):
print(f"Generating tiles for zoom level {zoom}")
# Get all tiles that cover the entire geometry bounds
Expand All @@ -72,7 +73,7 @@ def generate_tiles(self):
}
for future in as_completed(future_to_tiles):
future.result()
future_to_tiles.pop(future)
future_to_tiles.pop(future) # Free RAM after completion
gc.collect() # just to be sure gc is called...

def _get_queryset_bounds(self) -> Dict[str, float]:
Expand Down Expand Up @@ -147,25 +148,31 @@ def _prepare_mvt_features(queryset, tile_polygon) -> List[Dict[str, Any]]:
Returns:
List of features to encode in MVT format.
"""
MVT_EXTENT = 4096
features = []
(x0, y0, x_max, y_max) = tile_polygon.extent
x_span = x_max - x0
y_span = y_max - y0
for obj in tqdm(queryset, desc="Preparing MVT features", total=len(queryset)):
clipped_geom = obj.clipped_geometry
if not clipped_geom.empty:
tile_based_coords = [] # Transform geometry in tile relative coordinate
for x_merc, y_merc in clipped_geom.coords[0]:
tile_based_coord = (
int((x_merc - x0) * MVT_EXTENT / x_span),
int((y_merc - y0) * MVT_EXTENT / y_span),
)
tile_based_coords.append(tile_based_coord)
tile_based_coords = MVTGenerator.transform_to_tile_relative(
clipped_geom, x0, y0, x_span, y_span
)
feature = {
"geometry": Polygon_shapely(tile_based_coords),
"properties": obj.get_layer_properties(),
}
features.append(feature)

return features

@staticmethod
def transform_to_tile_relative(clipped_geom, x0, y0, x_span, y_span):
tile_based_coords = [] # Transform geometry in tile relative coordinate
for x_merc, y_merc in clipped_geom.coords[0]:
tile_based_coord = (
int((x_merc - x0) * MVT_EXTENT / x_span),
int((y_merc - y0) * MVT_EXTENT / y_span),
)
tile_based_coords.append(tile_based_coord)
return tile_based_coords
Loading

0 comments on commit 1faecaa

Please sign in to comment.