MSP-1388 [+] тесты

This commit is contained in:
Leonid Nikolaev authored and d1slike committed 2023-02-01 13:44:29 +03:00
1 parent 9bd31bf6c9
commit 42d2dd18ac
22 files changed
+160 -700

No files matched your search

-77
View File
@@ -1,77 +0,0 @@
package tests_test
//
// import (
// "testing"
// "time"
//
// "isp-lock-service/entity"
//
// "github.com/integration-system/isp-kit/dbx"
// "github.com/integration-system/isp-kit/test"
// "github.com/integration-system/isp-kit/test/dbt"
// "github.com/integration-system/isp-kit/test/grmqt"
// "github.com/rabbitmq/amqp091-go"
// )
//
// func TestConsuming(t *testing.T) {
// t.Parallel()
// test, require := test.New(t)
// testMq := grmqt.New(test)
// testDb := dbt.New(test, dbx.WithMigration("../migrations"))
//
// // locator := assembly.NewLocator(test.Logger())
// // config := conf.Consumer{
// // Config: grmqx.Consumer{
// // Queue: "test",
// // Dlq: true,
// // },
// // }
// // brokerConfig := locator.BrokerConfig(config)
// // testMq.Upgrade(brokerConfig)
//
// // invalid message
// testMq.Publish("", "test", amqp091.Publishing{Body: []byte("invalid json")})
// time.Sleep(1 * time.Second)
// require.EqualValues(1, testMq.QueueLength("test.DLQ"))
//
// // insert new message
// expected := entity.Message{
// Id: 1,
// Version: 2,
// Data: entity.MessageData{Text: "a"},
// }
//
// testMq.PublishJson("", "test", expected)
// time.Sleep(1 * time.Second)
// actual := entity.Message{}
// testDb.Must().SelectRow(&actual, "select * from message where id = $1", expected.Id)
// require.EqualValues(0, testMq.QueueLength("test"))
// require.EqualValues(expected, actual)
//
// // ignore message
// oldMsg := entity.Message{
// Id: 1,
// Version: 1,
// Data: entity.MessageData{Text: "b"},
// }
// testMq.PublishJson("", "test", oldMsg)
// time.Sleep(1 * time.Second)
// actual = entity.Message{}
// testDb.Must().SelectRow(&actual, "select * from message where id = $1", expected.Id)
// require.EqualValues(0, testMq.QueueLength("test"))
// require.EqualValues(expected, actual)
//
// // update message
// expected = entity.Message{
// Id: 1,
// Version: 3,
// Data: entity.MessageData{Text: "b"},
// }
// testMq.PublishJson("", "test", expected)
// time.Sleep(1 * time.Second)
// actual = entity.Message{}
// testDb.Must().SelectRow(&actual, "select * from message where id = $1", expected.Id)
// require.EqualValues(0, testMq.QueueLength("test"))
// require.EqualValues(expected, actual)
// }
-109
View File
@@ -1,109 +0,0 @@
package tests_test
//
// import (
// "context"
// "testing"
//
// "isp-lock-service/assembly"
//
// "github.com/integration-system/isp-kit/dbx"
// "github.com/integration-system/isp-kit/grpc/client"
// "github.com/integration-system/isp-kit/test"
// "github.com/integration-system/isp-kit/test/dbt"
// "github.com/integration-system/isp-kit/test/grpct"
// "github.com/stretchr/testify/require"
// "google.golang.org/grpc/codes"
// "google.golang.org/grpc/status"
// )
//
// type Object struct {
// Name string
// }
//
// func TestGetAll(t *testing.T) {
// t.Parallel()
// assert, testDb, cli := prepareTest(t)
//
// result := make([]Object, 0)
// err := cli.Invoke("isp-lock-service/object/all").
// ReadJsonResponse(&result).
// Do(context.Background())
// assert.NoError(err)
// assert.Empty(result)
//
// testDb.Must().Exec("insert into object (id, name) values ($1, $2)", 1, "a")
// testDb.Must().Exec("insert into object (id, name) values ($1, $2)", 2, "b")
//
// result = make([]Object, 0)
// err = cli.Invoke("isp-lock-service/object/all").
// ReadJsonResponse(&result).
// Do(context.Background())
// assert.NoError(err)
//
// expected := []Object{{
// Name: "a",
// }, {
// Name: "b",
// }}
// assert.EqualValues(expected, result)
// }
//
// func TestGetById(t *testing.T) {
// t.Parallel()
// assert, testDb, cli := prepareTest(t)
//
// testDb.Must().Exec("insert into object (id, name) values ($1, $2)", 1, "a")
//
// type reqBody struct {
// Id int
// }
//
// // empty req body
// result := Object{}
// err := cli.Invoke("isp-lock-service/object/get_by_id").
// ReadJsonResponse(&result).
// Do(context.Background())
// assert.Error(err)
// assert.EqualValues(codes.InvalidArgument, status.Code(err))
//
// // id is required
// result = Object{}
// err = cli.Invoke("isp-lock-service/object/get_by_id").
// ReadJsonResponse(&result).
// JsonRequestBody(reqBody{}).
// Do(context.Background())
// assert.Error(err)
// assert.EqualValues(codes.InvalidArgument, status.Code(err))
//
// // not found
// result = Object{}
// err = cli.Invoke("isp-lock-service/object/get_by_id").
// ReadJsonResponse(&result).
// JsonRequestBody(reqBody{Id: 2}).
// Do(context.Background())
// assert.Error(err)
// assert.EqualValues(codes.NotFound, status.Code(err))
//
// // happy path
// result = Object{}
// err = cli.Invoke("isp-lock-service/object/get_by_id").
// ReadJsonResponse(&result).
// JsonRequestBody(reqBody{Id: 1}).
// Do(context.Background())
// assert.NoError(err)
//
// expected := Object{Name: "a"}
// assert.EqualValues(expected, result)
// }
//
// func prepareTest(t *testing.T) (*require.Assertions, *dbt.TestDb, *client.Client) {
// t.Helper()
// test, assert := test.New(t)
// testDb := dbt.New(test, dbx.WithMigration("../migrations"))
//
// locator := assembly.NewLocator(test.Logger())
// _, cli := grpct.TestServer(test, locator.Handler())
//
// return assert, testDb, cli
// }
+80
View File
@@ -0,0 +1,80 @@
package tests
import (
"fmt"
"testing"
"time"
"isp-lock-service/rc"
"github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4"
"github.com/integration-system/isp-kit/test"
)
func NewRedis(test *test.Test) *redis.Client {
redisHost := test.Config().Optional().String("REDIS_HOST", "localhost")
redisPort := test.Config().Optional().String("REDIS_PORT", "6379")
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
return redis.NewClient(&redis.Options{Addr: addr})
}
func TestOne(t *testing.T) {
tst, _ := test.New(t)
rcli := NewRedis(tst)
r, err := rc.NewRCWithClient("testPrefix", time.Second*2, nil, rcli)
if err != nil {
t.Error(err)
}
// success story
l, err := r.Lock("abc", time.Second)
if err != nil {
t.Error("#1.1::", err)
}
_, err = r.UnLock("abc", l)
if err != nil {
t.Error("#1.2::", err)
}
// look at wait
l, err = r.Lock("abc", time.Second)
if err != nil {
t.Error("#2.1::", err)
}
n := time.Now()
l, err = r.Lock("abc", time.Second)
if err != nil {
t.Error("#2.2::", err)
}
diff := time.Now().Sub(n)
if diff < time.Second {
t.Error("#2.3::error with second lock::", diff.String())
}
// look at error
l, err = r.Lock("abc", time.Minute)
if err != nil {
t.Error("#3.1::", err)
}
n = time.Now()
_, err = r.Lock("abc", time.Second)
diff = time.Now().Sub(n)
if err == nil {
t.Error("#3.2::", err)
} else {
if _, ok := err.(redsync.ErrTaken); !ok {
t.Error("#3.3::", err)
}
}
_, err = r.UnLock("abc", l)
if err != nil {
t.Error("#3.4::", err)
}
}