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
104 changes: 99 additions & 5 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ type testUser struct {
ID string `json:"id,omitempty"`
}

// a simple user struct for testing
type testUserWithFriend[I any] struct {
marshal.Basemodel `table:"user_with_friend"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ID string `json:"id,omitempty"`
Friends []I `json:"friends,omitempty"`
}

func TestSurrealDBSuite(t *testing.T) {
SurrealDBSuite := new(SurrealDBTestSuite)
SurrealDBSuite.connImplementations = make(map[string]conn.Connection)
Expand Down Expand Up @@ -120,10 +129,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 +233,92 @@ func (s *SurrealDBTestSuite) TestDelete() {
s.Require().NoError(err)
}

func (s *SurrealDBTestSuite) TestFetch() {
// Define initial user slice
userSlice := []testUserWithFriend[string]{
{
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() {
s.T().Skip("TODO(gh-116) Fetch unimplemented")
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)

userSlice, err := marshal.SmartUnmarshal[testUserWithFriend[testUserWithFriend[interface{}]]](res, err)
s.NoError(err)

s.Require().Len(userSlice, 1)
s.Require().Len(userSlice[0].Friends, 1)
s.Require().NotEmpty(userSlice[0].Friends[0], 1)
})

s.Run("Run fetch on query using map[string]interface{} for thing and fetchString", func() {
s.T().Skip("TODO(gh-116) Fetch unimplemented")
res, err := s.db.Query("select * from $record fetch $fetchstr;", map[string]interface{}{
"record": "users",
"fetchstr": "friends.*",
})
s.NoError(err)
s.NotEmpty(res)
})

s.Run("Run fetch on query using map[string]interface{} for fetchString", func() {
s.T().Skip("TODO(gh-116) Fetch unimplemented")
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)

userSlice, err := marshal.SmartUnmarshal[testUserWithFriend[testUserWithFriend[interface{}]]](res, err)
s.NoError(err)

s.Require().Len(userSlice, 1)
s.Require().Len(userSlice[0].Friends, 1)
s.Require().NotEmpty(userSlice[0].Friends[0], 1)
})
}

func (s *SurrealDBTestSuite) TestInsert() {
s.Run("raw map works", func() {
userData, err := s.db.Insert("user", map[string]interface{}{
Expand Down Expand Up @@ -324,7 +417,8 @@ func (s *SurrealDBTestSuite) TestCreate() {
data = append(data,
testUser{
Username: "johnny",
Password: "123"},
Password: "123",
},
testUser{
Username: "joe",
Password: "123",
Expand Down