def test_insert_row_master():
    """ One row has inserted in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify master dataset - add a row
    open_layer_and_create_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"),
        "trees",
        "POINT(9 19)",
        {
            "tree_species_id": 1,
            "farm_id": 4
        },
    )

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be one new tree in master and one new tree for Kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=10)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=3)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_row_exists(os.path.join(output_dir, "Kyle.gpkg"), "trees", 1000002)
def test_update_row_master():
    """ One row has been updated in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify master dataset - update a tree (master fid 9 mapped to 1000001 for Kyle)
    open_layer_and_update_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"), "trees", 9,
        {"age_years": 20})

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be the same number of rows as initially
    # and updated age both in master + kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=9)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=2)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_value_equals(os.path.join(output_dir, "master.gpkg"), "trees", 9,
                         "age_years", 20)
    _assert_value_equals(os.path.join(output_dir, "Kyle.gpkg"), "trees",
                         1000001, "age_years", 20)
def test_delete_row_master():
    """ One row deleted in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify a WP dataset - delete a tree (master fid 9 mapped to 1000001 for Kyle)
    open_layer_and_delete_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"), "trees", 9)

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be one tree missing for master and for Kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=8)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=1)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_row_missing(os.path.join(output_dir, "Kyle.gpkg"), "trees",
                        1000001)
def _make_initial_farm_work_packages(config_file):
    """
    1. create the initial "farms" dataset
    2. run the WP algorithm with the initial dataset and given config file
    Returns temporary directory object.
    """
    tmp_dir_obj = TemporaryDirectory(prefix="test-mergin-work-packages-")
    tmp_dir = tmp_dir_obj.name
    os.makedirs(os.path.join(tmp_dir, "input"))

    # get data
    create_farm_dataset(os.path.join(tmp_dir, "input", "master.gpkg"))
    # get config
    wp_config = load_config_from_yaml(
        os.path.join(this_dir, "config-farm-basic.yml"))
    # run alg
    make_work_packages(tmp_dir, wp_config)
    return tmp_dir_obj
Beispiel #5
0
            files.append(filename_relative)
    return files


#
# 1. prepare directory with inputs
#    - fetch master mergin project, read configuration in config.db, copy base files and master input file
#    - fetch WP projects and copy their input files
#

print("Downloading master project " + master_mergin_project + "...")
mc.download_project(master_mergin_project, master_dir)
print("Done.")

print("Reading configuration from " + master_config_yaml)
wp_config = load_config_from_yaml(master_config_yaml)

# Handling removed work packages
wp_names = {f"{wp.name}.gpkg" for wp in wp_config.wp_names}
master_wp_dir = os.path.join(master_dir, "work-packages")
if os.path.exists(master_wp_dir):
    for f in os.listdir(master_wp_dir):
        if f.endswith(".gpkg") and f != "master.gpkg" and f not in wp_names:
            missing_wp_name = f[:-5]  # strip the suffix
            print(
                f"Removing '{missing_wp_name}' work package as it's not used anymore."
            )
            os.remove(os.path.join(master_wp_dir, f))

gpkg_path = wp_config.master_gpkg