Skip to content

Commit

Permalink
Add initial tests (#1)
Browse files Browse the repository at this point in the history
* Add 3 tests

* Add golangci-lint and fix linting errors

* Add pinned go version for CI

* Add checkout to CI GHA

* Remove golangci-lint from CI
  • Loading branch information
gwenwindflower authored Apr 2, 2024
1 parent 0eaa83a commit e1dbb93
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 66 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on:
pull_request:
branches:
- main

jobs:
ci:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup Go environment
uses: actions/[email protected]
with:
go-version: ">=1.22.1"
- name: Run tests
run: go test
54 changes: 54 additions & 0 deletions get_dbt_profile_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
processingElapsed float64
)
s := spinner.New()
s.Action(func() {
err := s.Action(func() {
connectionStart := time.Now()
buildDir := formResponse.BuildDir

Expand Down Expand Up @@ -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.")
}
5 changes: 1 addition & 4 deletions prep_build_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
16 changes: 16 additions & 0 deletions prep_build_dir_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
61 changes: 0 additions & 61 deletions sourcerer/get_tables_test.go

This file was deleted.

46 changes: 46 additions & 0 deletions write_yaml_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit e1dbb93

Please sign in to comment.