Skip to content

Commit

Permalink
Adding unit tests to detect breaking changes in the code (#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshGahlot authored Dec 5, 2024
1 parent 4ab55b3 commit e242809
Show file tree
Hide file tree
Showing 16 changed files with 2,279 additions and 74 deletions.
429 changes: 429 additions & 0 deletions yb-voyager/cmd/common_test.go

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions yb-voyager/cmd/exportDataStatus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/yugabyte/yb-voyager/yb-voyager/src/testutils"
"github.com/yugabyte/yb-voyager/yb-voyager/src/utils/sqlname"
)

func TestExportSnapshotStatusStructs(t *testing.T) {

test := []struct {
name string
actualType reflect.Type
expectedType interface{}
}{
{
name: "Validate TableExportStatus Struct Definition",
actualType: reflect.TypeOf(TableExportStatus{}),
expectedType: struct {
TableName string `json:"table_name"`
FileName string `json:"file_name"`
Status string `json:"status"`
ExportedRowCountSnapshot int64 `json:"exported_row_count_snapshot"`
}{},
},
{
name: "Validate ExportSnapshotStatus Struct Definition",
actualType: reflect.TypeOf(ExportSnapshotStatus{}),
expectedType: struct {
Tables map[string]*TableExportStatus `json:"tables"`
}{},
},
}

for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
testutils.CompareStructs(t, tt.actualType, reflect.TypeOf(tt.expectedType), tt.name)
})
}
}

func TestExportSnapshotStatusJson(t *testing.T) {
// Create a table list of type []sqlname.NameTuple
o1 := sqlname.NewObjectName(POSTGRESQL, "public", "public", "table1")
o2 := sqlname.NewObjectName(POSTGRESQL, "public", "schema1", "table2")
tableList := []sqlname.NameTuple{
{CurrentName: o1, SourceName: o1, TargetName: o1},
{CurrentName: o2, SourceName: o2, TargetName: o2},
}

exportDir = filepath.Join(os.TempDir(), "export_snapshot_status_test")

// Make export directory
err := os.MkdirAll(filepath.Join(exportDir, "metainfo"), 0755)
if err != nil {
t.Fatalf("failed to create export directory: %v", err)
}

// Clean up the export directory
defer func() {
err := os.RemoveAll(exportDir)
if err != nil {
t.Fatalf("failed to remove export directory: %v", err)
}
}()

outputFilePath := filepath.Join(exportDir, "metainfo", "export_snapshot_status.json")

// Call initializeExportTableMetadata to create the export_snapshot_status.json file
initializeExportTableMetadata(tableList)

expectedExportSnapshotStatusJSON := `{
"tables": {
"public.\"table1\"": {
"table_name": "public.\"table1\"",
"file_name": "",
"status": "NOT-STARTED",
"exported_row_count_snapshot": 0
},
"schema1.\"table2\"": {
"table_name": "schema1.\"table2\"",
"file_name": "",
"status": "NOT-STARTED",
"exported_row_count_snapshot": 0
}
}
}`

// Compare the JSON representation of the sample ExportSnapshotStatus instance
testutils.CompareJson(t, outputFilePath, expectedExportSnapshotStatusJSON, exportDir)
}
93 changes: 68 additions & 25 deletions yb-voyager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/yugabyte/yb-voyager/yb-voyager
go 1.23.1

require (
cloud.google.com/go/storage v1.29.0
cloud.google.com/go/storage v1.38.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.0.0
github.com/DATA-DOG/go-sqlmock v1.5.2
Expand All @@ -13,11 +13,13 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.13.0
github.com/fergusstrange/embedded-postgres v1.29.0
github.com/go-sql-driver/mysql v1.7.0
github.com/godror/godror v0.30.2
github.com/google/uuid v1.3.0
github.com/google/uuid v1.6.0
github.com/gosuri/uilive v0.0.4
github.com/gosuri/uitable v0.0.4
github.com/hashicorp/go-version v1.7.0
github.com/jackc/pgconn v1.13.0
github.com/jackc/pgx/v4 v4.17.2
github.com/jackc/pgx/v5 v5.0.3
Expand All @@ -27,35 +29,76 @@ require (
github.com/nightlyone/lockfile v1.0.0
github.com/pganalyze/pg_query_go/v5 v5.1.0
github.com/samber/lo v1.38.1
github.com/sirupsen/logrus v1.9.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/tebeka/atexit v0.3.0
github.com/testcontainers/testcontainers-go v0.34.0
github.com/vbauerster/mpb/v8 v8.4.0
gocloud.dev v0.29.0
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/term v0.24.0
google.golang.org/api v0.118.0
google.golang.org/api v0.169.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/fergusstrange/embedded-postgres v1.29.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.2 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
)

require (
cloud.google.com/go v0.110.2 // indirect
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down Expand Up @@ -86,12 +129,12 @@ require (
github.com/godror/knownpb v0.1.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -102,7 +145,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/magiconair/properties v1.8.6
github.com/magiconair/properties v1.8.7
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand All @@ -120,17 +163,17 @@ require (
github.com/spf13/pflag v1.0.5
github.com/subosito/gotenv v1.4.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.33.0
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit e242809

Please sign in to comment.