Initial commit
This commit is contained in:
151
application/repository/logs_test.go
Normal file
151
application/repository/logs_test.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package repository_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ory/dockertest/v3"
|
||||
"github.com/ory/dockertest/v3/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"git.derfenix.pro/fenix/protect_trans_info/adapters/inmemorycache"
|
||||
"git.derfenix.pro/fenix/protect_trans_info/application"
|
||||
. "git.derfenix.pro/fenix/protect_trans_info/application/repository"
|
||||
)
|
||||
|
||||
func TestLogs_Get(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip long test")
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
|
||||
dockerPool, err := dockertest.NewPool("")
|
||||
dockerPool.MaxWait = time.Second * 10
|
||||
require.NoError(t, err)
|
||||
|
||||
resource, err := dockerPool.RunWithOptions(&dockertest.RunOptions{
|
||||
Repository: "postgres",
|
||||
Tag: "15",
|
||||
Env: []string{
|
||||
"POSTGRES_USER=test",
|
||||
"POSTGRES_PASSWORD=test",
|
||||
"POSTGRES_DB=test",
|
||||
"POSTGRES_HOST_AUTH_METHOD=md5",
|
||||
"POSTGRES_INITDB_ARGS=--auth-host=md5",
|
||||
},
|
||||
PortBindings: map[docker.Port][]docker.PortBinding{"5432/tcp": {{HostIP: "0.0.0.0", HostPort: "55432"}}},
|
||||
}, func(config *docker.HostConfig) {
|
||||
config.AutoRemove = true
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := dockerPool.Purge(resource)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
connector := pgdriver.NewConnector(pgdriver.WithDSN("postgresql://test:test@localhost:55432/test?sslmode=disable"))
|
||||
sqlDB := sql.OpenDB(connector)
|
||||
sqlDB.SetMaxOpenConns(10)
|
||||
db := bun.NewDB(sqlDB, pgdialect.New())
|
||||
|
||||
err = dockerPool.Retry(func() error {
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := db.PingContext(pingCtx); err != nil {
|
||||
return fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
logger := zaptest.NewLogger(t)
|
||||
|
||||
require.NoError(t, application.Migrate(ctx, db, logger))
|
||||
|
||||
testData := []ConnLog{
|
||||
{
|
||||
UserID: 1,
|
||||
IP: "123.123.123.123",
|
||||
TS: time.Now(),
|
||||
},
|
||||
{
|
||||
UserID: 2,
|
||||
IP: "123.123.123.123",
|
||||
TS: time.Now().Add(time.Hour),
|
||||
},
|
||||
{
|
||||
UserID: 3,
|
||||
IP: "124.123.123.123",
|
||||
TS: time.Now().Add(time.Hour * 2),
|
||||
},
|
||||
}
|
||||
|
||||
_, err = db.NewInsert().Model(&testData).Exec(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
repo, err := NewConnLogs(ctx, db, make(inmemorycache.Cache), logger, time.Millisecond*100)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("found dup", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
get, err := repo.Get(ctx, 1, 2)
|
||||
require.NoError(t, err)
|
||||
require.True(t, get)
|
||||
})
|
||||
|
||||
t.Run("no dup 1", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
get, err := repo.Get(ctx, 1, 3)
|
||||
require.NoError(t, err)
|
||||
require.False(t, get)
|
||||
})
|
||||
|
||||
t.Run("no dup 2", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
get, err := repo.Get(ctx, 2, 3)
|
||||
require.NoError(t, err)
|
||||
require.False(t, get)
|
||||
})
|
||||
|
||||
t.Run("added item", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
get, err := repo.Get(ctx, 4, 3)
|
||||
require.NoError(t, err)
|
||||
require.False(t, get)
|
||||
|
||||
_, err = db.NewInsert().Model(&ConnLog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
UserID: 4,
|
||||
IP: "124.123.123.123",
|
||||
TS: time.Now().Add(time.Hour * 3),
|
||||
}).Exec(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
get, err = repo.Get(ctx, 4, 3)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, get)
|
||||
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
|
||||
get, err = repo.Get(ctx, 4, 3)
|
||||
require.NoError(t, err)
|
||||
require.True(t, get)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user