|
| 1 | +import { expect } from "chai"; |
| 2 | + |
| 3 | +import { buildDependencies, getSchemaWithDependencies, typeofSchema } from "../src/utils/schemas"; |
| 4 | + |
| 5 | +describe("RJSF schema", () => { |
| 6 | + const TREE = { |
| 7 | + path: "/dft", |
| 8 | + dataSelector: { key: "type", value: "data.type.slug", name: "data.type.name" }, |
| 9 | + data: { |
| 10 | + type: { |
| 11 | + slug: "dft", |
| 12 | + name: "Density Functional Theory", |
| 13 | + }, |
| 14 | + }, |
| 15 | + children: [ |
| 16 | + { |
| 17 | + path: "/dft/lda", |
| 18 | + dataSelector: { |
| 19 | + key: "subtype", |
| 20 | + value: "data.subtype.slug", |
| 21 | + name: "data.subtype.name", |
| 22 | + }, |
| 23 | + data: { |
| 24 | + subtype: { |
| 25 | + slug: "lda", |
| 26 | + name: "LDA", |
| 27 | + }, |
| 28 | + }, |
| 29 | + children: [ |
| 30 | + { |
| 31 | + path: "/dft/lda/svwn", |
| 32 | + dataSelector: { |
| 33 | + key: "functional", |
| 34 | + value: "data.functional.slug", |
| 35 | + name: "data.functional.name", |
| 36 | + }, |
| 37 | + data: { |
| 38 | + functional: { |
| 39 | + slug: "svwn", |
| 40 | + name: "SVWN", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + path: "/dft/lda/pz", |
| 46 | + dataSelector: { |
| 47 | + key: "functional", |
| 48 | + value: "data.functional.slug", |
| 49 | + name: "data.functional.name", |
| 50 | + }, |
| 51 | + data: { |
| 52 | + functional: { |
| 53 | + slug: "pz", |
| 54 | + name: "PZ", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + path: "/dft/gga", |
| 62 | + dataSelector: { |
| 63 | + key: "subtype", |
| 64 | + value: "data.subtype.slug", |
| 65 | + name: "data.subtype.name", |
| 66 | + }, |
| 67 | + data: { |
| 68 | + subtype: { |
| 69 | + slug: "gga", |
| 70 | + name: "GGA", |
| 71 | + }, |
| 72 | + }, |
| 73 | + children: [ |
| 74 | + { |
| 75 | + path: "/dft/gga/pbe", |
| 76 | + dataSelector: { |
| 77 | + key: "functional", |
| 78 | + value: "data.functional.slug", |
| 79 | + name: "data.functional.name", |
| 80 | + }, |
| 81 | + data: { |
| 82 | + functional: { |
| 83 | + slug: "pbe", |
| 84 | + name: "PBE", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + path: "/dft/gga/pw91", |
| 90 | + dataSelector: { |
| 91 | + key: "functional", |
| 92 | + value: "data.functional.slug", |
| 93 | + name: "data.functional.name", |
| 94 | + }, |
| 95 | + data: { |
| 96 | + functional: { |
| 97 | + slug: "pw91", |
| 98 | + name: "PW91", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + ], |
| 105 | + }; |
| 106 | + const DFT_SCHEMA = { |
| 107 | + type: "object", |
| 108 | + properties: { |
| 109 | + type: { |
| 110 | + type: "string", |
| 111 | + }, |
| 112 | + subtype: { |
| 113 | + type: "string", |
| 114 | + }, |
| 115 | + functional: { |
| 116 | + type: "string", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + it("dependencies block can be created from tree", () => { |
| 122 | + const dependencies = buildDependencies([TREE]); |
| 123 | + |
| 124 | + const [dftCase] = dependencies.dependencies.type.oneOf; |
| 125 | + expect(dftCase.properties.subtype.enum).to.have.ordered.members(["lda", "gga"]); |
| 126 | + expect(dftCase.properties.subtype.enumNames).to.have.ordered.members(["LDA", "GGA"]); |
| 127 | + |
| 128 | + const [ldaCase, ggaCase] = dftCase.dependencies.subtype.oneOf; |
| 129 | + expect(ldaCase.properties.subtype.enum).to.have.length(1); |
| 130 | + expect(ldaCase.properties.functional.enum).to.have.ordered.members(["svwn", "pz"]); |
| 131 | + expect(ldaCase.properties.functional.enumNames).to.have.ordered.members(["SVWN", "PZ"]); |
| 132 | + expect(ldaCase).to.not.have.property("dependencies"); |
| 133 | + |
| 134 | + expect(ggaCase.properties.subtype.enum).to.have.length(1); |
| 135 | + expect(ggaCase.properties.functional.enum).to.have.ordered.members(["pbe", "pw91"]); |
| 136 | + expect(ggaCase.properties.functional.enumNames).to.have.ordered.members(["PBE", "PW91"]); |
| 137 | + expect(ggaCase).to.not.have.property("dependencies"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("can be created with dependencies from schema", () => { |
| 141 | + const rjsfSchema = getSchemaWithDependencies({ |
| 142 | + schema: DFT_SCHEMA, |
| 143 | + nodes: [TREE], |
| 144 | + }); |
| 145 | + expect(rjsfSchema.type).to.be.eql(DFT_SCHEMA.type); |
| 146 | + expect(rjsfSchema.properties).to.be.eql(DFT_SCHEMA.properties); |
| 147 | + expect(rjsfSchema).to.have.property("dependencies"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("enum and enumNames can be added to schema properties", () => { |
| 151 | + const rjsfSchema = getSchemaWithDependencies({ |
| 152 | + schema: DFT_SCHEMA, |
| 153 | + nodes: [TREE], |
| 154 | + modifyProperties: true, |
| 155 | + }); |
| 156 | + // console.log(JSON.stringify(rjsfSchema, null, 4)); |
| 157 | + expect(rjsfSchema.type).to.be.eql(DFT_SCHEMA.type); |
| 158 | + expect(rjsfSchema.properties.type).to.have.property("enum"); |
| 159 | + expect(rjsfSchema.properties.type.enum).to.be.eql(["dft"]); |
| 160 | + expect(rjsfSchema.properties.type).to.have.property("enumNames"); |
| 161 | + expect(rjsfSchema.properties.type.enumNames).to.be.eql(["Density Functional Theory"]); |
| 162 | + expect(rjsfSchema).to.have.property("dependencies"); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe("Schema utility", () => { |
| 167 | + const schemas = [ |
| 168 | + ["string", { type: "string" }], |
| 169 | + ["integer", { type: "integer" }], |
| 170 | + ["number", { type: "number" }], |
| 171 | + ["object", { type: "object" }], |
| 172 | + ["array", { type: "array" }], |
| 173 | + ]; |
| 174 | + const objSchemaNoType = { properties: { name: { type: "string" } } }; |
| 175 | + const arraySchemaNoType = { items: { type: "number" } }; |
| 176 | + it("type can be determined correctly", () => { |
| 177 | + schemas.forEach(([type, schema]) => { |
| 178 | + const currentType = typeofSchema(schema); |
| 179 | + expect(currentType).to.be.equal(type); |
| 180 | + }); |
| 181 | + expect(typeofSchema(objSchemaNoType)).to.be.equal("object"); |
| 182 | + expect(typeofSchema(arraySchemaNoType)).to.be.equal("array"); |
| 183 | + }); |
| 184 | +}); |
0 commit comments