Skip to content

Commit

Permalink
Fix tests (#130)
Browse files Browse the repository at this point in the history
* remove function from tests

* fix test

* cleanup update with delete_project_now

* black style

* update project delete

* add init_sync_from_db and functions

* tests for init from db

* tests data

* update tests

* update description

* use functions to make test more general

* skip test due to changes in MM

* use functions

* simplify

* add docstrings

* docstring

* sql file as parameter

* constant schema names

* update sql files for tests

* simplify tests according to review

* do not skip test

* install from requirements files

* remove

* require versions

* files that trigger testing

* run only basic tests

* fix project info obtaining using updated mergin-client api

* correctly access project info

* need to actively read project metadata, otherwise old info is stored

* correctly convert to list

* Revert "run only basic tests"

This reverts commit 57bcc93.

* removed deprecated metadata calls

* use full project name

* fix skip tables

* fix ignored tables setup

* install using requirements.txt

---------

Co-authored-by: Jan Caha <[email protected]>
  • Loading branch information
JanCaha and Jan Caha authored Sep 4, 2024
1 parent a861896 commit b84d41d
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 100 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build_windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ jobs:

- name: Install dependencies
run: |
python -m pip install dynaconf pyinstaller mergin-client psycopg2
python -m pip install -r requirements.txt
python -m pip install pyinstaller
- name: Build Binary
run: |
Expand Down
11 changes: 8 additions & 3 deletions .github/workflows/tests_mergin_db_sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
paths:
- "test/**"
- "**.py"
- "requirements.txt"
- "requirements-dev.txt"
- "pyproject.toml"
- ".github/workflows/tests_mergin_db_sync.yaml"

env:
Expand Down Expand Up @@ -46,14 +49,16 @@ jobs:
- name: Check Geodiff version
run: geodiff version

- name: Checkout
uses: actions/checkout@v4

- name: Install Python dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install mergin-client pytest pytest-cov dynaconf psycopg2
python3 -m pip install -r requirements.txt
python3 -m pip install -r requirements-dev.txt
- name: Checkout
uses: actions/checkout@v2
- name: Run tests
run: |
Expand Down
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tempfile

from dynaconf import Dynaconf
import dynaconf

from smtp_functions import create_connection_and_log_user

Expand Down Expand Up @@ -158,6 +159,10 @@ def get_ignored_tables(
connection.skip_tables,
list,
):
if len(connection.skip_tables) < 1:
return []
elif isinstance(connection.skip_tables, dynaconf.vendor.box.box_list.BoxList):
return connection.skip_tables.to_list()
return connection.skip_tables
else:
return []
Expand Down
40 changes: 16 additions & 24 deletions dbsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,7 @@ def _print_mergin_changes(
cached_mergin_project_objects = {}


def _get_mergin_project(
work_path,
):
def _get_mergin_project(work_path) -> MerginProject:
"""
Returns a cached MerginProject object or creates one if it does not exist yet.
This is to avoid creating many of these objects (e.g. every pull/push) because it does
Expand All @@ -538,23 +536,20 @@ def _get_mergin_project(
"""
if work_path not in cached_mergin_project_objects:
cached_mergin_project_objects[work_path] = MerginProject(work_path)
cached_mergin_project_objects[work_path]._read_metadata()
return cached_mergin_project_objects[work_path]


def _get_project_version(
work_path,
):
def _get_project_version(work_path) -> str:
"""Returns the current version of the project"""
mp = _get_mergin_project(work_path)
return mp.metadata["version"]
return mp.version()


def _get_project_id(
mp,
):
def _get_project_id(mp: MerginProject):
"""Returns the project ID"""
try:
project_id = uuid.UUID(mp.metadata["project_id"])
project_id = uuid.UUID(mp.project_id())
except (
KeyError,
ValueError,
Expand Down Expand Up @@ -636,10 +631,9 @@ def _validate_local_project_id(
local_project_id = _get_project_id(mp)
if local_project_id is None:
return
project_path = mp.metadata["name"]
if server_info is None:
try:
server_info = mc.project_info(project_path)
server_info = mc.project_info(mp.project_full_name())
except ClientError as e:
raise DbSyncError("Mergin Maps client error: " + str(e))

Expand Down Expand Up @@ -718,7 +712,7 @@ def revert_local_changes(
mp.dir,
update_delete_file,
update_delete_filepath,
mp.metadata["version"],
mp.version(),
)
except ClientError as e:
raise DbSyncError("Mergin Maps client error: " + str(e))
Expand Down Expand Up @@ -754,12 +748,11 @@ def pull(conn_cfg, mc):
# Make sure that local project ID (if available) is the same as on the server
_validate_local_project_id(mp, mc)

project_path = mp.metadata["name"]
local_version = mp.metadata["version"]
local_version = mp.version()

try:
projects = mc.get_projects_by_names([project_path])
server_version = projects[project_path]["version"]
projects = mc.get_projects_by_names([mp.project_full_name()])
server_version = projects[mp.project_full_name()]["version"]
except ClientError as e:
# this could be e.g. DNS error
raise DbSyncError("Mergin Maps client error: " + str(e))
Expand Down Expand Up @@ -900,8 +893,8 @@ def status(conn_cfg, mc):
mp.set_tables_to_skip(ignored_tables)
if mp.geodiff is None:
raise DbSyncError("Mergin Maps client installation problem: geodiff not available")
project_path = mp.metadata["name"]
local_version = mp.metadata["version"]
project_path = mp.project_full_name()
local_version = mp.version()
logging.debug("Checking status...")
try:
server_info = mc.project_info(
Expand Down Expand Up @@ -1009,12 +1002,11 @@ def push(conn_cfg, mc):
# Make sure that local project ID (if available) is the same as on the server
_validate_local_project_id(mp, mc)

project_path = mp.metadata["name"]
local_version = mp.metadata["version"]
local_version = mp.version()

try:
projects = mc.get_projects_by_names([project_path])
server_version = projects[project_path]["version"]
projects = mc.get_projects_by_names([mp.project_full_name()])
server_version = projects[mp.project_full_name()]["version"]
except ClientError as e:
# this could be e.g. DNS error
raise DbSyncError("Mergin Maps client error: " + str(e))
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest>=6.2
pytest-cov>=3.0
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mergin-client>=0.8.3
mergin-client==0.9.0
dynaconf>=3.1
psycopg2>=2.9
Loading

0 comments on commit b84d41d

Please sign in to comment.