-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
105 lines (83 loc) · 2.84 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"io"
"os"
"testing"
"time"
)
func TestParseArgumentsForDateRange(t *testing.T) {
t.Run("Start and end flags provided", func(t *testing.T) {
// Both start and end flags provided, so the function should return the start and end dates
os.Args = []string{"cmd", "-start", "2023-02-01", "-end", "2023-02-28"}
start, end, _ := parseArgumentsForDateRange()
if start != "2023-02-01" {
t.Errorf("Expected start date to be '2023-02-01', but got %s", start)
}
if end != "2023-02-28" {
t.Errorf("Expected end date to be '2023-02-28', but got %s", end)
}
})
}
func TestPrintPrettyFormattedAPOD(t *testing.T) {
apod := NasaAPOD{Date: "2022-02-12", Title: "Test Title", URL: "https://testurl.com"}
// Redirect stdout to buffer to capture output
old := os.Stdout
defer func() { os.Stdout = old }()
r, w, _ := os.Pipe()
os.Stdout = w
printPrettyFormattedAPOD(apod)
// Read from buffer to check output
w.Close()
out, _ := io.ReadAll(r)
expected := "Test Title\nFebruary 12, 2022\nhttps://testurl.com\n\n"
if string(out) != expected {
t.Errorf("Unexpected output. Expected: %q, got: %q", expected, string(out))
}
}
func TestPrintPrettyFormattedAPODError(t *testing.T) {
apod := NasaAPOD{Date: "invalid date", Title: "Test Title", URL: "https://testurl.com"}
printPrettyFormattedAPOD(apod)
}
func TestConstructURL(t *testing.T) {
apiKey := "test-api-key"
start := "2023-02-06"
end := "2023-02-13"
expectedURL := fmt.Sprintf(
"%s?api_key=%s&start_date=%s&end_date=%s",
apiURL, apiKey, start, end)
resultURL := constructURL(apiKey, start, end)
if resultURL != expectedURL {
t.Errorf("URL does not match expected value. Got %s, expected %s", resultURL, expectedURL)
}
// Test case for empty start and end dates
expectedURL = fmt.Sprintf(
"%s?api_key=%s&start_date=%s&end_date=%s",
apiURL, apiKey,
time.Now().AddDate(0, 0, -7).Format("2006-01-02"),
time.Now().Format("2006-01-02"))
resultURL = constructURL(apiKey, "", "")
if resultURL != expectedURL {
t.Errorf("URL does not match expected value. Got %s, expected %s", resultURL, expectedURL)
}
}
func TestDownloadImageSuccess(t *testing.T) {
testURL := "https://apod.nasa.gov/apod/image/2102/IC410_SHO_1024.jpg"
testFilename := "success_test"
downloadImage(testURL, testFilename)
if _, err := os.Stat("./images/" + testFilename + ".jpg"); os.IsNotExist(err) {
t.Errorf("File does not exist, download failed")
}
// Cleanup
os.Remove("./images/" + testFilename + ".jpg")
}
func TestDownloadImageFail(t *testing.T) {
testURL := "https://nonexistenturl.com/invalid.jpg"
testFilename := "fail_test"
downloadImage(testURL, testFilename)
if _, err := os.Stat("./images/" + testFilename + ".jpg"); !os.IsNotExist(err) {
t.Errorf("File exists but should not")
}
// Cleanup in case the file was created
os.Remove("./images/" + testFilename + ".jpg")
}