func TestSetUnset(t *testing.T) { // get bst plugin bstore, err := bytengine.NewByteStore("diskv", BSTORE_CONFIG) assert.Nil(t, err, "bst not created") // get bfs plugin mfs, err := bytengine.NewFileSystem("mongodb", BFS_CONFIG, &bstore) assert.Nil(t, err, "bfs not created") // set database db := "db1" // create parser parser, err := bytengine.NewParser("base", "") assert.Nil(t, err, "parser not created") script := ` @test.set "country"={"name":"ghana","major_cities":["kumasi","accra"]} in /users where "country" == "ghana" ` cmd, err := parser.Parse(script) assert.Nil(t, err, "couldn't parse script") count, err := mfs.BQLSet(db, cmd[0].Args) assert.Nil(t, err, "set data failed") assert.Equal(t, count, 2, "set data failed") j, err := mfs.ReadJson("/users/u1", db, []string{}) assert.Nil(t, err, "read file failed") data, ok := j.(bson.M) assert.True(t, ok, "couldn't cast file content to bson.M") country, ok := data["country"].(bson.M) assert.True(t, ok, "couldn't cast file content to bson.M") assert.Equal(t, country["name"], "ghana", "incorrect file content update") script = ` @test.unset "country" in /users where exists("country") == true ` cmd, err = parser.Parse(script) assert.Nil(t, err, "couldn't parse script") count, err = mfs.BQLUnset(db, cmd[0].Args) assert.Nil(t, err, "unset data failed") assert.Equal(t, count, 4, "unset data failed") script = `@test.select "name" in /users where exists("country") == false` cmd, err = parser.Parse(script) assert.Nil(t, err, "couldn't parse script") j, err = mfs.BQLSearch(db, cmd[0].Args) assert.Nil(t, err, "search failed") val2, ok := j.([]interface{}) assert.True(t, ok, "couldn't cast search result into []interface") assert.Len(t, val2, 5, "search failed") }
func TestSearch(t *testing.T) { // get bst plugin bstore, err := bytengine.NewByteStore("diskv", BSTORE_CONFIG) assert.Nil(t, err, "bst not created") // get bfs plugin mfs, err := bytengine.NewFileSystem("mongodb", BFS_CONFIG, &bstore) assert.Nil(t, err, "bfs not created") // set database db := "db1" // create dir and add files err = mfs.NewDir("/users", db) assert.Nil(t, err, "directory not created") err = mfs.NewFile("/users/u1", db, map[string]interface{}{ "name": "john", "age": 34, "country": "ghana", }) assert.Nil(t, err, "file not created") err = mfs.NewFile("/users/u2", db, map[string]interface{}{ "name": "jason", "age": 18, "country": "ghana", }) assert.Nil(t, err, "file not created") err = mfs.NewFile("/users/u3", db, map[string]interface{}{ "name": "juliette", "age": 18, }) assert.Nil(t, err, "file not created") err = mfs.NewFile("/users/u4", db, map[string]interface{}{ "name": "michelle", "age": 21, "country": "uk", }) assert.Nil(t, err, "file not created") err = mfs.NewFile("/users/u5", db, map[string]interface{}{ "name": "dennis", "age": 22, "country": "france", }) assert.Nil(t, err, "file not created") // create parser parser, err := bytengine.NewParser("base", "") assert.Nil(t, err, "parser not created") // search users by country script := `@test.select "name" "age" in /users where "country" in ["ghana"]` cmd, err := parser.Parse(script) assert.Nil(t, err, "couldn't parse script") rep, err := mfs.BQLSearch(db, cmd[0].Args) assert.Nil(t, err, "search failed") val, ok := rep.([]interface{}) assert.True(t, ok, "couldn't cast search result into []interface") assert.Len(t, val, 2, "search failed") // search users by regular expression on name script = ` @test.select "name" "age" in /users where regex("name","i") == "^j\\w*n$"` cmd, err = parser.Parse(script) assert.Nil(t, err, "couldn't parse script") rep, err = mfs.BQLSearch(db, cmd[0].Args) assert.Nil(t, err, "search failed") val, ok = rep.([]interface{}) assert.True(t, ok, "couldn't cast search result into []interface") assert.Len(t, val, 2, "search failed") // search users that have a country field using 'exists' script = ` @test.select "name" "age" in /users where exists("country") == true` cmd, err = parser.Parse(script) assert.Nil(t, err, "couldn't parse script") rep, err = mfs.BQLSearch(db, cmd[0].Args) assert.Nil(t, err, "search failed") val, ok = rep.([]interface{}) assert.True(t, ok, "couldn't cast search result into []interface") assert.Len(t, val, 4, "search failed") // search users and return number using 'count' script = `@test.select "name" "age" in /users count` cmd, err = parser.Parse(script) assert.Nil(t, err, "couldn't parse script") rep, err = mfs.BQLSearch(db, cmd[0].Args) assert.Nil(t, err, "search failed") val2, ok := rep.(int) assert.True(t, ok, "couldn't cast search result into int") assert.Equal(t, val2, 5, "search failed") }