diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..43a45d4 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,19 @@ +name: CI + +on: + pull_request: + branches: + - main + +jobs: + ci: + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v4.1.1 + - name: Setup Go environment + uses: actions/setup-go@v5.0.0 + with: + go-version: ">=1.22.1" + - name: Run tests + run: go test diff --git a/get_dbt_profile_test.go b/get_dbt_profile_test.go new file mode 100644 index 0000000..91d1ad7 --- /dev/null +++ b/get_dbt_profile_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestGetDbtProfile(t *testing.T) { + // Create a temporary profiles.yml file for testing + tmpDir := t.TempDir() + err := os.Mkdir(filepath.Join(tmpDir, ".dbt"), 0755) + if err != nil { + t.Fatalf("Failed to create temporary .dbt directory: %v", err) + } + profilesFile := filepath.Join(tmpDir, ".dbt", "profiles.yml") + content := []byte(` +test_profile: + target: dev + outputs: + dev: + type: snowflake + account: testaccount + user: testuser + password: testpassword + database: testdb + warehouse: testwh + schema: testschema +`) + err = os.WriteFile(profilesFile, content, 0644) + if err != nil { + t.Fatalf("Failed to create temporary profiles.yml file: %v", err) + } + + os.Setenv("HOME", tmpDir) + + // Profile exists + profile, err := GetDbtProfile("test_profile") + if err != nil { + t.Errorf("GetDbtProfile returned an error for an existing profile: %v", err) + } + if profile.Target != "dev" { + t.Errorf("Expected target 'dev', got '%s'", profile.Target) + } + + // Profile does not exist + profile, err = GetDbtProfile("aragorn") + if err == nil { + t.Error("GetDbtProfile did not return an error for a non-existing profile") + } + if profile != nil { + t.Error("GetDbtProfile returned a non-nil profile for a non-existing profile") + } +} diff --git a/main.go b/main.go index 43eb682..d50b0ea 100644 --- a/main.go +++ b/main.go @@ -41,7 +41,7 @@ func main() { processingElapsed float64 ) s := spinner.New() - s.Action(func() { + err := s.Action(func() { connectionStart := time.Now() buildDir := formResponse.BuildDir @@ -71,6 +71,9 @@ func main() { wg.Wait() processingElapsed = time.Since(processingStart).Seconds() }).Title("🏎️✨ Generating YAML and SQL files...").Run() + if err != nil { + log.Fatalf("Error running spinner action: %v\n", err) + } fmt.Printf("🏁 Done in %.1fs getting data from the db and %.1fs processing! ", dbElapsed, processingElapsed) fmt.Println("Your YAML and SQL files are in the build directory.") } diff --git a/prep_build_dir.go b/prep_build_dir.go index 061667b..2fac191 100644 --- a/prep_build_dir.go +++ b/prep_build_dir.go @@ -7,13 +7,10 @@ import ( func PrepBuildDir(buildDir string) { _, err := os.Stat(buildDir) - if err != nil { - log.Fatalf("Failed to get directory info %v\n", err) - } if os.IsNotExist(err) { dirErr := os.MkdirAll(buildDir, 0755) if dirErr != nil { - log.Fatalf("Failed to create directory %v\n", dirErr) + log.Fatalf("Failed to create directory %v", dirErr) } } } diff --git a/prep_build_dir_test.go b/prep_build_dir_test.go new file mode 100644 index 0000000..8b87270 --- /dev/null +++ b/prep_build_dir_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "os" + "testing" +) + +func TestPrepBuildDir(t *testing.T) { + buildDir := "testPrepBuildDir" + PrepBuildDir(buildDir) + _, err := os.Stat(buildDir) + if os.IsNotExist(err) { + t.Errorf("PrepBuildDir did not create the directory") + } + os.Remove(buildDir) +} diff --git a/sourcerer/get_tables_test.go b/sourcerer/get_tables_test.go deleted file mode 100644 index e64ac4d..0000000 --- a/sourcerer/get_tables_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package sourcerer - -// -// import ( -// "context" -// "fmt" -// "testing" -// -// sqlmock "github.com/DATA-DOG/go-sqlmock" -// ) -// -// func TestGetTables(t *testing.T) { -// db, mock, err := sqlmock.New() -// if err != nil { -// t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) -// } -// defer db.Close() -// -// ctx := context.TODO() -// -// rows := sqlmock.NewRows([]string{"table_name"}). -// AddRow("raw_orders"). -// AddRow("raw_order_items"). -// AddRow("raw_locations") -// -// schema := "test_schema" -// mock.ExpectQuery(fmt.Sprintf("SELECT table_name FROM information_schema.tables where table_schema = '%s'", schema)).WillReturnRows(rows) -// -// result, err := GetTables(db, ctx, schema) -// if err != nil { -// t.Fatalf("error was not expected while retrieving tables: %s", err) -// } -// -// if len(result.SourceTables) != 3 || result.SourceTables[0].Name != "raw_orders" || result.SourceTables[1].Name != "raw_order_items" { -// t.Errorf("result not match, got %+v", result) -// } -// -// if err := mock.ExpectationsWereMet(); err != nil { -// t.Errorf("there were unfulfilled expectations: %s", err) -// } -// } -// -// func TestGetTablesNoTablesInSchema(t *testing.T) { -// db, mock, err := sqlmock.New() -// if err != nil { -// t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) -// } -// defer db.Close() -// ctx := context.TODO() -// -// schema := "test_schema" -// mock.NewColumn("table_name") -// mock.ExpectQuery(fmt.Sprintf("SELECT table_name FROM information_schema.tables where table_schema = '%s'", schema)).WillReturnError(err) -// _, err = GetTables(db, ctx, schema) -// if err == nil { -// t.Fatalf("error was expected while retrieving tables") -// } -// if err := mock.ExpectationsWereMet(); err != nil { -// t.Errorf("there were unfulfilled expectations: %s", err) -// } -// } diff --git a/write_yaml_test.go b/write_yaml_test.go new file mode 100644 index 0000000..e773ade --- /dev/null +++ b/write_yaml_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "os" + "tbd/shared" + "testing" +) + +func TestWriteYAML(t *testing.T) { + tablesFixture := shared.SourceTables{ + SourceTables: []shared.SourceTable{ + { + Name: "table1", + Columns: []shared.Column{ + { + Name: "column1", + Description: "column1 description", + DataType: "int", + Tests: []string{}, + }, + }, + DataTypeGroups: map[string][]shared.Column{ + "int": { + shared.Column{ + Name: "column1", + Description: "column1 description", + DataType: "int", + Tests: []string{}, + }, + }, + }, + }, + }, + } + buildDir := "testWriteYAML" + PrepBuildDir(buildDir) + WriteYAML(tablesFixture, buildDir) + _, err := os.Stat(buildDir + "/_sources.yml") + if os.IsNotExist(err) { + t.Errorf("WriteYAML did not create the file") + } + err = os.RemoveAll(buildDir) + if err != nil { + t.Errorf("Failed to clean up test directory") + } +}