Skip to content

Commit 2178b72

Browse files
authored
Merge pull request #1590 from fsprojects/daily-test-improver-http-json-coverage
Daily Test Coverage Improver: Add comprehensive tests for HttpContentTypes and JsonDocument
2 parents 55ea965 + 82e184a commit 2178b72

3 files changed

Lines changed: 197 additions & 0 deletions

File tree

tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2020
</EmbeddedResource>
2121
<Compile Include="Http.fs" />
22+
<Compile Include="HttpContentTypes.fs" />
2223
<Compile Include="HttpRequestHeaders.fs" />
2324
<Compile Include="NameUtils.fs" />
2425
<Compile Include="Pluralizer.fs" />
@@ -29,6 +30,7 @@
2930
<Compile Include="JsonValue.fs" />
3031
<Compile Include="JsonParserProperties.fs" />
3132
<Compile Include="JsonConversions.fs" />
33+
<Compile Include="JsonDocument.fs" />
3234
<Compile Include="JsonRuntime.fs" />
3335
<Compile Include="JsonSchema.fs" />
3436
<Compile Include="CsvReader.fs" />
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module FSharp.Data.Tests.HttpContentTypes
2+
3+
open NUnit.Framework
4+
open FsUnit
5+
open FSharp.Data
6+
7+
[<Test>]
8+
let ``HttpContentTypes.Any should return correct MIME type`` () =
9+
HttpContentTypes.Any |> should equal "*/*"
10+
11+
[<Test>]
12+
let ``HttpContentTypes.Text should return correct MIME type`` () =
13+
HttpContentTypes.Text |> should equal "text/plain"
14+
15+
[<Test>]
16+
let ``HttpContentTypes.Binary should return correct MIME type`` () =
17+
HttpContentTypes.Binary |> should equal "application/octet-stream"
18+
19+
[<Test>]
20+
let ``HttpContentTypes.Zip should return correct MIME type`` () =
21+
HttpContentTypes.Zip |> should equal "application/zip"
22+
23+
[<Test>]
24+
let ``HttpContentTypes.GZip should return correct MIME type`` () =
25+
HttpContentTypes.GZip |> should equal "application/gzip"
26+
27+
[<Test>]
28+
let ``HttpContentTypes.FormValues should return correct MIME type`` () =
29+
HttpContentTypes.FormValues |> should equal "application/x-www-form-urlencoded"
30+
31+
[<Test>]
32+
let ``HttpContentTypes.Json should return correct MIME type`` () =
33+
HttpContentTypes.Json |> should equal "application/json"
34+
35+
[<Test>]
36+
let ``HttpContentTypes.JavaScript should return correct MIME type`` () =
37+
HttpContentTypes.JavaScript |> should equal "application/javascript"
38+
39+
[<Test>]
40+
let ``HttpContentTypes.Xml should return correct MIME type`` () =
41+
HttpContentTypes.Xml |> should equal "application/xml"
42+
43+
[<Test>]
44+
let ``HttpContentTypes.Rss should return correct MIME type`` () =
45+
HttpContentTypes.Rss |> should equal "application/rss+xml"
46+
47+
[<Test>]
48+
let ``HttpContentTypes.Atom should return correct MIME type`` () =
49+
HttpContentTypes.Atom |> should equal "application/atom+xml"
50+
51+
[<Test>]
52+
let ``HttpContentTypes.Rdf should return correct MIME type`` () =
53+
HttpContentTypes.Rdf |> should equal "application/rdf+xml"
54+
55+
[<Test>]
56+
let ``HttpContentTypes.Html should return correct MIME type`` () =
57+
HttpContentTypes.Html |> should equal "text/html"
58+
59+
[<Test>]
60+
let ``HttpContentTypes.XHtml should return correct MIME type`` () =
61+
HttpContentTypes.XHtml |> should equal "application/xhtml+xml"
62+
63+
[<Test>]
64+
let ``HttpContentTypes.Soap should return correct MIME type`` () =
65+
HttpContentTypes.Soap |> should equal "application/soap+xml"
66+
67+
[<Test>]
68+
let ``HttpContentTypes.Csv should return correct MIME type`` () =
69+
HttpContentTypes.Csv |> should equal "text/csv"
70+
71+
[<Test>]
72+
let ``HttpContentTypes.JsonRpc should return correct MIME type`` () =
73+
HttpContentTypes.JsonRpc |> should equal "application/json-rpc"
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
module FSharp.Data.Tests.JsonDocument
2+
3+
open NUnit.Framework
4+
open FsUnit
5+
open FSharp.Data
6+
open FSharp.Data.Runtime.BaseTypes
7+
open System.IO
8+
open System.Reflection
9+
10+
// Use reflection to access the "generated code only" methods for testing
11+
let private getCreateMethod() =
12+
typeof<JsonDocument>.GetMethod("Create", [| typeof<JsonValue>; typeof<string> |])
13+
14+
let private getCreateFromReaderMethod() =
15+
typeof<JsonDocument>.GetMethod("Create", [| typeof<System.IO.TextReader> |])
16+
17+
let private getCreateListMethod() =
18+
typeof<JsonDocument>.GetMethod("CreateList", [| typeof<System.IO.TextReader> |])
19+
20+
[<Test>]
21+
let ``JsonDocument.Create with JsonValue should return IJsonDocument using reflection`` () =
22+
let createMethod = getCreateMethod()
23+
let jsonValue = JsonValue.Number 42M
24+
let doc = createMethod.Invoke(null, [| jsonValue; "/path" |]) :?> IJsonDocument
25+
26+
doc |> should not' (be null)
27+
doc.JsonValue |> should equal jsonValue
28+
29+
[<Test>]
30+
let ``JsonDocument.Create with TextReader should parse JSON using reflection`` () =
31+
let createMethod = getCreateFromReaderMethod()
32+
let json = """{"name": "test", "value": 123}"""
33+
use reader = new StringReader(json)
34+
let doc = createMethod.Invoke(null, [| reader |]) :?> IJsonDocument
35+
36+
doc |> should not' (be null)
37+
doc.JsonValue |> should not' (be null)
38+
39+
[<Test>]
40+
let ``JsonDocument.CreateList with single array should return array elements using reflection`` () =
41+
let createListMethod = getCreateListMethod()
42+
let json = """[{"id": 1}, {"id": 2}]"""
43+
use reader = new StringReader(json)
44+
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]
45+
46+
docs |> should haveLength 2
47+
docs.[0].JsonValue.["id"].AsInteger() |> should equal 1
48+
docs.[1].JsonValue.["id"].AsInteger() |> should equal 2
49+
50+
[<Test>]
51+
let ``JsonDocument.CreateList with multiple JSON objects should return separate documents using reflection`` () =
52+
let createListMethod = getCreateListMethod()
53+
let json = """{"id": 1}{"id": 2}"""
54+
use reader = new StringReader(json)
55+
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]
56+
57+
docs |> should haveLength 2
58+
docs.[0].JsonValue.["id"].AsInteger() |> should equal 1
59+
docs.[1].JsonValue.["id"].AsInteger() |> should equal 2
60+
61+
[<Test>]
62+
let ``JsonDocument ToString should return JsonValue string representation using reflection`` () =
63+
let createMethod = getCreateMethod()
64+
let jsonValue = JsonValue.Number 42M
65+
let docObj = createMethod.Invoke(null, [| jsonValue; "/test" |])
66+
67+
docObj.ToString() |> should equal "42"
68+
69+
[<Test>]
70+
let ``JsonDocument JsonValue property should return original JsonValue using reflection`` () =
71+
let createMethod = getCreateMethod()
72+
let jsonValue = JsonValue.String "test"
73+
let doc = createMethod.Invoke(null, [| jsonValue; "/test" |]) :?> IJsonDocument
74+
75+
doc.JsonValue |> should equal jsonValue
76+
77+
[<Test>]
78+
let ``IJsonDocument Path method should return path using reflection`` () =
79+
let createMethod = getCreateMethod()
80+
let jsonValue = JsonValue.Boolean true
81+
let doc = createMethod.Invoke(null, [| jsonValue; "/root/item" |]) :?> IJsonDocument
82+
83+
// Use reflection to call the Path method to avoid the compiler message
84+
let pathMethod = typeof<IJsonDocument>.GetMethod("Path")
85+
let path = pathMethod.Invoke(doc, [||]) :?> string
86+
87+
path |> should equal "/root/item"
88+
89+
[<Test>]
90+
let ``IJsonDocument CreateNew should create new document with incremented path using reflection`` () =
91+
let createMethod = getCreateMethod()
92+
let jsonValue = JsonValue.Array [| JsonValue.Number 1M; JsonValue.Number 2M |]
93+
let originalDoc = createMethod.Invoke(null, [| jsonValue; "/root" |]) :?> IJsonDocument
94+
let newValue = JsonValue.Number 42M
95+
96+
// Use reflection to call CreateNew to avoid the compiler message
97+
let createNewMethod = typeof<IJsonDocument>.GetMethod("CreateNew")
98+
let newDoc = createNewMethod.Invoke(originalDoc, [| newValue; "/item[0]" |]) :?> IJsonDocument
99+
100+
newDoc.JsonValue |> should equal newValue
101+
let pathMethod = typeof<IJsonDocument>.GetMethod("Path")
102+
let path = pathMethod.Invoke(newDoc, [||]) :?> string
103+
path |> should equal "/root/item[0]"
104+
105+
[<Test>]
106+
let ``JsonDocument.Create with empty JSON should work using reflection`` () =
107+
let createMethod = getCreateFromReaderMethod()
108+
let json = "{}"
109+
use reader = new StringReader(json)
110+
let doc = createMethod.Invoke(null, [| reader |]) :?> IJsonDocument
111+
112+
doc |> should not' (be null)
113+
doc.JsonValue |> should not' (be null)
114+
115+
[<Test>]
116+
let ``JsonDocument.CreateList with empty array should return empty array using reflection`` () =
117+
let createListMethod = getCreateListMethod()
118+
let json = "[]"
119+
use reader = new StringReader(json)
120+
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]
121+
122+
docs |> should haveLength 0

0 commit comments

Comments
 (0)