Skip to content

Commit

Permalink
added the test case for crud and validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivek Yadav committed Nov 19, 2024
1 parent 5072573 commit 6a85898
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ <h3 class="title">
<mat-icon [matMenuTriggerFor]="menu">more_vert</mat-icon>
<mat-menu #menu="matMenu" xPosition="before">
<button [disabled]="!isCcEditMode" mat-menu-item (click)="dropCc(element)">
<span>Remove Check Constraints</span>
<span>Remove constraint</span>
</button>
</mat-menu>
</div>
Expand Down
5 changes: 2 additions & 3 deletions webv2/api/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func RestoreSecondaryIndex(w http.ResponseWriter, r *http.Request) {

}

// updated the check constraint and check it supported by spanner or not
// UpdateCheckConstraint processes the request to update spanner table check constraints, ensuring session and schema validity, and responds with the updated conversion metadata.
func UpdateCheckConstraint(w http.ResponseWriter, r *http.Request) {
tableId := r.FormValue("table")
reqBody, err := ioutil.ReadAll(r.Body)
Expand Down Expand Up @@ -526,7 +526,7 @@ func UpdateCheckConstraint(w http.ResponseWriter, r *http.Request) {

}

// validate the type of column when there is check constraints
// ValidateCheckConstraint verifies if the type of a database column has been altered and add an error if a change is detected.
func ValidateCheckConstraint(w http.ResponseWriter, r *http.Request) {
sessionState := session.GetSessionState()
if sessionState.Conv == nil || sessionState.Driver == "" {
Expand All @@ -538,7 +538,6 @@ func ValidateCheckConstraint(w http.ResponseWriter, r *http.Request) {

sp := sessionState.Conv.SpSchema
srcschema := sessionState.Conv.SrcSchema
// issue := sessionState.Conv.SchemaIssues

flag := true

Expand Down
128 changes: 128 additions & 0 deletions webv2/api/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -23,6 +24,8 @@ import (
"go.uber.org/zap"
)

type errReader struct{}

func init() {
logger.Log = zap.NewNop()
}
Expand Down Expand Up @@ -2291,6 +2294,18 @@ func buildConvMySQL(conv *internal.Conv) {
Name: "table1",
Id: "t1",
ColIds: []string{"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15", "c16"},
CheckConstraints: []schema.CheckConstraints{
{
Id: "ck1",
Name: "check_1",
Expr: "age > 0",
},
{
Id: "ck1",
Name: "check_2",
Expr: "age < 99",
},
},
ColDefs: map[string]schema.Column{
"c1": {Name: "a", Id: "c1", Type: schema.Type{Name: "bool"}},
"c2": {Name: "b", Id: "c2", Type: schema.Type{Name: "text"}},
Expand Down Expand Up @@ -2325,6 +2340,18 @@ func buildConvMySQL(conv *internal.Conv) {
Name: "table1",
Id: "t1",
ColIds: []string{"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15", "c16"},
CheckConstraint: []ddl.Checkconstraint{
{
Id: "ck1",
Name: "check_1",
Expr: "age > 0",
},
{
Id: "ck1",
Name: "check_2",
Expr: "age < 99",
},
},
ColDefs: map[string]ddl.ColumnDef{
"c1": {Name: "a", Id: "c1", T: ddl.Type{Name: ddl.Bool}},
"c2": {Name: "b", Id: "c2", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}},
Expand Down Expand Up @@ -2581,3 +2608,104 @@ func TestUpdateCheckConstraint(t *testing.T) {

assert.Equal(t, expectedCheckConstraint, updatedSp.CheckConstraint)
}

func TestUpdateCheckConstraint_ParseError(t *testing.T) {
sessionState := session.GetSessionState()
sessionState.Driver = constants.MYSQL
sessionState.Conv = internal.MakeConv()

invalidJSON := "invalid json body"

rr := httptest.NewRecorder()
req, err := http.NewRequest("POST", "update/cks", io.NopCloser(strings.NewReader(invalidJSON)))
assert.NoError(t, err)

handler := http.HandlerFunc(api.UpdateCheckConstraint)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusBadRequest, rr.Code)

expectedErrorMessage := "Request Body parse error"
assert.Contains(t, rr.Body.String(), expectedErrorMessage)
}

func (errReader) Read(p []byte) (n int, err error) {
return 0, fmt.Errorf("simulated read error")
}

func TestUpdateCheckConstraint_ImproperSession(t *testing.T) {
sessionState := session.GetSessionState()
sessionState.Conv = nil // Simulate no conversion

rr := httptest.NewRecorder()
req, err := http.NewRequest("POST", "update/cks", io.NopCloser(errReader{}))
assert.NoError(t, err)

handler := http.HandlerFunc(api.UpdateCheckConstraint)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Contains(t, rr.Body.String(), "Schema is not converted or Driver is not configured properly")

}

func TestValidateCheckConstraint_ImproperSession(t *testing.T) {
sessionState := session.GetSessionState()
sessionState.Conv = nil // Simulate no conversion

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/validateCheckConstraint", nil)

handler := http.HandlerFunc(api.ValidateCheckConstraint)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusNotFound, rr.Code)
assert.Contains(t, rr.Body.String(), "Schema is not converted or Driver is not configured properly")

}

func TestValidateCheckConstraint_NoTypeMismatch(t *testing.T) {
sessionState := session.GetSessionState()
sessionState.Driver = constants.MYSQL
sessionState.Conv = internal.MakeConv()

buildConvMySQL(sessionState.Conv)

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/validateCheckConstraint", nil)

handler := http.HandlerFunc(api.ValidateCheckConstraint)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var responseFlag bool
json.NewDecoder(rr.Body).Decode(&responseFlag)
assert.True(t, responseFlag)
}

func TestValidateCheckConstraint_TypeMismatch(t *testing.T) {
sessionState := session.GetSessionState()
sessionState.Driver = constants.MYSQL
sessionState.Conv = internal.MakeConv()

buildConvMySQL(sessionState.Conv)

rr1 := httptest.NewRecorder()
req1 := httptest.NewRequest("GET", "/spannerDefaultTypeMap", nil)

handler1 := http.HandlerFunc(api.SpannerDefaultTypeMap)
handler1.ServeHTTP(rr1, req1)

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/validateCheckConstraint", nil)

handler := http.HandlerFunc(api.ValidateCheckConstraint)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var responseFlag bool
json.NewDecoder(rr.Body).Decode(&responseFlag)
assert.False(t, responseFlag)
issues := sessionState.Conv.SchemaIssues["t1"].ColumnLevelIssues["c7"]
assert.Contains(t, issues, internal.TypeMismatch)
}

0 comments on commit 6a85898

Please sign in to comment.