2021-02-14 04:48:09 +00:00
|
|
|
package spec_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/spec"
|
2021-02-14 04:48:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ErrorTestCase struct {
|
|
|
|
Name string
|
|
|
|
Error error
|
|
|
|
ExpectedString string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
testTable := []ErrorTestCase{
|
|
|
|
{
|
|
|
|
Name: "UnknownOperationError",
|
|
|
|
Error: spec.NewUnknownOperationError("Search"),
|
|
|
|
ExpectedString: "unknown operation 'Search'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "StructFieldNotFoundError",
|
2021-03-03 14:01:32 +00:00
|
|
|
Error: spec.NewStructFieldNotFoundError([]string{"Phone", "Number"}),
|
|
|
|
ExpectedString: "struct field 'PhoneNumber' not found",
|
2021-02-14 04:48:09 +00:00
|
|
|
},
|
2021-06-02 11:39:15 +00:00
|
|
|
{
|
|
|
|
Name: "UnsupportedReturnError",
|
|
|
|
Error: spec.NewUnsupportedReturnError(code.SimpleType("User"), 0),
|
|
|
|
ExpectedString: "return type 'User' at index 0 is not supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "OperationReturnCountUnmatchedError",
|
|
|
|
Error: spec.NewOperationReturnCountUnmatchedError(2),
|
|
|
|
ExpectedString: "operation requires return count of 2",
|
|
|
|
},
|
2021-02-14 04:48:09 +00:00
|
|
|
{
|
|
|
|
Name: "InvalidQueryError",
|
2021-03-03 14:01:32 +00:00
|
|
|
Error: spec.NewInvalidQueryError([]string{"And"}),
|
|
|
|
ExpectedString: "invalid query 'And'",
|
2021-02-14 04:48:09 +00:00
|
|
|
},
|
2021-02-23 12:10:25 +00:00
|
|
|
{
|
|
|
|
Name: "IncompatibleComparatorError",
|
|
|
|
Error: spec.NewIncompatibleComparatorError(spec.ComparatorTrue, code.StructField{
|
|
|
|
Name: "Age",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeInt,
|
2021-02-23 12:10:25 +00:00
|
|
|
}),
|
|
|
|
ExpectedString: "cannot use comparator EQUAL_TRUE with struct field 'Age' of type 'int'",
|
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
{
|
|
|
|
Name: "InvalidSortError",
|
|
|
|
Error: spec.NewInvalidSortError([]string{"Order", "By"}),
|
|
|
|
ExpectedString: "invalid sort 'OrderBy'",
|
|
|
|
},
|
2021-05-03 07:12:58 +00:00
|
|
|
{
|
|
|
|
Name: "ArgumentTypeNotMatchedError",
|
2022-10-18 12:37:50 +00:00
|
|
|
Error: spec.NewArgumentTypeNotMatchedError("Age", code.TypeInt, code.TypeFloat64),
|
2021-05-03 07:12:58 +00:00
|
|
|
ExpectedString: "field 'Age' requires an argument of type 'int' (got 'float64')",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IncompatibleUpdateOperatorError",
|
2022-05-16 15:37:03 +00:00
|
|
|
Error: spec.NewIncompatibleUpdateOperatorError(spec.UpdateOperatorInc, spec.FieldReference{
|
|
|
|
code.StructField{
|
|
|
|
Name: "City",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeString,
|
2022-05-16 15:37:03 +00:00
|
|
|
},
|
|
|
|
}),
|
2021-05-03 07:12:58 +00:00
|
|
|
ExpectedString: "cannot use update operator INC with struct field 'City' of type 'string'",
|
|
|
|
},
|
2021-02-14 04:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testTable {
|
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
|
|
if testCase.Error.Error() != testCase.ExpectedString {
|
2021-03-31 11:44:31 +00:00
|
|
|
t.Errorf("Expected = %+v\nReceived = %+v", testCase.ExpectedString, testCase.Error.Error())
|
2021-02-14 04:48:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|