refactor: unit testable units and cleaner dep injection
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package livestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cloud.google.com/go/firestore"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source ./membershippublisher.go -destination ./mocks/membershippublisher.go
|
||||
|
||||
// MembershipPublisher publishes network membership changes to the live store
|
||||
// (Firestore) that clients subscribe to. Postgres remains the source of truth;
|
||||
// the membership reconciler heals any drift, so callers may log and ignore
|
||||
// publish failures.
|
||||
type MembershipPublisher interface {
|
||||
Add(ctx context.Context, humanId, networkID string) error
|
||||
Remove(ctx context.Context, humanId, networkID string) error
|
||||
}
|
||||
|
||||
func NewMembershipPublisher(fs *firestore.Client) MembershipPublisher {
|
||||
return &firestoreMembershipPublisher{fs: fs}
|
||||
}
|
||||
|
||||
type firestoreMembershipPublisher struct {
|
||||
fs *firestore.Client
|
||||
}
|
||||
|
||||
func (p *firestoreMembershipPublisher) Add(ctx context.Context, humanId, networkID string) error {
|
||||
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
||||
"networks": firestore.ArrayUnion(networkID),
|
||||
"updated_at": firestore.ServerTimestamp,
|
||||
}, firestore.MergeAll)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *firestoreMembershipPublisher) Remove(ctx context.Context, humanId, networkID string) error {
|
||||
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
||||
"networks": firestore.ArrayRemove(networkID),
|
||||
"updated_at": firestore.ServerTimestamp,
|
||||
}, firestore.MergeAll)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: ./membershippublisher.go
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -source ./membershippublisher.go -destination ./mocks/membershippublisher.go
|
||||
//
|
||||
|
||||
// Package mock_livestore is a generated GoMock package.
|
||||
package mock_livestore
|
||||
|
||||
import (
|
||||
context "context"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
// MockMembershipPublisher is a mock of MembershipPublisher interface.
|
||||
type MockMembershipPublisher struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockMembershipPublisherMockRecorder
|
||||
isgomock struct{}
|
||||
}
|
||||
|
||||
// MockMembershipPublisherMockRecorder is the mock recorder for MockMembershipPublisher.
|
||||
type MockMembershipPublisherMockRecorder struct {
|
||||
mock *MockMembershipPublisher
|
||||
}
|
||||
|
||||
// NewMockMembershipPublisher creates a new mock instance.
|
||||
func NewMockMembershipPublisher(ctrl *gomock.Controller) *MockMembershipPublisher {
|
||||
mock := &MockMembershipPublisher{ctrl: ctrl}
|
||||
mock.recorder = &MockMembershipPublisherMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockMembershipPublisher) EXPECT() *MockMembershipPublisherMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Add mocks base method.
|
||||
func (m *MockMembershipPublisher) Add(ctx context.Context, humanId, networkID string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Add", ctx, humanId, networkID)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Add indicates an expected call of Add.
|
||||
func (mr *MockMembershipPublisherMockRecorder) Add(ctx, humanId, networkID any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockMembershipPublisher)(nil).Add), ctx, humanId, networkID)
|
||||
}
|
||||
|
||||
// Remove mocks base method.
|
||||
func (m *MockMembershipPublisher) Remove(ctx context.Context, humanId, networkID string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Remove", ctx, humanId, networkID)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Remove indicates an expected call of Remove.
|
||||
func (mr *MockMembershipPublisherMockRecorder) Remove(ctx, humanId, networkID any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Remove", reflect.TypeOf((*MockMembershipPublisher)(nil).Remove), ctx, humanId, networkID)
|
||||
}
|
||||
Reference in New Issue
Block a user