Skip to content

Commit 0195dfd

Browse files
committed
refactor(repository): remove test-only methods and improve test setup
1 parent ec16956 commit 0195dfd

9 files changed

Lines changed: 500 additions & 842 deletions

File tree

internal/jsonrpc/jsonrpc_test.go

Lines changed: 361 additions & 456 deletions
Large diffs are not rendered by default.

internal/jsonrpc/util_test.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919
"github.com/cartesi/rollups-node/internal/config"
2020
"github.com/cartesi/rollups-node/internal/model"
2121
"github.com/cartesi/rollups-node/internal/repository/factory"
22+
"github.com/cartesi/rollups-node/internal/repository/repotest"
2223
"github.com/cartesi/rollups-node/pkg/service"
2324
"github.com/cartesi/rollups-node/test/tooling/db"
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/common/hexutil"
26-
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
2728
)
2829

2930
// histogram to gather statistics for each method
@@ -73,16 +74,16 @@ func newTestService(t *testing.T, name string) *Service {
7374
ctx := context.Background()
7475

7576
dbTestEndpoint, err := db.GetTestDatabaseEndpoint()
76-
assert.Nil(t, err)
77+
require.NoError(t, err)
7778

7879
err = db.SetupTestPostgres(dbTestEndpoint)
79-
assert.Nil(t, err)
80+
require.NoError(t, err)
8081

8182
repo, err := factory.NewRepositoryFromConnectionString(ctx, dbTestEndpoint)
82-
assert.Nil(t, err)
83+
require.NoError(t, err)
8384

8485
logLevel, err := config.GetLogLevel()
85-
assert.Nil(t, err)
86+
require.NoError(t, err)
8687

8788
ci := CreateInfo{
8889
CreateInfo: service.CreateInfo{
@@ -93,7 +94,7 @@ func newTestService(t *testing.T, name string) *Service {
9394
Repository: repo,
9495
}
9596
s, err := Create(ctx, &ci)
96-
assert.Nil(t, err, "on new test service")
97+
require.NoError(t, err, "on new test service")
9798

9899
return s
99100
}
@@ -108,17 +109,14 @@ func numberToName(x uint64) string {
108109
}
109110

110111
// create an application with mostly stub values.
111-
func (s *Service) newTestApplication(ctx context.Context, t *testing.T, test, i uint64) int64 {
112+
func (s *Service) newTestApplication(ctx context.Context, t *testing.T, i uint64) int64 {
112113
hex := numberToName(i)
113-
id, err := s.repository.CreateApplication(ctx, &model.Application{
114-
Name: hex,
115-
IApplicationAddress: common.HexToAddress(hex),
116-
DataAvailability: []byte{0x00, 0x00, 0x00, 0x00},
117-
State: model.ApplicationState_Enabled,
118-
ConsensusType: model.Consensus_Authority,
119-
}, false)
120-
assert.Nil(t, err, "on test case: %v, when creating application: %v", test, i)
121-
return id
114+
app := repotest.NewApplicationBuilder().
115+
WithName(hex).
116+
WithAddress(common.HexToAddress(hex)).
117+
WithDataAvailability([]byte{0x00, 0x00, 0x00, 0x00}).
118+
Create(ctx, t, s.repository)
119+
return app.ID
122120
}
123121

124122
func (s *Service) doRequest(t *testing.T, i uint64, reqData []byte) []byte {
@@ -134,7 +132,7 @@ func (s *Service) doRequest(t *testing.T, i uint64, reqData []byte) []byte {
134132

135133
body := new(strings.Builder)
136134
_, err := io.Copy(body, w.Result().Body)
137-
assert.Nil(t, err, "on test case: %v", i)
135+
require.NoError(t, err, "on test case: %v", i)
138136

139137
return []byte(body.String())
140138
}
@@ -151,6 +149,35 @@ func emptyVoucher() []byte {
151149
return raw
152150
}
153151

152+
// createTestEpoch creates an epoch using production CreateEpochsAndInputs.
153+
func (s *Service) createTestEpoch(ctx context.Context, t *testing.T, appName string, epoch *model.Epoch) {
154+
t.Helper()
155+
err := s.repository.CreateEpochsAndInputs(ctx, appName,
156+
map[*model.Epoch][]*model.Input{epoch: {}}, 10)
157+
require.NoError(t, err)
158+
}
159+
160+
// createTestEpochWithInput creates an epoch with one input using production CreateEpochsAndInputs.
161+
func (s *Service) createTestEpochWithInput(
162+
ctx context.Context, t *testing.T, appName string,
163+
epoch *model.Epoch, input *model.Input,
164+
) {
165+
t.Helper()
166+
err := s.repository.CreateEpochsAndInputs(ctx, appName,
167+
map[*model.Epoch][]*model.Input{epoch: {input}}, 10)
168+
require.NoError(t, err)
169+
}
170+
171+
// advanceInput stores an advance result (outputs/reports) for an input using production StoreAdvanceResult.
172+
func (s *Service) advanceInput(
173+
ctx context.Context, t *testing.T, appID int64,
174+
epochIndex, inputIndex uint64, outputs [][]byte, reports [][]byte,
175+
) {
176+
t.Helper()
177+
repotest.StoreAdvanceResult(ctx, t, s.repository, appID, epochIndex, inputIndex,
178+
model.InputCompletionStatus_Accepted, outputs, reports)
179+
}
180+
154181
type listTournamentsResult struct {
155182
EpochIndex hex64 `json:"epoch_index"`
156183
Address common.Address `json:"address"`

internal/repository/postgres/test_only.go

Lines changed: 0 additions & 135 deletions
This file was deleted.

internal/repository/postgres/util.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ func hashToBytes(h *common.Hash) any {
4141
return (*h)[:]
4242
}
4343

44-
func addressToBytes(a *common.Address) any {
45-
if a == nil {
46-
return nil
47-
}
48-
return (*a)[:]
49-
}
50-
5144
// SubstrBytea returns a SUBSTR expression properly typed as ByteaExpression.
5245
func SubstrBytea(col postgres.ColumnBytea, from, count int64) postgres.ByteaExpression {
5346
qualified := pgx.Identifier{col.TableName(), col.Name()}.Sanitize()

internal/repository/repository.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,6 @@ type ClaimerRepository interface {
210210
) error
211211
}
212212

213-
type TestRepository interface {
214-
CreateEpoch(ctx context.Context, e *Epoch) error
215-
CreateInput(ctx context.Context, inp *Input) error
216-
CreateOutput(ctx context.Context, out *Output) error
217-
CreateReport(ctx context.Context, report *Report) error
218-
}
219-
220213
type Repository interface {
221214
ApplicationRepository
222215
EpochRepository
@@ -231,7 +224,6 @@ type Repository interface {
231224
BulkOperationsRepository
232225
NodeConfigRepository
233226
ClaimerRepository
234-
TestRepository
235227
Close()
236228
}
237229

0 commit comments

Comments
 (0)