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
85 changes: 78 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,78 @@ func (s *SurrealDBTestSuite) TestDelete() {
s.Require().NoError(err)
}

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() {
for _, v := range userSlice {
res, err := s.db.Query("select * from $table fetch $fetchstr;", map[string]interface{}{
"record": v.ID,
"fetchstr": "friends.*",
})
// TODO: This should be fixed once the code is fixed
s.Error(err)
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.Nil(res)
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
}
})

s.Run("Run fetch on hardcoded query", func() {
query := "SELECT * from users fetch friends.*"
res, err := s.db.Query(query, map[string]interface{}{})
s.NoError(err)
s.NotNil(res)
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
})

s.Run("Run fetch on query using map[string]interface{} for thing and fetchString", func() {
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.Error(err)
s.Nil(res)
})

s.Run("Run fetch on query using map[string]interface{} for fetchString", func() {
res, err := s.db.Query("select * from users fetch $fetchstr;", map[string]interface{}{
"fetchstr": "friends.*",
})
// TODO: This should be fixed once the code is fixed
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
s.Error(err)
s.Nil(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",
})
s.NoError(err)
s.NotNil(res)
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
})
}

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