| 1 | package null |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | timeString = "2012-12-21T21:21:21Z" |
| 11 | timeJSON = []byte(`"` + timeString + `"`) |
| 12 | nullTimeJSON = []byte(`null`) |
| 13 | timeValue, _ = time.Parse(time.RFC3339, timeString) |
| 14 | timeObject = []byte(`{"Time":"2012-12-21T21:21:21Z","Valid":true}`) |
| 15 | nullObject = []byte(`{"Time":"0001-01-01T00:00:00Z","Valid":false}`) |
| 16 | badObject = []byte(`{"hello": "world"}`) |
| 17 | ) |
| 18 | |
| 19 | func TestUnmarshalTimeJSON(t *testing.T) { |
| 20 | var ti Time |
| 21 | err := json.Unmarshal(timeJSON, &ti) |
| 22 | maybePanic(err) |
| 23 | assertTime(t, ti, "UnmarshalJSON() json") |
| 24 | |
| 25 | var null Time |
| 26 | err = json.Unmarshal(nullTimeJSON, &null) |
| 27 | maybePanic(err) |
| 28 | assertNullTime(t, null, "null time json") |
| 29 | |
| 30 | var fromObject Time |
| 31 | err = json.Unmarshal(timeObject, &fromObject) |
| 32 | maybePanic(err) |
| 33 | assertTime(t, fromObject, "time from object json") |
| 34 | |
| 35 | var nullFromObj Time |
| 36 | err = json.Unmarshal(nullObject, &nullFromObj) |
| 37 | maybePanic(err) |
| 38 | assertNullTime(t, nullFromObj, "null from object json") |
| 39 | |
| 40 | var invalid Time |
| 41 | err = invalid.UnmarshalJSON(invalidJSON) |
| 42 | if _, ok := err.(*json.SyntaxError); !ok { |
| 43 | t.Errorf("expected json.SyntaxError, not %T", err) |
| 44 | } |
| 45 | assertNullTime(t, invalid, "invalid from object json") |
| 46 | |
| 47 | var bad Time |
| 48 | err = json.Unmarshal(badObject, &bad) |
| 49 | if err == nil { |
| 50 | t.Errorf("expected error: bad object") |
| 51 | } |
| 52 | assertNullTime(t, bad, "bad from object json") |
| 53 | |
| 54 | var wrongType Time |
| 55 | err = json.Unmarshal(intJSON, &wrongType) |
| 56 | if err == nil { |
| 57 | t.Errorf("expected error: wrong type JSON") |
| 58 | } |
| 59 | assertNullTime(t, wrongType, "wrong type object json") |
| 60 | } |
| 61 | |
| 62 | func TestUnmarshalTimeText(t *testing.T) { |
| 63 | ti := TimeFrom(timeValue) |
| 64 | txt, err := ti.MarshalText() |
| 65 | maybePanic(err) |
| 66 | assertJSONEquals(t, txt, timeString, "marshal text") |
| 67 | |
| 68 | var unmarshal Time |
| 69 | err = unmarshal.UnmarshalText(txt) |
| 70 | maybePanic(err) |
| 71 | assertTime(t, unmarshal, "unmarshal text") |
| 72 | |
| 73 | var null Time |
| 74 | err = null.UnmarshalText(nullJSON) |
| 75 | maybePanic(err) |
| 76 | assertNullTime(t, null, "unmarshal null text") |
| 77 | txt, err = null.MarshalText() |
| 78 | maybePanic(err) |
| 79 | assertJSONEquals(t, txt, string(nullJSON), "marshal null text") |
| 80 | |
| 81 | var invalid Time |
| 82 | err = invalid.UnmarshalText([]byte("hello world")) |
| 83 | if err == nil { |
| 84 | t.Error("expected error") |
| 85 | } |
| 86 | assertNullTime(t, invalid, "bad string") |
| 87 | } |
| 88 | |
| 89 | func TestMarshalTime(t *testing.T) { |
| 90 | ti := TimeFrom(timeValue) |
| 91 | data, err := json.Marshal(ti) |
| 92 | maybePanic(err) |
| 93 | assertJSONEquals(t, data, string(timeJSON), "non-empty json marshal") |
| 94 | |
| 95 | ti.Valid = false |
| 96 | data, err = json.Marshal(ti) |
| 97 | maybePanic(err) |
| 98 | assertJSONEquals(t, data, string(nullJSON), "null json marshal") |
| 99 | } |
| 100 | |
| 101 | func TestTimeFrom(t *testing.T) { |
| 102 | ti := TimeFrom(timeValue) |
| 103 | assertTime(t, ti, "TimeFrom() time.Time") |
| 104 | } |
| 105 | |
| 106 | func TestTimeFromPtr(t *testing.T) { |
| 107 | ti := TimeFromPtr(&timeValue) |
| 108 | assertTime(t, ti, "TimeFromPtr() time") |
| 109 | |
| 110 | null := TimeFromPtr(nil) |
| 111 | assertNullTime(t, null, "TimeFromPtr(nil)") |
| 112 | } |
| 113 | |
| 114 | func TestTimeSetValid(t *testing.T) { |
| 115 | var ti time.Time |
| 116 | change := NewTime(ti, false) |
| 117 | assertNullTime(t, change, "SetValid()") |
| 118 | change.SetValid(timeValue) |
| 119 | assertTime(t, change, "SetValid()") |
| 120 | } |
| 121 | |
| 122 | func TestTimePointer(t *testing.T) { |
| 123 | ti := TimeFrom(timeValue) |
| 124 | ptr := ti.Ptr() |
| 125 | if *ptr != timeValue { |
| 126 | t.Errorf("bad %s time: %#v ≠ %v\n", "pointer", ptr, timeValue) |
| 127 | } |
| 128 | |
| 129 | var nt time.Time |
| 130 | null := NewTime(nt, false) |
| 131 | ptr = null.Ptr() |
| 132 | if ptr != nil { |
| 133 | t.Errorf("bad %s time: %#v ≠ %s\n", "nil pointer", ptr, "nil") |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestTimeScanValue(t *testing.T) { |
| 138 | var ti Time |
| 139 | err := ti.Scan(timeValue) |
| 140 | maybePanic(err) |
| 141 | assertTime(t, ti, "scanned time") |
| 142 | if v, err := ti.Value(); v != timeValue || err != nil { |
| 143 | t.Error("bad value or err:", v, err) |
| 144 | } |
| 145 | |
| 146 | var null Time |
| 147 | err = null.Scan(nil) |
| 148 | maybePanic(err) |
| 149 | assertNullTime(t, null, "scanned null") |
| 150 | if v, err := null.Value(); v != nil || err != nil { |
| 151 | t.Error("bad value or err:", v, err) |
| 152 | } |
| 153 | |
| 154 | var wrong Time |
| 155 | err = wrong.Scan(int64(42)) |
| 156 | if err == nil { |
| 157 | t.Error("expected error") |
| 158 | } |
| 159 | assertNullTime(t, wrong, "scanned wrong") |
| 160 | } |
| 161 | |
| 162 | func assertTime(t *testing.T, ti Time, from string) { |
| 163 | if ti.Time != timeValue { |
| 164 | t.Errorf("bad %v time: %v ≠ %v\n", from, ti.Time, timeValue) |
| 165 | } |
| 166 | if !ti.Valid { |
| 167 | t.Error(from, "is invalid, but should be valid") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func assertNullTime(t *testing.T, ti Time, from string) { |
| 172 | if ti.Valid { |
| 173 | t.Error(from, "is valid, but should be invalid") |
| 174 | } |
| 175 | } |