Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huskar-t committed Nov 22, 2024
1 parent c37974c commit cf5e8e7
Show file tree
Hide file tree
Showing 3 changed files with 344 additions and 3 deletions.
32 changes: 29 additions & 3 deletions controller/ws/ws/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func TestMode(t *testing.T) {

}

func TestWrongConnect(t *testing.T) {
// mock tool.GetWhitelist return error

}

func TestWsQuery(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
Expand Down Expand Up @@ -147,15 +152,26 @@ func TestWsQuery(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(1), connResp.ReqID)
assert.Equal(t, 0, connResp.Code, connResp.Message)
assert.Equal(t, Connect, connResp.Action)

// query
queryReq := queryRequest{ReqID: 2, Sql: "select * from stb1"}
// wrong sql
queryReq := queryRequest{ReqID: 2, Sql: "wrong sql"}
resp, err = doWebSocket(ws, WSQuery, &queryReq)
assert.NoError(t, err)
var queryResp queryResponse
err = json.Unmarshal(resp, &queryResp)
assert.NoError(t, err)
assert.Equal(t, uint64(2), queryResp.ReqID)
assert.NotEqual(t, 0, queryResp.Code)
assert.Equal(t, WSQuery, queryResp.Action)

// query
queryReq = queryRequest{ReqID: 2, Sql: "select * from stb1"}
resp, err = doWebSocket(ws, WSQuery, &queryReq)
assert.NoError(t, err)
err = json.Unmarshal(resp, &queryResp)
assert.NoError(t, err)
assert.Equal(t, uint64(2), queryResp.ReqID)
assert.Equal(t, 0, queryResp.Code, queryResp.Message)

// fetch
Expand Down Expand Up @@ -510,7 +526,17 @@ func TestWsQuery(t *testing.T) {
assert.Equal(t, 0, fetchResp.Code, fetchResp.Message)

assert.Equal(t, true, fetchResp.Completed)
time.Sleep(time.Second)

// insert
queryReq = queryRequest{ReqID: 14, Sql: `insert into t4 using stb1 tags ('{\"table\":\"t4\"}') values (now-2s,true,2,3,4,5,6,7,8,9,10,11,'中文\"binary','中文nchar','\xaabbcc','point(100 100)')(now-1s,false,12,13,14,15,16,17,18,19,110,111,'中文\"binary','中文nchar','\xaabbcc','point(100 100)')(now,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null)`}
resp, err = doWebSocket(ws, WSQuery, &queryReq)
assert.NoError(t, err)
err = json.Unmarshal(resp, &queryResp)
assert.NoError(t, err)
assert.Equal(t, uint64(14), queryResp.ReqID)
assert.Equal(t, 0, queryResp.Code, queryResp.Message)
assert.Equal(t, WSQuery, queryResp.Action)
assert.Equal(t, 3, queryResp.AffectedRows)
}

type FetchRawBlockResponse struct {
Expand Down
155 changes: 155 additions & 0 deletions controller/ws/ws/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/taosdata/taosadapter/v3/controller/ws/wstool"
"github.com/taosdata/taosadapter/v3/tools/generator"
)

func TestWSTMQWriteRaw(t *testing.T) {
Expand Down Expand Up @@ -121,4 +122,158 @@ func TestWSTMQWriteRaw(t *testing.T) {
assert.Equal(t, expect[rowIndex][i], values[i])
}
}
// wrong meta type
buffer.Reset()
metaType = 0
reqID := uint64(generator.GetReqID())
wstool.WriteUint64(&buffer, reqID) // req id
wstool.WriteUint64(&buffer, 0) // message id
wstool.WriteUint64(&buffer, uint64(TMQRawMessage))
wstool.WriteUint32(&buffer, length)
wstool.WriteUint16(&buffer, metaType)
buffer.Write(data)
err = ws.WriteMessage(websocket.BinaryMessage, buffer.Bytes())
assert.NoError(t, err)
_, resp, err = ws.ReadMessage()
assert.NoError(t, err)
err = json.Unmarshal(resp, &tmqResp)
assert.NoError(t, err)
assert.Equal(t, reqID, tmqResp.ReqID)
assert.NotEqual(t, 0, tmqResp.Code)
assert.Equal(t, getActionString(TMQRawMessage), tmqResp.Action)
}

