Skip to content

Commit

Permalink
Merge branch 'main' into context
Browse files Browse the repository at this point in the history
  • Loading branch information
Keitio authored Mar 8, 2023
2 parents 0b98822 + 5c526f0 commit 6cb7125
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 112 deletions.
183 changes: 72 additions & 111 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/surrealdb/surrealdb.go"
)

Expand All @@ -16,6 +17,12 @@ type testUser struct {
ID string
}

func (t testUser) String() string {
// TODO I found out we can use go generate stringer to generate these, but it was a bit confusing and too much
// overhead atm, so doing this as a shortcut
return fmt.Sprintf("testUser{Username: %+v, Password: %+v, ID: %+v}", t.Username, t.Password, t.ID)
}

func openConnection(t *testing.T) *surrealdb.DB {
url := os.Getenv("SURREALDB_URL")
if url == "" {
Expand Down Expand Up @@ -53,33 +60,22 @@ func TestDelete(t *testing.T) {
_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

userData, err := db.Create(ctx, "users", testUser{
Username: "johnny",
Password: "123",
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

// unmarshal the data into a user struct
var user testUser
err = surrealdb.Unmarshal(userData, &user)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

// Delete the users...
_, err = db.Delete(ctx, "users")

if err != nil {
t.Fatal(err)
}

// Output:
assert.NoError(t, err)
}

func TestCreate(t *testing.T) {
Expand All @@ -92,36 +88,26 @@ func TestCreate(t *testing.T) {
_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

userMap, err := db.Create(ctx, "users", map[string]interface{}{
"username": "john",
"password": "123",
})

if err != nil || userMap == nil {
t.Fatal(err)
}
assert.NoError(t, err)
assert.NotEmpty(t, userMap)

userData, err := db.Create(ctx, "users", testUser{
Username: "johnny",
Password: "123",
})
if err != nil || userMap == nil {
t.Fatal(err)
}
assert.NoError(t, err)
assert.NotEmpty(t, userMap)

var user testUser
err = surrealdb.Unmarshal(userData, &user)
if err != nil {
t.Fatal(err)
}

fmt.Println(user.Username)

// Output: johnny
assert.NoError(t, err)
assert.Equal(t, "johnny", user.Username)
}

func TestSelect(t *testing.T) {
Expand All @@ -134,37 +120,25 @@ func TestSelect(t *testing.T) {
_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

_, err = db.Create(ctx, "users", testUser{
Username: "johnnyjohn",
Password: "123",
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

userData, err := db.Select(ctx, "users")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

// unmarshal the data into a user slice
var users []testUser
err = surrealdb.Unmarshal(userData, &users)
if err != nil {
t.Fatal(err)
}

for _, user := range users {
if user.Username == "johnnyjohn" {
fmt.Println(user.Username)
break
}
}
// Output: johnnyjohn
assert.NoError(t, err)
matching := assertContains(t, users, func(item testUser) bool {
return item.Username == "johnnyjohn"
})
assert.GreaterOrEqual(t, len(matching), 1)
}

func TestUpdate(t *testing.T) {
Expand All @@ -177,46 +151,35 @@ func TestUpdate(t *testing.T) {
_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)


userData, err := db.Create(ctx, "users", testUser{
Username: "johnny",
Password: "123",
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

// unmarshal the data into a user struct
var user testUser
err = surrealdb.Unmarshal(userData, &testUser{})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

user.Password = "456"

// Update the user

userData, err = db.Update(ctx, "users", &user)
assert.NoError(t, err)

if err != nil {
t.Fatal(err)
}

// unmarshal the data into a user struct
var updatedUser []testUser
err = surrealdb.Unmarshal(userData, &updatedUser)

if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

// TODO: check if this updates only the user with the same ID or all users
fmt.Println(updatedUser[0].Password)

// Output: 456
assert.Equal(t, "456", updatedUser[0].Password)
}

func TestUnmarshalRaw(t *testing.T) {
Expand All @@ -229,14 +192,11 @@ func TestUnmarshalRaw(t *testing.T) {
_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

_, err = db.Delete(ctx, "users")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)


username := "johnny"
password := "123"
Expand All @@ -246,59 +206,47 @@ func TestUnmarshalRaw(t *testing.T) {
"user": username,
"pass": password,
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

var user testUser
ok, err := surrealdb.UnmarshalRaw(userData, &user)
if err != nil {
t.Fatal(err)
}
if !ok || user.Username != username || user.Password != password {
panic("response does not match the request")
}
assert.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, username, user.Username)
assert.Equal(t, password, user.Password)

// send query with empty result and unmarshal
userData, err = db.Query(ctx, "select * from users where id = $id", map[string]interface{}{
"id": "users:jim",
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

ok, err = surrealdb.UnmarshalRaw(userData, &user)
if err != nil {
t.Fatal(err)
}
if ok {
panic("select should return an empty result")
}

// Output:
assert.NoError(t, err)
assert.False(t, ok, "select should return an empty result")
}

func TestModify(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

t.Skip("There is a permission issue with this test that may need to be solved in a different change")
db := openConnection(t)
defer db.Close()

_ = signin(t, db)

_, err := db.Use(ctx, "test", "test")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

_, err = db.Delete(ctx, "users:999") // Cleanup for reproducibility
assert.NoError(t, err)

_, err = db.Create(ctx, "users:999", map[string]interface{}{
"username": "john999",
"password": "123",
})
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err) // TODO: permission error, "Unable to access record:users:999"

patches := []surrealdb.Patch{
{Op: "add", Path: "nickname", Value: "johnny"},
Expand All @@ -307,16 +255,29 @@ func TestModify(t *testing.T) {

// Update the user
_, err = db.Modify(ctx, "users:999", patches)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

user2, err := db.Select(ctx, "users:999")
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
_, err = db.Modify("users:999", patches)
assert.NoError(t, err)

user2, err := db.Select("users:999")
assert.NoError(t, err)

// // TODO: this needs to simplified for the end user somehow
fmt.Println((user2).(map[string]interface{})["age"])
// Output: 44
assert.Equal(t, "44", (user2).(map[string]interface{})["age"])
}

// assertContains performs an assertion on a list, asserting that at least one element matches a provided condition.
// All the matching elements are returned from this function, which can be used as a filter.
func assertContains[K fmt.Stringer](t *testing.T, input []K, matcher func(K) bool) []K {
matching := make([]K, 0)
for i := range input {
if matcher(input[i]) {
matching = append(matching, input[i])
}
}
assert.NotEmptyf(t, matching, "Input %+v did not contain matching element", fmt.Sprintf("%+v", input))
return matching
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/surrealdb/surrealdb.go

go 1.18

require github.com/gorilla/websocket v1.5.0
require (
github.com/gorilla/websocket v1.5.0
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 6cb7125

Please sign in to comment.