Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Add tests for fetch using passing arguments #115

Merged
merged 12 commits into from
Jan 15, 2024
139 changes: 132 additions & 7 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type SurrealDBTestSuite struct {
// a simple user struct for testing
type testUser struct {
marshal.Basemodel `table:"test"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ID string `json:"id,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Friends []string `json:"friends,omitempty"`
ID string `json:"id,omitempty"`
}

func TestSurrealDBSuite(t *testing.T) {
Expand Down Expand Up @@ -120,10 +121,8 @@ func (s *SurrealDBTestSuite) SetupSuite() {
// Can be used with any user
func signin(s *SurrealDBTestSuite) interface{} {
authData := &surrealdb.Auth{
Database: "test",
Namespace: "test",
Username: "root",
Password: "root",
Username: "root",
Password: "root",
}
signin, err := s.db.Signin(authData)
s.Require().NoError(err)
Expand Down Expand Up @@ -226,6 +225,132 @@ func (s *SurrealDBTestSuite) TestDelete() {
s.Require().NoError(err)
}

// TODO: This can maybe be replaced by smartUnmarshal
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
func (s *SurrealDBTestSuite) validateFriends(actualUser interface{}, expectedUserID string) {
s.T().Helper()
output, ok := actualUser.([]interface{})
if !ok {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Errorf("Failed to convert to map[string]interface{}")
}

// Access the first element in the output slice
if len(output) > 0 {
resultMap, ok := output[0].(map[string]interface{})
if !ok {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Errorf("Failed to convert to map[string]interface{}")
}

// Access the "result" key
results, ok := resultMap["result"].([]interface{})
if !ok {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Errorf("Failed to get 'result' key as a slice")
}

// Access the first element in the "result" slice
if len(results) > 0 {
firstResult, ok := results[0].(map[string]interface{})
if !ok {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Errorf("Failed to get the first result as a map")
}

// Access the "friends" key in the first result
friends, ok := firstResult["friends"].([]interface{})
if !ok {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Errorf("Failed to get 'friends' key as a slice")
}

// Access the first element in the "result" slice
if len(friends) > 0 {
firstFriend, ok := friends[0].(map[string]interface{})
if !ok {
s.T().Errorf("Failed to get the first result as a map")
}

s.Equal(expectedUserID, firstFriend["id"])
}
}
}
}

func (s *SurrealDBTestSuite) TestFetch() {
// Define initial user slice
userSlice := []testUser{
{
ID: "users:arthur",
Username: "arthur",
Password: "deer",
Friends: []string{"users:john"},
},
{
ID: "users:john",
Username: "john",
Password: "wolf",
Friends: []string{"users:arthur"},
},
}

// Initialize data using users
for _, v := range userSlice {
data, err := s.db.Create(v.ID, v)
s.NoError(err)
s.NotNil(data)
}

// User rows are individually fetched
s.Run("Run fetch for individual users", func() {
// TODO: This should be fixed once the code is fixed
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.T().Skip()
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range userSlice {
res, err := s.db.Query("select * from $table fetch $fetchstr;", map[string]interface{}{
"record": v.ID,
"fetchstr": "friends.*",
})
s.NoError(err)
s.NotEmpty(res)
}
})

s.Run("Run fetch on hardcoded query", func() {
query := "SELECT * from users:arthur fetch friends.*"
res, err := s.db.Query(query, map[string]interface{}{})
s.NoError(err)
s.NotEmpty(res)

s.validateFriends(res, userSlice[0].Friends[0])
})

s.Run("Run fetch on query using map[string]interface{} for thing and fetchString", func() {
// TODO: This should be fixed once the code is fixed
s.T().Skip()
res, err := s.db.Query("select * from $record fetch $fetchstr;", map[string]interface{}{
"record": "users",
"fetchstr": "friends.*",
})
// TODO: This should be fixed once the code is fixed
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.NoError(err)
s.NotEmpty(res)
})

s.Run("Run fetch on query using map[string]interface{} for fetchString", func() {
// TODO: This should be fixed once the code is fixed
s.T().Skip()
res, err := s.db.Query("select * from users fetch $fetchstr;", map[string]interface{}{
"fetchstr": "friends.*",
})
s.NoError(err)
s.NotEmpty(res)
})

s.Run("Run fetch on query using map[string]interface{} for thing or tableName", func() {
res, err := s.db.Query("select * from $record fetch friends.*;", map[string]interface{}{
"record": "users:arthur",
})
s.NoError(err)
s.NotEmpty(res)
s.validateFriends(res, userSlice[0].Friends[0])
})
}

func (s *SurrealDBTestSuite) TestInsert() {
s.Run("raw map works", func() {
userData, err := s.db.Insert("user", map[string]interface{}{
Expand Down