1414
1515def test_undefined_list ():
1616 with pytest .raises (ValueError ):
17- ld_list ([{}] )
17+ ld_list ({}, key = "foo" )
1818 with pytest .raises (ValueError ):
19- ld_list ([{"spam " : [{"@value" : "bacon" }]}])
19+ ld_list ([{"@set " : [{"@value" : "bacon" }]}], key = "foo" )
2020 with pytest .raises (ValueError ):
21- ld_list ([{"@list" : [ 0 ], "spam" : [{ "@ value" : "bacon" }]}] )
21+ ld_list ([{"@value" : "bacon" }], key = "@type" )
2222 with pytest .raises (ValueError ):
23- ld_list ([{ "@list" : [ "a" , "b" ], "@set" : [ "foo" , "bar" ]}] )
23+ ld_list (["bacon" ], key = "eggs" )
2424 with pytest .raises (ValueError ):
2525 ld_list ([{"@list" : ["a" , "b" ]}]) # no given key
26- with pytest .raises (ValueError ):
27- ld_list ([{"@list" : ["a" , "b" ]}, {"@set" : ["foo" , "bar" ]}])
2826
2927
3028def test_list_basics ():
3129 li_data = [{"@list" : [{"@value" : "bar" }]}]
3230 li = ld_list (li_data , key = "foo" )
3331 assert li ._data is li_data
3432 assert li .item_list is li_data [0 ]["@list" ]
33+ li_data = [{"@graph" : [{"@value" : "bar" }]}]
34+ li = ld_list (li_data , key = "foo" )
35+ assert li ._data is li_data
36+ assert li .item_list is li_data [0 ]["@graph" ]
37+ li_data = [{"@value" : "bar" }]
38+ li = ld_list (li_data , key = "foo" )
39+ assert li ._data is li_data
40+ assert li .item_list is li_data
41+ assert li .container_type == "@set"
3542
3643
3744def test_build_in_get ():
@@ -135,6 +142,12 @@ def test_build_in_iter():
135142
136143
137144def test_append ():
145+ li = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
146+ li .append (ld_list ([{"@value" : "foo" }], key = "https://schema.org/name" ))
147+ assert isinstance (li [0 ], ld_list ) and li [0 ].container_type == "@list"
148+ li = ld_list ([{"@graph" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
149+ li .append ({"schema:name" : "foo" })
150+ assert li [0 ] == {"https://schema.org/name" : "foo" } and len (li ) == 1
138151 li = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
139152 li .append ("foo" )
140153 assert li [0 ] == "foo" and li .item_list [0 ] == {"@value" : "foo" } and len (li ) == 1
@@ -153,6 +166,7 @@ def test_append():
153166
154167def test_build_in_contains ():
155168 li = ld_list ([], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
169+ assert [] in li
156170 li .append ("foo" )
157171 li .append ({"@type" : "A" , "schema:name" : "a" })
158172 assert "foo" in li and {"@type" : "A" , "schema:name" : "a" } in li
@@ -162,9 +176,18 @@ def test_build_in_contains():
162176 li .append ({"@id" : "schema:foo" , "schema:name" : "foo" })
163177 assert {"@id" : "schema:foo" } in li and {"@id" : "schema:foo" , "schema:name" : "foobar" } in li
164178 assert {"schema:name" : "foo" } in li
179+ li = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
180+ li .append ("foo" )
181+ assert "foo" in li
165182
166183
167184def test_build_in_comparison ():
185+ li = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
186+ li .append ({"@id" : "foo" , "schema:bar" : "foobar" })
187+ assert [{"@list" : [{"@id" : "foo" , "schema:bar" : "barfoo" }]}] == li
188+ assert [{"@list" : [{"@id" : "bar" , "schema:bar" : "foobar" }]}] != li
189+ assert [{"@set" : [{"@id" : "foo" , "schema:bar" : "barfoo" }]}] == li
190+ assert [{"@graph" : [{"@id" : "foo" , "schema:bar" : "barfoo" }]}] == li
168191 li = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema" : "https://schema.org/" }])
169192 li2 = ld_list ([{"@list" : []}], key = "https://schema.org/name" , context = [{"schema2" : "https://schema.org/" }])
170193 assert li == [] and [] == li
@@ -269,6 +292,10 @@ def test_is_container():
269292
270293
271294def test_from_list ():
295+ with pytest .raises (ValueError ):
296+ ld_list .from_list ([], key = "@type" , container_type = "@list" )
297+ with pytest .raises (ValueError ):
298+ ld_list .from_list ([], container_type = "foo" )
272299 li = ld_list .from_list ([], key = "schema:foo" )
273300 assert li .item_list == li .context == [] and li .parent is li .index is None and li .key == "schema:foo"
274301 assert li ._data == [] and li .container_type == "@set"
@@ -290,3 +317,9 @@ def test_get_item_list_from_container():
290317 assert ld_list .get_item_list_from_container ({"@graph" : ["a" ]}) == ["a" ]
291318 with pytest .raises (ValueError ):
292319 ld_list .get_item_list_from_container (["a" ])
320+ with pytest .raises (ValueError ):
321+ ld_list .get_item_list_from_container ({"@list" : [], "@set" : []})
322+ with pytest .raises (ValueError ):
323+ ld_list .get_item_list_from_container ({"@list" : {}})
324+ with pytest .raises (ValueError ):
325+ ld_list .get_item_list_from_container ({"foo" : []})
0 commit comments