tro-utils 0.4.3__tar.gz → 0.4.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. {tro_utils-0.4.3 → tro_utils-0.4.5}/.gitignore +6 -0
  2. {tro_utils-0.4.3 → tro_utils-0.4.5}/PKG-INFO +1 -1
  3. tro_utils-0.4.5/tests/conftest.py +100 -0
  4. tro_utils-0.4.5/tests/helpers.py +29 -0
  5. {tro_utils-0.4.3 → tro_utils-0.4.5}/tests/test_cli.py +108 -0
  6. tro_utils-0.4.5/tests/test_extra_context.py +93 -0
  7. tro_utils-0.4.5/tests/test_models_artifact_arrangement.py +218 -0
  8. tro_utils-0.4.5/tests/test_models_artifact_composition.py +57 -0
  9. tro_utils-0.4.5/tests/test_models_artifact_location.py +18 -0
  10. tro_utils-0.4.5/tests/test_models_composition_fingerprint.py +23 -0
  11. tro_utils-0.4.5/tests/test_models_hash_value.py +64 -0
  12. tro_utils-0.4.5/tests/test_models_performance.py +79 -0
  13. tro_utils-0.4.5/tests/test_models_research_artifact.py +59 -0
  14. tro_utils-0.4.5/tests/test_models_transparent_research_object.py +144 -0
  15. tro_utils-0.4.5/tests/test_models_trs.py +57 -0
  16. tro_utils-0.4.5/tests/test_models_tsa.py +22 -0
  17. tro_utils-0.4.5/tests/test_replication_package_model.py +96 -0
  18. tro_utils-0.4.5/tests/test_replication_package_verification.py +408 -0
  19. tro_utils-0.4.5/tests/test_tro_arrangements.py +190 -0
  20. tro_utils-0.4.5/tests/test_tro_creation.py +79 -0
  21. tro_utils-0.4.5/tests/test_tro_hashing.py +44 -0
  22. tro_utils-0.4.5/tests/test_tro_performances.py +246 -0
  23. tro_utils-0.4.5/tests/test_tro_reporting.py +67 -0
  24. tro_utils-0.4.5/tests/test_tro_save_load.py +68 -0
  25. tro_utils-0.4.5/tests/test_tro_signing.py +59 -0
  26. tro_utils-0.4.5/tests/test_tro_timestamping.py +104 -0
  27. tro_utils-0.4.5/tests/test_tro_utility_methods.py +40 -0
  28. tro_utils-0.4.5/tests/test_tro_workflow.py +180 -0
  29. tro_utils-0.4.5/tests/test_version.py +25 -0
  30. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/_version.py +3 -3
  31. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/cli.py +37 -0
  32. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/tro.py +21 -9
  33. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/tro_utils.py +4 -0
  34. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/PKG-INFO +1 -1
  35. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/SOURCES.txt +26 -1
  36. tro_utils-0.4.3/tests/test_tro_utils.py +0 -2365
  37. {tro_utils-0.4.3 → tro_utils-0.4.5}/.coveragerc +0 -0
  38. {tro_utils-0.4.3 → tro_utils-0.4.5}/.editorconfig +0 -0
  39. {tro_utils-0.4.3 → tro_utils-0.4.5}/.github/ISSUE_TEMPLATE.md +0 -0
  40. {tro_utils-0.4.3 → tro_utils-0.4.5}/.github/workflows/release.yml +0 -0
  41. {tro_utils-0.4.3 → tro_utils-0.4.5}/.github/workflows/test.yml +0 -0
  42. {tro_utils-0.4.3 → tro_utils-0.4.5}/.readthedocs.yaml +0 -0
  43. {tro_utils-0.4.3 → tro_utils-0.4.5}/.travis.yml +0 -0
  44. {tro_utils-0.4.3 → tro_utils-0.4.5}/LICENSE +0 -0
  45. {tro_utils-0.4.3 → tro_utils-0.4.5}/MANIFEST.in +0 -0
  46. {tro_utils-0.4.3 → tro_utils-0.4.5}/Makefile +0 -0
  47. {tro_utils-0.4.3 → tro_utils-0.4.5}/README.md +0 -0
  48. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/Makefile +0 -0
  49. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/authors.md +0 -0
  50. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/conf.py +0 -0
  51. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/contributing.md +0 -0
  52. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/history.md +0 -0
  53. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/index.md +0 -0
  54. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/installation.md +0 -0
  55. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/make.bat +0 -0
  56. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/readme.md +0 -0
  57. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/requirements.txt +0 -0
  58. {tro_utils-0.4.3 → tro_utils-0.4.5}/docs/usage.md +0 -0
  59. {tro_utils-0.4.3 → tro_utils-0.4.5}/pyproject.toml +0 -0
  60. {tro_utils-0.4.3 → tro_utils-0.4.5}/pytest.ini +0 -0
  61. {tro_utils-0.4.3 → tro_utils-0.4.5}/requirements_dev.txt +0 -0
  62. {tro_utils-0.4.3 → tro_utils-0.4.5}/setup.cfg +0 -0
  63. {tro_utils-0.4.3 → tro_utils-0.4.5}/tests/__init__.py +0 -0
  64. {tro_utils-0.4.3 → tro_utils-0.4.5}/tox.ini +0 -0
  65. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/__init__.py +0 -0
  66. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/default.jinja2 +0 -0
  67. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/__init__.py +0 -0
  68. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/_base.py +0 -0
  69. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/arrangement.py +0 -0
  70. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/artifact.py +0 -0
  71. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/attribute.py +0 -0
  72. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/composition.py +0 -0
  73. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/hash_value.py +0 -0
  74. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/performance.py +0 -0
  75. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/trs.py +0 -0
  76. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/models/tsa.py +0 -0
  77. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils/replication_package.py +0 -0
  78. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/dependency_links.txt +0 -0
  79. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/entry_points.txt +0 -0
  80. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/requires.txt +0 -0
  81. {tro_utils-0.4.3 → tro_utils-0.4.5}/tro_utils.egg-info/top_level.txt +0 -0