func TestWSWriteRawBlockError(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
ws, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(s.URL, "http")+"/ws", nil)
if err != nil {
t.Error(err)
return
}
defer func() {
err = ws.Close()
assert.NoError(t, err)
}()
code, message := doRestful("create database if not exists test_ws_write_raw_block_error", "")
assert.Equal(t, 0, code, message)
defer func() {
code, message := doRestful("drop database if exists test_ws_write_raw_block_error", "")
assert.Equal(t, 0, code, message)
}()
code, message = doRestful("create table test_ws_write_raw_block_error.tb1 (ts timestamp,v int)", "")
assert.Equal(t, 0, code, message)
code, message = doRestful("insert into test_ws_write_raw_block_error.tb1 values(now , 1)", "")
assert.Equal(t, 0, code, message)
// connect without db
connReq := connRequest{ReqID: 1, User: "root", Password: "taosdata", DB: ""}
resp, err := doWebSocket(ws, Connect, &connReq)
assert.NoError(t, err)
var connResp commonResp
err = json.Unmarshal(resp, &connResp)
assert.NoError(t, err)
assert.Equal(t, uint64(1), connResp.ReqID)
assert.Equal(t, 0, connResp.Code, connResp.Message)

// query
queryReq := queryRequest{ReqID: 2, Sql: "select * from test_ws_write_raw_block_error.tb1"}
resp, err = doWebSocket(ws, WSQuery, &queryReq)
assert.NoError(t, err)
var queryResp queryResponse
err = json.Unmarshal(resp, &queryResp)
assert.NoError(t, err)
assert.Equal(t, uint64(2), queryResp.ReqID)
assert.Equal(t, 0, queryResp.Code, queryResp.Message)

// fetch
fetchReq := fetchRequest{ReqID: 3, ID: queryResp.ID}
resp, err = doWebSocket(ws, WSFetch, &fetchReq)
assert.NoError(t, err)
var fetchResp fetchResponse
err = json.Unmarshal(resp, &fetchResp)
assert.NoError(t, err)
assert.Equal(t, uint64(3), fetchResp.ReqID)
assert.Equal(t, 0, fetchResp.Code, fetchResp.Message)
assert.Equal(t, 1, fetchResp.Rows)

// fetch block
fetchBlockReq := fetchBlockRequest{ReqID: 4, ID: queryResp.ID}
fetchBlockResp, err := doWebSocket(ws, WSFetchBlock, &fetchBlockReq)
assert.NoError(t, err)

fetchReq = fetchRequest{ReqID: 5, ID: queryResp.ID}
resp, err = doWebSocket(ws, WSFetch, &fetchReq)
assert.NoError(t, err)
err = json.Unmarshal(resp, &fetchResp)
assert.NoError(t, err)
assert.Equal(t, uint64(5), fetchResp.ReqID)
assert.Equal(t, 0, fetchResp.Code, fetchResp.Message)

assert.Equal(t, true, fetchResp.Completed)

// write raw block
var buffer bytes.Buffer
wstool.WriteUint64(&buffer, 300) // req id
wstool.WriteUint64(&buffer, 400) // message id
wstool.WriteUint64(&buffer, uint64(RawBlockMessage)) // action
wstool.WriteUint32(&buffer, uint32(fetchResp.Rows)) // rows
wstool.WriteUint16(&buffer, uint16(2)) // table name length
buffer.WriteString("t2") // table name
buffer.Write(fetchBlockResp[16:]) // raw block
err = ws.WriteMessage(websocket.BinaryMessage, buffer.Bytes())
assert.NoError(t, err)
_, resp, err = ws.ReadMessage()
assert.NoError(t, err)
var writeResp commonResp
err = json.Unmarshal(resp, &writeResp)
assert.NoError(t, err)
assert.Equal(t, getActionString(RawBlockMessage), writeResp.Action)
assert.NotEqual(t, 0, writeResp.Code)

