-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testutils: Add script-based test utilities
Add generic testscript commands for testing against StateDB tables. This allows implementing tests as scripts, which becomes useful when tests perform multiple steps on tables and need to verify the output each step. Signed-off-by: Jussi Maki <jussi.maki@isovalent.com>
- Loading branch information
Showing
10 changed files
with
809 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package statedb | ||
|
||
import ( | ||
"iter" | ||
) | ||
|
||
// AnyTable allows any-typed access to a StateDB table. This is intended | ||
// for building generic tooling for accessing the table and should be | ||
// avoided if possible. | ||
type AnyTable struct { | ||
Meta TableMeta | ||
} | ||
|
||
func (t AnyTable) All(txn ReadTxn) iter.Seq2[any, Revision] { | ||
indexTxn := txn.getTxn().mustIndexReadTxn(t.Meta, PrimaryIndexPos) | ||
return anySeq(indexTxn.Iterator()) | ||
} | ||
|
||
func (t AnyTable) UnmarshalYAML(data []byte) (any, error) { | ||
return t.Meta.unmarshalYAML(data) | ||
} | ||
|
||
func (t AnyTable) Insert(txn WriteTxn, obj any) (old any, hadOld bool, err error) { | ||
var iobj object | ||
iobj, hadOld, err = txn.getTxn().insert(t.Meta, Revision(0), obj) | ||
if hadOld { | ||
old = iobj.data | ||
} | ||
return | ||
} | ||
|
||
func (t AnyTable) Delete(txn WriteTxn, obj any) (old any, hadOld bool, err error) { | ||
var iobj object | ||
iobj, hadOld, err = txn.getTxn().delete(t.Meta, Revision(0), obj) | ||
if hadOld { | ||
old = iobj.data | ||
} | ||
return | ||
} | ||
|
||
func (t AnyTable) Prefix(txn ReadTxn, key string) iter.Seq2[any, Revision] { | ||
indexTxn := txn.getTxn().mustIndexReadTxn(t.Meta, PrimaryIndexPos) | ||
iter, _ := indexTxn.Prefix([]byte(key)) | ||
return anySeq(iter) | ||
} | ||
|
||
func (t AnyTable) LowerBound(txn ReadTxn, key string) iter.Seq2[any, Revision] { | ||
indexTxn := txn.getTxn().mustIndexReadTxn(t.Meta, PrimaryIndexPos) | ||
iter := indexTxn.LowerBound([]byte(key)) | ||
return anySeq(iter) | ||
} | ||
|
||
func (t AnyTable) TableHeader() []string { | ||
zero := t.Meta.proto() | ||
if tw, ok := zero.(TableWritable); ok { | ||
return tw.TableHeader() | ||
} | ||
return nil | ||
} | ||
|
||
func (t AnyTable) Proto() any { | ||
return t.Meta.proto() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.