@@ -1,4 +1,10 @@
1
+ example/
2
+ *.jsonld
3
+ *.sig
4
+ *.tsr
5
+
1
6
  # Byte-compiled / optimized / DLL files
7
+ tro_utils/_version.py
2
8
  __pycache__/
3
9
  *.py[cod]
4
10
  *$py.class
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tro-utils
3
- Version: 0.4.3
3
+ Version: 0.4.5
4
4
  Summary: Utilities for creating, editing and interacting with TROs
5
5
  Author-email: Kacper Kowalik <xarthisius.kk@gmail.com>, Craig Willis <willis8@illinois.edu>
6
6
  Maintainer-email: Kacper Kowalik <xarthisius.kk@gmail.com>
@@ -0,0 +1,100 @@
1
+ """Shared fixtures for tro_utils tests."""
2
+
3
+ import json
4
+
5
+ import gnupg
6
+ import pytest
7
+
8
+
9
+ # ---------------------------------------------------------------------------
10
+ # Fixtures
11
+ # ---------------------------------------------------------------------------
12
+
13
+
14
+ @pytest.fixture
15
+ def temp_workspace(tmp_path):
16
+ """Create a temporary workspace with sample files."""
17
+ workspace = tmp_path / "workspace"
18
+ workspace.mkdir()
19
+
20
+ # Create sample CSV file with mock data
21
+ csv_file = workspace / "input_data.csv"
22
+ csv_file.write_text("name,value,category\nitem1,100,A\nitem2,200,B\nitem3,150,A\n")
23
+
24
+ # Create a text file with some initial content
25
+ txt_file = workspace / "notes.txt"
26
+ txt_file.write_text("Initial notes\n")
27
+
28
+ # Create a config file
29
+ config_file = workspace / "config.json"
30
+ config_file.write_text('{"threshold": 150, "mode": "filter"}')
31
+
32
+ return workspace
33
+
34
+
35
+ @pytest.fixture(scope="session")
36
+ def gpg_setup(tmp_path_factory):
37
+ """Set up a temporary GPG environment for testing (once per session)."""
38
+ gpg_home = tmp_path_factory.mktemp("gnupg")
39
+ gpg_home.chmod(0o700)
40
+
41
+ # Create GPG instance
42
+ gpg = gnupg.GPG(gnupghome=str(gpg_home))
43
+
44
+ # Generate a test key (this is slow, so we only do it once)
45
+ input_data = gpg.gen_key_input(
46
+ key_type="RSA",
47
+ key_length=2048,
48
+ name_real="Test User",
49
+ name_email="test@example.com",
50
+ passphrase="test_passphrase",
51
+ )
52
+ key = gpg.gen_key(input_data)
53
+
54
+ # Get the key fingerprint and keyid
55
+ # Note: gnupg library doesn't populate key_map on initial generation
56
+ # We need to use the key result directly
57
+ keys = gpg.list_keys()
58
+ fingerprint = str(key) # The gen_key returns the fingerprint
59
+
60
+ # Find the keyid from the keys list
61
+ keyid = None
62
+ for k in keys:
63
+ if k["fingerprint"] == fingerprint:
64
+ keyid = k["keyid"]
65
+ break
66
+
67
+ return {
68
+ "gpg_home": str(gpg_home),
69
+ "gpg": gpg,
70
+ "fingerprint": fingerprint,
71
+ "keyid": keyid,
72
+ "passphrase": "test_passphrase",
73
+ }
74
+
75
+
76
+ @pytest.fixture(scope="session")
77
+ def trs_profile(tmp_path_factory):
78
+ """Create a TRS profile file (once per session)."""
79
+ profile_dir = tmp_path_factory.mktemp("profiles")
80
+ profile_file = profile_dir / "trs.jsonld"
81
+ profile_data = {
82
+ "rdfs:comment": "Test TRS for testing purposes",
83
+ "trov:hasCapability": [
84
+ {"@id": "trs/capability/1", "@type": "trov:CanRecordInternetAccess"},
85
+ {"@id": "trs/capability/2", "@type": "trov:CanProvideInternetIsolation"},
86
+ ],
87
+ "trov:owner": "Test User",
88
+ "trov:description": "Test TRS",
89
+ "trov:contact": "test@example.com",
90
+ "trov:url": "http://localhost/",
91
+ "trov:name": "test-trs",
92
+ }
93
+ profile_file.write_text(json.dumps(profile_data, indent=2))
94
+ return str(profile_file)
95
+
96
+
97
+ @pytest.fixture
98
+ def tro_declaration(tmp_path):
99
+ """Return path for TRO declaration file."""
100
+ return str(tmp_path / "test_tro.jsonld")
@@ -0,0 +1,29 @@
1
+ """Shared helper functions for tro_utils tests."""
2
+
3
+ import os
4
+
5
+ from tro_utils.tro_utils import TRO
6
+
7
+
8
+ def create_tro_with_gpg(filepath, gpg_setup, **kwargs):
9
+ """Helper to create TRO with proper GPG configuration."""
10
+ # Set GPG_HOME environment variable
11
+ os.environ["GPG_HOME"] = gpg_setup["gpg_home"]
12
+
13
+ # Temporarily remove gpg_fingerprint from kwargs to avoid key_map lookup error
14
+ gpg_fingerprint = kwargs.pop("gpg_fingerprint", None)
15
+
16
+ # Create TRO instance without fingerprint first
17
+ tro = TRO(filepath=filepath, **kwargs)
18
+
19
+ # Now manually set up the GPG key if fingerprint was provided
20
+ # This works around the key_map issue in the gnupg library
21
+ if gpg_fingerprint:
22
+ tro.gpg = gpg_setup["gpg"]
23
+ tro.gpg_key_id = gpg_setup["keyid"]
24
+ tro.data["@graph"][0]["trov:wasAssembledBy"]["trov:publicKey"] = (
25
+ tro.gpg.export_keys(tro.gpg_key_id)
26
+ )
27
+ tro.gpg_passphrase = kwargs.get("gpg_passphrase")
28
+
29
+ return tro
@@ -661,3 +661,111 @@ class TestCLIPerformance:
661
661
  by_id = {r["trov:arrangement"]["@id"]: r for r in accessed}