// write raw block with fields
buffer.Reset()
wstool.WriteUint64(&buffer, 300) // req id
wstool.WriteUint64(&buffer, 400) // message id
wstool.WriteUint64(&buffer, uint64(RawBlockMessageWithFields)) // action
wstool.WriteUint32(&buffer, uint32(fetchResp.Rows)) // rows
wstool.WriteUint16(&buffer, uint16(2)) // table name length
buffer.WriteString("t2") // table name
buffer.Write(fetchBlockResp[16:]) // raw block
fields := []byte{
// ts
0x74, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
// type
0x09,
// padding
0x00, 0x00,
// bytes
0x08, 0x00, 0x00, 0x00,

// v1
0x76, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
// type
0x04,
// padding
0x00, 0x00,
// bytes
0x04, 0x00, 0x00, 0x00,
}
buffer.Write(fields)
err = ws.WriteMessage(websocket.BinaryMessage, buffer.Bytes())
assert.NoError(t, err)
_, resp, err = ws.ReadMessage()
assert.NoError(t, err)
err = json.Unmarshal(resp, &writeResp)
assert.NoError(t, err)
assert.Equal(t, getActionString(RawBlockMessageWithFields), writeResp.Action)
assert.NotEqual(t, 0, writeResp.Code)

}
160 changes: 160 additions & 0 deletions controller/ws/ws/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
stmtCommon "github.com/taosdata/driver-go/v3/common/stmt"
"github.com/taosdata/driver-go/v3/types"
"github.com/taosdata/taosadapter/v3/controller/ws/wstool"
"github.com/taosdata/taosadapter/v3/tools/generator"
"github.com/taosdata/taosadapter/v3/tools/parseblock"
)

Expand Down Expand Up @@ -898,3 +899,162 @@ func TestStmtGetParams(t *testing.T) {
assert.Equal(t, 9, getParamsResp.DataType)
assert.Equal(t, 8, getParamsResp.Length)
}

func TestStmtInvalidStmtID(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
ws, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(s.URL, "http")+"/ws", nil)
if err != nil {
t.Error(err)
return
}
defer func() {
err = ws.Close()
assert.NoError(t, err)
}()

// connect
reqID := uint64(generator.GetReqID())
connReq := connRequest{ReqID: reqID, User: "root", Password: "taosdata"}
resp, err := doWebSocket(ws, Connect, &connReq)
assert.NoError(t, err)
var connResp commonResp
err = json.Unmarshal(resp, &connResp)
assert.NoError(t, err)
assert.Equal(t, Connect, connResp.Action)
assert.Equal(t, reqID, connResp.ReqID)
assert.Equal(t, 0, connResp.Code, connResp.Message)

// prepare
reqID = uint64(generator.GetReqID())
prepareReq := stmtPrepareRequest{ReqID: reqID, StmtID: 0, SQL: "insert into ? using test_ws_stmt_ws.stb tags (?) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"}
resp, err = doWebSocket(ws, STMTPrepare, &prepareReq)
assert.NoError(t, err)
var prepareResp stmtPrepareResponse
err = json.Unmarshal(resp, &prepareResp)
assert.NoError(t, err)
assert.Equal(t, STMTPrepare, prepareResp.Action)
assert.Equal(t, reqID, prepareResp.ReqID)
assert.NotEqual(t, 0, prepareResp.Code)

// set table name
reqID = uint64(generator.GetReqID())
setTableNameReq := stmtSetTableNameRequest{ReqID: reqID, StmtID: prepareResp.StmtID, Name: "d1"}
resp, err = doWebSocket(ws, STMTSetTableName, &setTableNameReq)
assert.NoError(t, err)
var setTableNameResp stmtSetTableNameResponse
err = json.Unmarshal(resp, &setTableNameResp)
assert.NoError(t, err)
assert.Equal(t, STMTSetTableName, setTableNameResp.Action)
assert.Equal(t, reqID, setTableNameResp.ReqID)
assert.NotEqual(t, 0, setTableNameResp.Code)

// set tags
reqID = uint64(generator.GetReqID())
setTagsReq := stmtSetTagsRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTSetTags, &setTagsReq)
assert.NoError(t, err)
var setTagsResp stmtSetTagsResponse
err = json.Unmarshal(resp, &setTagsResp)
assert.NoError(t, err)
assert.Equal(t, STMTSetTags, setTagsResp.Action)
assert.Equal(t, reqID, setTagsResp.ReqID)
assert.NotEqual(t, 0, setTagsResp.Code)

