@@ -44,29 +44,32 @@ def test_init_nested_object():
4444 "author" : [{"name" : "Foo" }, {"name" : "Bar" }],
4545 }
4646 data = SoftwareMetadata (my_software , extra_vocabs = {"foo" : "https://foo.bar" })
47- assert data ["schema:softwareName" ][ 0 ] == "MySoftware"
48- assert data ["maintainer" ][ 0 ]["name" ][ 0 ] == "Some Name"
47+ assert data ["schema:softwareName" ] == [ "MySoftware" ]
48+ assert len ( data ["maintainer" ]) == 1 and data [ "maintainer" ][ 0 ]["name" ] == [ "Some Name" ]
4949 for author in data ["author" ]:
5050 for name in author ["name" ]:
5151 assert name in ["Foo" , "Bar" ]
5252
5353
5454def test_append ():
5555 data = SoftwareMetadata ()
56- data ["foo" ].append ("a" )
57- assert type (data ["foo" ]) is ld_list and data ["foo" ][0 ] == "a" and data ["foo" ].item_list == [{"@value" : "a" }]
58- data ["foo" ].append ("b" )
59- assert type (data ["foo" ]) is ld_list and data ["foo" ].item_list == [{"@value" : "a" }, {"@value" : "b" }]
60- data ["foo" ].append ("c" )
61- assert data ["foo" ].item_list == [{"@value" : "a" }, {"@value" : "b" }, {"@value" : "c" }]
56+ data ["schema:foo" ].append ("a" )
57+ assert type (data ["schema:foo" ]) is ld_list
58+ assert data ["schema:foo" ][0 ] == "a" and data ["schema:foo" ].item_list == [{"@value" : "a" }]
59+ data ["schema:foo" ].append ("b" )
60+ assert type (data ["schema:foo" ]) is ld_list
61+ assert data ["schema:foo" ] == [{"@value" : "a" }, {"@value" : "b" }]
62+ data ["schema:foo" ].append ("c" )
63+ assert data ["schema:foo" ] == [{"@value" : "a" }, {"@value" : "b" }, {"@value" : "c" }]
6264 data = SoftwareMetadata ()
63- data ["foo" ].append ({"schema:name" : "foo" })
64- assert type (data ["foo" ]) is ld_list and type (data ["foo" ][0 ]) is ld_dict
65- assert data ["foo" ][0 ].data_dict == {"http://schema.org/name" : [{"@value" : "foo" }]}
66- data ["foo" ].append ({"schema:name" : "foo" })
67- assert type (data ["foo" ]) is ld_list and data ["foo" ].item_list == 2 * [{"http://schema.org/name" : [{"@value" : "foo" }]}]
68- data ["foo" ].append ({"schema:name" : "foo" })
69- assert data ["foo" ].item_list == 3 * [{"http://schema.org/name" : [{"@value" : "foo" }]}]
65+ data ["schema:foo" ].append ({"schema:name" : "bar" })
66+ assert type (data ["schema:foo" ]) is ld_list and type (data ["schema:foo" ][0 ]) is ld_dict
67+ assert data ["schema:foo" ] == [{"http://schema.org/name" : [{"@value" : "bar" }]}]
68+ data ["schema:foo" ].append ({"schema:name" : "bar" })
69+ assert type (data ["schema:foo" ]) is ld_list
70+ assert data ["schema:foo" ] == 2 * [{"http://schema.org/name" : [{"@value" : "bar" }]}]
71+ data ["schema:foo" ].append ({"schema:name" : "bar" })
72+ assert data ["schema:foo" ] == 3 * [{"http://schema.org/name" : [{"@value" : "bar" }]}]
7073
7174
7275def test_iterative_assignment ():
@@ -78,9 +81,10 @@ def test_iterative_assignment():
7881 assert isinstance (authors , ld_list )
7982 author1 = authors [0 ]
8083 author1 [
"email" ]
= "[email protected] " 81- authors [0 ] = author1
8284 authors .
append ({
"name" :
"Bar" ,
"email" :
"[email protected] " })
8385 assert len (authors ) == 2
86+ del authors [0 ]
87+ assert len (authors ) == 1
8488
8589
8690def test_usage ():
@@ -95,38 +99,38 @@ def test_usage():
9599 harvest = {
96100 "authors" : [
97101 {"name" : "Foo" , "affiliation" : ["Uni A" , "Lab B" ], "kw" : ["a" , "b" , "c" ]},
98- {
"name" :
"Bar" ,
"affiliation" : [
"Uni C" ],
"email" :
"[email protected] " },
102+ {
"name" :
"Bar" ,
"affiliation" : [
"Uni C" ],
"email" :
"[email protected] " , "kw" : "egg" },
99103 {"name" : "Baz" , "affiliation" : ["Lab E" ]},
100104 ]
101105 }
102106 for author in harvest ["authors" ]:
103107 for exist_author in data ["author" ]:
104- if author ["name" ] == exist_author ["name" ][0 ]:
105- exist_author ["affiliation" ] = author ["affiliation" ]
106- if "email" in author :
107- exist_author ["email" ].append (author ["email" ])
108- if "kw" in author :
109- exist_author ["schema:knowsAbout" ].extend (author ["kw" ])
108+ if author ["name" ] in exist_author ["name" ]:
109+ exist_author ["affiliation" ] = author .get ("affiliation" , [])
110+ exist_author ["email" ].extend (email if isinstance ((email := author .get ("email" , [])), list ) else [email ])
111+ exist_author ["schema:knowsAbout" ].extend (kw if isinstance ((kw := author .get ("kw" , [])), list ) else [kw ])
110112 break
111113 else :
112114 data ["author" ].append (author )
113115 assert len (data ["author" ]) == 3
114116 foo , bar , baz = data ["author" ]
115117 assert foo ["name" ][0 ] == "Foo"
116- assert foo ["affiliation" ]. to_python () == ["Uni A" , "Lab B" ]
117- assert foo ["schema:knowsAbout" ]. to_python () == ["a" , "b" , "c" ]
118- assert foo [
"email" ]
. to_python () == [
"[email protected] " ,
"[email protected] " ]
118+ assert foo ["affiliation" ] == ["Uni A" , "Lab B" ]
119+ assert foo ["schema:knowsAbout" ] == ["a" , "b" , "c" ]
120+ 119121 assert bar ["name" ][0 ] == "Bar"
120- assert bar ["affiliation" ]. to_python () == ["Uni C" ]
121- assert bar [
"email" ]
. to_python () == [
"[email protected] " ]
122+ assert bar ["affiliation" ] == ["Uni C" ]
123+ assert bar [
"email" ]
== [
"[email protected] " ]
122124 assert baz ["name" ][0 ] == "Baz"
123- assert baz ["affiliation" ]. to_python () == ["Lab E" ]
125+ assert baz ["affiliation" ] == ["Lab E" ]
124126 assert len (baz ["schema:knowsAbout" ]) == 0
125127 assert len (baz ["email" ]) == 0
126128 for author in data ["author" ]:
127129 assert "name" in author
128130 assert "email" in author
129- if "schema:knowsAbout" not in author :
131+ if author [ "schema:knowsAbout" ] == [ "egg" ] :
130132 # FIXME: None has to be discussed
133+ # json-ld processor just removes it in expansion
131134 author ["schema:knowsAbout" ] = None
132135 author ["schema:pronouns" ] = "they/them"
136+ assert len (bar ["schema:knowsAbout" ]) == 0
0 commit comments