Skip to content

Commit

Permalink
refactor: move api test config to api file
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Apr 7, 2024
1 parent 980d766 commit d90d4d6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func doJSONRequest[T any](do func(r *http.Request) (*http.Response, error), req
if err != nil {
return result, err
}
defer closeAndIgnoreErrors(res.Body)
defer closeAndIgnoreError(res.Body)
switch res.StatusCode {
case http.StatusOK, http.StatusCreated:
default:
Expand All @@ -106,7 +106,7 @@ func doJSONRequest[T any](do func(r *http.Request) (*http.Response, error), req
return result, nil
}

func closeAndIgnoreErrors(closer io.Closer) {
func closeAndIgnoreError(closer io.Closer) {
_ = closer.Close()
}

Expand Down
40 changes: 40 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,54 @@ package portfolio_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/portfoliotree/portfolio"
"github.com/portfoliotree/portfolio/portfoliotest"
)

func TestMain(m *testing.M) {
server := httptest.NewServer(testdataAssetReturns(portfoliotest.ComponentReturnsProvider()))
http.DefaultClient = server.Client()
_ = os.Setenv(portfolio.ServerURLEnvironmentVariableName, server.URL)
os.Exit(func() int {
defer server.Close()
return m.Run()
}())
}

func testdataAssetReturns(crp portfolio.ComponentReturnsProvider) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
assets, err := portfolio.ParseComponentsFromURL(req.URL.Query(), "asset")
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
table, err := crp.ComponentReturnsTable(req.Context(), assets...)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
writeJSONResponse(res, table)
}
}

func writeJSONResponse(res http.ResponseWriter, data any) {
buf, err := json.Marshal(data)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
_, _ = res.Write(buf)
}

func Test_APIEndpoints(t *testing.T) {
if value, found := os.LookupEnv("CI"); !found || value != "true" {
t.Skip("Skipping test in CI environment")
Expand Down
4 changes: 2 additions & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ParseSpecificationFile(specificationFilePath string) ([]Document, error) {
if err != nil {
return nil, err
}
defer closeAndIgnoreErrors(f)
defer closeAndIgnoreError(f)
return portfoliosFromFile(specificationFilePath, f)
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func WalkDirectoryAndParseSpecificationFiles(dir fs.FS) ([]Document, error) {
if err != nil {
return err
}
defer closeAndIgnoreErrors(f)
defer closeAndIgnoreError(f)
specs, err := portfoliosFromFile(filePath, f)
if err != nil {
return err
Expand Down
41 changes: 0 additions & 41 deletions portfolio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package portfolio_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
Expand All @@ -15,46 +11,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/portfoliotree/portfolio"
"github.com/portfoliotree/portfolio/portfoliotest"
"github.com/portfoliotree/portfolio/returns"
)

func TestMain(m *testing.M) {
server := httptest.NewServer(testdataAssetReturns(portfoliotest.ComponentReturnsProvider()))
http.DefaultClient = server.Client()
_ = os.Setenv(portfolio.ServerURLEnvironmentVariableName, server.URL)
os.Exit(func() int {
defer server.Close()
return m.Run()
}())
}

func testdataAssetReturns(crp portfolio.ComponentReturnsProvider) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
assets, err := portfolio.ParseComponentsFromURL(req.URL.Query(), "asset")
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
table, err := crp.ComponentReturnsTable(req.Context(), assets...)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
writeJSONResponse(res, table)
}
}

func writeJSONResponse(res http.ResponseWriter, data any) {
buf, err := json.Marshal(data)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
_, _ = res.Write(buf)
}

func ExampleParse() {
// language=yaml
specYAML := `
Expand Down

0 comments on commit d90d4d6

Please sign in to comment.