| 1 | package tests |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "math/rand" |
| 8 | "os" |
| 9 | "runtime" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | test "gopkg.in/check.v1" |
| 14 | r "gopkg.in/rethinkdb/rethinkdb-go.v6" |
| 15 | ) |
| 16 | |
| 17 | var session *r.Session |
| 18 | var testdata = flag.Bool("rethinkdb.testdata", true, "create test data") |
| 19 | var url, url1, url2, url3, db, authKey string |
| 20 | |
| 21 | func init() { |
| 22 | // Fixing test.testlogfile parsing error on Go 1.13+. |
| 23 | if runtime.Version() < "go1.13" { |
| 24 | flag.Parse() |
| 25 | } |
| 26 | |
| 27 | r.SetVerbose(true) |
| 28 | |
| 29 | // If the test is being run by wercker look for the rethink url |
| 30 | url = os.Getenv("RETHINKDB_URL") |
| 31 | if url == "" { |
| 32 | url = "localhost:28015" |
| 33 | } |
| 34 | |
| 35 | url1 = os.Getenv("RETHINKDB_URL_1") |
| 36 | if url1 == "" { |
| 37 | url1 = "localhost:28016" |
| 38 | } |
| 39 | |
| 40 | url2 = os.Getenv("RETHINKDB_URL_2") |
| 41 | if url2 == "" { |
| 42 | url2 = "localhost:28017" |
| 43 | } |
| 44 | |
| 45 | url3 = os.Getenv("RETHINKDB_URL_3") |
| 46 | if url3 == "" { |
| 47 | url3 = "localhost:28018" |
| 48 | } |
| 49 | |
| 50 | db = os.Getenv("RETHINKDB_DB") |
| 51 | if db == "" { |
| 52 | db = "test" |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // |
| 57 | // Begin TestMain(), Setup, Teardown |
| 58 | // |
| 59 | func testSetup(m *testing.M) { |
| 60 | var err error |
| 61 | session, err = r.Connect(r.ConnectOpts{ |
| 62 | Address: url, |
| 63 | }) |
| 64 | if err != nil { |
| 65 | r.Log.Fatalln(err.Error()) |
| 66 | } |
| 67 | |
| 68 | setupTestData() |
| 69 | } |
| 70 | func testTeardown(m *testing.M) { |
| 71 | session.Close() |
| 72 | } |
| 73 | |
| 74 | func testBenchmarkSetup() { |
| 75 | r.DBDrop("benchmarks").Exec(session) |
| 76 | r.DBCreate("benchmarks").Exec(session) |
| 77 | |
| 78 | r.DB("benchmarks").TableDrop("benchmarks").Run(session) |
| 79 | r.DB("benchmarks").TableCreate("benchmarks").Run(session) |
| 80 | } |
| 81 | |
| 82 | func testBenchmarkTeardown() { |
| 83 | r.DBDrop("benchmarks").Run(session) |
| 84 | } |
| 85 | |
| 86 | func TestMain(m *testing.M) { |
| 87 | // seed randomness for use with tests |
| 88 | rand.Seed(time.Now().UTC().UnixNano()) |
| 89 | |
| 90 | testSetup(m) |
| 91 | testBenchmarkSetup() |
| 92 | res := m.Run() |
| 93 | testBenchmarkTeardown() |
| 94 | testTeardown(m) |
| 95 | |
| 96 | os.Exit(res) |
| 97 | } |
| 98 | |
| 99 | // |
| 100 | // End TestMain(), Setup, Teardown |
| 101 | // |
| 102 | |
| 103 | // Hook up gocheck into the gotest runner. |
| 104 | func Test(t *testing.T) { test.TestingT(t) } |
| 105 | |
| 106 | type RethinkSuite struct{} |
| 107 | |
| 108 | var _ = test.Suite(&RethinkSuite{}) |
| 109 | |
| 110 | // Expressions used in tests |
| 111 | var now = time.Now() |
| 112 | var arr = []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 113 | var darr = []interface{}{1, 1, 2, 2, 3, 3, 5, 5, 6} |
| 114 | var narr = []interface{}{ |
| 115 | 1, 2, 3, 4, 5, 6, []interface{}{ |
| 116 | 7.1, 7.2, 7.3, |
| 117 | }, |
| 118 | } |
| 119 | var obj = map[string]interface{}{"a": 1, "b": 2, "c": 3} |
| 120 | var nobj = map[string]interface{}{ |
| 121 | "A": 1, |
| 122 | "B": 2, |
| 123 | "C": map[string]interface{}{ |
| 124 | "1": 3, |
| 125 | "2": 4, |
| 126 | }, |
| 127 | } |
| 128 | |
| 129 | var noDupNumObjList = []interface{}{ |
| 130 | map[string]interface{}{"id": 1, "g1": 1, "g2": 1, "num": 0}, |
| 131 | map[string]interface{}{"id": 2, "g1": 2, "g2": 2, "num": 5}, |
| 132 | map[string]interface{}{"id": 3, "g1": 3, "g2": 2, "num": 10}, |
| 133 | map[string]interface{}{"id": 5, "g1": 2, "g2": 3, "num": 100}, |
| 134 | map[string]interface{}{"id": 6, "g1": 1, "g2": 1, "num": 15}, |
| 135 | map[string]interface{}{"id": 8, "g1": 4, "g2": 2, "num": 50}, |
| 136 | map[string]interface{}{"id": 9, "g1": 2, "g2": 3, "num": 25}, |
| 137 | } |
| 138 | var objList = []interface{}{ |
| 139 | map[string]interface{}{"id": 1, "g1": 1, "g2": 1, "num": 0}, |
| 140 | map[string]interface{}{"id": 2, "g1": 2, "g2": 2, "num": 5}, |
| 141 | map[string]interface{}{"id": 3, "g1": 3, "g2": 2, "num": 10}, |
| 142 | map[string]interface{}{"id": 4, "g1": 2, "g2": 3, "num": 0}, |
| 143 | map[string]interface{}{"id": 5, "g1": 2, "g2": 3, "num": 100}, |
| 144 | map[string]interface{}{"id": 6, "g1": 1, "g2": 1, "num": 15}, |
| 145 | map[string]interface{}{"id": 7, "g1": 1, "g2": 2, "num": 0}, |
| 146 | map[string]interface{}{"id": 8, "g1": 4, "g2": 2, "num": 50}, |
| 147 | map[string]interface{}{"id": 9, "g1": 2, "g2": 3, "num": 25}, |
| 148 | } |
| 149 | var nameList = []interface{}{ |
| 150 | map[string]interface{}{"id": 1, "first_name": "John", "last_name": "Smith", "gender": "M"}, |
| 151 | map[string]interface{}{"id": 2, "first_name": "Jane", "last_name": "Smith", "gender": "F"}, |
| 152 | } |
| 153 | |
| 154 | type TStr string |
| 155 | type TMap map[string]interface{} |
| 156 | |
| 157 | type T struct { |
| 158 | A string `rethinkdb:"id, omitempty"` |
| 159 | B int |
| 160 | C int `rethinkdb:"-"` |
| 161 | D map[string]interface{} |
| 162 | E []interface{} |
| 163 | F X |
| 164 | } |
| 165 | |
| 166 | type SimpleT struct { |
| 167 | A string |
| 168 | B int |
| 169 | } |
| 170 | |
| 171 | type X struct { |
| 172 | XA int |
| 173 | XB string |
| 174 | XC []string |
| 175 | XD Y |
| 176 | XE TStr |
| 177 | XF []TStr |
| 178 | } |
| 179 | |
| 180 | type Y struct { |
| 181 | YA int |
| 182 | YB map[string]interface{} |
| 183 | YC map[string]string |
| 184 | YD TMap |
| 185 | } |
| 186 | |
| 187 | type PseudoTypes struct { |
| 188 | T time.Time |
| 189 | Z time.Time |
| 190 | B []byte |
| 191 | } |
| 192 | |
| 193 | var str = T{ |
| 194 | A: "A", |
| 195 | B: 1, |
| 196 | C: 1, |
| 197 | D: map[string]interface{}{ |
| 198 | "D1": 1, |
| 199 | "D2": "2", |
| 200 | }, |
| 201 | E: []interface{}{ |
| 202 | "E1", "E2", "E3", 4, |
| 203 | }, |
| 204 | F: X{ |
| 205 | XA: 2, |
| 206 | XB: "B", |
| 207 | XC: []string{"XC1", "XC2"}, |
| 208 | XD: Y{ |
| 209 | YA: 3, |
| 210 | YB: map[string]interface{}{ |
| 211 | "1": "1", |
| 212 | "2": "2", |
| 213 | "3": 3, |
| 214 | }, |
| 215 | YC: map[string]string{ |
| 216 | "YC1": "YC1", |
| 217 | }, |
| 218 | YD: TMap{ |
| 219 | "YD1": "YD1", |
| 220 | }, |
| 221 | }, |
| 222 | XE: "XE", |
| 223 | XF: []TStr{ |
| 224 | "XE1", "XE2", |
| 225 | }, |
| 226 | }, |
| 227 | } |
| 228 | |
| 229 | type Author struct { |
| 230 | ID string `rethinkdb:"id,omitempty"` |
| 231 | Name string `rethinkdb:"name"` |
| 232 | } |
| 233 | |
| 234 | type Book struct { |
| 235 | ID string `rethinkdb:"id,omitempty"` |
| 236 | Title string `rethinkdb:"title"` |
| 237 | Author Author `rethinkdb:"author_id,reference" rethinkdb_ref:"id"` |
| 238 | } |
| 239 | |
| 240 | type TagsTest struct { |
| 241 | A string `rethinkdb:"a"` |
| 242 | B string `json:"b"` |
| 243 | C string `rethinkdb:"c1" json:"c2"` |
| 244 | } |
| 245 | |
| 246 | func (s *RethinkSuite) BenchmarkExpr(c *test.C) { |
| 247 | for i := 0; i < c.N; i++ { |
| 248 | // Test query |
| 249 | query := r.Expr(true) |
| 250 | err := query.Exec(session) |
| 251 | c.Assert(err, test.IsNil) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func (s *RethinkSuite) BenchmarkNoReplyExpr(c *test.C) { |
| 256 | for i := 0; i < c.N; i++ { |
| 257 | // Test query |
| 258 | query := r.Expr(true) |
| 259 | err := query.Exec(session, r.ExecOpts{NoReply: true}) |
| 260 | c.Assert(err, test.IsNil) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | func (s *RethinkSuite) BenchmarkGet(c *test.C) { |
| 265 | // Ensure table + database exist |
| 266 | r.DBCreate("testb1").RunWrite(session) |
| 267 | r.DB("testb1").TableCreate("TestManyBench1").RunWrite(session) |
| 268 | r.DB("testb1").Table("TestManyBench1").Delete().RunWrite(session) |
| 269 | |
| 270 | // Insert rows |
| 271 | data := []interface{}{} |
| 272 | for i := 0; i < 100; i++ { |
| 273 | data = append(data, map[string]interface{}{ |
| 274 | "id": i, |
| 275 | }) |
| 276 | } |
| 277 | r.DB("testb1").Table("TestManyBench1").Insert(data).Run(session) |
| 278 | |
| 279 | for i := 0; i < c.N; i++ { |
| 280 | n := rand.Intn(100) |
| 281 | |
| 282 | // Test query |
| 283 | var response interface{} |
| 284 | query := r.DB("testb1").Table("TestManyBench1").Get(n) |
| 285 | res, err := query.Run(session) |
| 286 | c.Assert(err, test.IsNil) |
| 287 | |
| 288 | err = res.One(&response) |
| 289 | |
| 290 | c.Assert(err, test.IsNil) |
| 291 | c.Assert(response, JsonEquals, map[string]interface{}{"id": n}) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | func (s *RethinkSuite) BenchmarkGetStruct(c *test.C) { |
| 296 | // Ensure table + database exist |
| 297 | r.DBCreate("testb2").RunWrite(session) |
| 298 | r.DB("testb2").TableCreate("TestManyBench2").RunWrite(session) |
| 299 | r.DB("testb2").Table("TestManyBench2").Delete().RunWrite(session) |
| 300 | |
| 301 | // Insert rows |
| 302 | data := []interface{}{} |
| 303 | for i := 0; i < 100; i++ { |
| 304 | data = append(data, map[string]interface{}{ |
| 305 | "id": i, |
| 306 | "name": "Object 1", |
| 307 | "Attrs": []interface{}{map[string]interface{}{ |
| 308 | "Name": "attr 1", |
| 309 | "Value": "value 1", |
| 310 | }}, |
| 311 | }) |
| 312 | } |
| 313 | r.DB("testb2").Table("TestManyBench2").Insert(data).Run(session) |
| 314 | |
| 315 | for i := 0; i < c.N; i++ { |
| 316 | n := rand.Intn(100) |
| 317 | |
| 318 | // Test query |
| 319 | var resObj object |
| 320 | query := r.DB("testb2").Table("TestManyBench2").Get(n) |
| 321 | res, err := query.Run(session) |
| 322 | c.Assert(err, test.IsNil) |
| 323 | |
| 324 | err = res.One(&resObj) |
| 325 | |
| 326 | c.Assert(err, test.IsNil) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func (s *RethinkSuite) BenchmarkSelectMany(c *test.C) { |
| 331 | // Ensure table + database exist |
| 332 | r.DBCreate("testb3").RunWrite(session) |
| 333 | r.DB("testb3").TableCreate("TestManyBench3").RunWrite(session) |
| 334 | r.DB("testb3").Table("TestManyBench3").Delete().RunWrite(session) |
| 335 | |
| 336 | // Insert rows |
| 337 | data := []interface{}{} |
| 338 | for i := 0; i < 100; i++ { |
| 339 | data = append(data, map[string]interface{}{ |
| 340 | "id": i, |
| 341 | }) |
| 342 | } |
| 343 | r.DB("testb3").Table("TestManyBench3").Insert(data).Run(session) |
| 344 | |
| 345 | for i := 0; i < c.N; i++ { |
| 346 | // Test query |
| 347 | res, err := r.DB("testb3").Table("TestManyBench3").Run(session) |
| 348 | c.Assert(err, test.IsNil) |
| 349 | |
| 350 | var response []map[string]interface{} |
| 351 | err = res.All(&response) |
| 352 | |
| 353 | c.Assert(err, test.IsNil) |
| 354 | c.Assert(response, test.HasLen, 100) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | func (s *RethinkSuite) BenchmarkSelectManyStruct(c *test.C) { |
| 359 | // Ensure table + database exist |
| 360 | r.DBCreate("testb4").RunWrite(session) |
| 361 | r.DB("testb4").TableCreate("TestManyBench4").RunWrite(session) |
| 362 | r.DB("testb4").Table("TestManyBench4").Delete().RunWrite(session) |
| 363 | |
| 364 | // Insert rows |
| 365 | data := []interface{}{} |
| 366 | for i := 0; i < 100; i++ { |
| 367 | data = append(data, map[string]interface{}{ |
| 368 | "id": i, |
| 369 | "name": "Object 1", |
| 370 | "Attrs": []interface{}{map[string]interface{}{ |
| 371 | "Name": "attr 1", |
| 372 | "Value": "value 1", |
| 373 | }}, |
| 374 | }) |
| 375 | } |
| 376 | r.DB("testb4").Table("TestManyBench4").Insert(data).Run(session) |
| 377 | |
| 378 | for i := 0; i < c.N; i++ { |
| 379 | // Test query |
| 380 | res, err := r.DB("testb4").Table("TestManyBench4").Run(session) |
| 381 | c.Assert(err, test.IsNil) |
| 382 | |
| 383 | var response []object |
| 384 | err = res.All(&response) |
| 385 | |
| 386 | c.Assert(err, test.IsNil) |
| 387 | c.Assert(response, test.HasLen, 100) |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Test utils |
| 392 | |
| 393 | // Print variable as JSON |
| 394 | func jsonPrint(v interface{}) { |
| 395 | b, err := json.MarshalIndent(v, "", " ") |
| 396 | if err != nil { |
| 397 | fmt.Println(err) |
| 398 | return |
| 399 | } |
| 400 | |
| 401 | fmt.Println(string(b)) |
| 402 | } |