Skip to content

Commit d66b362

Browse files
authored
fix query and the unit tests (#765)
fix query and the unit tests (#765)
1 parent 1fefc0e commit d66b362

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

common/query/nodeRegistrationQuery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ func (nrq *NodeRegistrationQuery) GetNodeRegistrationsWithZeroScore(registration
191191

192192
// GetNodeRegistryAtHeight returns unique latest node registry record at specific height
193193
func (nrq *NodeRegistrationQuery) GetNodeRegistryAtHeight(height uint32) string {
194-
return fmt.Sprintf("SELECT %s, max(height) AS max_height FROM %s where height <= %d AND registration_status = 0 "+
195-
"GROUP BY id ORDER BY height DESC",
196-
strings.Join(nrq.Fields, ", "), nrq.getTableName(), height)
194+
return fmt.Sprintf("SELECT %s FROM %s where registration_status = 0 AND (id,height) in (SELECT id,MAX(height) "+
195+
"FROM %s WHERE height <= %d GROUP BY id) ORDER BY height DESC",
196+
strings.Join(nrq.Fields, ", "), nrq.getTableName(), nrq.getTableName(), height)
197197
}
198198

199199
// ExtractModel extract the model struct fields to the order of NodeRegistrationQuery.Fields

common/query/nodeRegistrationQuery_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,34 @@ func TestNodeRegistrationQuery_SelectDataForSnapshot(t *testing.T) {
504504
})
505505
}
506506
}
507+
508+
func TestNodeRegistrationQuery_GetNodeRegistryAtHeight(t *testing.T) {
509+
type args struct {
510+
height uint32
511+
}
512+
tests := []struct {
513+
name string
514+
args args
515+
want string
516+
}{
517+
{
518+
name: "GetNodeRegistryAtHeightQuery",
519+
args: args{
520+
height: 11120,
521+
},
522+
want: "SELECT id, node_public_key, account_address, registration_height, node_address, " +
523+
"locked_balance, registration_status, latest, height FROM node_registry " +
524+
"where registration_status = 0 AND (id,height) in " +
525+
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 11120 GROUP BY id) " +
526+
"ORDER BY height DESC",
527+
},
528+
}
529+
for _, tt := range tests {
530+
t.Run(tt.name, func(t *testing.T) {
531+
nrq := NewNodeRegistrationQuery()
532+
if got := nrq.GetNodeRegistryAtHeight(tt.args.height); got != tt.want {
533+
t.Errorf("NodeRegistrationQuery.GetNodeRegistryAtHeight() = %v, want %v", got, tt.want)
534+
}
535+
})
536+
}
537+
}

core/service/blockMainService_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,20 @@ func (*mockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...inter
531531
mockPublishedReceipt[0].ReceiptIndex,
532532
mockPublishedReceipt[0].PublishedIndex,
533533
))
534-
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, " +
535-
"registration_status, latest, height, max(height) AS max_height FROM node_registry where height <= 0 AND " +
536-
"registration_status = 0 GROUP BY id ORDER BY height DESC":
534+
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
535+
"locked_balance, registration_status, latest, height " +
536+
"FROM node_registry where registration_status = 0 AND (id,height) in " +
537+
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 0 GROUP BY id) " +
538+
"ORDER BY height DESC":
537539
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
538540
"id", "node_public_key", "account_address", "registration_height", "node_address", "locked_balance",
539541
"registration_status", "latest", "height",
540542
}))
541-
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, " +
542-
"registration_status, latest, height, max(height) AS max_height FROM node_registry where height <= 1 " +
543-
"AND registration_status = 0 GROUP BY id ORDER BY height DESC":
543+
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
544+
"locked_balance, registration_status, latest, height " +
545+
"FROM node_registry where registration_status = 0 AND (id,height) in " +
546+
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 1 GROUP BY id) " +
547+
"ORDER BY height DESC":
544548
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
545549
"id", "node_public_key", "account_address", "registration_height", "node_address", "locked_balance",
546550
"registration_status", "latest", "height",

core/service/nodeRegistrationCoreService_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ func (*nrsMockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...in
181181
"participation_score",
182182
},
183183
).AddRow(1, nrsNodePubKey1, 8000))
184-
case "SELECT id, node_public_key, account_address, registration_height, node_address, locked_balance, registration_status, " +
185-
"latest, height, max(height) AS max_height FROM node_registry where height <= 1 AND registration_status = 0 " +
186-
"GROUP BY id ORDER BY height DESC":
184+
case "SELECT id, node_public_key, account_address, registration_height, node_address, " +
185+
"locked_balance, registration_status, latest, height " +
186+
"FROM node_registry where registration_status = 0 AND (id,height) in " +
187+
"(SELECT id,MAX(height) FROM node_registry WHERE height <= 1 GROUP BY id) " +
188+
"ORDER BY height DESC":
187189
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
188190
"id",
189191
"node_public_key",
@@ -194,10 +196,9 @@ func (*nrsMockQueryExecutorSuccess) ExecuteSelect(qe string, tx bool, args ...in
194196
"registration_status",
195197
"latest",
196198
"height",
197-
"max_height",
198199
},
199200
).AddRow(1, nrsNodePubKey1, nrsAddress1, 10, "10.10.10.10", 100000000,
200-
uint32(model.NodeRegistrationState_NodeRegistered), true, 200, 200))
201+
uint32(model.NodeRegistrationState_NodeRegistered), true, 200))
201202
default:
202203
return nil, errors.New("InvalidQuery")
203204
}

0 commit comments

Comments
 (0)