2024-08-14 12:02:20 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2024-08-15 04:23:36 +00:00
|
|
|
"io"
|
2024-08-14 12:02:20 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2024-08-15 04:23:36 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
2024-08-14 12:02:20 +00:00
|
|
|
"l12.xyz/dal/adapter"
|
2024-08-15 04:23:36 +00:00
|
|
|
"l12.xyz/dal/proto"
|
2024-08-14 12:02:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryHandler(t *testing.T) {
|
2024-08-15 07:06:11 +00:00
|
|
|
adapter.RegisterDialect("sqlite3", adapter.CommonDialect{})
|
2024-08-15 04:23:36 +00:00
|
|
|
a := adapter.DBAdapter{Type: "sqlite3"}
|
|
|
|
db, err := a.Open("file::memory:?cache=shared")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open db: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
_, err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name BLOB, data TEXT)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create table: %v", err)
|
|
|
|
}
|
|
|
|
_, err = db.Exec("INSERT INTO test (name, data) VALUES (?,?)", "test", "y")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to insert data: %v", err)
|
|
|
|
}
|
|
|
|
data := proto.Request{
|
|
|
|
Id: 0,
|
|
|
|
Db: "file::memory:?cache=shared",
|
2024-08-15 11:31:56 +00:00
|
|
|
Commands: []proto.BuilderMethod{
|
2024-08-15 04:23:36 +00:00
|
|
|
{Method: "In", Args: []interface{}{"test t"}},
|
|
|
|
{Method: "Find", Args: []interface{}{
|
|
|
|
map[string]interface{}{"id": 1},
|
|
|
|
}},
|
|
|
|
{Method: "Fields", Args: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"name": "Name",
|
|
|
|
"data": 1,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
body, _ := data.MarshalMsg(nil)
|
2024-08-14 12:02:20 +00:00
|
|
|
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
2024-08-15 07:06:11 +00:00
|
|
|
handler := QueryHandler(a)
|
2024-08-14 12:02:20 +00:00
|
|
|
handler.ServeHTTP(rr, req)
|
2024-08-15 04:23:36 +00:00
|
|
|
res, _ := io.ReadAll(rr.Result().Body)
|
|
|
|
result := proto.UnmarshalRows(res)
|
|
|
|
fmt.Println(result)
|
2024-08-14 12:02:20 +00:00
|
|
|
}
|