Browse code

Add test coverage to pkg/timeutils

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/05/29 22:09:35
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+package timeutils
1
+
2
+import (
3
+	"testing"
4
+	"time"
5
+)
6
+
7
+// Testing to ensure 'year' fields is between 0 and 9999
8
+func TestFastMarshalJSONWithInvalidDate(t *testing.T) {
9
+	aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
10
+	json, err := FastMarshalJSON(aTime)
11
+	if err == nil {
12
+		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
13
+	}
14
+	anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
15
+	json, err = FastMarshalJSON(anotherTime)
16
+	if err == nil {
17
+		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
18
+	}
19
+
20
+}
21
+
22
+func TestFastMarshalJSON(t *testing.T) {
23
+	aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
24
+	json, err := FastMarshalJSON(aTime)
25
+	if err != nil {
26
+		t.Fatal(err)
27
+	}
28
+	expected := "\"2015-05-29T11:01:02.000000003Z\""
29
+	if json != expected {
30
+		t.Fatalf("Expected %v, got %v", expected, json)
31
+	}
32
+
33
+	location, err := time.LoadLocation("Europe/Paris")
34
+	if err != nil {
35
+		t.Fatal(err)
36
+	}
37
+	aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
38
+	json, err = FastMarshalJSON(aTime)
39
+	if err != nil {
40
+		t.Fatal(err)
41
+	}
42
+	expected = "\"2015-05-29T11:01:02.000000003+02:00\""
43
+	if json != expected {
44
+		t.Fatalf("Expected %v, got %v", expected, json)
45
+	}
46
+}