-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathSchema.Parser.Tests.fs
103 lines (85 loc) · 3.26 KB
/
Schema.Parser.Tests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module SwaggerProvider.Tests.v3
open Xunit
open FsUnitTyped
open System
open System.IO
module V2 =
open SwaggerProvider.Internal.v2.Compilers
open SwaggerProvider.Internal.v2.Parser
let testSchema schemaStr =
let schema = SwaggerParser.parseSchema schemaStr
let defCompiler = DefinitionCompiler(schema, false)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
ignore <| defCompiler.Namespace.GetProvidedTypes()
module V3 =
open SwaggerProvider.Internal.v3.Compilers
let testSchema schemaStr =
let openApiReader = Microsoft.OpenApi.Readers.OpenApiStringReader()
let schema, diagnostic = openApiReader.Read(schemaStr)
(* if diagnostic.Errors.Count > 0 then
failwithf "Schema parse errors:\n- %s"
(diagnostic.Errors
|> Seq.map (fun e -> e.Message)
|> String.concat ";\n- ")*)
try
let defCompiler = DefinitionCompiler(schema, false)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
ignore <| defCompiler.Namespace.GetProvidedTypes()
with e when e.Message.IndexOf("not supported yet") >= 0 ->
()
let parserTestBody(path: string) =
task {
let! schemaStr =
match Uri.TryCreate(path, UriKind.Absolute) with
| true, uri when path.IndexOf("http") >= 0 -> APIsGuru.httpClient.GetStringAsync(uri)
| _ when File.Exists(path) -> File.ReadAllTextAsync path
| _ -> failwithf $"Cannot find schema '%s{path}'"
if not <| String.IsNullOrEmpty(schemaStr) then
if path.IndexOf("v2") >= 0 then
V2.testSchema schemaStr
else
V3.testSchema schemaStr
}
let rootFolder =
Path.Combine(__SOURCE_DIRECTORY__, "../SwaggerProvider.ProviderTests/Schemas")
|> Path.GetFullPath
let allSchemas =
Directory.GetFiles(rootFolder, "*.*", SearchOption.AllDirectories)
|> List.ofArray
|> List.map(fun s -> Path.GetRelativePath(rootFolder, s))
let knownSchemaPaths =
allSchemas
|> List.filter(fun s -> s.IndexOf("unsupported") < 0)
|> List.map(fun s -> [| box s |])
[<Theory; MemberData(nameof(knownSchemaPaths))>]
let Parse file =
let file = Path.Combine(rootFolder, file)
parserTestBody file
let unsupportedSchemaPaths =
allSchemas
|> List.filter(fun s -> s.IndexOf("unsupported") > 0)
|> List.map(fun s -> [| box s |])
[<Theory(Skip = "no samples"); MemberData(nameof(unsupportedSchemaPaths))>]
let ``Fail to parse`` file =
let file = Path.Combine(rootFolder, file)
shouldFail(fun () -> parserTestBody file |> Async.AwaitTask |> Async.RunSynchronously)
[<Fact>]
let ``Parse PetStore``() =
parserTestBody(
__SOURCE_DIRECTORY__
+ "/../SwaggerProvider.ProviderTests/Schemas/v2/petstore.json"
)
(*
[<Tests>]
let parseJsonSchemaTests =
APIsGuru.Schemas.Value
|> List.ofArray
|> List.map (fun url ->
testCaseAsync
(sprintf "Parse %s" url)
(parserTestBody url)
)
|> testList "Integration/Schema"
*)