Skip to content

Commit 8d8b3dc

Browse files
committed
test: add tests for $or conditions with partial and exact address prefixes
- Introduced a new test case in volumes_test.go to validate the behavior of $or queries when combining partial and exact address prefixes. - Ensured that the tests cover scenarios with both matching and non-matching exact addresses, confirming correct volume retrieval. - Enhanced parallel execution of tests for improved performance and reliability.
1 parent ebda6ee commit 8d8b3dc

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

internal/storage/ledger/volumes_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,86 @@ func TestVolumesWithMiddleWildcardPatterns(t *testing.T) {
13821382
})
13831383
}
13841384

1385+
func TestVolumesWithOrPartialAndExactSamePrefix(t *testing.T) {
1386+
t.Parallel()
1387+
store := newLedgerStore(t)
1388+
now := time.Now()
1389+
ctx := logging.TestingContext()
1390+
1391+
// Case from Alix: $or with partial and exact on same prefix
1392+
// This is a bit silly but should work correctly
1393+
tx1 := ledger.NewTransaction().
1394+
WithPostings(
1395+
ledger.NewPosting("world", "quux:truc", "COIN", big.NewInt(100)),
1396+
ledger.NewPosting("world", "quux:other", "COIN", big.NewInt(100)),
1397+
ledger.NewPosting("world", "other:account", "COIN", big.NewInt(100)),
1398+
).
1399+
WithTimestamp(now)
1400+
require.NoError(t, store.CommitTransaction(ctx, &tx1, nil))
1401+
1402+
pit := now.Add(time.Minute)
1403+
1404+
t.Run("$or with partial and non-matching exact - quux: OR quux:nope", func(t *testing.T) {
1405+
t.Parallel()
1406+
1407+
// $or([{$match: {address: "quux:"}}, {$match: {address: "quux:nope"}}])
1408+
// quux: matches quux:truc and quux:other (2 segments)
1409+
// quux:nope matches nothing (no such account)
1410+
// Result should be quux:truc and quux:other
1411+
volumes, err := store.Volumes().Paginate(ctx,
1412+
ledgercontroller.OffsetPaginatedQuery[ledgercontroller.GetVolumesOptions]{
1413+
Options: ledgercontroller.ResourceQuery[ledgercontroller.GetVolumesOptions]{
1414+
PIT: &pit,
1415+
Builder: query.Or(
1416+
query.Match("account", "quux:"),
1417+
query.Match("account", "quux:nope"),
1418+
),
1419+
},
1420+
},
1421+
)
1422+
require.NoError(t, err)
1423+
require.Len(t, volumes.Data, 2) // quux:truc and quux:other
1424+
})
1425+
1426+
t.Run("$or with partial and matching exact - quux: OR quux:truc", func(t *testing.T) {
1427+
t.Parallel()
1428+
1429+
// quux: matches quux:truc and quux:other
1430+
// quux:truc matches quux:truc
1431+
// OR union = quux:truc and quux:other (no duplicates)
1432+
volumes, err := store.Volumes().Paginate(ctx,
1433+
ledgercontroller.OffsetPaginatedQuery[ledgercontroller.GetVolumesOptions]{
1434+
Options: ledgercontroller.ResourceQuery[ledgercontroller.GetVolumesOptions]{
1435+
PIT: &pit,
1436+
Builder: query.Or(
1437+
query.Match("account", "quux:"),
1438+
query.Match("account", "quux:truc"),
1439+
),
1440+
},
1441+
},
1442+
)
1443+
require.NoError(t, err)
1444+
require.Len(t, volumes.Data, 2) // quux:truc and quux:other
1445+
})
1446+
1447+
t.Run("without PIT - same query should work", func(t *testing.T) {
1448+
t.Parallel()
1449+
1450+
volumes, err := store.Volumes().Paginate(ctx,
1451+
ledgercontroller.OffsetPaginatedQuery[ledgercontroller.GetVolumesOptions]{
1452+
Options: ledgercontroller.ResourceQuery[ledgercontroller.GetVolumesOptions]{
1453+
Builder: query.Or(
1454+
query.Match("account", "quux:"),
1455+
query.Match("account", "quux:nope"),
1456+
),
1457+
},
1458+
},
1459+
)
1460+
require.NoError(t, err)
1461+
require.Len(t, volumes.Data, 2) // quux:truc and quux:other
1462+
})
1463+
}
1464+
13851465
func TestVolumesWithSpecialCharactersAndLongPatterns(t *testing.T) {
13861466
t.Parallel()
13871467
store := newLedgerStore(t)

0 commit comments

Comments
 (0)