|
| 1 | +import shutil |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from django.core.management import call_command |
| 6 | + |
| 7 | +from rdmo.projects.models import Membership, Project |
| 8 | +from rdmo.projects.tests.helpers.xml import add_memberships_to_xml |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.django_db |
| 12 | +@pytest.mark.parametrize("include_memberships", [True, False]) |
| 13 | +@pytest.mark.parametrize("xml_has_memberships", [True, False]) |
| 14 | +def test_import_projects_memberships_toggle(tmp_path, project_xml, capsys, include_memberships, xml_has_memberships): |
| 15 | + |
| 16 | + xml_path = tmp_path / "project_with_members.xml" |
| 17 | + shutil.copyfile(project_xml, xml_path) |
| 18 | + |
| 19 | + if xml_has_memberships: |
| 20 | + add_memberships_to_xml( |
| 21 | + str(xml_path), |
| 22 | + members=[ |
| 23 | + {"role": "owner", "user": {"username": "owner"}}, |
| 24 | + {"role": "author", "user": {"username": "author"}}, |
| 25 | + {"role": "guest", "user": {"username": "guest"}}, |
| 26 | + ], |
| 27 | + ) |
| 28 | + |
| 29 | + before = Project.objects.count() |
| 30 | + |
| 31 | + args = ["import_projects", "--files", str(xml_path)] |
| 32 | + if include_memberships: |
| 33 | + args.append("--include-memberships") |
| 34 | + |
| 35 | + call_command(*args) |
| 36 | + out = capsys.readouterr().out |
| 37 | + assert "→ Importing" in out |
| 38 | + assert "imported successfully" in out |
| 39 | + |
| 40 | + assert Project.objects.count() == before + 1 |
| 41 | + new_project = Project.objects.order_by("-pk").first() |
| 42 | + |
| 43 | + expected = {("owner", "owner"), ("author", "author"), ("guest", "guest")} |
| 44 | + if include_memberships and xml_has_memberships: |
| 45 | + actual = { |
| 46 | + (m.user.username, m.role) |
| 47 | + for m in Membership.objects.filter(project=new_project).select_related("user") |
| 48 | + } |
| 49 | + assert actual == expected |
| 50 | + else: |
| 51 | + assert not Membership.objects.filter(project=new_project).exists() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.django_db |
| 55 | +def test_import_projects_from_files_explicit(tmp_path, project_xml, capsys): |
| 56 | + """Import a project via explicit --files path (no memberships).""" |
| 57 | + xml_path = tmp_path / "project.xml" |
| 58 | + shutil.copyfile(project_xml, xml_path) |
| 59 | + |
| 60 | + before = Project.objects.count() |
| 61 | + call_command("import_projects", "--files", str(xml_path)) |
| 62 | + |
| 63 | + out = capsys.readouterr().out |
| 64 | + assert "→ Importing" in out |
| 65 | + assert "imported successfully" in out |
| 66 | + assert Project.objects.count() == before + 1 |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.django_db |
| 70 | +def test_import_projects_dir_scan_with_filter_and_missing(tmp_path, project_xml, capsys): |
| 71 | + """ |
| 72 | + Import via --dir with one valid numeric folder and one missing. |
| 73 | + Expect a warning and a successful import of the existing one. |
| 74 | + """ |
| 75 | + good_id = 42 |
| 76 | + (tmp_path / str(good_id)).mkdir(parents=True) |
| 77 | + shutil.copyfile(project_xml, tmp_path / str(good_id) / "project.xml") |
| 78 | + (tmp_path / "999999").mkdir() # empty folder |
| 79 | + |
| 80 | + before = Project.objects.count() |
| 81 | + call_command("import_projects", "--dir", str(tmp_path), "--projects", str(good_id), "999999") |
| 82 | + |
| 83 | + out = capsys.readouterr().out |
| 84 | + assert ("Some projects could not be found" in out) or ('No XML file found' in out) |
| 85 | + assert "imported successfully" in out |
| 86 | + assert Project.objects.count() == before + 1 |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.django_db |
| 90 | +def test_import_projects_files_skips_non_xml(tmp_path, project_xml, capsys): |
| 91 | + """Passing a non-XML alongside a valid XML: skip with warning, still import XML.""" |
| 92 | + good_xml = tmp_path / "project.xml" |
| 93 | + shutil.copyfile(project_xml, good_xml) |
| 94 | + |
| 95 | + junk = tmp_path / "notes.txt" |
| 96 | + junk.write_text("hello") |
| 97 | + |
| 98 | + before = Project.objects.count() |
| 99 | + call_command("import_projects", "--files", str(junk), str(good_xml)) |
| 100 | + |
| 101 | + out = capsys.readouterr().out |
| 102 | + assert "Skipping non-XML file" in out |
| 103 | + assert "imported successfully" in out |
| 104 | + assert Project.objects.count() == before + 1 |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.django_db |
| 108 | +@pytest.mark.parametrize("export_include_memberships", [True, False]) |
| 109 | +@pytest.mark.parametrize("import_include_memberships", [True, False]) |
| 110 | +def test_roundtrip_export_then_import_memberships_toggle( |
| 111 | + tmp_path, capsys, export_include_memberships, import_include_memberships |
| 112 | +): |
| 113 | + """ |
| 114 | + Round-trip: export project id=1 to XML, then import it back. |
| 115 | + Memberships in the imported project should exist iff BOTH: |
| 116 | + - export included memberships, and |
| 117 | + - import requested memberships. |
| 118 | + """ |
| 119 | + project = Project.objects.get(id=1) |
| 120 | + src_members = set( |
| 121 | + Membership.objects.filter(project=project) |
| 122 | + .values_list("user_id", "role") |
| 123 | + ) |
| 124 | + # --- export --- |
| 125 | + export_args = [ |
| 126 | + "export_projects", |
| 127 | + "--projects", str(project.id), |
| 128 | + "--export-mode", "project", |
| 129 | + "--format", "xml", |
| 130 | + "--path", str(tmp_path), |
| 131 | + ] |
| 132 | + if export_include_memberships: |
| 133 | + assert src_members, "should not be empty here" |
| 134 | + export_args.append("--include-memberships") |
| 135 | + |
| 136 | + call_command(*export_args) |
| 137 | + |
| 138 | + out = capsys.readouterr().out |
| 139 | + assert "Exported 1 project(s) to" in out |
| 140 | + |
| 141 | + # --- import --- |
| 142 | + import_args = ["import_projects", "--dir", str(tmp_path)] |
| 143 | + if import_include_memberships: |
| 144 | + import_args.append("--include-memberships") |
| 145 | + |
| 146 | + before_count = Project.objects.count() |
| 147 | + call_command(*import_args) |
| 148 | + |
| 149 | + out = capsys.readouterr().out |
| 150 | + assert "→ Importing" in out |
| 151 | + assert "imported successfully" in out |
| 152 | + |
| 153 | + # a new project must have been created |
| 154 | + assert Project.objects.count() == before_count + 1 |
| 155 | + imported = Project.objects.exclude(pk=project.pk).order_by("-pk").first() |
| 156 | + |
| 157 | + imported_members = set( |
| 158 | + Membership.objects.filter(project=imported) |
| 159 | + .values_list("user_id", "role") |
| 160 | + ) |
| 161 | + |
| 162 | + expect_members = export_include_memberships and import_include_memberships |
| 163 | + |
| 164 | + if expect_members: |
| 165 | + # imported memberships should match the source memberships (set compare) |
| 166 | + assert imported_members, "should not be empty here" |
| 167 | + assert imported_members == src_members |
| 168 | + else: |
| 169 | + # no memberships should have been imported |
| 170 | + assert imported_members == set() |
0 commit comments