|
|
|
@ -345,3 +345,116 @@ func Test_MarshalQueryAssociativeRowses(t *testing.T) {
|
|
|
|
|
t.Fatalf("failed to marshal QueryRows: exp %s, got %s", exp, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_MarshalExecuteQueryResponse(t *testing.T) {
|
|
|
|
|
enc := Encoder{}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
responses []*command.ExecuteQueryResponse
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "Test with ExecuteResult",
|
|
|
|
|
responses: []*command.ExecuteQueryResponse{
|
|
|
|
|
{
|
|
|
|
|
Result: &command.ExecuteQueryResponse_E{
|
|
|
|
|
E: &command.ExecuteResult{
|
|
|
|
|
LastInsertId: 123,
|
|
|
|
|
RowsAffected: 456,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `[{"last_insert_id":123,"rows_affected":456}]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Test with QueryRows",
|
|
|
|
|
responses: []*command.ExecuteQueryResponse{
|
|
|
|
|
{
|
|
|
|
|
Result: &command.ExecuteQueryResponse_Q{
|
|
|
|
|
Q: &command.QueryRows{
|
|
|
|
|
Columns: []string{"column1", "column2"},
|
|
|
|
|
Types: []string{"type1", "type2"},
|
|
|
|
|
Values: []*command.Values{
|
|
|
|
|
{
|
|
|
|
|
Parameters: []*command.Parameter{
|
|
|
|
|
{
|
|
|
|
|
Value: &command.Parameter_I{
|
|
|
|
|
I: 123,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Value: &command.Parameter_S{
|
|
|
|
|
S: "fiona",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `[{"columns":["column1","column2"],"types":["type1","type2"],"values":[[123,"fiona"]]}]`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Test with ExecuteResult and QueryRows",
|
|
|
|
|
responses: []*command.ExecuteQueryResponse{
|
|
|
|
|
{
|
|
|
|
|
Result: &command.ExecuteQueryResponse_E{
|
|
|
|
|
E: &command.ExecuteResult{
|
|
|
|
|
LastInsertId: 123,
|
|
|
|
|
RowsAffected: 456,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Result: &command.ExecuteQueryResponse_E{
|
|
|
|
|
E: &command.ExecuteResult{
|
|
|
|
|
Error: "unique constraint failed",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Result: &command.ExecuteQueryResponse_Q{
|
|
|
|
|
Q: &command.QueryRows{
|
|
|
|
|
Columns: []string{"column1", "column2"},
|
|
|
|
|
Types: []string{"int", "text"},
|
|
|
|
|
Values: []*command.Values{
|
|
|
|
|
{
|
|
|
|
|
Parameters: []*command.Parameter{
|
|
|
|
|
{
|
|
|
|
|
Value: &command.Parameter_I{
|
|
|
|
|
I: 456,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Value: &command.Parameter_S{
|
|
|
|
|
S: "declan",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: `[{"last_insert_id":123,"rows_affected":456},{"error":"unique constraint failed"},{"columns":["column1","column2"],"types":["int","text"],"values":[[456,"declan"]]}]`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
data, err := enc.JSONMarshal(tt.responses)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("failed to marshal ExecuteQueryResponse: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if string(data) != tt.expected {
|
|
|
|
|
t.Errorf("unexpected JSON output: got %v want %v", string(data), tt.expected)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|