-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor whilst I still have copilot access
- Loading branch information
Patrick Birchall
committed
Dec 9, 2024
1 parent
6572d9f
commit 25550de
Showing
5 changed files
with
99 additions
and
7 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,40 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func MockResponse(statusCode int, body string) *http.Response { | ||
return &http.Response{ | ||
StatusCode: statusCode, | ||
Body: io.NopCloser(strings.NewReader(body)), | ||
} | ||
} | ||
|
||
func TestFetchAPOD(t *testing.T) { | ||
os.Unsetenv("NASA_KEY") | ||
_, err := FetchAPOD("https://api.nasa.gov/planetary/apod") | ||
if err == nil || err.Error() != "NASA_KEY environment variable not set" { | ||
t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err) | ||
} | ||
|
||
os.Setenv("NASA_KEY", "DEMO_KEY") | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`)) | ||
})) | ||
defer server.Close() | ||
|
||
resp, err := FetchAPOD(server.URL) | ||
if err != nil { | ||
t.Errorf("expected no error, got %v", err) | ||
} | ||
if resp.Title != "Test Title" { | ||
t.Errorf("expected title 'Test Title', got %v", resp.Title) | ||
} | ||
} |
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,36 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
type MockImageDownloader struct{} | ||
|
||
func (d MockImageDownloader) DownloadImage(url, dir, filename string) error { | ||
return nil | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
downloader := MockImageDownloader{} | ||
|
||
os.Unsetenv("NASA_KEY") | ||
err := run(downloader) | ||
if err == nil || err.Error() != "NASA_KEY environment variable not set" { | ||
t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err) | ||
} | ||
|
||
os.Setenv("NASA_KEY", "DEMO_KEY") | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`)) | ||
})) | ||
defer server.Close() | ||
|
||
err = run(downloader) | ||
if err != nil { | ||
t.Errorf("expected no error, got %v", err) | ||
} | ||
} |