Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Apr 3, 2024
1 parent 873e19a commit a5612be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
3 changes: 2 additions & 1 deletion internal/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func GetResult(d, url, acceptContentType string) []byte {

r, err := getResult(url, acceptContentType)
if err != nil {
log.Fatal(err)
log.Printf("Failed to get %s: %v", url, err)
return nil
}
WriteCachedFile(d, string(r))

Expand Down
10 changes: 7 additions & 3 deletions pkg/doi/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ func GetDoi(d, url string) (Article, error) {
dirPath := filepath.Join("dois", d)
dirPath, err = utils.MkTmpDir(dirPath)
if err != nil {
return Article{}, fmt.Errorf("Unable to create cached file directory: %v", err)
return Article{}, fmt.Errorf("unable to create cached file directory: %v", err)
}

dir := filepath.Join(dirPath, "doi.json")
u := fmt.Sprintf("%/%", url, d)
u := fmt.Sprintf("%s/%s", url, d)
result := utils.GetResult(dir, u, "application/json")
if result == nil {
return Article{}, fmt.Errorf("could not find DOI %s", d)
}

err = json.Unmarshal(result, &a)
if err != nil {
return Article{}, fmt.Errorf("Could not unmarshal JSON for %s: %v", d, err)
return Article{}, fmt.Errorf("could not unmarshal JSON for %s: %v", d, err)
}
return a, nil
}
Expand Down
28 changes: 11 additions & 17 deletions pkg/doi/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,7 @@ func TestGetObject(t *testing.T) {

// Mock server to simulate HTTP responses
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/test1" {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("Test response 1"))
if err != nil {
log.Fatal(err)
}
} else if r.URL.Path == "/test2" {
if r.URL.Path == "/doi2" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

Expand All @@ -119,26 +112,27 @@ func TestGetObject(t *testing.T) {

tests := []struct {
url string
doi string
acceptContentType string
want []byte
wantErr bool
}{
// Test case for plain text response
{url: ts.URL + "/test1", acceptContentType: "text/plain", want: []byte("Test response 1"), wantErr: false},
// Test case for JSON response
{url: ts.URL + "/test2", acceptContentType: "application/json", want: articleJSON, wantErr: false},
{url: ts.URL, doi: "doi2", acceptContentType: "application/json", want: articleJSON, wantErr: false},
// Test case for non-existent URL
{url: ts.URL + "/notfound", acceptContentType: "text/plain", want: nil, wantErr: true},
{url: ts.URL, doi: "notfound", acceptContentType: "text/plain", want: nil, wantErr: true},
}

for _, test := range tests {
got, err := GetObject(test.url, test.acceptContentType)
got, err := GetDoi(test.doi, test.url)
if (err != nil) != test.wantErr {
t.Errorf("GetObject(%s, %s) error = %v, wantErr %v", test.url, test.acceptContentType, err, test.wantErr)
t.Errorf("GetDoi(%s, %s) error = %v, wantErr %v", test.url, test.acceptContentType, err, test.wantErr)
continue
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("GetObject(%s, %s) = %v, want %v", test.url, test.acceptContentType, got, test.want)
if err == nil {
gotByte, err := json.Marshal(got)
if err != nil || !reflect.DeepEqual(gotByte, test.want) {
t.Errorf("GetDoi(%s, %s) = %v, want %v", test.url, test.acceptContentType, got, test.want)
}
}
}
}

0 comments on commit a5612be

Please sign in to comment.