Exemple #1
0
func main() {
	fmt.Println("Connecting to RethinkDB")

	session, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:7704",
		Database: "players",
	})

	session.SetMaxOpenConns(10)

	if err != nil {
		log.Fatal("Could not connect")
	}

	r.DBDrop("players").Exec(session)
	err = r.DBCreate("players").Exec(session)
	if err != nil {
		panic(err)
	}

	err = r.DB("players").TableDrop("scores").Exec(session)
	err = r.DB("players").TableCreate("scores").Exec(session)
	if err != nil {
		log.Fatal("Could not create table", err)
	}

	err = r.DB("players").Table("scores").IndexCreate("Score").Exec(session)
	if err != nil {
		log.Fatal("Could not create index")
	}

	fmt.Println("Inserting new records")
	for i := 0; i < 10000; i++ {
		player := new(ScoreEntry)
		player.ID = strconv.Itoa(i)
		player.PlayerName = fmt.Sprintf("Player %d", i)
		player.Score = rand.Intn(100)
		_, err := r.Table("scores").Insert(player).RunWrite(session)
		if err != nil {
			log.Fatal(err)
		}
	}
}
func TestDial(t *testing.T) {
	Convey("Given rethinkdb host, db name, table name", t, func() {
		host := "localhost:28015"
		dbName := "rethinker_test"
		tableName := "contacts"

		Printf("host[%s] db[%s] table[%s] \n", host, dbName, tableName)

		Convey("Startup connection", func() {
			var err error
			err = Startup(r.ConnectOpts{
				Address: host,
			})

			So(err, ShouldBeNil)

			Convey("Create db", func() {
				_, err = r.DBCreate(dbName).RunWrite(Session())
				So(err, ShouldBeNil)

				Convey("Create table", func() {
					_, err = r.DB(dbName).TableCreate(tableName).RunWrite(Session())
					So(err, ShouldBeNil)

					Convey("Insert item", func() {
						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
							return table.Insert(map[string]interface{}{
								"first_name": "tester",
							})
						})
						So(err, ShouldBeNil)
					})

					//					Convey("Find item", func() {
					//						c, err := Run(dbName, tableName, func(table r.Term) r.Term {
					//							return table.GetAllByIndex(0)
					//						})

					//						So(err, ShouldBeNil)

					//						item := make(map[string]interface{})
					//						c.One(&item)

					//						So(item["first_name"], ShouldEqual, "tester")
					//					})

					//					Convey("Delete item", func() {
					//						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
					//							return table.Delete()
					//						})
					//						So(err, ShouldBeNil)
					//					})

					Reset(func() {
						r.DB(dbName).TableDrop(tableName).RunWrite(Session())
					})
				})

			})

			Reset(func() {
				r.DBDrop(dbName).RunWrite(Session())
			})
		})
	})
}
func dropTestDatabase() {
	r.DBDrop(RethinkDBName).RunWrite(Rethink)
}
Exemple #4
0
func DBDrop(db string) (r.WriteResponse, error) {
	return r.DBDrop(db).RunWrite(Session())
}