// bind
reqID = uint64(generator.GetReqID())
bindReq := stmtBindRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTBind, &bindReq)
assert.NoError(t, err)
var bindResp stmtBindResponse
err = json.Unmarshal(resp, &bindResp)
assert.NoError(t, err)
assert.Equal(t, STMTBind, bindResp.Action)
assert.Equal(t, reqID, bindResp.ReqID)
assert.NotEqual(t, 0, bindResp.Code)

// add batch
reqID = uint64(generator.GetReqID())
addBatchReq := stmtAddBatchRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTAddBatch, &addBatchReq)
assert.NoError(t, err)
var addBatchResp stmtAddBatchResponse
err = json.Unmarshal(resp, &addBatchResp)
assert.NoError(t, err)
assert.Equal(t, STMTAddBatch, addBatchResp.Action)
assert.Equal(t, reqID, addBatchResp.ReqID)
assert.NotEqual(t, 0, addBatchResp.Code)

// exec
reqID = uint64(generator.GetReqID())
execReq := stmtExecRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTExec, &execReq)
assert.NoError(t, err)
var execResp stmtExecResponse
err = json.Unmarshal(resp, &execResp)
assert.NoError(t, err)
assert.Equal(t, STMTExec, execResp.Action)
assert.Equal(t, reqID, execResp.ReqID)
assert.NotEqual(t, 0, execResp.Code)

// use result
reqID = uint64(generator.GetReqID())
useResultReq := stmtUseResultRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTUseResult, &useResultReq)
assert.NoError(t, err)
var useResultResp stmtUseResultResponse
err = json.Unmarshal(resp, &useResultResp)
assert.NoError(t, err)
assert.Equal(t, STMTUseResult, useResultResp.Action)
assert.Equal(t, reqID, useResultResp.ReqID)
assert.NotEqual(t, 0, useResultResp.Code)

// get tag fields
reqID = uint64(generator.GetReqID())
getTagFieldsReq := stmtGetTagFieldsRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTGetTagFields, &getTagFieldsReq)
assert.NoError(t, err)
var getTagFieldsResp stmtGetTagFieldsResponse
err = json.Unmarshal(resp, &getTagFieldsResp)
assert.NoError(t, err)
assert.Equal(t, STMTGetTagFields, getTagFieldsResp.Action)
assert.Equal(t, reqID, getTagFieldsResp.ReqID)
assert.NotEqual(t, 0, getTagFieldsResp.Code)

// get col fields
reqID = uint64(generator.GetReqID())
getColFieldsReq := stmtGetColFieldsRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTGetColFields, &getColFieldsReq)
assert.NoError(t, err)
var getColFieldsResp stmtGetColFieldsResponse
err = json.Unmarshal(resp, &getColFieldsResp)
assert.NoError(t, err)
assert.Equal(t, STMTGetColFields, getColFieldsResp.Action)
assert.Equal(t, reqID, getColFieldsResp.ReqID)
assert.NotEqual(t, 0, getColFieldsResp.Code)

// num params
reqID = uint64(generator.GetReqID())
numParamsReq := stmtNumParamsRequest{ReqID: reqID, StmtID: prepareResp.StmtID}
resp, err = doWebSocket(ws, STMTNumParams, &numParamsReq)
assert.NoError(t, err)
var numParamsResp stmtNumParamsResponse
err = json.Unmarshal(resp, &numParamsResp)
assert.NoError(t, err)
assert.Equal(t, STMTNumParams, numParamsResp.Action)
assert.Equal(t, reqID, numParamsResp.ReqID)
assert.NotEqual(t, 0, numParamsResp.Code)

// get param
reqID = uint64(generator.GetReqID())
getParamsReq := stmtGetParamRequest{ReqID: reqID, StmtID: prepareResp.StmtID, Index: 0}
resp, err = doWebSocket(ws, STMTGetParam, &getParamsReq)
assert.NoError(t, err)
var getParamsResp stmtGetParamResponse
err = json.Unmarshal(resp, &getParamsResp)
assert.NoError(t, err)
assert.Equal(t, STMTGetParam, getParamsResp.Action)
assert.Equal(t, reqID, getParamsResp.ReqID)
assert.NotEqual(t, 0, getParamsResp.Code)

}

0 comments on commit cf5e8e7

Please sign in to comment.