Skip to content

Commit

Permalink
refactor: remove unnecessary packages and add a stores testkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Dec 27, 2024
1 parent 55ce85e commit d85187d
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 270 deletions.
4 changes: 1 addition & 3 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ lint:
local-test:
FROM +vendor

WITH DOCKER --pull postgres:11
RUN go-acc ./... -o coverage.out --ignore egopb,test,example,mocks -- -mod=vendor -timeout 0 -race -v
END
RUN go-acc ./... -o coverage.out --ignore egopb,test,example,mocks -- -mod=vendor -timeout 0 -race -v

SAVE ARTIFACT coverage.out AS LOCAL coverage.out

Expand Down
31 changes: 4 additions & 27 deletions durable_state_actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
memory "github.com/tochemey/ego-contrib/durablestore/memory"
pgstore "github.com/tochemey/ego-contrib/durablestore/postgres"
"github.com/tochemey/goakt/v2/actors"
"github.com/tochemey/goakt/v2/log"
"google.golang.org/protobuf/proto"
Expand All @@ -42,6 +40,7 @@ import (
"github.com/tochemey/ego/v3/eventstream"
"github.com/tochemey/ego/v3/internal/lib"
testpb "github.com/tochemey/ego/v3/test/data/pb/v3"
"github.com/tochemey/ego/v3/testkit"
)

