-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add function to check error on unique index. #74
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks promising but it needs a rewrite for the PR's unit test.
Just re wrote the tests using testify. all of them are passing. |
you can resolve the discussions has been fixed with this commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contribution is definitely helpful and looking forward to the final PR. The Todos are not for you to work on but for us to look into more. Happy to approve once the changes have been done.
-- Regex is known for decreasing performance and we will go ahead and add error code to make the implementation uniform across all drivers.
if err == nil { | ||
return false | ||
} | ||
match, _ := regexp.MatchString(`Database index .* already contains .*, with record .*`, err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToDo:
Create ticket on SurrealDB project to include error code in error response.
@@ -10,7 +10,7 @@ import ( | |||
"github.com/stretchr/testify/require" | |||
"github.com/stretchr/testify/suite" | |||
"github.com/surrealdb/surrealdb.go" | |||
gorilla "github.com/surrealdb/surrealdb.go/pkg/gorilla" | |||
"github.com/surrealdb/surrealdb.go/pkg/gorilla" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToDo:
Add missing dependencies to depgaurd.
@@ -145,7 +145,7 @@ func (s *SurrealDBTestSuite) TestCreate() { | |||
|
|||
s.Run("Single create works", func() { | |||
userData, err := s.db.Create("users", testUser{ | |||
Username: "johnny", | |||
Username: "tim", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related to a test?
If not, please revert to original text.
@@ -156,7 +156,7 @@ func (s *SurrealDBTestSuite) TestCreate() { | |||
s.Require().NoError(err) | |||
s.Len(userSlice, 1) | |||
|
|||
s.Equal("johnny", userSlice[0].Username) | |||
s.Equal("tim", userSlice[0].Username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@@ -165,7 +165,7 @@ func (s *SurrealDBTestSuite) TestCreate() { | |||
data := make([]testUser, 0) | |||
data = append(data, | |||
testUser{ | |||
Username: "johnny", | |||
Username: "lu", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
|
||
func (s *SurrealDBTestSuite) TestIsDuplicateUniqueIdx() { | ||
query := `DEFINE INDEX uniqueNameIdx ON TABLE users COLUMNS username UNIQUE;` | ||
_, err := s.db.Query(query, nil) // because of this query, I modified the 'username' field in other tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should be independent. Please declare every table and data in this test.
}) | ||
err = nil | ||
|
||
// and finally, check with a tricky ID; this is the only way the function can give a false positive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this to an ignored testcase? That's because we want to verify important behaviour rather than verify implementation. Also it's also ok to have to a test in the comments rather than a working test.
Thanks, @timpratim ! |
Linking this error ticket which also covers incorrect handling of error responses |
Just as the
os
module provides users with theos.IsNotExist
function to test if the error was generated by trying to open a non existent file, regardless of its name, this commit adds the same functionality to check if the error comes from trying to insert data to a table with an unique index. This example, which is in the test generated for the function, shows its behavior:Will print: