File: datastores_parser_test.go

package info (click to toggle)
golang-github-stacktic-dropbox 0.0~git20160424.0.58f839b-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 184 kB
  • ctags: 262
  • sloc: makefile: 2
file content (202 lines) | stat: -rw-r--r-- 6,194 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
** Copyright (c) 2014 Arnaud Ysmal.  All Rights Reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
 */

package dropbox

import (
	"encoding/json"
	"fmt"
	"math"
	"reflect"
	"testing"
	"time"
)

type equaller interface {
	equals(equaller) bool
}

func (s *atom) equals(o *atom) bool {
	switch v1 := s.Value.(type) {
	case float64:
		if v2, ok := o.Value.(float64); ok {
			if math.IsNaN(v1) && math.IsNaN(v2) {
				return true
			}
			return v1 == v2
		}
	default:
		return reflect.DeepEqual(s, o)
	}
	return false
}

func (s *value) equals(o *value) bool {
	return reflect.DeepEqual(s, o)
}

func (s Fields) equals(o Fields) bool {
	return reflect.DeepEqual(s, o)
}

func (s opDict) equals(o opDict) bool {
	return reflect.DeepEqual(s, o)
}

func (s *change) equals(o *change) bool {
	return reflect.DeepEqual(s, o)
}

func (s *fieldOp) equals(o *fieldOp) bool {
	return reflect.DeepEqual(s, o)
}

func testDSAtom(t *testing.T, c *atom, e string) {
	var c2 atom
	var err error
	var js []byte

	if js, err = json.Marshal(c); err != nil {
		t.Errorf("%s", err)
	}
	if err = c2.UnmarshalJSON(js); err != nil {
		t.Errorf("%s", err)
	}
	if !c.equals(&c2) {
		t.Errorf("expected %#v type %s got %#v of type %s", c.Value, reflect.TypeOf(c.Value).Name(), c2.Value, reflect.TypeOf(c2.Value).Name())
	}
	c2 = atom{}
	if err = c2.UnmarshalJSON([]byte(e)); err != nil {
		t.Errorf("%s", err)
	}
	if !c.equals(&c2) {
		t.Errorf("expected %#v type %s got %#v of type %s", c.Value, reflect.TypeOf(c.Value).Name(), c2.Value, reflect.TypeOf(c2.Value).Name())
	}
}

func TestDSAtomUnmarshalJSON(t *testing.T) {
	testDSAtom(t, &atom{Value: 32.5}, `32.5`)
	testDSAtom(t, &atom{Value: true}, `true`)
	testDSAtom(t, &atom{Value: int64(42)}, `{"I":"42"}`)
	testDSAtom(t, &atom{Value: math.NaN()}, `{"N":"nan"}`)
	testDSAtom(t, &atom{Value: math.Inf(1)}, `{"N":"+inf"}`)
	testDSAtom(t, &atom{Value: math.Inf(-1)}, `{"N":"-inf"}`)
	testDSAtom(t, &atom{Value: []byte(`random string converted to bytes`)}, `{"B":"cmFuZG9tIHN0cmluZyBjb252ZXJ0ZWQgdG8gYnl0ZXM="}`)

	now := time.Now().Round(time.Millisecond)
	js := fmt.Sprintf(`{"T": "%d"}`, now.UnixNano()/int64(time.Millisecond))
	testDSAtom(t, &atom{Value: now}, js)
}

func testDSChange(t *testing.T, c *change, e string) {
	var c2 change
	var err error
	var js []byte

	if js, err = json.Marshal(c); err != nil {
		t.Errorf("%s", err)
	}
	if err = c2.UnmarshalJSON(js); err != nil {
		t.Errorf("%s", err)
	}
	if !c.equals(&c2) {
		t.Errorf("mismatch: got:\n\t%#v\nexpected:\n\t%#v", c2, *c)
	}
	c2 = change{}
	if err = c2.UnmarshalJSON([]byte(e)); err != nil {
		t.Errorf("%s", err)
	}
	if !c.equals(&c2) {
		t.Errorf("mismatch")
	}
}

func TestDSChangeUnmarshalJSON(t *testing.T) {
	testDSChange(t,
		&change{
			Op:       recordInsert,
			TID:      "dropbox",
			RecordID: "test",
			Data:     Fields{"float": value{values: []interface{}{float64(42)}, isList: false}},
		}, `["I","dropbox","test",{"float":42}]`)
	testDSChange(t,
		&change{
			Op:       recordUpdate,
			TID:      "dropbox",
			RecordID: "test",
			Ops:      opDict{"field": fieldOp{Op: fieldPut, Data: value{values: []interface{}{float64(42)}, isList: false}}},
		}, `["U","dropbox","test",{"field":["P", 42]}]`)
	testDSChange(t,
		&change{
			Op:       recordUpdate,
			TID:      "dropbox",
			RecordID: "test",
			Ops:      opDict{"field": fieldOp{Op: listCreate}},
		}, `["U","dropbox","test",{"field":["LC"]}]`)

	testDSChange(t,
		&change{
			Op:       recordDelete,
			TID:      "dropbox",
			RecordID: "test",
		}, `["D","dropbox","test"]`)
}

func testCheckfieldOp(t *testing.T, fo *fieldOp, e string) {
	var fo2 fieldOp
	var js []byte
	var err error

	if js, err = json.Marshal(fo); err != nil {
		t.Errorf("%s", err)
	}
	if string(js) != e {
		t.Errorf("marshalling error got %s expected %s", string(js), e)
	}
	if err = json.Unmarshal(js, &fo2); err != nil {
		t.Errorf("%s %s", err, string(js))
	}
	if !fo.equals(&fo2) {
		t.Errorf("%#v != %#v\n", fo, fo2)
	}
	fo2 = fieldOp{}
	if err = json.Unmarshal([]byte(e), &fo2); err != nil {
		t.Errorf("%s %s", err, string(js))
	}
	if !fo.equals(&fo2) {
		t.Errorf("%#v != %#v\n", fo, fo2)
	}
}

func TestDSfieldOpMarshalling(t *testing.T) {
	testCheckfieldOp(t, &fieldOp{Op: "P", Data: value{values: []interface{}{"bar"}, isList: false}}, `["P","bar"]`)
	testCheckfieldOp(t, &fieldOp{Op: "P", Data: value{values: []interface{}{"ga", "bu", "zo", "meuh", int64(42), 4.5, true}, isList: true}}, `["P",["ga","bu","zo","meuh",{"I":"42"},4.5,true]]`)
	testCheckfieldOp(t, &fieldOp{Op: "D"}, `["D"]`)
	testCheckfieldOp(t, &fieldOp{Op: "LC"}, `["LC"]`)
	testCheckfieldOp(t, &fieldOp{Op: "LP", Index: 1, Data: value{values: []interface{}{"baz"}}}, `["LP",1,"baz"]`)
	testCheckfieldOp(t, &fieldOp{Op: "LI", Index: 1, Data: value{values: []interface{}{"baz"}}}, `["LI",1,"baz"]`)
	testCheckfieldOp(t, &fieldOp{Op: "LD", Index: 1}, `["LD",1]`)
	testCheckfieldOp(t, &fieldOp{Op: "LM", Index: 1, Index2: 2}, `["LM",1,2]`)
}