package db import ( "reflect" "testing" ) func TestShouldSkipSQLiteImportTable(t *testing.T) { tests := []struct { name string tableName string wantSkip bool }{ {name: "empty", tableName: "", wantSkip: true}, {name: "sqlite sequence", tableName: "sqlite_sequence", wantSkip: true}, {name: "goose table", tableName: "goose_db_version", wantSkip: true}, {name: "normal table", tableName: "Inventory", wantSkip: false}, {name: "snapshot table", tableName: "inventory_hourly_1700000000", wantSkip: false}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := shouldSkipSQLiteImportTable(tc.tableName) if got != tc.wantSkip { t.Fatalf("skip mismatch: got %t want %t", got, tc.wantSkip) } }) } } func TestIntersectImportColumns(t *testing.T) { source := []string{"Iid", "Name", "Vcenter", "CreationTime"} dest := []postgresColumn{ {Name: "Iid", DataType: "bigint"}, {Name: "Name", DataType: "text"}, {Name: "Vcenter", DataType: "text"}, {Name: "DeletionTime", DataType: "bigint"}, } got := intersectImportColumns(source, dest) want := []importColumn{ {SourceName: "Iid", DestinationName: "Iid", DestinationType: "bigint"}, {SourceName: "Name", DestinationName: "Name", DestinationType: "text"}, {SourceName: "Vcenter", DestinationName: "Vcenter", DestinationType: "text"}, } if !reflect.DeepEqual(got, want) { t.Fatalf("intersect mismatch:\n got: %#v\nwant: %#v", got, want) } } func TestCoerceSQLiteValueForPostgresBoolean(t *testing.T) { tests := []struct { name string input interface{} destinationType string want interface{} }{ {name: "string true", input: "true", destinationType: "boolean", want: true}, {name: "string false", input: "0", destinationType: "boolean", want: false}, {name: "int true", input: int64(1), destinationType: "boolean", want: true}, {name: "int false", input: int64(0), destinationType: "boolean", want: false}, {name: "bytes text", input: []byte("hello"), destinationType: "text", want: "hello"}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := coerceSQLiteValueForPostgres(tc.input, tc.destinationType) if !reflect.DeepEqual(got, tc.want) { t.Fatalf("coerce mismatch: got %#v want %#v", got, tc.want) } }) } } func TestPostgresPlaceholders(t *testing.T) { got := postgresPlaceholders(3) if got != "$1, $2, $3" { t.Fatalf("unexpected placeholders: %q", got) } }