1010 ALL_CONTEXTS ,
1111)
1212
13+ from hermes .model .error import HermesContextError
14+
1315
1416@pytest .fixture
1517def ctx ():
@@ -18,18 +20,29 @@ def ctx():
1820
1921def test_ctx ():
2022 ctx = ContextPrefix (["u1" , {"2" : "u2" }])
21- assert ctx .prefix [None ] == "u1"
22- assert ctx .prefix ["2" ] == "u2"
23+ assert ctx .context [None ] == "u1"
24+ assert ctx .context ["2" ] == "u2"
2325
2426
27+ @pytest .mark .xfail (
28+ raises = AssertionError ,
29+ reason = "Currently, the wrong CodeMeta IRI is used in the implementation: "
30+ "https://github.com/softwarepub/hermes/issues/419" ,
31+ )
2532def test_codemeta_prefix (ctx ):
2633 """Default vocabulary in context has the correct base IRI."""
27- assert ctx .prefix [None ] == "https://codemeta.github.io/terms/"
34+ assert ctx .context [None ] == "https://codemeta.github.io/terms/"
2835
2936
30- def test_get_codemeta_item (ctx ):
37+ @pytest .mark .xfail (
38+ raises = AssertionError ,
39+ reason = "Currently, the wrong CodeMeta IRI is used in the implementation, so expanding terms doesn't work correctly,"
40+ " see https://github.com/softwarepub/hermes/issues/419" ,
41+ )
42+ @pytest .mark .parametrize ("compacted" , ["maintainer" , (None , "maintainer" )])
43+ def test_get_item_from_default_vocabulary_pass (ctx , compacted ):
3144 """Context returns fully expanded terms for default vocabulary in the context."""
32- item = ctx ["maintainer" ]
45+ item = ctx [compacted ]
3346 assert item == "https://codemeta.github.io/terms/maintainer"
3447
3548
@@ -41,37 +54,104 @@ def test_get_codemeta_item(ctx):
4154 "hermes:semanticVersion" ,
4255 "https://schema.software-metadata.pub/hermes-content/1.0/semanticVersion" , # TODO: Change on #393 fix
4356 ),
57+ (("schema" , "Organization" ), "http://schema.org/Organization" ),
58+ (
59+ ("hermes" , "semanticVersion" ),
60+ "https://schema.software-metadata.pub/hermes-content/1.0/semanticVersion" ,
61+ ), # TODO: Change on #393 fix
4462 ],
4563)
46- def test_get_prefixed_items (ctx , compacted , expanded ):
47- """Context returns fully expanded terms for prefixed vocabularies in the context."""
64+ def test_get_item_from_prefixed_vocabulary_pass (ctx , compacted , expanded ):
65+ """
66+ Context returns fully expanded terms for prefixed vocabularies in the context,
67+ for all accepted parameter formats.
68+ """
4869 item = ctx [compacted ]
4970 assert item == expanded
5071
5172
52- def test_get_protocol_items_pass (ctx ):
53- item = ctx ["https://schema.org/Organisation" ]
54- assert item == "https://schema.org/Organisation"
73+ @pytest .mark .parametrize (
74+ "prefix,not_exist" ,
75+ [
76+ ("foobar" , item )
77+ for item in [
78+ "foobar:baz" ,
79+ ("foobar" , "baz" ),
80+ ]
81+ ],
82+ )
83+ def test_get_item_from_prefixed_vocabulary_raises_on_prefix_not_exist (
84+ ctx , prefix , not_exist
85+ ):
86+ """
87+ Tests that an exception is raised when trying to get compacted items for which there is no
88+ prefixed vocabulary in the context.
89+ """
90+ with pytest .raises (HermesContextError ) as hce :
91+ _ = ctx [not_exist ]
92+ assert str (hce .value ) == prefix
5593
5694
57- def test_get_protocol_items_fail (ctx ):
58- with pytest .raises (Exception ) as e :
59- ctx ["https://foo.bar/baz" ]
60- assert "cannot access local variable" not in str (e .value ) # FIXME: Replace with custom error
95+ @pytest .mark .parametrize (
96+ "term,not_exist" ,
97+ [
98+ ("baz" , item )
99+ for item in [
100+ "baz" ,
101+ "hermes:baz" ,
102+ "schema:baz" ,
103+ (None , "baz" ),
104+ ("hermes" , "baz" ),
105+ ("schema" , "baz" ),
106+ ]
107+ ],
108+ )
109+ @pytest .mark .xfail (
110+ raises = NotImplementedError ,
111+ reason = "Not yet implemented/decided: Check if terms exist in given vocabulary." ,
112+ )
113+ def test_get_item_from_prefixed_vocabulary_raises_on_term_not_exist (
114+ ctx , term , not_exist
115+ ):
116+ """
117+ Tests that an exception is raised when trying to get compacted items for which the vocabulary exists,
118+ but doesn't contain the requested term.
119+ """
120+ with pytest .raises (HermesContextError ) as hce :
121+ _ = ctx [not_exist ]
122+ with pytest .raises (Exception ):
123+ assert str (hce .value ) == term
124+ raise NotImplementedError
61125
62126
63127@pytest .mark .parametrize (
64- "compacted, expanded" ,
128+ "expanded" ,
65129 [
66- ([None , "maintainer" ], "https://codemeta.github.io/terms/maintainer" ),
67- (["schema" , "Organization" ], "http://schema.org/Organization" ),
68- ((None , "maintainer" ), "https://codemeta.github.io/terms/maintainer" ),
69- (("schema" , "Organization" ), "http://schema.org/Organization" ),
130+ "https://codemeta.github.io/terms/maintainer" ,
131+ "https://schema.org/Organisation" ,
132+ "https://schema.software-metadata.pub/hermes-content/1.0/semanticVersion" ,
70133 ],
71134)
72- def test_get_valid_non_str_items (ctx , compacted , expanded ):
73- """Context returns fully expanded terms for valid non-string inputs."""
74- assert ctx [compacted ] == expanded
135+ @pytest .mark .xfail (
136+ raises = NotImplementedError ,
137+ reason = "Passing back expanded terms on their input if they are valid in the context "
138+ "is not yet implemented (or decided)." ,
139+ )
140+ def test_get_item_from_expanded_pass (ctx , expanded ):
141+ """
142+ Tests that getting items via their fully expanded terms works as expected.
143+ """
144+ with pytest .raises (Exception ):
145+ assert ctx [expanded ] == expanded
146+ raise NotImplementedError
147+
148+
149+ def test_get_item_from_expanded_fail (ctx ):
150+ """
151+ Tests that context raises on unsupported expanded term input.
152+ """
153+ with pytest .raises (HermesContextError ):
154+ ctx ["https://foo.bar/baz" ]
75155
76156
77157@pytest .mark .parametrize (
@@ -88,20 +168,31 @@ def test_get_non_str_item_fail(ctx, non_str, error_type):
88168 "item" ,
89169 [
90170 "" ,
91- "fooBar" ,
171+ pytest .param (
172+ "fooBar" ,
173+ marks = pytest .mark .xfail (
174+ reason = "Not yet implemented/decided: Check if terms exist in given vocabulary."
175+ ),
176+ ),
92177 [0 , "foo" ],
93178 (0 , "foo" ),
94179 {"foo" : "bar" , "baz" : "foo" },
95- "schema:fooBar" ,
96- "hermes:fooBar" ,
180+ pytest .param (
181+ "schema:fooBar" ,
182+ marks = pytest .mark .xfail (
183+ reason = "Not yet implemented/decided: Check if terms exist in given vocabulary."
184+ ),
185+ ),
186+ pytest .param (
187+ "hermes:fooBar" ,
188+ marks = pytest .mark .xfail (
189+ reason = "Not yet implemented/decided: Check if terms exist in given vocabulary."
190+ ),
191+ ),
97192 "codemeta:maintainer" , # Prefixed CodeMeta doesn't exist in context
98- # Even a dict with valid terms should fail, as it is unclear what to expect
99- {None : "maintainer" , "schema" : "Organization" },
100193 ],
101194)
102195def test_get_item_validate_fail (ctx , item ):
103- """Context raises on terms that don't exist in the context."""
104- with pytest .raises (
105- Exception
106- ): # FIXME: Replace with custom error, e.g., hermes.model.errors.InvalidTermException
196+ """Context raises on theoretically valid compressed terms that don't exist in the context."""
197+ with pytest .raises (HermesContextError ):
107198 ctx [item ]
0 commit comments