Skip to content

Commit

Permalink
Merge pull request #33 from MerginMaps/fix-26
Browse files Browse the repository at this point in the history
Fix 26
  • Loading branch information
JanCaha authored Mar 20, 2024
2 parents c70ecd7 + ed127bb commit 51fae51
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 17 deletions.
12 changes: 10 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ def validate_config(config):
if not (config.allowed_extensions and len(config.allowed_extensions)):
raise ConfigError("Config error: Allowed extensions can not be empty")

if not (config.references and len(config.references)):
raise ConfigError("Config error: References list can not be empty")
if "references" not in config:
config.update({"references": []})

if config.references is None:
config.update({"references": []})

if not isinstance(config.references, list):
raise ConfigError(
"Config error: Incorrect reference settings. Needs to be list of references."
)

for ref in config.references:
if not all(
Expand Down
37 changes: 23 additions & 14 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,33 @@ def _reset_config():
"MINIO__ACCESS_KEY": MINIO_ACCESS_KEY,
"MINIO__SECRET_KEY": MINIO_SECRET_KEY,
"MINIO__BUCKET": "test",
"BASE_PATH": "",
}
)


def test_config():
# valid config
_reset_config()
validate_config(config)

_reset_config()
config.update({"REFERENCES": None})
validate_config(config)

_reset_config()
config.update(
{
"REFERENCES": [
{
"file": "survey.gpkg",
"table": "table",
"local_path_column": "local_path_column",
"driver_path_column": "driver_path_column",
}
],
"BASE_PATH": "",
]
}
)


def test_config():
# valid config
_reset_config()
validate_config(config)

with pytest.raises(ConfigError, match="Config error: Incorrect mergin settings"):
Expand Down Expand Up @@ -84,13 +95,6 @@ def test_config():
config.update({"ALLOWED_EXTENSIONS": []})
validate_config(config)

_reset_config()
with pytest.raises(
ConfigError, match="Config error: References list can not be empty"
):
config.update({"REFERENCES": []})
validate_config(config)

_reset_config()
with pytest.raises(
ConfigError, match="Config error: Incorrect media reference settings"
Expand All @@ -102,3 +106,8 @@ def test_config():
with pytest.raises(ConfigError, match="Config error: Unsupported operation mode"):
config.update({"OPERATION_MODE": ""})
validate_config(config)

_reset_config()
with pytest.raises(ConfigError, match="Config error: Incorrect reference settings"):
config.update({"REFERENCES": "text"})
validate_config(config)
63 changes: 62 additions & 1 deletion test/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
WORKSPACE,
TMP_DIR,
USER_PWD,
USER_PWD,
SERVER_URL,
MINIO_URL,
MINIO_ACCESS_KEY,
Expand Down Expand Up @@ -493,3 +492,65 @@ def test_multiple_tables(mc):
sql = f"SELECT count(*) FROM {config.references[1].table} WHERE {config.references[1].driver_path_column}='{copied_file}'"
gpkg_cur.execute(sql)
assert gpkg_cur.fetchone()[0] == 1


@pytest.mark.parametrize(
"project_name,config_update",
[
(
"mediasync_test_without_references",
{},
),
(
"mediasync_test_with_references_none",
{"REFERENCES": None},
),
(
"mediasync_test_with_references_empty_list",
{"REFERENCES": []},
),
],
)
def test_sync_without_references(mc, project_name: str, config_update: dict):
"""
Test media sync running sync without references. It should not fail and just copy files.
The test just checks that main() runs without errors.
"""
full_project_name = WORKSPACE + "/" + project_name
work_project_dir = os.path.join(
TMP_DIR, project_name + "_work"
) # working dir for mediasync
driver_dir = os.path.join(
TMP_DIR, project_name + "_driver"
) # destination dir for 'local' driver

cleanup(mc, full_project_name, [work_project_dir, driver_dir])
prepare_mergin_project(mc, full_project_name)

config.update(
{
"ALLOWED_EXTENSIONS": ["png", "jpg"],
"MERGIN__USERNAME": API_USER,
"MERGIN__PASSWORD": USER_PWD,
"MERGIN__URL": SERVER_URL,
"MERGIN__PROJECT_NAME": full_project_name,
"PROJECT_WORKING_DIR": work_project_dir,
"DRIVER": "local",
"LOCAL__DEST": driver_dir,
"OPERATION_MODE": "copy",
"BASE_PATH": None,
}
)
config.update(config_update)

main()

gpkg_conn = sqlite3.connect(os.path.join(work_project_dir, "survey.gpkg"))
gpkg_cur = gpkg_conn.cursor()
sql = "SELECT count(*) FROM photos WHERE ext_url IS NULL"
gpkg_cur.execute(sql)
assert gpkg_cur.fetchone()[0] == 1

sql = "SELECT count(*) FROM notes WHERE ext_url IS NULL"
gpkg_cur.execute(sql)
assert gpkg_cur.fetchone()[0] == 3

0 comments on commit 51fae51

Please sign in to comment.