-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Test] GenerateColumnDescriptions testing and some refactoring (#4)
Venturing into the bold new world of API call mocking this morning, using [httpmock](https://github.com/jarcoal/httpmock) to mock the Groq API calls required in GenerateColumnDescriptions. Next stop: mocking database calls! The test caught that there was a missing `wg.Wait()` call. Also does some very minor refactoring to wrap the `WriteYAML` and `WriteStagingModels` calls in `main.go` with a `WriteFiles` function to improve the simplicity and readability of `main.go`.
- Loading branch information
Showing
11 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
) | ||
|
||
func TestGetGroqResponse(t *testing.T) { | ||
prompt := "Who destroyed Orthanc" | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
httpmock.RegisterResponder("POST", "https://api.groq.com/openai/v1/chat/completions", | ||
httpmock.NewStringResponder(200, `{"choices": [{"index": 0, "message": {"role": "assistant","content": "Treebeard and the Ents destroyed Orthanc."}}]}`)) | ||
GroqResponse, err := GetGroqResponse(prompt) | ||
if err != nil { | ||
t.Error("expected", nil, "got", err) | ||
} | ||
info := httpmock.GetCallCountInfo() | ||
if info["POST https://api.groq.com/openai/v1/chat/completions"] != 1 { | ||
t.Error("expected", 1, "got", info["POST https://api.groq.com/openai/v1/chat/completions"]) | ||
} | ||
expected := "Treebeard and the Ents destroyed Orthanc." | ||
if GroqResponse.Choices[0].Message.Content != expected { | ||
t.Error("expected", expected, "got", GroqResponse.Choices[0].Message.Content) | ||
} | ||
} | ||
|
||
func TestGenerateColumnDescriptions(t *testing.T) { | ||
ts := CreateTempSourceTables() | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
httpmock.RegisterResponder("POST", "https://api.groq.com/openai/v1/chat/completions", | ||
httpmock.NewStringResponder(200, `{"choices": [{"index": 0, "message": {"role": "assistant","content": "lord of rivendell"}}]}`)) | ||
GenerateColumnDescriptions(ts) | ||
|
||
info := httpmock.GetCallCountInfo() | ||
if info["POST https://api.groq.com/openai/v1/chat/completions"] != 2 { | ||
t.Error("expected", 2, "got", info["POST https://api.groq.com/openai/v1/chat/completions"]) | ||
} | ||
|
||
expected := "lord of rivendell" | ||
desc := ts.SourceTables[0].Columns[0].Description | ||
if desc != expected { | ||
t.Error("expected", expected, "got", desc) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/gwenwindflower/tbd/shared" | ||
) | ||
|
||
func WriteFiles(ts shared.SourceTables, bd string) error { | ||
if len(ts.SourceTables) == 0 { | ||
return errors.New("no tables to write") | ||
} | ||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go func() { | ||
defer wg.Done() | ||
WriteYAML(ts, bd) | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
WriteStagingModels(ts, bd) | ||
}() | ||
wg.Wait() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gwenwindflower/tbd/shared" | ||
) | ||
|
||
func TestWriteFiles(t *testing.T) { | ||
ts := shared.SourceTables{ | ||
SourceTables: []shared.SourceTable{ | ||
{ | ||
Name: "table1", | ||
Columns: []shared.Column{ | ||
{ | ||
Name: "column1", | ||
DataType: "type1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
bd := t.TempDir() | ||
WriteFiles(ts, bd) | ||
} | ||
|
||
func TestWriteFilesError(t *testing.T) { | ||
ts := shared.SourceTables{ | ||
SourceTables: []shared.SourceTable{}, | ||
} | ||
bd := t.TempDir() | ||
|
||
err := WriteFiles(ts, bd) | ||
if err == nil { | ||
t.Error("expected error, got nil") | ||
} else { | ||
if !strings.Contains(err.Error(), "no tables to write") { | ||
t.Errorf("expected error to contain 'no tables to write', got %v", err) | ||
} | ||
} | ||
} |