Skip to content

Commit

Permalink
Make fixture structs and parser public (#991)
Browse files Browse the repository at this point in the history
* make fixture structs and parser public

* fix linter issues
  • Loading branch information
etsai-stripe authored Oct 20, 2022
1 parent 5ed23d4 commit 4e59028
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 147 deletions.
72 changes: 38 additions & 34 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,30 @@ import (
// SupportedVersions is the version number of the fixture template the CLI supports
const SupportedVersions = 0

type metaFixture struct {
// MetaFixture contains fixture metadata
type MetaFixture struct {
Version int `json:"template_version"`
ExcludeMetadata bool `json:"exclude_metadata"`
}

type fixtureFile struct {
Meta metaFixture `json:"_meta"`
Fixtures []fixture `json:"fixtures"`
// FixtureData contains the whole fixture file
type FixtureData struct {
Meta MetaFixture `json:"_meta"`
Requests []FixtureRequest `json:"fixtures"`
Env map[string]string `json:"env"`
}

type fixture struct {
// FixtureRequest is the individual request payload
type FixtureRequest struct {
Name string `json:"name"`
ExpectedErrorType string `json:"expected_error_type"`
Path string `json:"path"`
Method string `json:"method"`
Params map[string]interface{} `json:"params"`
}

type fixtureQuery struct {
// FixtureQuery describes the query in fixture request
type FixtureQuery struct {
Match string // The substring that matched the query pattern regex
Name string
Query string
Expand All @@ -59,8 +63,8 @@ type Fixture struct {
Additions map[string]interface{}
Removals map[string]interface{}
BaseURL string
responses map[string]gjson.Result
fixture fixtureFile
Responses map[string]gjson.Result
FixtureData FixtureData
}

// NewFixtureFromFile creates a to later run steps for populating test data
Expand All @@ -71,7 +75,7 @@ func NewFixtureFromFile(fs afero.Fs, apiKey, stripeAccount, baseURL, file string
StripeAccount: stripeAccount,
Skip: skip,
BaseURL: baseURL,
responses: make(map[string]gjson.Result),
Responses: make(map[string]gjson.Result),
}

var filedata []byte
Expand All @@ -94,7 +98,7 @@ func NewFixtureFromFile(fs afero.Fs, apiKey, stripeAccount, baseURL, file string
}
}

err = json.Unmarshal(filedata, &fxt.fixture)
err = json.Unmarshal(filedata, &fxt.FixtureData)
if err != nil {
return nil, err
}
Expand All @@ -110,8 +114,8 @@ func NewFixtureFromFile(fs afero.Fs, apiKey, stripeAccount, baseURL, file string
return nil, err
}

if fxt.fixture.Meta.Version > SupportedVersions {
return nil, fmt.Errorf("Fixture version not supported: %s", fmt.Sprint(fxt.fixture.Meta.Version))
if fxt.FixtureData.Meta.Version > SupportedVersions {
return nil, fmt.Errorf("Fixture version not supported: %s", fmt.Sprint(fxt.FixtureData.Meta.Version))
}

return &fxt, nil
Expand All @@ -125,24 +129,24 @@ func NewFixtureFromRawString(fs afero.Fs, apiKey, stripeAccount, baseURL, raw st
StripeAccount: stripeAccount,
Skip: []string{},
BaseURL: baseURL,
responses: make(map[string]gjson.Result),
Responses: make(map[string]gjson.Result),
}

err := json.Unmarshal([]byte(raw), &fxt.fixture)
err := json.Unmarshal([]byte(raw), &fxt.FixtureData)
if err != nil {
return nil, err
}

if fxt.fixture.Meta.Version > SupportedVersions {
return nil, fmt.Errorf("Fixture version not supported: %s", fmt.Sprint(fxt.fixture.Meta.Version))
if fxt.FixtureData.Meta.Version > SupportedVersions {
return nil, fmt.Errorf("Fixture version not supported: %s", fmt.Sprint(fxt.FixtureData.Meta.Version))
}

return &fxt, nil
}

// GetFixtureFileContent returns the file content of the given fixture file name
func (fxt *Fixture) GetFixtureFileContent() string {
data, err := json.MarshalIndent(fxt.fixture, "", " ")
data, err := json.MarshalIndent(fxt.FixtureData, "", " ")
if err != nil {
return ""
}
Expand Down Expand Up @@ -171,7 +175,7 @@ func (e fixtureRewriteError) Error() string {
var nameError missingFixtureNameError
if errors.As(e.err, &nameError) {
fixtureNames := []string{}
for _, fixture := range e.fixture.fixture.Fixtures {
for _, fixture := range e.fixture.FixtureData.Requests {
fixtureNames = append(fixtureNames, fixture.Name)
}

Expand All @@ -198,7 +202,7 @@ func (fxt *Fixture) Override(overrides []string) error {
if err != nil {
return fixtureRewriteError{operation: "override", err: err, fixture: fxt}
}
for _, f := range fxt.fixture.Fixtures {
for _, f := range fxt.FixtureData.Requests {
if _, ok := data[f.Name]; ok {
if err := mergo.Merge(&f.Params, data[f.Name], mergo.WithOverride); err != nil {
fmt.Println(err)
Expand All @@ -214,17 +218,17 @@ func (fxt *Fixture) Override(overrides []string) error {
// over. For that, `Override` should be used
func (fxt *Fixture) Add(additions []string) error {
// If the params is empty, initialize it before merging with added data
for i, data := range fxt.fixture.Fixtures {
for i, data := range fxt.FixtureData.Requests {
if data.Method == "post" && data.Params == nil {
fxt.fixture.Fixtures[i].Params = make(map[string]interface{})
fxt.FixtureData.Requests[i].Params = make(map[string]interface{})
}
}

data, err := buildRewrites(additions, false)
if err != nil {
return fixtureRewriteError{operation: "add", err: err, fixture: fxt}
}
for _, f := range fxt.fixture.Fixtures {
for _, f := range fxt.FixtureData.Requests {
if _, ok := data[f.Name]; ok {
if err := mergo.Merge(&f.Params, data[f.Name]); err != nil {
fmt.Println(err)
Expand All @@ -240,7 +244,7 @@ func (fxt *Fixture) Remove(removals []string) error {
if err != nil {
return fixtureRewriteError{operation: "remove", err: err, fixture: fxt}
}
for _, f := range fxt.fixture.Fixtures {
for _, f := range fxt.FixtureData.Requests {
if _, ok := data[f.Name]; ok {
for remove := range data[f.Name].(map[string]interface{}) {
delete(f.Params, remove)
Expand All @@ -253,8 +257,8 @@ func (fxt *Fixture) Remove(removals []string) error {
// Execute takes the parsed fixture file and runs through all the requests
// defined to populate the user's account
func (fxt *Fixture) Execute(ctx context.Context, apiVersion string) ([]string, error) {
requestNames := make([]string, len(fxt.fixture.Fixtures))
for i, data := range fxt.fixture.Fixtures {
requestNames := make([]string, len(fxt.FixtureData.Requests))
for i, data := range fxt.FixtureData.Requests {
if isNameIn(data.Name, fxt.Skip) {
fmt.Printf("Skipping fixture for: %s\n", data.Name)
continue
Expand All @@ -269,7 +273,7 @@ func (fxt *Fixture) Execute(ctx context.Context, apiVersion string) ([]string, e
return nil, err
}

fxt.responses[data.Name] = gjson.ParseBytes(resp)
fxt.Responses[data.Name] = gjson.ParseBytes(resp)
}
return requestNames, nil
}
Expand All @@ -284,17 +288,17 @@ func errWasExpected(err error, expectedErrorType string) bool {
// UpdateEnv uses the results of the fixtures command just executed and
// updates a local .env with the resulting data
func (fxt *Fixture) UpdateEnv() error {
if len(fxt.fixture.Env) > 0 {
return fxt.updateEnv(fxt.fixture.Env)
if len(fxt.FixtureData.Env) > 0 {
return fxt.updateEnv(fxt.FixtureData.Env)
}

return nil
}

func (fxt *Fixture) makeRequest(ctx context.Context, data fixture, apiVersion string) ([]byte, error) {
func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVersion string) ([]byte, error) {
var rp requests.RequestParameters

if data.Method == "post" && !fxt.fixture.Meta.ExcludeMetadata {
if data.Method == "post" && !fxt.FixtureData.Meta.ExcludeMetadata {
now := time.Now().String()
metadata := fmt.Sprintf("metadata[_created_by_fixture]=%s", now)
rp.AppendData([]string{metadata})
Expand All @@ -307,7 +311,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data fixture, apiVersion st
Parameters: rp,
}

path, err := fxt.parsePath(data)
path, err := fxt.ParsePath(data)

if err != nil {
return make([]byte, 0), err
Expand All @@ -324,7 +328,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data fixture, apiVersion st

func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*requests.RequestParameters, error) {
requestParams := requests.RequestParameters{}
parsed, err := fxt.parseInterface(params)
parsed, err := fxt.ParseInterface(params)
if err != nil {
return &requestParams, err
}
Expand All @@ -339,7 +343,7 @@ func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*reques
return &requestParams, nil
}

func getEnvVar(query fixtureQuery) (string, error) {
func getEnvVar(query FixtureQuery) (string, error) {
key := query.Query
// Check if env variable is present
envValue := os.Getenv(key)
Expand Down Expand Up @@ -388,7 +392,7 @@ func (fxt *Fixture) updateEnv(env map[string]string) error {
}

for key, value := range env {
parsed, err := fxt.parseQuery(value)
parsed, err := fxt.ParseQuery(value)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4e59028

Please sign in to comment.