Skip to content

Commit

Permalink
feat(restore_test): add a way to compare table rows not only by a count
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski committed Jul 9, 2024
1 parent db4810f commit 527e125
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/service/restore/helper_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"strings"
"testing"

Expand Down Expand Up @@ -316,6 +317,30 @@ func rowCount(t *testing.T, s gocqlx.Session, ks, tab string) int {
return cnt
}

func validateTableContent[K, V comparable](t *testing.T, src, dst gocqlx.Session, keyspace, tab, keyColumn, valueColumn string) {
srcM := selectTableAsMap[K, V](t, src, keyspace, tab, keyColumn, valueColumn)
dstM := selectTableAsMap[K, V](t, dst, keyspace, tab, keyColumn, valueColumn)
if !maps.Equal(srcM, dstM) {
t.Fatalf("tables have different contents\nsrc:\n%v\ndst:\n%v", srcM, dstM)
}
}

func selectTableAsMap[K, V comparable](t *testing.T, s gocqlx.Session, keyspace, tab, keyColumn, valueColumn string) map[K]V {
var (
k K
v V
out = make(map[K]V)
)
it := s.Session.Query(fmt.Sprintf("SELECT %s, %s FROM %q.%q", keyColumn, valueColumn, keyspace, tab)).Iter()
for it.Scan(&k, &v) {
out[k] = v
}
if err := it.Close(); err != nil {
t.Fatal(errors.Wrapf(err, "select rows %s, %s from %q.%q", keyColumn, valueColumn, keyspace, tab))
}
return out
}

func filteredTables(t *testing.T, s gocqlx.Session, filter []string) []string {
f, err := ksfilter.NewFilter(filter)
if err != nil {
Expand Down

0 comments on commit 527e125

Please sign in to comment.