Skip to content

Commit

Permalink
Merge pull request #3 from PatrickBirchall/minor-fixes
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
PatrickBirchall authored Dec 10, 2024
2 parents 6b5ef9f + 19e51fc commit 6272ae9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ jobs:
with:
go-version: '1.21'

- name: Run tests
run: go test

- name: Build
run: go build -v ./...
53 changes: 53 additions & 0 deletions downloadImage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)

func TestDownloadImage(t *testing.T) {
downloader := RealImageDownloader{}

// Create a temporary directory for testing
tempDir := t.TempDir()

// Test case: Successful download
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test image content"))
}))
defer server.Close()

filename := "test_image.jpg"
err := downloader.DownloadImage(server.URL, tempDir, filename)
if err != nil {
t.Errorf("expected no error, got %v", err)
}

// Check if the file was created
filePath := filepath.Join(tempDir, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Errorf("expected file %s to be created, but it does not exist", filePath)
}

// Check the file content
file, err := os.Open(filePath)
if err != nil {
t.Errorf("expected no error opening file, got %v", err)
}
defer file.Close()

content, err := io.ReadAll(file)
if err != nil {
t.Errorf("expected no error reading file, got %v", err)
}

expectedContent := "test image content"
if string(content) != expectedContent {
t.Errorf("expected file content %s, got %s", expectedContent, string(content))
}
}

0 comments on commit 6272ae9

Please # to comment.