This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel_sync_transactions_test.go
136 lines (114 loc) · 3.41 KB
/
model_sync_transactions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package bux
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSyncTransaction_GetModelName will test the method GetModelName()
func TestSyncTransaction_GetModelName(t *testing.T) {
t.Parallel()
t.Run("valid name", func(t *testing.T) {
syncTx := newSyncTransaction(testTxID, &SyncConfig{SyncOnChain: true, Broadcast: true}, New())
require.NotNil(t, syncTx)
assert.Equal(t, ModelSyncTransaction.String(), syncTx.GetModelName())
})
t.Run("missing config", func(t *testing.T) {
syncTx := newSyncTransaction(testTxID, nil, New())
require.Nil(t, syncTx)
})
}
func Test_areParentsBroadcast(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, true, withTaskManagerMockup())
defer deferMe()
opts := []ModelOps{WithClient(client)}
tx, err := txFromHex(testTxHex, append(opts, New())...)
require.NoError(t, err)
txErr := tx.Save(ctx)
require.NoError(t, txErr)
tx2, err := txFromHex(testTx2Hex, append(opts, New())...)
require.NoError(t, err)
txErr = tx2.Save(ctx)
require.NoError(t, txErr)
tx3, err := txFromHex(testTx3Hex, append(opts, New())...)
require.NoError(t, err)
txErr = tx3.Save(ctx)
require.NoError(t, txErr)
// input of testTxID
syncTx := newSyncTransaction("65bb8d2733298b2d3b441a871868d6323c5392facf0d3eced3a6c6a17dc84c10", &SyncConfig{SyncOnChain: false, Broadcast: false}, append(opts, New())...)
syncTx.BroadcastStatus = SyncStatusComplete
txErr = syncTx.Save(ctx)
require.NoError(t, txErr)
// input of testTxInID
syncTx = newSyncTransaction("89fbccca3a5e2bfc8a161bf7f54e8cb5898e296ae8c23b620b89ed570711f931", &SyncConfig{SyncOnChain: false, Broadcast: false}, append(opts, New())...)
txErr = syncTx.Save(ctx)
require.NoError(t, txErr)
type args struct {
tx *Transaction
opts []ModelOps
}
tests := []struct {
name string
args args
want bool
wantErr assert.ErrorAssertionFunc
}{
{
name: "no parents",
args: args{
tx: tx3,
opts: opts,
},
want: true,
wantErr: assert.NoError,
},
{
name: "parent not broadcast",
args: args{
tx: tx2,
opts: opts,
},
want: false,
wantErr: assert.NoError,
},
{
name: "parent broadcast",
args: args{
tx: tx,
opts: opts,
},
want: true,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := _areParentsBroadcasted(ctx, tt.args.tx, tt.args.opts...)
if !tt.wantErr(t, err, fmt.Sprintf("areParentsBroadcast(%v, %v, %v)", ctx, tt.args.tx, tt.args.opts)) {
return
}
assert.Equalf(t, tt.want, got, "areParentsBroadcast(%v, %v, %v)", ctx, tt.args.tx, tt.args.opts)
})
}
}
func TestSyncTransaction_SaveHook(t *testing.T) {
t.Parallel()
t.Run("trim Results to last 20 messages", func(t *testing.T) {
// Given
ctx, client, deferMe := CreateTestSQLiteClient(t, false, true, withTaskManagerMockup())
defer deferMe()
opts := []ModelOps{WithClient(client), New()}
syncTx := newSyncTransaction(testTxID, &SyncConfig{SyncOnChain: true, Broadcast: true}, opts...)
txErr := syncTx.Save(ctx)
require.NoError(t, txErr)
// When
for i := 0; i < 40; i++ {
syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{Action: "test", StatusMessage: "msg"})
}
txErr = syncTx.Save(ctx)
require.NoError(t, txErr)
// Then
resultsLen := len(syncTx.Results.Results)
require.Equal(t, 20, resultsLen)
})
}