-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
214c313
commit 68262dc
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package observations | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestUnmarshalObservation(t *testing.T) { | ||
data := []byte(` | ||
{ | ||
"phenomenonTime": "1970-01-01T00:00:00Z", | ||
"receivedTime": "1970-01-01T00:00:00Z", | ||
"result": 10 | ||
} | ||
`) | ||
var o Observation | ||
if err := json.Unmarshal(data, &o); err != nil { | ||
t.Fatalf("error during unmarshal json data: %s", err.Error()) | ||
} | ||
if o.PhenomenonTime.Unix() != 0 { | ||
t.Fatalf("error during parsing phenomenonTime: %d != 0", o.PhenomenonTime.Unix()) | ||
} | ||
} | ||
|
||
func TestUnmarshalErroneousObservation(t *testing.T) { | ||
data := []byte(` | ||
{ | ||
"phenomenonTime": "1970-01-01T00:00:00Z", | ||
"receivedTime": "1970-01-01T00:00:00Z", | ||
"result": 500 | ||
} | ||
`) | ||
var o Observation | ||
if err := json.Unmarshal(data, &o); err != nil { | ||
t.Fatalf("error during unmarshal json data: %s", err.Error()) | ||
} | ||
if o.Result != 255 { | ||
t.Fatalf("result should be reset to a valid value on overflow") | ||
} | ||
|
||
data = []byte(` | ||
{ | ||
"phenomenonTime": "1970-01-01T00:00:00Z", | ||
"receivedTime": "1970-01-01T00:00:00Z", | ||
"result": -1 | ||
} | ||
`) | ||
if err := json.Unmarshal(data, &o); err != nil { | ||
t.Fatalf("error during unmarshal json data: %s", err.Error()) | ||
} | ||
if o.Result != 0 { | ||
t.Fatalf("result should be reset to a valid value on underflow") | ||
} | ||
} |