|
| 1 | +import json |
| 2 | + |
| 3 | +from osw.utils.templates import eval_handlebars_template |
| 4 | + |
| 5 | + |
| 6 | +def test_category_template(): |
| 7 | + template = """ |
| 8 | +{ |
| 9 | + "@context": [ |
| 10 | + {{#each subclass_of}} |
| 11 | + "/wiki/{{{.}}}?action=raw\u0026slot=jsonschema"{{#unless @last}},{{/unless}} |
| 12 | + {{/each}} |
| 13 | + ], |
| 14 | + "allOf": [ |
| 15 | + { |
| 16 | + {{#each subclass_of}} |
| 17 | + "$ref": "/wiki/{{{.}}}?action=raw\u0026slot=jsonschema"{{#unless @last}},{{/unless}} |
| 18 | + {{/each}} |
| 19 | + } |
| 20 | + ], |
| 21 | + "type": "object", |
| 22 | + "uuid": "{{{uuid}}}", |
| 23 | + "title": "{{{name}}}", |
| 24 | + "title*": { |
| 25 | + {{#each label}} |
| 26 | + "{{{lang}}}": "{{{text}}}"{{#unless @last}},{{/unless}} |
| 27 | + {{/each}} |
| 28 | + }, |
| 29 | + "description": "{{{description.[0].text}}}", |
| 30 | + "description*": { |
| 31 | + {{#each description}} |
| 32 | + "{{{lang}}}": "{{{text}}}"{{#unless @last}},{{/unless}} |
| 33 | + {{/each}} |
| 34 | + }, |
| 35 | + "required": ["type"], |
| 36 | + "properties": { |
| 37 | + "type": { |
| 38 | + "default": ["{{{_page_title}}}"] |
| 39 | + } |
| 40 | + } |
| 41 | +}""" |
| 42 | + |
| 43 | + data = { |
| 44 | + "type": ["Category:Category"], |
| 45 | + "subclass_of": ["Category:Category"], |
| 46 | + "uuid": "379d5a15-89c7-4c82-bc0d-e47938264d00", |
| 47 | + "label": [{"text": "OwlThing", "lang": "en"}], |
| 48 | + "metaclass": ["Category:OSW725a3cf5458f4daea86615fcbd0029f8"], |
| 49 | + "description": [ |
| 50 | + { |
| 51 | + "text": "Represents the set of all individuals. In the DL literature this is often called the top concept.", |
| 52 | + "lang": "en", |
| 53 | + } |
| 54 | + ], |
| 55 | + "name": "OwlThing", |
| 56 | + } |
| 57 | + |
| 58 | + expected = """ |
| 59 | +{ |
| 60 | + "@context": [ |
| 61 | + "/wiki/Category:Category?action=raw&slot=jsonschema" |
| 62 | + ], |
| 63 | + "allOf": [ |
| 64 | + { |
| 65 | + "$ref": "/wiki/Category:Category?action=raw&slot=jsonschema" |
| 66 | + } |
| 67 | + ], |
| 68 | + "type": "object", |
| 69 | + "uuid": "379d5a15-89c7-4c82-bc0d-e47938264d00", |
| 70 | + "title": "OwlThing", |
| 71 | + "title*": { |
| 72 | + "en": "OwlThing" |
| 73 | + }, |
| 74 | + "description": "Represents the set of all individuals. In the DL literature this is often called the top concept.", |
| 75 | + "description*": { |
| 76 | + "en": "Represents the set of all individuals. In the DL literature this is often called the top concept." |
| 77 | + }, |
| 78 | + "required": [ |
| 79 | + "type" |
| 80 | + ], |
| 81 | + "properties": { |
| 82 | + "type": { |
| 83 | + "default": [ |
| 84 | + "Category:OSW379d5a1589c74c82bc0de47938264d00" |
| 85 | + ] |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +""" |
| 90 | + |
| 91 | + output = json.loads( |
| 92 | + eval_handlebars_template( |
| 93 | + template, |
| 94 | + data, |
| 95 | + {"_page_title": "Category:OSW379d5a1589c74c82bc0de47938264d00"}, |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + assert output == json.loads(expected) |
| 100 | + |
| 101 | + |
| 102 | +def test_metamodel_template(): |
| 103 | + template = """ |
| 104 | + { |
| 105 | + "title": "{{{name}}}", |
| 106 | + "required": ["uuid"], |
| 107 | + "properties": { |
| 108 | + "uuid": { |
| 109 | + "type": "string", |
| 110 | + "format": "uuid", |
| 111 | + "options": { |
| 112 | + "hidden": true |
| 113 | + } |
| 114 | + } {{#each parameters}}, |
| 115 | + "{{{name}}}": { |
| 116 | + "title": "{{{name}}}", |
| 117 | + "type": "{{{type}}}" {{#if default}}, |
| 118 | + "default": "{{{default}}}"{{/if}} |
| 119 | + } {{/each}}{{#each submodels}}, |
| 120 | + "{{{name}}}": |
| 121 | + {{> self}} |
| 122 | + {{/each}} |
| 123 | + } |
| 124 | + }""" |
| 125 | + |
| 126 | + data = { |
| 127 | + "submodels": [ |
| 128 | + { |
| 129 | + "name": "Geometrie", |
| 130 | + "parameters": [{"name": "FaceArea"}, {"name": "dimension"}], |
| 131 | + "submodels": [ |
| 132 | + { |
| 133 | + "name": "NegativeElectrode", |
| 134 | + "parameters": [], |
| 135 | + "submodels": [ |
| 136 | + { |
| 137 | + "name": "ActiveMaterial", |
| 138 | + "parameters": [{"name": "thickness"}], |
| 139 | + "submodels": [], |
| 140 | + } |
| 141 | + ], |
| 142 | + }, |
| 143 | + { |
| 144 | + "name": "PositiveElectrode", |
| 145 | + "parameters": [], |
| 146 | + "submodels": [ |
| 147 | + { |
| 148 | + "name": "ActiveMaterial", |
| 149 | + "parameters": [{"name": "thickness"}], |
| 150 | + "submodels": [], |
| 151 | + } |
| 152 | + ], |
| 153 | + }, |
| 154 | + ], |
| 155 | + } |
| 156 | + ], |
| 157 | + "type": ["Category:OSWecff4345b4b049218f8d6628dc2f2f21"], |
| 158 | + "uuid": "dab254dd-1006-4ddf-9554-64b7a5baf332", |
| 159 | + "name": "BattmoModel", |
| 160 | + "label": [{"text": "BattMo Model", "lang": "en"}], |
| 161 | + } |
| 162 | + |
| 163 | + expected = """{ |
| 164 | + "title":"BattmoModel", |
| 165 | + "required":[ |
| 166 | + "uuid" |
| 167 | + ], |
| 168 | + "properties":{ |
| 169 | + "uuid":{ |
| 170 | + "type":"string", |
| 171 | + "format":"uuid", |
| 172 | + "options":{ |
| 173 | + "hidden":true |
| 174 | + } |
| 175 | + }, |
| 176 | + "Geometrie":{ |
| 177 | + "title":"Geometrie", |
| 178 | + "required":[ |
| 179 | + "uuid" |
| 180 | + ], |
| 181 | + "properties":{ |
| 182 | + "uuid":{ |
| 183 | + "type":"string", |
| 184 | + "format":"uuid", |
| 185 | + "options":{ |
| 186 | + "hidden":true |
| 187 | + } |
| 188 | + }, |
| 189 | + "FaceArea":{ |
| 190 | + "title":"FaceArea", |
| 191 | + "type":"" |
| 192 | + }, |
| 193 | + "dimension":{ |
| 194 | + "title":"dimension", |
| 195 | + "type":"" |
| 196 | + }, |
| 197 | + "NegativeElectrode":{ |
| 198 | + "title":"NegativeElectrode", |
| 199 | + "required":[ |
| 200 | + "uuid" |
| 201 | + ], |
| 202 | + "properties":{ |
| 203 | + "uuid":{ |
| 204 | + "type":"string", |
| 205 | + "format":"uuid", |
| 206 | + "options":{ |
| 207 | + "hidden":true |
| 208 | + } |
| 209 | + }, |
| 210 | + "ActiveMaterial":{ |
| 211 | + "title":"ActiveMaterial", |
| 212 | + "required":[ |
| 213 | + "uuid" |
| 214 | + ], |
| 215 | + "properties":{ |
| 216 | + "uuid":{ |
| 217 | + "type":"string", |
| 218 | + "format":"uuid", |
| 219 | + "options":{ |
| 220 | + "hidden":true |
| 221 | + } |
| 222 | + }, |
| 223 | + "thickness":{ |
| 224 | + "title":"thickness", |
| 225 | + "type":"" |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + }, |
| 231 | + "PositiveElectrode":{ |
| 232 | + "title":"PositiveElectrode", |
| 233 | + "required":[ |
| 234 | + "uuid" |
| 235 | + ], |
| 236 | + "properties":{ |
| 237 | + "uuid":{ |
| 238 | + "type":"string", |
| 239 | + "format":"uuid", |
| 240 | + "options":{ |
| 241 | + "hidden":true |
| 242 | + } |
| 243 | + }, |
| 244 | + "ActiveMaterial":{ |
| 245 | + "title":"ActiveMaterial", |
| 246 | + "required":[ |
| 247 | + "uuid" |
| 248 | + ], |
| 249 | + "properties":{ |
| 250 | + "uuid":{ |
| 251 | + "type":"string", |
| 252 | + "format":"uuid", |
| 253 | + "options":{ |
| 254 | + "hidden":true |
| 255 | + } |
| 256 | + }, |
| 257 | + "thickness":{ |
| 258 | + "title":"thickness", |
| 259 | + "type":"" |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + }""" |
| 269 | + |
| 270 | + output = json.loads(eval_handlebars_template(template, data)) |
| 271 | + |
| 272 | + assert output == json.loads(expected) |
0 commit comments