Code

Fixed compatibility issues with Go 1.0 and 1.1.
[sysdb/go.git] / sysdb / store_test.go
1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 package sysdb
28 import (
29         "testing"
30         "time"
31 )
33 func TestMarshalDuration(t *testing.T) {
34         for _, test := range []struct {
35                 d        Duration
36                 expected string
37         }{
38                 {Duration(0), `"0s"`},
39                 {Duration(123), `".000000123s"`},
40                 {Duration(1000123000), `"1.000123s"`},
41                 {Duration(47940228000000000), `"1Y6M7D"`},
42                 {Second, `"1s"`},
43                 {Minute, `"1m"`},
44                 {Hour, `"1h"`},
45                 {Day, `"1D"`},
46                 {Month, `"1M"`},
47                 {Year, `"1Y"`},
48                 {Year + Day + Minute, `"1Y1D1m"`},
49         } {
50                 got, err := test.d.MarshalJSON()
51                 if err != nil || string(got) != test.expected {
52                         t.Errorf("%s.MarshalJSON() = %s, %v; want %s, <nil>",
53                                 test.d, string(got), err, test.expected)
54                 }
55         }
56 }
58 func TestUnmarshalDuration(t *testing.T) {
59         for _, test := range []struct {
60                 data     string
61                 expected Duration
62                 err      bool
63         }{
64                 {"0s", 0, true},  // unquoted
65                 {`"0"`, 0, true}, // missing unit
66                 {`"0.0"`, 0, true},
67                 {`".0"`, 0, true},
68                 {`"s"`, 0, true},   // missing decimal
69                 {`"1y"`, 0, true},  // invalid unit
70                 {`"abc"`, 0, true}, // all invalid
71                 {`"0s"`, 0, false},
72                 {`"1.0s"`, Second, false},
73                 {`".5s"`, 500000000, false},
74                 {`"1.000123s"`, 1000123000, false},
75                 {`"1.0001234s"`, 1000123400, false},
76                 {`"1.00012345s"`, 1000123450, false},
77                 {`"1.000123456s"`, 1000123456, false},
78                 {`"1.0001234567s"`, 1000123456, false},
79                 {`"1.000123000123s"`, 1000123000, false},
80                 {`"1Y6M7D"`, 47940228000000000, false},
81                 {`"1s"`, Second, false},
82                 {`"1m"`, Minute, false},
83                 {`"1h"`, Hour, false},
84                 {`"1D"`, Day, false},
85                 {`"1M"`, Month, false},
86                 {`"1Y"`, Year, false},
87         } {
88                 var d Duration
89                 err := d.UnmarshalJSON([]byte(test.data))
90                 if (err != nil) != test.err || d != test.expected {
91                         e := "<nil>"
92                         if test.err {
93                                 e = "<err>"
94                         }
95                         t.Errorf("UnmarshalJSON(%s) = %v (%s); want %s (%s)",
96                                 test.data, err, d, e, test.expected)
97                 }
98         }
99 }
101 func TestMarshalTime(t *testing.T) {
102         tm := Time(time.Date(2014, 9, 18, 23, 42, 12, 123, time.UTC))
103         expected := `"2014-09-18 23:42:12 +0000"`
104         got, err := tm.MarshalJSON()
105         if err != nil || string(got) != expected {
106                 t.Errorf("%s.MarshalJSON() = %s, %v; %s, <nil>", tm, got, err, expected)
107         }
110 func TestUnmarshalTime(t *testing.T) {
111         for _, test := range []struct {
112                 data     string
113                 expected Time
114                 err      bool
115         }{
116                 {
117                         `"2014-09-18 23:42:12 +0000"`,
118                         Time(time.Date(2014, 9, 18, 23, 42, 12, 0, time.UTC)),
119                         false,
120                 },
121                 {
122                         `2014-09-18 23:42:12 +0000`,
123                         Time{},
124                         true,
125                 },
126                 {
127                         `"2014-09-18T23:42:12Z00:00"`,
128                         Time{},
129                         true,
130                 },
131         } {
132                 var tm Time
133                 err := tm.UnmarshalJSON([]byte(test.data))
134                 if (err != nil) != test.err || !tm.Equal(test.expected) {
135                         e := "<nil>"
136                         if test.err {
137                                 e = "<err>"
138                         }
139                         t.Errorf("UnmarshalJSON(%s) = %v (%s); want %s (%s)",
140                                 test.data, err, tm, e, test.expected)
141                 }
142         }
145 // vim: set tw=78 sw=4 sw=4 noexpandtab :