2019-12-16 18:09:59 +03:00
|
|
|
package cockroach
|
2019-11-01 17:13:21 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kr/pretty"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-11-01 17:13:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSQL(t *testing.T) {
|
|
|
|
connection := fmt.Sprintf(
|
|
|
|
"host=%s port=%d user=%s sslmode=disable dbname=%s",
|
|
|
|
"localhost",
|
|
|
|
5432,
|
|
|
|
"jake",
|
|
|
|
"test",
|
|
|
|
)
|
|
|
|
db, err := sql.Open("postgres", connection)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-06 20:44:32 +03:00
|
|
|
sqlStore := NewStore(
|
2019-11-01 17:13:21 +03:00
|
|
|
store.Namespace("testsql"),
|
|
|
|
store.Nodes(connection),
|
|
|
|
)
|
|
|
|
|
|
|
|
records, err := sqlStore.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Logf("%# v\n", pretty.Formatter(records))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqlStore.Write(
|
|
|
|
&store.Record{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
},
|
2020-01-09 01:23:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Write(
|
2019-11-01 17:13:21 +03:00
|
|
|
&store.Record{
|
|
|
|
Key: "bar",
|
|
|
|
Value: []byte("baz"),
|
|
|
|
},
|
2020-01-09 01:23:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Write(
|
2019-11-01 17:13:21 +03:00
|
|
|
&store.Record{
|
|
|
|
Key: "qux",
|
|
|
|
Value: []byte("aasad"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Delete("qux")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqlStore.Write(&store.Record{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
Expiry: time.Minute,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err = sqlStore.Read("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2019-12-03 22:59:44 +03:00
|
|
|
}
|
|
|
|
t.Logf("%# v\n", pretty.Formatter(records))
|
|
|
|
if string(records[0].Value) != "bar" {
|
|
|
|
t.Error("Expected bar, got ", string(records[0].Value))
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(61 * time.Second)
|
2019-12-03 22:59:44 +03:00
|
|
|
_, err = sqlStore.Read("test")
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2019-11-01 17:13:21 +03:00
|
|
|
t.Error("Key test should have expired")
|
2019-12-03 22:59:44 +03:00
|
|
|
default:
|
|
|
|
t.Error(err)
|
|
|
|
case store.ErrNotFound:
|
|
|
|
break
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
|
|
|
}
|