Repository navigation
Expand file tree
/
Copy pathcountry_geography_test.go
More file actions
40 lines (37 loc) · 2.04 KB
/
Copy pathcountry_geography_test.go
File metadata and controls
40 lines (37 loc) · 2.04 KB
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
package parseapi
import (
"encoding/json"
"testing"
)
func TestCountryGeographyValues(t *testing.T) {
var country Country
err := json.Unmarshal([]byte(`{"country":"XX","deep":{"gdp":30769700000000,"gdp_per_capita":90026.5163,"population_growth":-0.25,"urban_population":0,"internet_users":95.5,"land_area":10010.5,"water_area":0,"coastline":0,"elevation":0,"lowest_point":{"name":null,"elevation":-430.5},"highest_point":{"name":"Summit","elevation":8848.86}}}`), &country)
if err != nil {
t.Fatal(err)
}
c := country.Deep
if c == nil || c.GDP == nil || *c.GDP != 30769700000000 || c.GDPPerCapita == nil || *c.GDPPerCapita != 90026.5163 || c.PopulationGrowth == nil || *c.PopulationGrowth != -0.25 || c.UrbanPopulation == nil || *c.UrbanPopulation != 0 || c.InternetUsers == nil || *c.InternetUsers != 95.5 {
t.Fatalf("country statistics: %#v", c)
}
if c == nil || c.LandArea == nil || *c.LandArea != 10010.5 || c.WaterArea == nil || *c.WaterArea != 0 || c.Coastline == nil || *c.Coastline != 0 || c.Elevation == nil || *c.Elevation != 0 {
t.Fatalf("country geography: %#v", c)
}
if c.LowestPoint == nil || c.LowestPoint.Name != nil || c.LowestPoint.Elevation != -430.5 || c.HighestPoint == nil || c.HighestPoint.Name == nil || *c.HighestPoint.Name != "Summit" || c.HighestPoint.Elevation != 8848.86 {
t.Fatalf("country extremes: %#v / %#v", c.LowestPoint, c.HighestPoint)
}
}
func TestCountryGeographyMissingAndNull(t *testing.T) {
for _, body := range []string{`{}`, `{"deep":{}}`, `{"deep":{"land_area":null,"water_area":null,"coastline":null,"elevation":null,"lowest_point":null,"highest_point":null}}`} {
var country Country
if err := json.Unmarshal([]byte(body), &country); err != nil {
t.Fatal(err)
}
if (country.Deep == nil) != (body == `{}`) {
t.Fatal("omitted and locked deep were conflated")
}
c := country.Deep
if c != nil && (c.LandArea != nil || c.WaterArea != nil || c.Coastline != nil || c.Elevation != nil || c.LowestPoint != nil || c.HighestPoint != nil) {
t.Fatalf("unknown country geography became known: %#v", c)
}
}
}