Skip to content

Commit

Permalink
Update _testoutput format (#19)
Browse files Browse the repository at this point in the history
* update test output format to include test name (and other future context)
* save test output for the example tests
* update test output format to support multiple keys
  • Loading branch information
elh authored Feb 7, 2022
1 parent b3efe91 commit cdbf060
Show file tree
Hide file tree
Showing 91 changed files with 2,212 additions and 1,380 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/elh/bitempura.svg)](https://pkg.go.dev/github.com/elh/bitempura)
[![Build Status](https://github.com/elh/bitempura/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/elh/bitempura/actions/workflows/go.yml?query=branch%3Amain)
[![Go Report Card](https://goreportcard.com/badge/github.com/elh/bitempura)](https://goreportcard.com/report/github.com/elh/bitempura)
[![Go Non-Test Lines Of Code](https://tokei.rs/b1/github/elh/bitempura?category=code)](https://github.com/elh/bitempura/blob/main/.tokeignore)
[![Go Non-Test Lines Of Code](https://tokei.rs/b1/github/elh/bitempura?category=code)](https://github.com/elh/bitempura/blob/main/.tokeignore) <sup><sup>*non-test</sup></sup>

**Bitempura.DB is a simple, [in-memory](https://github.com/elh/bitempura/blob/main/memory/db.go), [bitemporal](https://en.wikipedia.org/wiki/Bitemporal_Modeling) key-value database.**

Expand Down
51 changes: 34 additions & 17 deletions dbtest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func TestGet(t *testing.T, oldValue, newValue Value, dbFn func(kvs []*VersionedK
t.Run(fmt.Sprintf("%v: %v", s.fixtures.name, tC.desc), func(t *testing.T) {
db, closeFn, err := dbFn(s.fixtures.vKVs())
defer closeFn()
defer writeOutputHistory(db, "A", t.Name())
defer WriteOutputHistory(db, []string{"A"}, t.Name())
require.Nil(t, err)
ret, err := db.Get(tC.key, tC.readOpts...)
if tC.expectErrNotFound {
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestList(t *testing.T, oldValue, newValue Value, dbFn func(kvs []*Versioned
t.Run(fmt.Sprintf("%v: %v", s.fixtures.name, tC.desc), func(t *testing.T) {
db, closeFn, err := dbFn(s.fixtures.vKVs())
defer closeFn()
defer writeOutputHistory(db, "A", t.Name())
defer WriteOutputHistory(db, []string{"A"}, t.Name())
require.Nil(t, err)
ret, err := db.List(tC.readOpts...)
if tC.expectErr {
Expand Down Expand Up @@ -921,7 +921,7 @@ func TestSet(t *testing.T, dbFn func(kvs []*VersionedKV, clock Clock) (DB, error
t.Run(fmt.Sprintf("%v: %v", s.fixtures.name, tC.desc), func(t *testing.T) {
clock := &TestClock{}
db, err := dbFn(s.fixtures.vKVs(), clock)
defer writeOutputHistory(db, "A", t.Name())
defer WriteOutputHistory(db, []string{"A"}, t.Name())
require.Nil(t, err)
if tC.now != nil {
require.Nil(t, clock.SetNow(*tC.now))
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func TestDelete(t *testing.T, oldValue, newValue Value, dbFn func(kvs []*Version
clock := &TestClock{}
db, closeFn, err := dbFn(s.fixtures.vKVs(), clock)
defer closeFn()
defer writeOutputHistory(db, "A", t.Name())
defer WriteOutputHistory(db, []string{"A"}, t.Name())
require.Nil(t, err)
if tC.now != nil {
require.Nil(t, clock.SetNow(*tC.now))
Expand Down Expand Up @@ -1615,7 +1615,7 @@ func TestHistory(t *testing.T, oldValue, newValue Value, dbFn func(kvs []*Versio
t.Run(fmt.Sprintf("%v: %v", s.fixtures.name, tC.desc), func(t *testing.T) {
db, closeFn, err := dbFn(s.fixtures.vKVs())
defer closeFn()
defer writeOutputHistory(db, "A", t.Name())
defer WriteOutputHistory(db, []string{"A"}, t.Name())
require.Nil(t, err)
ret, err := db.History(tC.key)
if tC.expectErrNotFound {
Expand Down Expand Up @@ -1648,27 +1648,44 @@ func toJSON(v interface{}) string {
return string(out)
}

func writeOutputHistory(db DB, key, testName string) {
// TestOutput is the format for saving test data for debugging and visualization.
type TestOutput struct {
TestName string
Histories map[string][]*VersionedKV // key -> history
}

// WriteOutputHistory writes to a file the final "history" for specified keys at the end of a test. This is used for
// debugging and visualization.
func WriteOutputHistory(db DB, keys []string, testName string) {
if !outputHistory {
return
}

histories := map[string][]*VersionedKV{}
for _, key := range keys {
kvs, err := db.History(key)
if errors.Is(err, ErrNotFound) {
kvs = []*VersionedKV{}
} else if err != nil {
fmt.Printf("failed to get output history for test=%v\n: %v", testName, err)
return
}
histories[key] = kvs
}
o := TestOutput{
TestName: testName,
Histories: histories,
}
kvsJSON := toJSON(o)

// format test name for file friendliness
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
log.Fatal(err)
}
testName = reg.ReplaceAllString(testName, "_")

kvs, err := db.History(key)
if errors.Is(err, ErrNotFound) {
kvs = []*VersionedKV{}
} else if err != nil {
fmt.Printf("failed to get output history for test=%v\n: %v", testName, err)
return
}
kvsJSON := toJSON(kvs)
fileName := reg.ReplaceAllString(testName, "_")
_ = os.Mkdir(outputDir, 0777)
err = os.WriteFile(fmt.Sprintf("%s/%s.json", outputDir, testName), []byte(kvsJSON), 0644)
err = os.WriteFile(fmt.Sprintf("%s/%s.json", outputDir, fileName), []byte(kvsJSON), 0644)
if err != nil {
fmt.Printf("failed to write output history for test=%v\n: %v", testName, err)
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
[]
{
"TestName": "TestDelete/empty_db:_delete_with_no_match_is_nop",
"Histories": {
"A": []
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
[
{
"Key": "A",
"Value": "NEW",
"TxTimeStart": "2022-01-04T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-04T00:00:00Z",
"ValidTimeEnd": null
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-04T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-02T00:00:00Z"
},
{
"Key": "A",
"Value": "NEW",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-03T00:00:00Z",
"ValidTimeEnd": null
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-03T00:00:00Z"
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-03T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entries._multiple_valid_time_ranges_active:_set_overlaps_multiple_versions",
"Histories": {
"A": [
{
"Key": "A",
"Value": "NEW",
"TxTimeStart": "2022-01-04T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-04T00:00:00Z",
"ValidTimeEnd": null
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-04T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-02T00:00:00Z"
},
{
"Key": "A",
"Value": "NEW",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-03T00:00:00Z",
"ValidTimeEnd": null
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-03T00:00:00Z"
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-03T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
[
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-03T00:00:00Z"
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-03T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entry_-_no_valid_end:_basic_delete",
"Histories": {
"A": [
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-03T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": "2022-01-03T00:00:00Z"
},
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-03T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entry_-_no_valid_end:_error_if_end_valid_time_before_valid_time",
"Histories": {
"A": [
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entry_-_no_valid_end:_error_if_end_valid_time_before_valid_time_(default_valid_time)",
"Histories": {
"A": [
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entry_-_no_valid_end:_error_if_end_valid_time_equal_to_valid_time",
"Histories": {
"A": [
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": null,
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
{
"TestName": "TestDelete/existing_entry_-_no_valid_end:_set_w/_valid_time_end._no_overhang",
"Histories": {
"A": [
{
"Key": "A",
"Value": "OLD",
"TxTimeStart": "2022-01-01T00:00:00Z",
"TxTimeEnd": "2022-01-04T00:00:00Z",
"ValidTimeStart": "2022-01-01T00:00:00Z",
"ValidTimeEnd": null
}
]
}
]
}
Loading

0 comments on commit cdbf060

Please sign in to comment.