Beispiel #1
0
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)
}
Beispiel #2
0
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) 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
			}
		}
	}
}
Beispiel #4
0
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))
}
Beispiel #5
0
func doConcurrentTest(c *test.C, ct func()) {
	maxProcs, numReqs := 1, 150
	if testing.Short() {
		maxProcs, numReqs = 4, 50
	}
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(maxProcs))

	var wg sync.WaitGroup
	wg.Add(numReqs)

	reqs := make(chan bool)
	defer close(reqs)

	for i := 0; i < maxProcs*2; i++ {
		go func() {
			for _ = range reqs {
				ct()
				if c.Failed() {
					wg.Done()
					continue
				}
				wg.Done()
			}
		}()
	}

	for i := 0; i < numReqs; i++ {
		reqs <- true
	}

	wg.Wait()
}
Beispiel #6
0
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)
	}
}
Beispiel #7
0
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()
}
Beispiel #8
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},
		},
	})
}
Beispiel #9
0
func (s *RethinkSuite) TestSessionConnectError(c *test.C) {
	var err error
	_, err = Connect(ConnectOpts{
		Address: "nonexistanturl",
		Timeout: time.Second,
	})
	c.Assert(err, test.NotNil)
}
Beispiel #10
0
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)
	}
}
Beispiel #11
0
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)
}
Beispiel #12
0
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)
}
Beispiel #13
0
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)
}
Beispiel #14
0
func (s *RethinkSuite) TestConcurrentSelectManyRows(c *test.C) {
	if testing.Short() {
		c.Skip("Skipping long test")
	}

	// Ensure table + database exist
	DBCreate("test").RunWrite(session)
	DB("test").TableCreate("TestMany").RunWrite(session)
	DB("test").Table("TestMany").Delete().RunWrite(session)

	// Insert rows
	for i := 0; i < 100; i++ {
		DB("test").Table("TestMany").Insert(map[string]interface{}{
			"i": i,
		}).Exec(session)
	}

	// Test queries concurrently
	attempts := 10
	waitChannel := make(chan error, attempts)

	for i := 0; i < attempts; i++ {
		go func(i int, c chan error) {
			res, err := DB("test").Table("TestMany").Run(session)
			if err != nil {
				c <- err
				return
			}

			var response []map[string]interface{}
			err = res.All(&response)
			if err != nil {
				c <- err
				return
			}

			if len(response) != 100 {
				c <- fmt.Errorf("expected response length 100, received %d", len(response))
				return
			}

			res.Close()

			c <- nil
		}(i, waitChannel)
	}

	for i := 0; i < attempts; i++ {
		ret := <-waitChannel
		if ret != nil {
			c.Fatalf("non-nil error returned (%s)", ret)
		}
	}
}
Beispiel #15
0
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)
}
Beispiel #16
0
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)
}
Beispiel #17
0
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)
}
Beispiel #18
0
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)
}
Beispiel #19
0
func (s *RethinkSuite) TestGeospatialDecodeGeometryPseudoType(c *test.C) {
	var response types.Geometry

	// setup coordinates
	coords := [][][]float64{
		{
			{-122.423246, 37.779388},
			{-122.423246, 37.329898},
			{-121.88642, 37.329898},
			{-121.88642, 37.329898},
			{-122.423246, 37.779388},
		},
	}

	gt := "Polygon"
	res, err := Expr(map[string]interface{}{
		"$reql_type$": "GEOMETRY",
		"type":        "Polygon",
		"coordinates": coords,
	}).Run(session)
	c.Assert(err, test.IsNil)

	err = res.One(&response)
	c.Assert(err, test.IsNil)

	// test shape
	if response.Type != gt {
		c.Errorf("expected [%v], instead [%v]", gt, response.Type)
	}

	// assert points are within threshold
	c.Assert(response, geometryEquals, "Polygon", coords)
}
Beispiel #20
0
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},
	})
}
Beispiel #21
0
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)
}
Beispiel #22
0
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)
}
Beispiel #23
0
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)
}
Beispiel #24
0
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)
}
Beispiel #25
0
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",
			}},
		},
	})
}
Beispiel #26
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")
}
Beispiel #27
0
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\")")
}
Beispiel #28
0
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})
}
Beispiel #29
0
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)
}
Beispiel #30
0
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})
}