662
662
  assert by_id["arrangement/0"]["trov:boundTo"] == "/mnt/a"
663
663
  assert "trov:boundTo" not in by_id["arrangement/1"]
664
+
665
+
666
+ class TestExtraContextCLI:
667
+ """Tests for --extra-context CLI option."""
668
+
669
+ def test_extra_context_prefix_mapping(
670
+ self, runner, tmp_path, temp_workspace, trs_profile
671
+ ):
672
+ """--extra-context PREFIX=URI adds a prefix mapping dict to @context."""
673
+ tro_file = tmp_path / "tro.jsonld"
674
+ result = runner.invoke(
675
+ cli,
676
+ [
677
+ "--declaration",
678
+ str(tro_file),
679
+ "--profile",
680
+ trs_profile,
681
+ "--extra-context",
682
+ "ex=http://example.org/",
683
+ "arrangement",
684
+ "add",
685
+ "--comment",
686
+ "test",
687
+ str(temp_workspace),
688
+ ],
689
+ )
690
+ assert result.exit_code == 0, result.output
691
+ with open(tro_file) as f:
692
+ data = json.load(f)
693
+ assert data["@context"]["ex"] == "http://example.org/"
694
+
695
+ def test_extra_context_multiple(
696
+ self, runner, tmp_path, temp_workspace, trs_profile
697
+ ):
698
+ """Multiple --extra-context PREFIX=URI flags all appear in @context."""
699
+ tro_file = tmp_path / "tro.jsonld"
700
+ result = runner.invoke(
701
+ cli,
702
+ [
703
+ "--declaration",
704
+ str(tro_file),
705
+ "--profile",
706
+ trs_profile,
707
+ "--extra-context",
708
+ "ex=http://example.org/",
709
+ "--extra-context",
710
+ "foaf=http://xmlns.com/foaf/0.1/",
711
+ "arrangement",
712
+ "add",
713
+ str(temp_workspace),
714
+ ],
715
+ )
716
+ assert result.exit_code == 0, result.output
717
+ with open(tro_file) as f:
718
+ data = json.load(f)
719
+ assert data["@context"]["ex"] == "http://example.org/"
720
+ assert data["@context"]["foaf"] == "http://xmlns.com/foaf/0.1/"
721
+
722
+ def test_extra_context_invalid_format(
723
+ self, runner, tmp_path, temp_workspace, trs_profile
724
+ ):
725
+ """--extra-context value that is not PREFIX=URI produces an error."""
726
+ tro_file = tmp_path / "tro.jsonld"
727
+ result = runner.invoke(
728
+ cli,
729
+ [
730
+ "--declaration",
731
+ str(tro_file),
732
+ "--profile",
733
+ trs_profile,
734
+ "--extra-context",
735
+ "https://example.org/vocab.jsonld",
736
+ "arrangement",
737
+ "add",
738
+ str(temp_workspace),
739
+ ],
740
+ )
741
+ assert result.exit_code != 0
742
+
743
+ def test_extra_context_base_context_preserved(
744
+ self, runner, tmp_path, temp_workspace, trs_profile
745
+ ):
746
+ """The standard base @context entry is always present even with extra context."""
747
+ tro_file = tmp_path / "tro.jsonld"
748
+ runner.invoke(
749
+ cli,
750
+ [
751
+ "--declaration",
752
+ str(tro_file),
753
+ "--profile",
754
+ trs_profile,
755
+ "--extra-context",
756
+ "ex=http://example.org/",
757
+ "arrangement",
758
+ "add",
759
+ str(temp_workspace),
760
+ ],
761
+ )
762
+ with open(tro_file) as f:
763
+ data = json.load(f)
764
+ base = data["@context"]
765
+ assert "trov" in base
766
+
767
+ def test_cli_help_shows_extra_context(self, runner):
768
+ """--extra-context option is visible in CLI help output."""
769
+ result = runner.invoke(cli, ["--help"])
770
+ assert result.exit_code == 0
771
+ assert "extra-context" in result.output
@@ -0,0 +1,93 @@
1
+ """Tests for extra vocabulary support in @context."""
2
+
3
+ import json
4
+
5
+ import pytest
6
+
7
+ from tro_utils.models import TransparentResearchObject
8
+ from tro_utils.tro_utils import TRO
9
+
10
+
11
+ class TestExtraContext:
12
+ """Tests for extra vocabulary support in @context."""
13
+
14
+ def test_extra_context_default_empty(self):
15
+ """TransparentResearchObject.extra_context defaults to an empty dict."""
16
+ tro = TransparentResearchObject()
17
+ assert tro.extra_context == {}
18
+
19
+ def test_extra_context_dict_in_jsonld(self):
20
+ """A dict extra_context entry appears in the serialised @context."""
21
+ tro = TransparentResearchObject()
22
+ tro.extra_context = {"ex": "http://example.org/"}
23
+ data = tro.to_jsonld()
24
+ assert data["@context"]["ex"] == "http://example.org/"
25
+
26
+ def test_extra_context_multiple_entries(self):
27
+ """Multiple extra_context entries all appear in @context."""
28
+ tro = TransparentResearchObject()
29
+ tro.extra_context = {
30
+ "ex": "http://example.org/",
31
+ "foaf": "http://xmlns.com/foaf/0.1/",
32
+ }
33
+ data = tro.to_jsonld()
34
+ assert data["@context"]["ex"] == "http://example.org/"
35
+ assert data["@context"]["foaf"] == "http://xmlns.com/foaf/0.1/"
36
+
37
+ def test_extra_context_base_context_still_present(self):
38
+ """The standard base context keys are always present in @context."""
39
+ tro = TransparentResearchObject()
40
+ tro.extra_context = {"ex": "http://example.org/"}
41
+ data = tro.to_jsonld()
42
+ base = data["@context"]
43
+ assert "trov" in base
44
+ assert "rdf" in base
45
+
46
+ def test_extra_context_roundtrip(self, tmp_path):
47
+ """Extra context entries survive a save/load roundtrip."""
48
+ tro = TransparentResearchObject()
49
+ tro.extra_context = {
50
+ "ex": "http://example.org/",
51
+ "foaf": "http://xmlns.com/foaf/0.1/",
52
+ }
53
+ filepath = tmp_path / "tro_with_extra_ctx.jsonld"
54
+ tro.save(str(filepath))
55
+
56
+ loaded = TransparentResearchObject.load(str(filepath))
57
+ assert loaded.extra_context["ex"] == "http://example.org/"
58
+ assert loaded.extra_context["foaf"] == "http://xmlns.com/foaf/0.1/"
59
+
60
+ def test_extra_context_via_tro_facade(self, tmp_path):
61
+ """TRO facade propagates extra_context to the underlying model."""
62
+ tro_file = tmp_path / "tro.jsonld"
63
+ tro = TRO(
64
+ filepath=str(tro_file),
65
+ extra_context={"ex": "http://example.org/"},
66
+ )
67
+ tro.save()
68
+
69
+ with open(tro_file) as f:
70
+ data = json.load(f)
71
+ assert data["@context"]["ex"] == "http://example.org/"
72
+
73
+ def test_extra_context_via_tro_facade_extends_existing(self, tmp_path):
74
+ """TRO facade merges extra_context into any context already in the file."""
75
+ tro_file = tmp_path / "tro.jsonld"
76
+ # First save with one extra context entry
77
+ tro = TRO(
78
+ filepath=str(tro_file),
79
+ extra_context={"ex": "http://example.org/"},
80
+ )
81
+ tro.save()
82
+
83
+ # Re-open and add a second entry
84
+ tro2 = TRO(
85
+ filepath=str(tro_file),
86
+ extra_context={"foaf": "http://xmlns.com/foaf/0.1/"},
87
+ )
88
+ tro2.save()
89
+
90
+ with open(tro_file) as f:
91
+ data = json.load(f)
92
+ assert data["@context"]["ex"] == "http://example.org/"
93
+ assert data["@context"]["foaf"] == "http://xmlns.com/foaf/0.1/"
@@ -0,0 +1,218 @@
1
+ """Unit tests for ArtifactArrangement."""
2
+
3
+ from tro_utils.models import (
4
+ ArtifactArrangement,
5
+ ArtifactComposition,
6
+ ArtifactLocation,
7
+ HashValue,
8
+ ResearchArtifact,
9
+ TransparentResearchObject,
10
+ )
11
+
12
+
13
+ class TestArtifactArrangement:
14
+ """Unit tests for ArtifactArrangement."""
15
+
16
+ def test_from_directory(self, tmp_path):
17
+ d = tmp_path / "workdir"
18
+ d.mkdir()
19
+ (d / "a.txt").write_text("alpha")
20
+ (d / "b.txt").write_text("beta")
21
+
22
+ comp = ArtifactComposition()
23
+ arr = ArtifactArrangement.from_directory(
24
+ d, comp, "arrangement/0", comment="test"
25
+ )
26
+
27
+ assert arr.arrangement_id == "arrangement/0"
28
+ assert arr.comment == "test"
29
+ assert len(arr.locations) == 2
30
+ assert len(comp.artifacts) == 2
31
+
32
+ def test_from_directory_no_duplicate_artifacts(self, tmp_path):
33
+ """Identical files across two arrangements share artifacts in composition."""
34
+ d = tmp_path / "workdir"
35
+ d.mkdir()
36
+ (d / "same.txt").write_text("identical content")
37
+
38
+ comp = ArtifactComposition()
39
+ ArtifactArrangement.from_directory(d, comp, "arrangement/0")
40
+ ArtifactArrangement.from_directory(d, comp, "arrangement/1")
41
+
42
+ assert len(comp.artifacts) == 1 # same artifact reused
43
+
44
+ def test_to_path_hash_map(self, tmp_path):
45
+ d = tmp_path / "workdir"
46
+ d.mkdir()
47
+ (d / "file.txt").write_text("content")
48
+
49
+ comp = ArtifactComposition()
50
+ arr = ArtifactArrangement.from_directory(d, comp, "arrangement/0")
51
+ binding = arr.to_path_hash_map(comp)
52
+
53
+ assert "file.txt" in binding
54
+ assert binding["file.txt"].startswith("sha256:")
55
+
56
+ def test_to_from_jsonld_roundtrip(self):
57
+ arr = ArtifactArrangement(
58
+ arrangement_id="arrangement/0",
59
+ comment="snapshot",
60
+ locations=[
61
+ ArtifactLocation(
62
+ "arrangement/0/location/0", "comp/1/artifact/0", "data/in.csv"
63
+ )
64
+ ],
65
+ )
66
+ jld = arr.to_jsonld()
67
+ restored = ArtifactArrangement.from_jsonld(jld)
68
+ assert restored.arrangement_id == arr.arrangement_id
69
+ assert restored.comment == arr.comment
70
+ assert len(restored.locations) == 1
71
+
72
+ # ------------------------------------------------------------------
73
+ # Snapshot tests
74
+ # ------------------------------------------------------------------
75
+
76
+ def test_to_snapshot_contains_artifacts_and_locations(self, tmp_path):
77
+ d = tmp_path / "workdir"
78
+ d.mkdir()
79
+ (d / "a.txt").write_text("alpha")
80
+ (d / "b.txt").write_text("beta")
81
+
82
+ comp = ArtifactComposition()
83
+ arr = ArtifactArrangement.from_directory(
84
+ d, comp, "arrangement/0", comment="snap"
85
+ )
86
+ snap = arr.to_snapshot(comp)
87
+
88
+ assert snap["@type"] == "trov:ArrangementSnapshot"
89
+ assert snap["rdfs:comment"] == "snap"
90
+ assert len(snap["trov:hasArtifactLocation"]) == 2
91
+ assert len(snap["trov:hasArtifact"]) == 2
92
+
93
+ def test_to_snapshot_only_includes_referenced_artifacts(self, tmp_path):
94
+ """Artifacts in the composition but not referenced by this arrangement are excluded."""
95
+ d1 = tmp_path / "dir1"
96
+ d1.mkdir()
97
+ (d1 / "a.txt").write_text("alpha")
98
+
99
+ d2 = tmp_path / "dir2"
100
+ d2.mkdir()
101
+ (d2 / "b.txt").write_text("beta")
102
+
103
+ comp = ArtifactComposition()
104
+ arr1 = ArtifactArrangement.from_directory(d1, comp, "arrangement/0")
105
+ ArtifactArrangement.from_directory(d2, comp, "arrangement/1")
106
+
107
+ # composition has 2 artifacts; arr1 only references 1
108
+ assert len(comp.artifacts) == 2
109
+ snap = arr1.to_snapshot(comp)
110
+ assert len(snap["trov:hasArtifact"]) == 1
111
+
112
+ def test_save_and_load_snapshot_roundtrip(self, tmp_path):
113
+ d = tmp_path / "workdir"
114
+ d.mkdir()
115
+ (d / "file.txt").write_text("content")
116
+
117
+ comp = ArtifactComposition()
118
+ arr = ArtifactArrangement.from_directory(
119
+ d, comp, "arrangement/0", comment="original"
120
+ )
121
+
122
+ snap_path = tmp_path / "snap.jsonld"
123
+ arr.save_snapshot(snap_path, comp)
124
+
125
+ # Load into a fresh composition
126
+ new_comp = ArtifactComposition()
127
+ restored = ArtifactArrangement.load_snapshot(
128
+ snap_path, new_comp, "arrangement/99", comment=None
129
+ )
130
+
131
+ assert restored.arrangement_id == "arrangement/99"
132
+ assert restored.comment == "original" # taken from snapshot
133
+ assert len(restored.locations) == 1
134
+ assert restored.locations[0].path == "file.txt"
135
+ assert len(new_comp.artifacts) == 1
136
+
137
+ def test_load_snapshot_comment_override(self, tmp_path):
138
+ d = tmp_path / "workdir"
139
+ d.mkdir()
140
+ (d / "file.txt").write_text("x")
141
+
142
+ comp = ArtifactComposition()
143
+ arr = ArtifactArrangement.from_directory(
144
+ d, comp, "arrangement/0", comment="original"
145
+ )
146
+ snap_path = tmp_path / "snap.jsonld"
147
+ arr.save_snapshot(snap_path, comp)
148
+
149
+ new_comp = ArtifactComposition()
150
+ restored = ArtifactArrangement.load_snapshot(
151
+ snap_path, new_comp, "arrangement/0", comment="overridden"
152
+ )
153
+ assert restored.comment == "overridden"
154
+
155
+ def test_load_snapshot_deduplicates_artifacts(self, tmp_path):
156
+ """Loading a snapshot whose artifacts already exist in the target composition does not create duplicates."""
157
+ d = tmp_path / "workdir"
158
+ d.mkdir()
159
+ (d / "file.txt").write_text("same content")
160
+
161
+ comp = ArtifactComposition()
162
+ arr = ArtifactArrangement.from_directory(
163
+ d, comp, "arrangement/0", comment="snap"
164
+ )
165
+ snap_path = tmp_path / "snap.jsonld"
166
+ arr.save_snapshot(snap_path, comp)
167
+
168
+ # target_comp already contains the same artifact (scanned independently)
169
+ target_comp = ArtifactComposition()
170
+ ArtifactArrangement.from_directory(d, target_comp, "arrangement/0")
171
+ assert len(target_comp.artifacts) == 1
172
+
173
+ restored = ArtifactArrangement.load_snapshot(
174
+ snap_path, target_comp, "arrangement/1"
175
+ )
176
+ # Still only one artifact - no duplicate
177
+ assert len(target_comp.artifacts) == 1
178
+ assert restored.locations[0].artifact_id == target_comp.artifacts[0].artifact_id
179
+
180
+ def test_from_snapshot_remaps_location_ids(self, tmp_path):
181
+ d = tmp_path / "workdir"
182
+ d.mkdir()
183
+ (d / "f.txt").write_text("data")
184
+
185
+ comp = ArtifactComposition()
186
+ arr = ArtifactArrangement.from_directory(d, comp, "arrangement/0")
187
+ snap = arr.to_snapshot(comp)
188
+
189
+ new_comp = ArtifactComposition()
190
+ restored = ArtifactArrangement.from_snapshot(snap, new_comp, "arrangement/7")
191
+ assert restored.locations[0].location_id == "arrangement/7/location/0"
192
+
193
+ def test_add_arrangement_from_snapshot_on_tro_model(self, tmp_path):
194
+ """TransparentResearchObject.add_arrangement_from_snapshot wires up correctly."""
195
+ d = tmp_path / "workdir"
196
+ d.mkdir()
197
+ (d / "data.txt").write_text("hello")
198
+
199
+ # Create a snapshot independently
200
+ comp = ArtifactComposition()
201
+ arr = ArtifactArrangement.from_directory(
202
+ d, comp, "arrangement/0", comment="pre-computed"
203
+ )
204
+ snap_path = tmp_path / "snap.jsonld"
205
+ arr.save_snapshot(snap_path, comp)
206
+
207
+ # Load into a fresh TRO model
208
+ tro_model = TransparentResearchObject()
209
+ added = tro_model.add_arrangement_from_snapshot(snap_path)
210
+
211
+ assert added.arrangement_id == "arrangement/0"
212
+ assert added.comment == "pre-computed"
213
+ assert len(tro_model.arrangements) == 1
214
+ assert len(tro_model.composition.artifacts) == 1
215
+ assert (
216
+ tro_model.arrangements[0].locations[0].artifact_id
217
+ == tro_model.composition.artifacts[0].artifact_id
218
+ )
@@ -0,0 +1,57 @@
1
+ """Unit tests for ArtifactComposition."""
2
+
3
+ from tro_utils.models import ArtifactComposition, HashValue, ResearchArtifact
4
+
5
+
6
+ class TestArtifactComposition:
7
+ """Unit tests for ArtifactComposition."""
8
+
9
+ def test_add_artifact_updates_fingerprint(self):
10
+ comp = ArtifactComposition()
11
+ assert comp.fingerprint is None
12
+ a = ResearchArtifact(
13
+ "comp/1/artifact/0", HashValue("sha256", "aaa"), "text/plain"
14
+ )
15
+ comp.add_artifact(a)
16
+ assert comp.fingerprint is not None
17
+
18
+ def test_get_by_hash(self):
19
+ comp = ArtifactComposition()
20
+ a = ResearchArtifact("x/0", HashValue("sha256", "abc"), "text/plain")
21
+ comp.add_artifact(a)
22
+ assert comp.get_by_hash("sha256:abc") is a
23
+ assert comp.get_by_hash("sha256:xyz") is None
24
+
25
+ def test_get_by_id(self):
26
+ comp = ArtifactComposition()
27
+ a = ResearchArtifact("x/0", HashValue("sha256", "abc"), "text/plain")
28
+ comp.add_artifact(a)
29
+ assert comp.get_by_id("x/0") is a
30
+ assert comp.get_by_id("x/9") is None
31
+
32
+ def test_next_artifact_id(self):
33
+ comp = ArtifactComposition(composition_id="composition/1")
34
+ assert comp.next_artifact_id() == "composition/1/artifact/0"
35
+ comp.add_artifact(
36
+ ResearchArtifact(
37
+ "composition/1/artifact/0", HashValue("sha256", "a"), "text/plain"
38
+ )
39
+ )
40
+ assert comp.next_artifact_id() == "composition/1/artifact/1"
41
+
42
+ def test_to_from_jsonld_roundtrip(self):
43
+ comp = ArtifactComposition()
44
+ comp.add_artifact(
45
+ ResearchArtifact(
46
+ "comp/1/artifact/0", HashValue("sha256", "aaa"), "text/plain"
47
+ )
48
+ )
49
+ comp.add_artifact(
50
+ ResearchArtifact(
51
+ "comp/1/artifact/1", HashValue("sha256", "bbb"), "text/csv"
52
+ )
53
+ )
54
+ jld = comp.to_jsonld()
55
+ restored = ArtifactComposition.from_jsonld(jld)
56
+ assert len(restored.artifacts) == 2
57
+ assert restored.fingerprint is not None
@@ -0,0 +1,18 @@
1
+ """Unit tests for ArtifactLocation."""
2
+
3
+ from tro_utils.models import ArtifactLocation
4
+
5
+
6
+ class TestArtifactLocation:
7
+ """Unit tests for ArtifactLocation."""
8
+
9
+ def test_to_from_jsonld(self):
10
+ loc = ArtifactLocation("arr/0/loc/0", "comp/1/artifact/0", "data/input.csv")
11
+ jld = loc.to_jsonld()
12
+ assert jld["@id"] == "arr/0/loc/0"
13
+ assert jld["trov:artifact"]["@id"] == "comp/1/artifact/0"
14
+ assert jld["trov:path"] == "data/input.csv"
15
+ restored = ArtifactLocation.from_jsonld(jld)
16
+ assert restored.location_id == loc.location_id
17
+ assert restored.artifact_id == loc.artifact_id
18
+ assert restored.path == loc.path
@@ -0,0 +1,23 @@
1
+ """Unit tests for CompositionFingerprint."""
2
+
3
+ from tro_utils.models import CompositionFingerprint, HashValue, ResearchArtifact
4
+
5
+
6
+ class TestCompositionFingerprint:
7
+ """Unit tests for CompositionFingerprint."""
8
+
9
+ def test_compute_deterministic(self):
10
+ artifacts = [
11
+ ResearchArtifact("a/0", HashValue("sha256", "aaa"), "text/plain"),
12
+ ResearchArtifact("a/1", HashValue("sha256", "bbb"), "text/csv"),
13
+ ]
14
+ fp1 = CompositionFingerprint.compute(artifacts)
15
+ fp2 = CompositionFingerprint.compute(list(reversed(artifacts)))
16
+ assert fp1.hash.value == fp2.hash.value # order-independent
17
+
18
+ def test_to_from_jsonld(self):
19
+ artifacts = [ResearchArtifact("a/0", HashValue("sha256", "abc"), "text/plain")]
20
+ fp = CompositionFingerprint.compute(artifacts)
21
+ jld = fp.to_jsonld()
22
+ restored = CompositionFingerprint.from_jsonld(jld)
23
+ assert restored.hash.value == fp.hash.value