git.iko.soy/typeable/swagger2
on master help sign in
Add some tests for schema inlining
commit148d95a729b6af8b81336ef9e4180a6db60ed395 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSun 13 Dec 2015, 15:09 (11 years ago) parentcd7ef9c
1 file changed+137
test/Data/Swagger/SchemaSpec.hs+137
test/Data/Swagger/SchemaSpec.hs+137
@@ -37,6 +37,19 @@ checkDefs proxy names =
3737 where
3838 defs = execDeclare (declareNamedSchema proxy) mempty
3939
40+checkInlinedSchema :: ToSchema a => Proxy a -> Value -> Spec
41+checkInlinedSchema proxy js = toInlinedSchema proxy <=> js
42+
43+checkInlinedSchemas :: ToSchema a => [String] -> Proxy a -> Value -> Spec
44+checkInlinedSchemas names proxy js = inlineSchemas (map Text.pack names) defs schema <=> js
45+ where
46+ (defs, schema) = runDeclare (declareSchema proxy) mempty
47+
48+checkInlinedRecSchema :: ToSchema a => Proxy a -> Value -> Spec
49+checkInlinedRecSchema proxy js = inlineNonRecursiveSchemas defs schema <=> js
50+ where
51+ (defs, schema) = runDeclare (declareSchema proxy) mempty
52+
4053spec :: Spec
4154spec = do
4255 describe "Generic ToSchema" $ do
@@ -70,6 +83,12 @@ spec = do
7083 context "Character" $ checkDefs (Proxy :: Proxy Character) ["Player", "Point"]
7184 context "MyRoseTree" $ checkDefs (Proxy :: Proxy MyRoseTree) ["RoseTree"]
7285 context "[Set (Unit, Maybe Color)]" $ checkDefs (Proxy :: Proxy [Set (Unit, Maybe Color)]) ["Unit", "Color"]
86+ describe "Inlining Schemas" $ do
87+ context "Paint" $ checkInlinedSchema (Proxy :: Proxy Paint) paintInlinedSchemaJSON
88+ context "Character" $ checkInlinedSchema (Proxy :: Proxy Character) characterInlinedSchemaJSON
89+ context "Character (inlining only Player)" $ checkInlinedSchemas ["Player"] (Proxy :: Proxy Character) characterInlinedPlayerSchemaJSON
90+ context "Light" $ checkInlinedSchema (Proxy :: Proxy Light) lightInlinedSchemaJSON
91+ context "MyRoseTree (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree) myRoseTreeSchemaJSON
7392
7493main :: IO ()
7594main = hspec spec
@@ -196,6 +215,22 @@ paintSchemaJSON = [aesonQQ|
196215}
197216|]
198217
218+paintInlinedSchemaJSON :: Value
219+paintInlinedSchemaJSON = [aesonQQ|
220+{
221+ "type": "object",
222+ "properties":
223+ {
224+ "color":
225+ {
226+ "type": "string",
227+ "enum": ["Red", "Green", "Blue"]
228+ }
229+ },
230+ "required": ["color"]
231+}
232+|]
233+
199234-- ========================================================================
200235-- Email (newtype with unwrapUnaryRecords set to True)
201236-- ========================================================================
@@ -407,6 +442,89 @@ characterSchemaJSON = [aesonQQ|
407442}
408443|]
409444
445+characterInlinedSchemaJSON :: Value
446+characterInlinedSchemaJSON = [aesonQQ|
447+{
448+ "type": "object",
449+ "properties":
450+ {
451+ "PC":
452+ {
453+ "type": "object",
454+ "properties":
455+ {
456+ "position":
457+ {
458+ "type": "object",
459+ "properties":
460+ {
461+ "x": { "type": "number" },
462+ "y": { "type": "number" }
463+ },
464+ "required": ["x", "y"]
465+ }
466+ },
467+ "required": ["position"]
468+ },
469+ "NPC":
470+ {
471+ "type": "object",
472+ "properties":
473+ {
474+ "npcName": { "type": "string" },
475+ "npcPosition":
476+ {
477+ "type": "object",
478+ "properties":
479+ {
480+ "x": { "type": "number" },
481+ "y": { "type": "number" }
482+ },
483+ "required": ["x", "y"]
484+ }
485+ },
486+ "required": ["npcName", "npcPosition"]
487+ }
488+ },
489+ "maxProperties": 1,
490+ "minProperties": 1
491+}
492+|]
493+
494+characterInlinedPlayerSchemaJSON :: Value
495+characterInlinedPlayerSchemaJSON = [aesonQQ|
496+{
497+ "type": "object",
498+ "properties":
499+ {
500+ "PC":
501+ {
502+ "type": "object",
503+ "properties":
504+ {
505+ "position":
506+ {
507+ "$ref": "#/definitions/Point"
508+ }
509+ },
510+ "required": ["position"]
511+ },
512+ "NPC":
513+ {
514+ "type": "object",
515+ "properties":
516+ {
517+ "npcName": { "type": "string" },
518+ "npcPosition": { "$ref": "#/definitions/Point" }
519+ },
520+ "required": ["npcName", "npcPosition"]
521+ }
522+ },
523+ "maxProperties": 1,
524+ "minProperties": 1
525+}
526+|]
527+
410528-- ========================================================================
411529-- Light (sum type with unwrapUnaryRecords)
412530-- ========================================================================
@@ -436,3 +554,22 @@ lightSchemaJSON = [aesonQQ|
436554}
437555|]
438556
557+lightInlinedSchemaJSON :: Value
558+lightInlinedSchemaJSON = [aesonQQ|
559+{
560+ "type": "object",
561+ "properties":
562+ {
563+ "LightFreq": { "type": "number" },
564+ "LightColor":
565+ {
566+ "type": "string",
567+ "enum": ["Red", "Green", "Blue"]
568+ },
569+ "LightWaveLength": { "type": "number" }
570+ },
571+ "maxProperties": 1,
572+ "minProperties": 1
573+}
574+|]
575+