func (s *RethinkSuite) BenchmarkGetStruct(c *test.C) { // Ensure table + database exist DBCreate("test").RunWrite(session) DB("test").TableCreate("TestMany").RunWrite(session) DB("test").Table("TestMany").Delete().RunWrite(session) // Insert rows data := []interface{}{} for i := 0; i < 100; i++ { data = append(data, map[string]interface{}{ "id": i, "name": "Object 1", "Attrs": []interface{}{map[string]interface{}{ "Name": "attr 1", "Value": "value 1", }}, }) } DB("test").Table("TestMany").Insert(data).Run(session) for i := 0; i < c.N; i++ { n := rand.Intn(100) // Test query var resObj object query := DB("test").Table("TestMany").Get(n) res, err := query.Run(session) c.Assert(err, test.IsNil) err = res.One(&resObj) c.Assert(err, test.IsNil) } }
func (s *RethinkSuite) TestTableChangesExitNoResults(c *test.C) { DB("test").TableDrop("changes").Exec(session) DB("test").TableCreate("changes").Exec(session) var n int res, err := DB("test").Table("changes").Changes().Run(session) if err != nil { c.Fatal(err.Error()) } c.Assert(res.Type(), test.Equals, "Feed") change := make(chan ChangeResponse) // Close cursor after one second go func() { <-time.After(time.Second) res.Close() }() // Listen for changes res.Listen(change) for _ = range change { n++ } if res.Err() == nil { c.Fatal("No error returned, expected connection closed") } c.Assert(n, test.Equals, 0) }
func (s *RethinkSuite) TestJoinInnerJoin(c *test.C) { // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableCreate("Join1").Exec(session) DB("test").TableCreate("Join2").Exec(session) // Insert rows DB("test").Table("Join1").Insert(joinTable1).Exec(session) DB("test").Table("Join2").Insert(joinTable2).Exec(session) // Test query var response []interface{} query := DB("test").Table("Join1").InnerJoin(DB("test").Table("Join2"), func(a, b Term) Term { return a.Field("id").Eq(b.Field("id")) }) res, err := query.Run(session) c.Assert(err, test.IsNil) err = res.All(&response) c.Assert(response, jsonEquals, []interface{}{ map[string]interface{}{ "right": map[string]interface{}{"title": "goof", "id": 0}, "left": map[string]interface{}{"name": "bob", "id": 0}, }, map[string]interface{}{ "right": map[string]interface{}{"title": "lmoe", "id": 2}, "left": map[string]interface{}{"name": "joe", "id": 2}, }, }) }
func (s *RethinkSuite) TestSelectGetAllCompoundIndex(c *test.C) { // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableDrop("TableCompound").Exec(session) DB("test").TableCreate("TableCompound").Exec(session) write, err := DB("test").Table("TableCompound").IndexCreateFunc("full_name", func(row Term) interface{} { return []interface{}{row.Field("first_name"), row.Field("last_name")} }).RunWrite(session) DB("test").Table("TableCompound").IndexWait().Exec(session) c.Assert(err, test.IsNil) c.Assert(write.Created, test.Equals, 1) // Insert rows DB("test").Table("TableCompound").Insert(nameList).Exec(session) // Test query var response interface{} query := DB("test").Table("TableCompound").GetAllByIndex("full_name", []interface{}{"John", "Smith"}) res, err := query.Run(session) c.Assert(err, test.IsNil) err = res.One(&response) c.Assert(err, test.IsNil) c.Assert(response, jsonEquals, map[string]interface{}{"id": 1, "first_name": "John", "last_name": "Smith", "gender": "M"}) res.Close() }
func (s *RethinkSuite) TestStringMatchFail(c *test.C) { query := Expr("id:0,foo:bar").Match("name:(\\w+)") res, err := query.Run(session) c.Assert(err, test.IsNil) c.Assert(res.IsNil(), test.Equals, true) }
func (s *RethinkSuite) TestClusterRecoverAfterNoNodes(c *test.C) { session, err := Connect(ConnectOpts{ Addresses: []string{url, url2}, DiscoverHosts: true, NodeRefreshInterval: time.Second, }) c.Assert(err, test.IsNil) t := time.NewTimer(time.Second * 30) hasHadZeroNodes := false for { select { // Fail if deadline has passed case <-t.C: c.Fatal("No node was added to the cluster") default: // Check if there are no nodes if len(session.cluster.GetNodes()) == 0 { hasHadZeroNodes = true } // Pass if another node was added if len(session.cluster.GetNodes()) >= 1 && hasHadZeroNodes { return } } } }
func (s *RethinkSuite) TestGeospatialEncodeGeometryPseudoType(c *test.C) { encoded, err := encode(types.Geometry{ Type: "Polygon", Lines: types.Lines{ types.Line{ types.Point{Lon: -122.423246, Lat: 37.779388}, types.Point{Lon: -122.423246, Lat: 37.329898}, types.Point{Lon: -121.88642, Lat: 37.329898}, types.Point{Lon: -121.88642, Lat: 37.779388}, types.Point{Lon: -122.423246, Lat: 37.779388}, }, }, }) c.Assert(err, test.IsNil) c.Assert(encoded, jsonEquals, map[string]interface{}{ "$reql_type$": "GEOMETRY", "type": "Polygon", "coordinates": []interface{}{ []interface{}{ []interface{}{-122.423246, 37.779388}, []interface{}{-122.423246, 37.329898}, []interface{}{-121.88642, 37.329898}, []interface{}{-121.88642, 37.779388}, []interface{}{-122.423246, 37.779388}, }, }, }) }
func (s *RethinkSuite) BenchmarkNoReplyExpr(c *test.C) { for i := 0; i < c.N; i++ { // Test query query := Expr(true) err := query.Exec(session, ExecOpts{NoReply: true}) c.Assert(err, test.IsNil) } }
func (s *RethinkSuite) TestSessionConnectError(c *test.C) { var err error _, err = Connect(ConnectOpts{ Address: "nonexistanturl", Timeout: time.Second, }) c.Assert(err, test.NotNil) }
func (s *RethinkSuite) TestWriteDelete(c *test.C) { query := DB("test").Table("test").Insert(map[string]interface{}{"num": 1}) _, err := query.Run(session) c.Assert(err, test.IsNil) // Delete the first row in the table query = DB("test").Table("test").Sample(1).Delete() _, err = query.Run(session) c.Assert(err, test.IsNil) }
func (s *RethinkSuite) TestTableCompoundIndexCreate(c *test.C) { DBCreate("test").Exec(session) DB("test").TableDrop("TableCompound").Exec(session) DB("test").TableCreate("TableCompound").Exec(session) response, err := DB("test").Table("TableCompound").IndexCreateFunc("full_name", func(row Term) interface{} { return []interface{}{row.Field("first_name"), row.Field("last_name")} }).RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.Created, test.Equals, 1) }
func (s *RethinkSuite) TestTableCreate(c *test.C) { DB("test").TableDrop("test").Exec(session) // Test database creation query := DB("test").TableCreate("test") response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.TablesCreated, jsonEquals, 1) }
func (s *RethinkSuite) TestDbCreate(c *test.C) { // Delete the test2 database if it already exists DBDrop("test").Exec(session) // Test database creation query := DBCreate("test") response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.DBsCreated, jsonEquals, 1) }
func (s *RethinkSuite) TestTableCreateSoftDurability(c *test.C) { DB("test").TableDrop("testOpts").Exec(session) // Test database creation query := DB("test").TableCreate("testOpts", TableCreateOpts{ Durability: "soft", }) response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.TablesCreated, jsonEquals, 1) }
func (s *RethinkSuite) TestGeospatialPointDistanceRootKm(c *test.C) { var response float64 f := 734.125249602186 res, err := Distance(Point(-122.423246, 37.779388), Point(-117.220406, 32.719464), DistanceOpts{Unit: "km"}).Run(session) c.Assert(err, test.IsNil) err = res.One(&response) c.Assert(err, test.IsNil) if !kindaclose(response, f) { c.Errorf("the deviation between the compared floats is too great [%v:%v]", response, f) } }
func (s *RethinkSuite) TestTableIndexCreate(c *test.C) { DB("test").TableCreate("test").Exec(session) DB("test").Table("test").IndexDrop("test").Exec(session) // Test database creation query := DB("test").Table("test").IndexCreate("test", IndexCreateOpts{ Multi: true, }) response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.Created, jsonEquals, 1) }
func (s *RethinkSuite) TestTableIndexRename(c *test.C) { DB("test").TableDrop("test").Exec(session) DB("test").TableCreate("test").Exec(session) DB("test").Table("test").IndexCreate("test").Exec(session) DB("test").Table("test").IndexWait().Exec(session) // Test index rename query := DB("test").Table("test").IndexRename("test", "test2") response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.Renamed, jsonEquals, 1) }
func (s *RethinkSuite) TestOrderByJoinEq(c *test.C) { type Map map[string]interface{} var err error // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableCreate("test").Exec(session) DB("test").TableCreate("test2").Exec(session) tab := DB("test").Table("test") tab2 := DB("test").Table("test2") // insert rows err = tab.Insert(Map{"S": "s1", "T": 2}).Exec(session) err = tab.Insert(Map{"S": "s1", "T": 1}).Exec(session) err = tab.Insert(Map{"S": "s1", "T": 3}).Exec(session) err = tab.Insert(Map{"S": "s2", "T": 3}).Exec(session) c.Assert(err, test.IsNil) err = tab2.Insert(Map{"id": "s1", "N": "Rob"}).Exec(session) err = tab2.Insert(Map{"id": "s2", "N": "Zar"}).Exec(session) c.Assert(err, test.IsNil) // Test query var response []Map res, err := tab.OrderBy("T").EqJoin("S", tab2).Run(session) c.Assert(err, test.IsNil) err = res.All(&response) c.Assert(err, test.IsNil) c.Check(len(response), test.Equals, 4, test.Commentf("%v", response)) }
func (s *RethinkSuite) TestAggregationGroupMapReduceTable(c *test.C) { // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableCreate("TestAggregationGroupedMapReduceTable").Exec(session) // Insert rows err := DB("test").Table("TestAggregationGroupedMapReduceTable").Insert(objList).Exec(session) c.Assert(err, test.IsNil) var response []interface{} query := DB("test").Table("TestAggregationGroupedMapReduceTable").Group(func(row Term) Term { return row.Field("id").Mod(2).Eq(0) }).Map(func(row Term) Term { return row.Field("num") }).Reduce(func(acc, num Term) Term { return acc.Add(num) }) res, err := query.Run(session) c.Assert(err, test.IsNil) err = res.All(&response) c.Assert(err, test.IsNil) c.Assert(response, jsonEquals, []interface{}{ map[string]interface{}{"reduction": 135, "group": false}, map[string]interface{}{"reduction": 70, "group": true}, }) }
func (s *RethinkSuite) TestDbDelete(c *test.C) { // Delete the test2 database if it already exists DBCreate("test").Exec(session) // Test database creation query := DBDrop("test") response, err := query.RunWrite(session) c.Assert(err, test.IsNil) c.Assert(response.DBsDropped, jsonEquals, 1) // Ensure that there is still a test DB after the test has finished DBCreate("test").Exec(session) }
func (s *RethinkSuite) TestTableIndexList(c *test.C) { var response []interface{} DB("test").TableCreate("test").Exec(session) DB("test").Table("test").IndexCreate("test").Exec(session) DB("test").Table("test").IndexWait().Exec(session) // Try and find it in the list success := false res, err := DB("test").Table("test").IndexList().Run(session) c.Assert(err, test.IsNil) err = res.All(&response) c.Assert(err, test.IsNil) c.Assert(response, test.FitsTypeOf, []interface{}{}) for _, db := range response { if db == "test" { success = true } } c.Assert(success, test.Equals, true) }
func (s *RethinkSuite) TestCursorChangesClose(c *test.C) { // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableDrop("Table3").Exec(session) DB("test").TableCreate("Table3").Exec(session) // Test query // res, err := DB("test").Table("Table3").Changes().Run(session) res, err := DB("test").Table("Table3").Changes().Run(session) c.Assert(err, test.IsNil) // Ensure that the cursor can be closed err = res.Close() c.Assert(err, test.IsNil) }
func (s *RethinkSuite) TestCursorAll(c *test.C) { // Ensure table + database exist DBCreate("test").Exec(session) DB("test").TableDrop("Table3").Exec(session) DB("test").TableCreate("Table3").Exec(session) DB("test").Table("Table3").IndexCreate("num").Exec(session) DB("test").Table("Table3").IndexWait().Exec(session) // Insert rows DB("test").Table("Table3").Insert([]interface{}{ map[string]interface{}{ "id": 2, "name": "Object 1", "Attrs": []interface{}{map[string]interface{}{ "Name": "attr 1", "Value": "value 1", }}, }, map[string]interface{}{ "id": 3, "name": "Object 2", "Attrs": []interface{}{map[string]interface{}{ "Name": "attr 1", "Value": "value 1", }}, }, }).Exec(session) // Test query query := DB("test").Table("Table3").OrderBy("id") res, err := query.Run(session) c.Assert(err, test.IsNil) var response []object err = res.All(&response) c.Assert(err, test.IsNil) c.Assert(response, test.HasLen, 2) c.Assert(response, test.DeepEquals, []object{ object{ ID: 2, Name: "Object 1", Attrs: []attr{attr{ Name: "attr 1", Value: "value 1", }}, }, object{ ID: 3, Name: "Object 2", Attrs: []attr{attr{ Name: "attr 1", Value: "value 1", }}, }, }) }
func (s *RethinkSuite) TestControlError(c *test.C) { query := Error("An error occurred") err := query.Exec(session) c.Assert(err, test.NotNil) c.Assert(err, test.NotNil) c.Assert(err, test.FitsTypeOf, RQLRuntimeError{}) c.Assert(err.Error(), test.Equals, "gorethink: An error occurred in: \nr.Error(\"An error occurred\")") }
func (s *RethinkSuite) TestCursorSlice(c *test.C) { res, err := Expr([]interface{}{1, 2, 3, 4, 5}).Run(session) c.Assert(err, test.IsNil) c.Assert(res.Type(), test.Equals, "Cursor") var response []interface{} err = res.All(&response) c.Assert(err, test.IsNil) c.Assert(response, jsonEquals, []interface{}{1, 2, 3, 4, 5}) }
func (s *RethinkSuite) TestCursorLiteral(c *test.C) { res, err := Expr(5).Run(session) c.Assert(err, test.IsNil) c.Assert(res.Type(), test.Equals, "Cursor") var response interface{} err = res.One(&response) c.Assert(err, test.IsNil) c.Assert(response, jsonEquals, 5) }
func (s *RethinkSuite) TestCursorAtomArray(c *test.C) { res, err := Expr([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).Run(session) c.Assert(err, test.IsNil) c.Assert(res.Type(), test.Equals, "Cursor") var response []int err = res.All(&response) c.Assert(err, test.IsNil) c.Assert(response, test.DeepEquals, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) }
func (s *RethinkSuite) TestCursorAtomString(c *test.C) { res, err := Expr("a").Run(session) c.Assert(err, test.IsNil) c.Assert(res.Type(), test.Equals, "Cursor") var response string err = res.One(&response) c.Assert(err, test.IsNil) c.Assert(response, test.Equals, "a") }
func (s *RethinkSuite) TestTableChanges(c *test.C) { DB("test").TableDrop("changes").Exec(session) DB("test").TableCreate("changes").Exec(session) var n int res, err := DB("test").Table("changes").Changes().Run(session) if err != nil { c.Fatal(err.Error()) } c.Assert(res.Type(), test.Equals, "Feed") wg := &sync.WaitGroup{} wg.Add(1) // Use goroutine to wait for changes. Prints the first 10 results go func() { var response interface{} for n < 10 && res.Next(&response) { n++ } if res.Err() != nil { c.Fatal(res.Err()) } wg.Done() }() DB("test").Table("changes").Insert(map[string]interface{}{"n": 1}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 2}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 3}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 4}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 5}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 6}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 7}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 8}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 9}).Exec(session) DB("test").Table("changes").Insert(map[string]interface{}{"n": 10}).Exec(session) wg.Wait() c.Assert(n, test.Equals, 10) }
func (s *RethinkSuite) TestGeospatialPoint(c *test.C) { var response types.Geometry res, err := Point(-122.423246, 37.779388).Run(session) c.Assert(err, test.IsNil) err = res.One(&response) c.Assert(err, test.IsNil) c.Assert(response, jsonEquals, types.Geometry{ Type: "Point", Point: types.Point{Lon: -122.423246, Lat: 37.779388}, }) c.Assert(response, geometryEquals, "Point", []float64{-122.423246, 37.779388}) }