|
| 1 | +package eo_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/planetlabs/go-stac/extensions/eo" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/planetlabs/go-stac" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestItemExtendedMarshal(t *testing.T) { |
| 14 | + cloudCover := float64(25) |
| 15 | + snowCover := float64(10) |
| 16 | + centerWavelength := float64(0.85) |
| 17 | + item := &stac.Item{ |
| 18 | + Version: "1.0.0", |
| 19 | + Id: "item-id", |
| 20 | + Geometry: map[string]any{ |
| 21 | + "type": "Point", |
| 22 | + "coordinates": []float64{0, 0}, |
| 23 | + }, |
| 24 | + Properties: map[string]any{ |
| 25 | + "test": "value", |
| 26 | + }, |
| 27 | + Links: []*stac.Link{ |
| 28 | + {Href: "https://example.com/stac/item-id", Rel: "self"}, |
| 29 | + }, |
| 30 | + Assets: map[string]*stac.Asset{ |
| 31 | + "image": { |
| 32 | + Title: "Image", |
| 33 | + Href: "https://example.com/stac/item-id/image.tif", |
| 34 | + Type: "image/tif", |
| 35 | + Extensions: []stac.Extension{ |
| 36 | + &eo.Asset{ |
| 37 | + CloudCover: &cloudCover, |
| 38 | + SnowCover: &snowCover, |
| 39 | + Bands: []*eo.Band{ |
| 40 | + { |
| 41 | + Name: "NIR", |
| 42 | + CenterWavelength: ¢erWavelength, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + data, err := json.Marshal(item) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + expected := `{ |
| 55 | + "type": "Feature", |
| 56 | + "stac_version": "1.0.0", |
| 57 | + "id": "item-id", |
| 58 | + "geometry": { |
| 59 | + "type": "Point", |
| 60 | + "coordinates": [0, 0] |
| 61 | + }, |
| 62 | + "properties": { |
| 63 | + "test": "value" |
| 64 | + }, |
| 65 | + "links": [ |
| 66 | + { |
| 67 | + "rel": "self", |
| 68 | + "href": "https://example.com/stac/item-id" |
| 69 | + } |
| 70 | + ], |
| 71 | + "assets": { |
| 72 | + "image": { |
| 73 | + "title": "Image", |
| 74 | + "href": "https://example.com/stac/item-id/image.tif", |
| 75 | + "type": "image/tif", |
| 76 | + "eo:cloud_cover": 25, |
| 77 | + "eo:snow_cover": 10, |
| 78 | + "eo:bands": [ |
| 79 | + { |
| 80 | + "name": "NIR", |
| 81 | + "center_wavelength": 0.85 |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + }, |
| 86 | + "stac_extensions": [ |
| 87 | + "https://stac-extensions.github.io/eo/v1.1.0/schema.json" |
| 88 | + ] |
| 89 | + }` |
| 90 | + |
| 91 | + assert.JSONEq(t, expected, string(data)) |
| 92 | +} |
| 93 | + |
| 94 | +func TestItemExtendedUnmarshal(t *testing.T) { |
| 95 | + data := []byte(`{ |
| 96 | + "type": "Feature", |
| 97 | + "stac_version": "1.0.0", |
| 98 | + "id": "item-id", |
| 99 | + "geometry": { |
| 100 | + "type": "Point", |
| 101 | + "coordinates": [0, 0] |
| 102 | + }, |
| 103 | + "properties": { |
| 104 | + "test": "value" |
| 105 | + }, |
| 106 | + "links": [ |
| 107 | + { |
| 108 | + "rel": "self", |
| 109 | + "href": "https://example.com/stac/item-id" |
| 110 | + } |
| 111 | + ], |
| 112 | + "assets": { |
| 113 | + "image": { |
| 114 | + "title": "Image", |
| 115 | + "href": "https://example.com/stac/item-id/image.tif", |
| 116 | + "type": "image/tif", |
| 117 | + "eo:cloud_cover": 25, |
| 118 | + "eo:snow_cover": 10, |
| 119 | + "eo:bands": [ |
| 120 | + { |
| 121 | + "name": "NIR", |
| 122 | + "center_wavelength": 0.85 |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | + }, |
| 127 | + "stac_extensions": [ |
| 128 | + "https://stac-extensions.github.io/eo/v1.1.0/schema.json" |
| 129 | + ] |
| 130 | + }`) |
| 131 | + |
| 132 | + item := &stac.Item{} |
| 133 | + require.NoError(t, json.Unmarshal(data, item)) |
| 134 | + |
| 135 | + cloudCover := float64(25) |
| 136 | + snowCover := float64(10) |
| 137 | + centerWavelength := float64(0.85) |
| 138 | + expected := &stac.Item{ |
| 139 | + Version: "1.0.0", |
| 140 | + Id: "item-id", |
| 141 | + Geometry: map[string]any{ |
| 142 | + "type": "Point", |
| 143 | + "coordinates": []any{float64(0), float64(0)}, |
| 144 | + }, |
| 145 | + Properties: map[string]any{ |
| 146 | + "test": "value", |
| 147 | + }, |
| 148 | + Links: []*stac.Link{ |
| 149 | + {Href: "https://example.com/stac/item-id", Rel: "self"}, |
| 150 | + }, |
| 151 | + Assets: map[string]*stac.Asset{ |
| 152 | + "image": { |
| 153 | + Title: "Image", |
| 154 | + Href: "https://example.com/stac/item-id/image.tif", |
| 155 | + Type: "image/tif", |
| 156 | + Extensions: []stac.Extension{ |
| 157 | + &eo.Asset{ |
| 158 | + CloudCover: &cloudCover, |
| 159 | + SnowCover: &snowCover, |
| 160 | + Bands: []*eo.Band{ |
| 161 | + { |
| 162 | + Name: "NIR", |
| 163 | + CenterWavelength: ¢erWavelength, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + assert.Equal(t, expected, item) |
| 173 | +} |
0 commit comments