func TestDurableStateBehavior(t *testing.T) {
Expand All @@ -61,7 +60,7 @@ func TestDurableStateBehavior(t *testing.T) {

lib.Pause(time.Second)

durableStore := memory.NewStateStore()
durableStore := testkit.NewDurableStore()
// create a persistence id
persistenceID := uuid.NewString()
behavior := NewAccountDurableStateBehavior(persistenceID)
Expand Down Expand Up @@ -157,7 +156,7 @@ func TestDurableStateBehavior(t *testing.T) {

lib.Pause(time.Second)

durableStore := memory.NewStateStore()
durableStore := testkit.NewDurableStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down Expand Up @@ -246,27 +245,7 @@ func TestDurableStateBehavior(t *testing.T) {

lib.Pause(time.Second)

var (
testDatabase = "testdb"
testUser = "testUser"
testDatabasePassword = "testPass"
)

testContainer := pgstore.NewTestContainer(testDatabase, testUser, testDatabasePassword)
db := testContainer.GetTestDB()
require.NoError(t, db.Connect(ctx))
schemaUtils := pgstore.NewSchemaUtils(db)
require.NoError(t, schemaUtils.CreateTable(ctx))

config := &pgstore.Config{
DBHost: testContainer.Host(),
DBPort: testContainer.Port(),
DBName: testDatabase,
DBUser: testUser,
DBPassword: testDatabasePassword,
DBSchema: testContainer.Schema(),
}
durableStore := pgstore.NewDurableStore(config)
durableStore := testkit.NewDurableStore()
require.NoError(t, durableStore.Connect(ctx))

lib.Pause(time.Second)
Expand Down Expand Up @@ -375,9 +354,7 @@ func TestDurableStateBehavior(t *testing.T) {
lib.Pause(time.Second)

// free resources
assert.NoError(t, schemaUtils.DropTable(ctx))
assert.NoError(t, durableStore.Disconnect(ctx))
testContainer.Cleanup()
eventStream.Close()
})
}
39 changes: 18 additions & 21 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,26 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tochemey/goakt/v2/actors"
"github.com/travisjeffery/go-dynaport"
"google.golang.org/protobuf/proto"

memstore "github.com/tochemey/ego-contrib/durablestore/memory"
memory "github.com/tochemey/ego-contrib/eventstore/memory"
offsetstore "github.com/tochemey/ego-contrib/offsetstore/memory"
"github.com/tochemey/goakt/v2/log"
mockdisco "github.com/tochemey/goakt/v2/mocks/discovery"
"github.com/travisjeffery/go-dynaport"
"google.golang.org/protobuf/proto"

"github.com/tochemey/ego/v3/egopb"
samplepb "github.com/tochemey/ego/v3/example/pbs/sample/pb/v1"
"github.com/tochemey/ego/v3/internal/lib"
"github.com/tochemey/ego/v3/projection"
testpb "github.com/tochemey/ego/v3/test/data/pb/v3"
testkit2 "github.com/tochemey/ego/v3/testkit"
)

func TestEgo(t *testing.T) {
t.Run("EventSourced entity With single node cluster enabled", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))
offsetStore := offsetstore.NewOffsetStore()
offsetStore := testkit2.NewOffsetStore()
require.NoError(t, offsetStore.Connect(ctx))

nodePorts := dynaport.Get(3)
Expand Down Expand Up @@ -175,7 +172,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With no cluster enabled", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
// connect to the event store
require.NoError(t, eventStore.Connect(ctx))
// create the ego engine
Expand Down Expand Up @@ -228,7 +225,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With SendCommand when not started", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

// create the ego engine
Expand All @@ -245,7 +242,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With SendCommand when entityID is not set", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

// create the ego engine
Expand All @@ -266,7 +263,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With SendCommand when entity is not found", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

// create the ego engine
Expand All @@ -287,7 +284,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With IsProjectionRunning when not started", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

// create the ego engine
Expand All @@ -303,11 +300,11 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With RemoveProjection", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
// connect to the event store
require.NoError(t, eventStore.Connect(ctx))

offsetStore := offsetstore.NewOffsetStore()
offsetStore := testkit2.NewOffsetStore()
require.NoError(t, offsetStore.Connect(ctx))

// create the ego engine
Expand Down Expand Up @@ -346,7 +343,7 @@ func TestEgo(t *testing.T) {
t.Run("EventSourced entity With RemoveProjection when not started", func(t *testing.T) {
ctx := context.TODO()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit2.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

// create the ego engine
Expand All @@ -361,7 +358,7 @@ func TestEgo(t *testing.T) {

t.Run("DurableStore entity With single node cluster enabled", func(t *testing.T) {
ctx := context.TODO()
stateStore := memstore.NewStateStore()
stateStore := testkit2.NewDurableStore()
require.NoError(t, stateStore.Connect(ctx))

nodePorts := dynaport.Get(3)
Expand Down Expand Up @@ -457,7 +454,7 @@ func TestEgo(t *testing.T) {
})
t.Run("DurableStore entity With no cluster enabled", func(t *testing.T) {
ctx := context.TODO()
stateStore := memstore.NewStateStore()
stateStore := testkit2.NewDurableStore()
require.NoError(t, stateStore.Connect(ctx))

// create the ego engine
Expand Down Expand Up @@ -509,7 +506,7 @@ func TestEgo(t *testing.T) {
t.Run("DurableStore entity With SendCommand when not started", func(t *testing.T) {
ctx := context.TODO()

stateStore := memstore.NewStateStore()
stateStore := testkit2.NewDurableStore()
require.NoError(t, stateStore.Connect(ctx))

// create the ego engine
Expand All @@ -527,7 +524,7 @@ func TestEgo(t *testing.T) {
})
t.Run("DurableStore entity With SendCommand when entityID is not set", func(t *testing.T) {
ctx := context.TODO()
stateStore := memstore.NewStateStore()
stateStore := testkit2.NewDurableStore()
require.NoError(t, stateStore.Connect(ctx))

// create the ego engine
Expand All @@ -549,7 +546,7 @@ func TestEgo(t *testing.T) {
t.Run("DurableStore entity With SendCommand when entity is not found", func(t *testing.T) {
ctx := context.TODO()

stateStore := memstore.NewStateStore()
stateStore := testkit2.NewDurableStore()
require.NoError(t, stateStore.Connect(ctx))

// create the ego engine
Expand Down
44 changes: 9 additions & 35 deletions event_sourced_actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

memory "github.com/tochemey/ego-contrib/eventstore/memory"
pgstore "github.com/tochemey/ego-contrib/eventstore/postgres"
"github.com/tochemey/goakt/v2/actors"
"github.com/tochemey/goakt/v2/log"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/tochemey/ego/v3/egopb"
"github.com/tochemey/ego/v3/eventstream"
"github.com/tochemey/ego/v3/internal/lib"
testpb "github.com/tochemey/ego/v3/test/data/pb/v3"
"github.com/tochemey/ego/v3/testkit"
)

func TestEventSourcedActor(t *testing.T) {
Expand All @@ -64,7 +62,7 @@ func TestEventSourcedActor(t *testing.T) {
lib.Pause(time.Second)

// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down Expand Up @@ -170,7 +168,7 @@ func TestEventSourcedActor(t *testing.T) {
lib.Pause(time.Second)

// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down Expand Up @@ -264,7 +262,7 @@ func TestEventSourcedActor(t *testing.T) {
lib.Pause(time.Second)

// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down Expand Up @@ -325,29 +323,7 @@ func TestEventSourcedActor(t *testing.T) {

lib.Pause(time.Second)

// create the event store
var (
testDatabase = "testdb"
testUser = "testUser"
testDatabasePassword = "testPass"
)

testContainer := pgstore.NewTestContainer(testDatabase, testUser, testDatabasePassword)
db := testContainer.GetTestDB()
// create the event store table
require.NoError(t, db.Connect(ctx))
schemaUtils := pgstore.NewSchemaUtils(db)
require.NoError(t, schemaUtils.CreateTable(ctx))

config := &pgstore.Config{
DBHost: testContainer.Host(),
DBPort: testContainer.Port(),
DBName: testDatabase,
DBUser: testUser,
DBPassword: testDatabasePassword,
DBSchema: testContainer.Schema(),
}
eventStore := pgstore.NewEventsStore(config)
eventStore := testkit.NewEventsStore()
require.NoError(t, eventStore.Connect(ctx))

lib.Pause(time.Second)
Expand Down Expand Up @@ -457,9 +433,7 @@ func TestEventSourcedActor(t *testing.T) {
assert.True(t, proto.Equal(expected, resultingState))

// free resources
assert.NoError(t, schemaUtils.DropTable(ctx))
assert.NoError(t, eventStore.Disconnect(ctx))
testContainer.Cleanup()
// close the stream
eventStream.Close()

Expand All @@ -486,7 +460,7 @@ func TestEventSourcedActor(t *testing.T) {
lib.Pause(time.Second)

// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down Expand Up @@ -613,7 +587,7 @@ func TestEventSourcedActor(t *testing.T) {
lib.Pause(time.Second)

// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// create a persistence id
persistenceID := uuid.NewString()
// create the persistence behavior
Expand Down
4 changes: 2 additions & 2 deletions example/durablestate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ import (
"time"

"github.com/google/uuid"
memory "github.com/tochemey/ego-contrib/durablestore/memory"
"google.golang.org/protobuf/proto"

"github.com/tochemey/ego/v3"
samplepb "github.com/tochemey/ego/v3/example/pbs/sample/pb/v1"
"github.com/tochemey/ego/v3/testkit"
)

func main() {
// create the go context
ctx := context.Background()
// create the event store
durableStore := memory.NewStateStore()
durableStore := testkit.NewDurableStore()
// connect the event store
_ = durableStore.Connect(ctx)
// create the ego engine
Expand Down
4 changes: 2 additions & 2 deletions example/eventssourced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ import (
"time"

"github.com/google/uuid"
memory "github.com/tochemey/ego-contrib/eventstore/memory"
"google.golang.org/protobuf/proto"

"github.com/tochemey/ego/v3"
samplepb "github.com/tochemey/ego/v3/example/pbs/sample/pb/v1"
"github.com/tochemey/ego/v3/testkit"
)

func main() {
// create the go context
ctx := context.Background()
// create the event store
eventStore := memory.NewEventsStore()
eventStore := testkit.NewEventsStore()
// connect the event store
_ = eventStore.Connect(ctx)
// create the ego engine
Expand Down
Loading

0 comments on commit d85187d

Please sign in to comment.