From 527e1258c7530595b4629dd43939e653172951dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= <2000michal@wp.pl> Date: Tue, 9 Jul 2024 13:00:01 +0200 Subject: [PATCH] feat(restore_test): add a way to compare table rows not only by a count --- .../restore/helper_integration_test.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/service/restore/helper_integration_test.go b/pkg/service/restore/helper_integration_test.go index 344666fa2..8dd79efe2 100644 --- a/pkg/service/restore/helper_integration_test.go +++ b/pkg/service/restore/helper_integration_test.go @@ -9,6 +9,7 @@ import ( "context" "encoding/json" "fmt" + "maps" "strings" "testing" @@ -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 {