Skip to content

Commit db22586

Browse files
authored
Merge pull request #1575 from fsprojects/daily-test-improver-worldbank-coverage
Daily Test Coverage Improver: Add WorldBank Runtime Tests
2 parents d2fb4e8 + 1a2bcec commit db22586

2 files changed

Lines changed: 263 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Compile Include="HtmlOperations.fs" />
3838
<Compile Include="HtmlAttributeExtensions.fs" />
3939
<Compile Include="HtmlCssSelectors.fs" />
40+
<Compile Include="WorldBankRuntime.fs" />
4041
<Compile Include="Program.fs" />
4142
</ItemGroup>
4243
<ItemGroup>
@@ -54,5 +55,11 @@
5455
<ItemGroup>
5556
<PackageReference Update="FSharp.Core" Version="6.0.1" />
5657
</ItemGroup>
58+
<ItemGroup>
59+
<PackageReference Include="coverlet.collector" Version="6.0.4">
60+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
61+
<PrivateAssets>all</PrivateAssets>
62+
</PackageReference>
63+
</ItemGroup>
5764
<Import Project="..\..\.paket\Paket.Restore.targets" />
5865
</Project>
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
module FSharp.Data.Tests.WorldBankRuntime
2+
3+
open NUnit.Framework
4+
open FsUnit
5+
open System
6+
open System.Collections.Generic
7+
open FSharp.Data
8+
open FSharp.Data.Runtime.WorldBank
9+
open FSharp.Data.Runtime.Caching
10+
11+
// Test data structures and mock implementations
12+
type MockCache() =
13+
let cache = Dictionary<string, string>()
14+
15+
interface ICache<string, string> with
16+
member _.TryRetrieve(key, ?extendCacheExpiration) =
17+
ignore extendCacheExpiration
18+
if cache.ContainsKey(key) then Some(cache.[key]) else None
19+
member _.Set(key, value) = cache.[key] <- value
20+
member _.Remove(key) = cache.Remove(key) |> ignore
21+
22+
// Mock JSON responses that mimic WorldBank API responses
23+
let mockIndicatorResponse = """
24+
[
25+
{
26+
"page": 1,
27+
"pages": 1,
28+
"per_page": 1000,
29+
"total": 2
30+
},
31+
[
32+
{
33+
"id": "AG.AGR.TRAC.NO",
34+
"name": "Agricultural machinery, tractors",
35+
"source": {
36+
"id": "2",
37+
"value": "World Development Indicators"
38+
},
39+
"sourceNote": "Agricultural machinery refers to the number of wheel and crawler tractors.",
40+
"topics": [
41+
{
42+
"id": "9",
43+
"value": "Infrastructure"
44+
}
45+
]
46+
},
47+
{
48+
"id": "AG.CON.FERT.ZS",
49+
"name": "Fertilizer consumption (kilograms per hectare of arable land)",
50+
"source": {
51+
"id": "2",
52+
"value": "World Development Indicators"
53+
},
54+
"sourceNote": "Fertilizer consumption measures the quantity of plant nutrients used per unit of arable land.",
55+
"topics": [
56+
{
57+
"id": "6",
58+
"value": "Environment"
59+
}
60+
]
61+
}
62+
]
63+
]
64+
"""
65+
66+
let mockCountryResponse = """
67+
[
68+
{
69+
"page": 1,
70+
"pages": 1,
71+
"per_page": 1000,
72+
"total": 3
73+
},
74+
[
75+
{
76+
"id": "ABW",
77+
"name": "Aruba",
78+
"capitalCity": "Oranjestad",
79+
"region": {
80+
"id": "LCN",
81+
"value": "Latin America & Caribbean"
82+
}
83+
},
84+
{
85+
"id": "AFG",
86+
"name": "Afghanistan",
87+
"capitalCity": "Kabul",
88+
"region": {
89+
"id": "SAS",
90+
"value": "South Asia"
91+
}
92+
},
93+
{
94+
"id": "WLD",
95+
"name": "World",
96+
"capitalCity": "",
97+
"region": {
98+
"id": "NA",
99+
"value": "Aggregates"
100+
}
101+
}
102+
]
103+
]
104+
"""
105+
106+
let mockTopicResponse = """
107+
[
108+
{
109+
"page": 1,
110+
"pages": 1,
111+
"per_page": 1000,
112+
"total": 2
113+
},
114+
[
115+
{
116+
"id": "1",
117+
"value": "Agriculture & Rural Development",
118+
"sourceNote": "For the 70 percent of the world's poor who live in rural areas, agriculture is the main source of income and employment."
119+
},
120+
{
121+
"id": "2",
122+
"value": "Aid Effectiveness",
123+
"sourceNote": "Aid effectiveness is the impact that aid has in reducing poverty and inequality."
124+
}
125+
]
126+
]
127+
"""
128+
129+
let mockRegionResponse = """
130+
[
131+
{
132+
"page": 1,
133+
"pages": 1,
134+
"per_page": 1000,
135+
"total": 2
136+
},
137+
[
138+
{
139+
"code": "EAS",
140+
"name": "East Asia & Pacific"
141+
},
142+
{
143+
"code": "ECS",
144+
"name": "Europe & Central Asia"
145+
}
146+
]
147+
]
148+
"""
149+
150+
let mockDataResponse = """
151+
[
152+
{
153+
"page": 1,
154+
"pages": 1,
155+
"per_page": 1000,
156+
"total": 3
157+
},
158+
[
159+
{
160+
"date": "2020",
161+
"value": "65894.86"
162+
},
163+
{
164+
"date": "2019",
165+
"value": "62888.17"
166+
},
167+
{
168+
"date": "2018",
169+
"value": null
170+
}
171+
]
172+
]
173+
"""
174+
175+
[<Test>]
176+
let ``IndicatorRecord should have correct IsRegion property for regular country`` () =
177+
let country = { Id = "USA"; Name = "United States"; CapitalCity = "Washington"; Region = "North America" }
178+
country.IsRegion |> should be False
179+
180+
[<Test>]
181+
let ``IndicatorRecord should have correct IsRegion property for aggregate region`` () =
182+
let region = { Id = "WLD"; Name = "World"; CapitalCity = ""; Region = "Aggregates" }
183+
region.IsRegion |> should be True
184+
185+
[<Test>]
186+
let ``IndicatorRecord should be constructed with all required fields`` () =
187+
let indicator = {
188+
Id = "AG.AGR.TRAC.NO"
189+
Name = "Agricultural machinery, tractors"
190+
TopicIds = ["9"]
191+
Source = "World Development Indicators"
192+
Description = "Agricultural machinery refers to the number of wheel and crawler tractors."
193+
}
194+
195+
indicator.Id |> should equal "AG.AGR.TRAC.NO"
196+
indicator.Name |> should equal "Agricultural machinery, tractors"
197+
indicator.TopicIds |> should equal ["9"]
198+
indicator.Source |> should equal "World Development Indicators"
199+
indicator.Description |> should equal "Agricultural machinery refers to the number of wheel and crawler tractors."
200+
201+
[<Test>]
202+
let ``CountryRecord should be constructed with all required fields`` () =
203+
let country = {
204+
Id = "USA"
205+
Name = "United States"
206+
CapitalCity = "Washington"
207+
Region = "North America"
208+
}
209+
210+
country.Id |> should equal "USA"
211+
country.Name |> should equal "United States"
212+
country.CapitalCity |> should equal "Washington"
213+
country.Region |> should equal "North America"
214+
country.IsRegion |> should be False
215+
216+
[<Test>]
217+
let ``TopicRecord should be constructed with all required fields`` () =
218+
let topic = {
219+
Id = "1"
220+
Name = "Agriculture & Rural Development"
221+
Description = "For the 70 percent of the world's poor who live in rural areas, agriculture is the main source of income and employment."
222+
}
223+
224+
topic.Id |> should equal "1"
225+
topic.Name |> should equal "Agriculture & Rural Development"
226+
topic.Description |> should equal "For the 70 percent of the world's poor who live in rural areas, agriculture is the main source of income and employment."
227+
228+
[<Test>]
229+
let ``WorldBankData should construct with sources`` () =
230+
let data = WorldBankData("http://api.worldbank.org", "World Development Indicators")
231+
data |> should not' (be Null)
232+
233+
[<Test>]
234+
let ``WorldBankData should construct with multiple sources`` () =
235+
let data = WorldBankData("http://api.worldbank.org", "World Development Indicators;Global Financial Development")
236+
data |> should not' (be Null)
237+
238+
[<Test>]
239+
let ``WorldBankData should handle empty sources`` () =
240+
let data = WorldBankData("http://api.worldbank.org", "")
241+
data |> should not' (be Null)
242+
243+
// Test Http.AppendQueryToUrl functionality used by WorldBank
244+
[<Test>]
245+
let ``Http.AppendQueryToUrl should construct proper URL with query parameters`` () =
246+
let baseUrl = "http://api.worldbank.org/indicator"
247+
let query = [("per_page", "1000"); ("format", "json")]
248+
let result = Http.AppendQueryToUrl(baseUrl, query)
249+
result |> should equal "http://api.worldbank.org/indicator?per_page=1000&format=json"
250+
251+
[<Test>]
252+
let ``Http.AppendQueryToUrl should handle special characters`` () =
253+
let baseUrl = "http://api.worldbank.org/country"
254+
let query = [("region", "Latin America & Caribbean")]
255+
let result = Http.AppendQueryToUrl(baseUrl, query)
256+
result |> should contain "region=Latin%20America%20%26%20Caribbean"

0 commit comments

Comments
 (0)