Add more tests (#241)
* add form builder tests * lint * add client tests * lint * add non-existent file test
This commit is contained in:
@@ -50,13 +50,13 @@ func TestAudio(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dir, cleanup := createTestDirectory(t)
|
||||
dir, cleanup := test.CreateTestDirectory(t)
|
||||
defer cleanup()
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(dir, "fake.mp3")
|
||||
createTestFile(t, path)
|
||||
test.CreateTestFile(t, path)
|
||||
|
||||
req := AudioRequest{
|
||||
FilePath: path,
|
||||
@@ -98,13 +98,13 @@ func TestAudioWithOptionalArgs(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dir, cleanup := createTestDirectory(t)
|
||||
dir, cleanup := test.CreateTestDirectory(t)
|
||||
defer cleanup()
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(dir, "fake.mp3")
|
||||
createTestFile(t, path)
|
||||
test.CreateTestFile(t, path)
|
||||
|
||||
req := AudioRequest{
|
||||
FilePath: path,
|
||||
@@ -119,27 +119,6 @@ func TestAudioWithOptionalArgs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// createTestFile creates a fake file with "hello" as the content.
|
||||
func createTestFile(t *testing.T, path string) {
|
||||
file, err := os.Create(path)
|
||||
checks.NoError(t, err, "failed to create file")
|
||||
|
||||
if _, err = file.WriteString("hello"); err != nil {
|
||||
t.Fatalf("failed to write to file %v", err)
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
|
||||
// createTestDirectory creates a temporary folder which will be deleted when cleanup is called.
|
||||
func createTestDirectory(t *testing.T) (path string, cleanup func()) {
|
||||
t.Helper()
|
||||
|
||||
path, err := os.MkdirTemp(os.TempDir(), "")
|
||||
checks.NoError(t, err)
|
||||
|
||||
return path, func() { os.RemoveAll(path) }
|
||||
}
|
||||
|
||||
// handleAudioEndpoint Handles the completion endpoint by the test server.
|
||||
func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
@@ -190,10 +169,10 @@ func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func TestAudioWithFailingFormBuilder(t *testing.T) {
|
||||
dir, cleanup := createTestDirectory(t)
|
||||
dir, cleanup := test.CreateTestDirectory(t)
|
||||
defer cleanup()
|
||||
path := filepath.Join(dir, "fake.mp3")
|
||||
createTestFile(t, path)
|
||||
test.CreateTestFile(t, path)
|
||||
|
||||
req := AudioRequest{
|
||||
FilePath: path,
|
||||
|
||||
Reference in New Issue
Block a user