From 43d0b43343adb7fbb2e532e0b0643625b388eb38 Mon Sep 17 00:00:00 2001 From: Sergei Makarov <33636772+siller174@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:39:36 +0100 Subject: [PATCH] [issues-65] fix bug and small refactoring (#67) * [issues-65] fix bug and small refactoring * [issues-65] add tests * [issues-65] add tests * [issues-65] fix linter --- .github/workflows/main.yml | 2 +- builder.go | 428 ++----------------------------------- builder_allure.go | 175 +++++++++++++++ builder_asserts.go | 32 +++ builder_middleware.go | 65 ++++++ builder_option.go | 75 +++++++ builder_table.go | 85 ++++++++ builder_table_test.go | 194 +++++++++++++++++ 8 files changed, 644 insertions(+), 412 deletions(-) create mode 100644 builder_allure.go create mode 100644 builder_middleware.go create mode 100644 builder_option.go create mode 100644 builder_table.go create mode 100644 builder_table_test.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 94febea..93b3aa9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -35,7 +35,7 @@ jobs: with: only-new-issues: true # golangci-lint command line arguments - args: --timeout=5m0s --new-from-rev=origin/master + args: --timeout=5m0s examples: name: examples diff --git a/builder.go b/builder.go index 4fd5510..e37c790 100644 --- a/builder.go +++ b/builder.go @@ -1,11 +1,8 @@ package cute import ( - "fmt" "net/http" "time" - - "github.com/ozontech/allure-go/pkg/allure" ) const defaultHTTPTimeout = 30 @@ -21,75 +18,6 @@ type HTTPTestMaker struct { jsonMarshaler JSONMarshaler } -type options struct { - httpClient *http.Client - httpTimeout time.Duration - httpRoundTripper http.RoundTripper - - jsonMarshaler JSONMarshaler - - middleware *Middleware -} - -// Option ... -type Option func(*options) - -// WithHTTPClient is a function for set custom http client -func WithHTTPClient(client *http.Client) Option { - return func(o *options) { - o.httpClient = client - } -} - -// WithJSONMarshaler is a function for set custom json marshaler -func WithJSONMarshaler(m JSONMarshaler) Option { - return func(o *options) { - o.jsonMarshaler = m - } -} - -// WithCustomHTTPTimeout is a function for set custom http client timeout -func WithCustomHTTPTimeout(t time.Duration) Option { - return func(o *options) { - o.httpTimeout = t - } -} - -// WithCustomHTTPRoundTripper is a function for set custom http round tripper -func WithCustomHTTPRoundTripper(r http.RoundTripper) Option { - return func(o *options) { - o.httpRoundTripper = r - } -} - -// WithMiddlewareAfter ... -func WithMiddlewareAfter(after ...AfterExecute) Option { - return func(o *options) { - o.middleware.After = append(o.middleware.After, after...) - } -} - -// WithMiddlewareAfterT ... -func WithMiddlewareAfterT(after ...AfterExecuteT) Option { - return func(o *options) { - o.middleware.AfterT = append(o.middleware.AfterT, after...) - } -} - -// WithMiddlewareBefore ... -func WithMiddlewareBefore(before ...BeforeExecute) Option { - return func(o *options) { - o.middleware.Before = append(o.middleware.Before, before...) - } -} - -// WithMiddlewareBeforeT ... -func WithMiddlewareBeforeT(beforeT ...BeforeExecuteT) Option { - return func(o *options) { - o.middleware.BeforeT = append(o.middleware.BeforeT, beforeT...) - } -} - // NewHTTPTestMaker is function for set options for all cute. func NewHTTPTestMaker(opts ...Option) *HTTPTestMaker { var ( @@ -159,29 +87,10 @@ func createDefaultTests(m *HTTPTestMaker) []*Test { } func createDefaultTest(m *HTTPTestMaker) *Test { - after := make([]AfterExecute, 0, len(m.middleware.After)) - after = append(after, m.middleware.After...) - - afterT := make([]AfterExecuteT, 0, len(m.middleware.AfterT)) - afterT = append(afterT, m.middleware.AfterT...) - - before := make([]BeforeExecute, 0, len(m.middleware.Before)) - before = append(before, m.middleware.Before...) - - beforeT := make([]BeforeExecuteT, 0, len(m.middleware.BeforeT)) - beforeT = append(beforeT, m.middleware.BeforeT...) - - middleware := &Middleware{ - After: after, - AfterT: afterT, - Before: before, - BeforeT: beforeT, - } - return &Test{ httpClient: m.httpClient, jsonMarshaler: m.jsonMarshaler, - Middleware: middleware, + Middleware: createMiddlewareFromTemplate(m.middleware), AllureStep: new(AllureStep), Request: &Request{ Repeat: new(RequestRepeatPolitic), @@ -190,166 +99,27 @@ func createDefaultTest(m *HTTPTestMaker) *Test { } } -func (qt *cute) Title(title string) AllureBuilder { - qt.allureInfo.title = title - - return qt -} - -func (qt *cute) Epic(epic string) AllureBuilder { - qt.allureLabels.epic = epic - - return qt -} - -func (qt *cute) Titlef(format string, args ...interface{}) AllureBuilder { - qt.allureInfo.title = fmt.Sprintf(format, args...) - - return qt -} - -func (qt *cute) Descriptionf(format string, args ...interface{}) AllureBuilder { - qt.allureInfo.description = fmt.Sprintf(format, args...) - - return qt -} - -func (qt *cute) Stage(stage string) AllureBuilder { - qt.allureInfo.stage = stage - - return qt -} - -func (qt *cute) Stagef(format string, args ...interface{}) AllureBuilder { - qt.allureInfo.stage = fmt.Sprintf(format, args...) - - return qt -} - -func (qt *cute) Layer(value string) AllureBuilder { - qt.allureLabels.layer = value - - return qt -} - -func (qt *cute) TmsLink(tmsLink string) AllureBuilder { - qt.allureLinks.tmsLink = tmsLink - - return qt -} - -func (qt *cute) TmsLinks(tmsLinks ...string) AllureBuilder { - qt.allureLinks.tmsLinks = append(qt.allureLinks.tmsLinks, tmsLinks...) - - return qt -} +func createMiddlewareFromTemplate(m *Middleware) *Middleware { + after := make([]AfterExecute, 0, len(m.After)) + after = append(after, m.After...) -func (qt *cute) SetIssue(issue string) AllureBuilder { - qt.allureLinks.issue = issue - - return qt -} - -func (qt *cute) SetTestCase(testCase string) AllureBuilder { - qt.allureLinks.testCase = testCase - - return qt -} - -func (qt *cute) Link(link *allure.Link) AllureBuilder { - qt.allureLinks.link = link - - return qt -} - -func (qt *cute) ID(value string) AllureBuilder { - qt.allureLabels.id = value - - return qt -} + afterT := make([]AfterExecuteT, 0, len(m.AfterT)) + afterT = append(afterT, m.AfterT...) -func (qt *cute) AllureID(value string) AllureBuilder { - qt.allureLabels.allureID = value + before := make([]BeforeExecute, 0, len(m.Before)) + before = append(before, m.Before...) - return qt -} - -func (qt *cute) AddSuiteLabel(value string) AllureBuilder { - qt.allureLabels.suiteLabel = value - - return qt -} - -func (qt *cute) AddSubSuite(value string) AllureBuilder { - qt.allureLabels.subSuite = value - - return qt -} - -func (qt *cute) AddParentSuite(value string) AllureBuilder { - qt.allureLabels.parentSuite = value - - return qt -} - -func (qt *cute) Story(value string) AllureBuilder { - qt.allureLabels.story = value - - return qt -} - -func (qt *cute) Tag(value string) AllureBuilder { - qt.allureLabels.tag = value - - return qt -} - -func (qt *cute) Severity(value allure.SeverityType) AllureBuilder { - qt.allureLabels.severity = value - - return qt -} - -func (qt *cute) Owner(value string) AllureBuilder { - qt.allureLabels.owner = value - - return qt -} + beforeT := make([]BeforeExecuteT, 0, len(m.BeforeT)) + beforeT = append(beforeT, m.BeforeT...) -func (qt *cute) Lead(value string) AllureBuilder { - qt.allureLabels.lead = value - - return qt -} - -func (qt *cute) Label(label *allure.Label) AllureBuilder { - qt.allureLabels.label = label - - return qt -} - -func (qt *cute) Labels(labels ...*allure.Label) AllureBuilder { - qt.allureLabels.labels = labels - - return qt -} - -func (qt *cute) Description(description string) AllureBuilder { - qt.allureInfo.description = description - - return qt -} - -func (qt *cute) Tags(tags ...string) AllureBuilder { - qt.allureLabels.tags = tags - - return qt -} - -func (qt *cute) Feature(feature string) AllureBuilder { - qt.allureLabels.feature = feature + middleware := &Middleware{ + After: after, + AfterT: afterT, + Before: before, + BeforeT: beforeT, + } - return qt + return middleware } func (qt *cute) Create() MiddlewareRequest { @@ -362,170 +132,6 @@ func (qt *cute) CreateStep(name string) MiddlewareRequest { return qt } -func (qt *cute) Parallel() AllureBuilder { - qt.parallel = true - - return qt -} - func (qt *cute) CreateRequest() RequestHTTPBuilder { return qt } - -func (qt *cute) StepName(name string) MiddlewareRequest { - qt.tests[qt.countTests].AllureStep.Name = name - - return qt -} - -func (qt *cute) BeforeExecute(fs ...BeforeExecute) MiddlewareRequest { - qt.tests[qt.countTests].Middleware.Before = append(qt.tests[qt.countTests].Middleware.Before, fs...) - - return qt -} - -func (qt *cute) BeforeExecuteT(fs ...BeforeExecuteT) MiddlewareRequest { - qt.tests[qt.countTests].Middleware.BeforeT = append(qt.tests[qt.countTests].Middleware.BeforeT, fs...) - - return qt -} - -func (qt *cute) After(fs ...AfterExecute) ExpectHTTPBuilder { - qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) - - return qt -} - -func (qt *cute) AfterT(fs ...AfterExecuteT) ExpectHTTPBuilder { - qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) - - return qt -} - -func (qt *cute) AfterExecute(fs ...AfterExecute) MiddlewareRequest { - qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) - - return qt -} - -func (qt *cute) AfterExecuteT(fs ...AfterExecuteT) MiddlewareRequest { - qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) - - return qt -} - -func (qt *cute) AfterTestExecute(fs ...AfterExecute) NextTestBuilder { - previousTest := 0 - if qt.countTests != 0 { - previousTest = qt.countTests - 1 - } - - qt.tests[previousTest].Middleware.After = append(qt.tests[previousTest].Middleware.After, fs...) - - return qt -} - -func (qt *cute) AfterTestExecuteT(fs ...AfterExecuteT) NextTestBuilder { - previousTest := 0 - if qt.countTests != 0 { - previousTest = qt.countTests - 1 - } - - qt.tests[previousTest].Middleware.AfterT = append(qt.tests[previousTest].Middleware.AfterT, fs...) - - return qt -} - -func (qt *cute) ExpectExecuteTimeout(t time.Duration) ExpectHTTPBuilder { - qt.tests[qt.countTests].Expect.ExecuteTime = t - - return qt -} - -func (qt *cute) ExpectStatus(code int) ExpectHTTPBuilder { - qt.tests[qt.countTests].Expect.Code = code - - return qt -} - -func (qt *cute) ExpectJSONSchemaString(schema string) ExpectHTTPBuilder { - qt.tests[qt.countTests].Expect.JSONSchema.String = schema - - return qt -} - -func (qt *cute) ExpectJSONSchemaByte(schema []byte) ExpectHTTPBuilder { - qt.tests[qt.countTests].Expect.JSONSchema.Byte = schema - - return qt -} - -func (qt *cute) ExpectJSONSchemaFile(filePath string) ExpectHTTPBuilder { - qt.tests[qt.countTests].Expect.JSONSchema.File = filePath - - return qt -} - -func (qt *cute) CreateTableTest() MiddlewareTable { - qt.isTableTest = true - - return qt -} - -func (qt *cute) PutNewTest(name string, r *http.Request, expect *Expect) TableTest { - // Validate, that first step is empty - if qt.countTests == 0 { - if qt.tests[0].Request.Base == nil && - len(qt.tests[0].Request.Builders) == 0 { - qt.tests[0].Expect = expect - qt.tests[0].Name = name - qt.tests[0].Request.Base = r - - return qt - } - } - - newTest := createDefaultTest(qt.baseProps) - newTest.Expect = expect - newTest.Name = name - newTest.Request.Base = r - qt.tests = append(qt.tests, newTest) - qt.countTests++ // async? - - return qt -} - -func (qt *cute) PutTests(params ...*Test) TableTest { - for _, param := range params { - if qt.baseProps != nil && qt.baseProps.httpClient != nil { - param.httpClient = qt.baseProps.httpClient - } - - if qt.baseProps != nil && qt.baseProps.jsonMarshaler != nil { - param.jsonMarshaler = qt.baseProps.jsonMarshaler - } - - // Validate, that first step is empty - if qt.countTests == 0 { - if qt.tests[0].Request.Base == nil && - len(qt.tests[0].Request.Builders) == 0 { - qt.tests[0] = param - - continue - } - } - - qt.tests = append(qt.tests, param) - qt.countTests++ - } - - return qt -} - -func (qt *cute) NextTest() NextTestBuilder { - qt.countTests++ // async? - - qt.tests = append(qt.tests, createDefaultTest(qt.baseProps)) - - return qt -} diff --git a/builder_allure.go b/builder_allure.go new file mode 100644 index 0000000..9aef5d3 --- /dev/null +++ b/builder_allure.go @@ -0,0 +1,175 @@ +package cute + +import ( + "fmt" + + "github.com/ozontech/allure-go/pkg/allure" +) + +func (qt *cute) Parallel() AllureBuilder { + qt.parallel = true + + return qt +} + +func (qt *cute) Title(title string) AllureBuilder { + qt.allureInfo.title = title + + return qt +} + +func (qt *cute) Epic(epic string) AllureBuilder { + qt.allureLabels.epic = epic + + return qt +} + +func (qt *cute) Titlef(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.title = fmt.Sprintf(format, args...) + + return qt +} + +func (qt *cute) Descriptionf(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.description = fmt.Sprintf(format, args...) + + return qt +} + +func (qt *cute) Stage(stage string) AllureBuilder { + qt.allureInfo.stage = stage + + return qt +} + +func (qt *cute) Stagef(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.stage = fmt.Sprintf(format, args...) + + return qt +} + +func (qt *cute) Layer(value string) AllureBuilder { + qt.allureLabels.layer = value + + return qt +} + +func (qt *cute) TmsLink(tmsLink string) AllureBuilder { + qt.allureLinks.tmsLink = tmsLink + + return qt +} + +func (qt *cute) TmsLinks(tmsLinks ...string) AllureBuilder { + qt.allureLinks.tmsLinks = append(qt.allureLinks.tmsLinks, tmsLinks...) + + return qt +} + +func (qt *cute) SetIssue(issue string) AllureBuilder { + qt.allureLinks.issue = issue + + return qt +} + +func (qt *cute) SetTestCase(testCase string) AllureBuilder { + qt.allureLinks.testCase = testCase + + return qt +} + +func (qt *cute) Link(link *allure.Link) AllureBuilder { + qt.allureLinks.link = link + + return qt +} + +func (qt *cute) ID(value string) AllureBuilder { + qt.allureLabels.id = value + + return qt +} + +func (qt *cute) AllureID(value string) AllureBuilder { + qt.allureLabels.allureID = value + + return qt +} + +func (qt *cute) AddSuiteLabel(value string) AllureBuilder { + qt.allureLabels.suiteLabel = value + + return qt +} + +func (qt *cute) AddSubSuite(value string) AllureBuilder { + qt.allureLabels.subSuite = value + + return qt +} + +func (qt *cute) AddParentSuite(value string) AllureBuilder { + qt.allureLabels.parentSuite = value + + return qt +} + +func (qt *cute) Story(value string) AllureBuilder { + qt.allureLabels.story = value + + return qt +} + +func (qt *cute) Tag(value string) AllureBuilder { + qt.allureLabels.tag = value + + return qt +} + +func (qt *cute) Severity(value allure.SeverityType) AllureBuilder { + qt.allureLabels.severity = value + + return qt +} + +func (qt *cute) Owner(value string) AllureBuilder { + qt.allureLabels.owner = value + + return qt +} + +func (qt *cute) Lead(value string) AllureBuilder { + qt.allureLabels.lead = value + + return qt +} + +func (qt *cute) Label(label *allure.Label) AllureBuilder { + qt.allureLabels.label = label + + return qt +} + +func (qt *cute) Labels(labels ...*allure.Label) AllureBuilder { + qt.allureLabels.labels = labels + + return qt +} + +func (qt *cute) Description(description string) AllureBuilder { + qt.allureInfo.description = description + + return qt +} + +func (qt *cute) Tags(tags ...string) AllureBuilder { + qt.allureLabels.tags = tags + + return qt +} + +func (qt *cute) Feature(feature string) AllureBuilder { + qt.allureLabels.feature = feature + + return qt +} diff --git a/builder_asserts.go b/builder_asserts.go index 31c55d4..a69e26c 100644 --- a/builder_asserts.go +++ b/builder_asserts.go @@ -1,5 +1,7 @@ package cute +import "time" + func (qt *cute) AssertBody(asserts ...AssertBody) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { @@ -287,3 +289,33 @@ func (qt *cute) RequireResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { return qt } + +func (qt *cute) ExpectExecuteTimeout(t time.Duration) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.ExecuteTime = t + + return qt +} + +func (qt *cute) ExpectStatus(code int) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.Code = code + + return qt +} + +func (qt *cute) ExpectJSONSchemaString(schema string) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.String = schema + + return qt +} + +func (qt *cute) ExpectJSONSchemaByte(schema []byte) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.Byte = schema + + return qt +} + +func (qt *cute) ExpectJSONSchemaFile(filePath string) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.File = filePath + + return qt +} diff --git a/builder_middleware.go b/builder_middleware.go new file mode 100644 index 0000000..a7506ba --- /dev/null +++ b/builder_middleware.go @@ -0,0 +1,65 @@ +package cute + +func (qt *cute) StepName(name string) MiddlewareRequest { + qt.tests[qt.countTests].AllureStep.Name = name + + return qt +} + +func (qt *cute) BeforeExecute(fs ...BeforeExecute) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.Before = append(qt.tests[qt.countTests].Middleware.Before, fs...) + + return qt +} + +func (qt *cute) BeforeExecuteT(fs ...BeforeExecuteT) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.BeforeT = append(qt.tests[qt.countTests].Middleware.BeforeT, fs...) + + return qt +} + +func (qt *cute) After(fs ...AfterExecute) ExpectHTTPBuilder { + qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) + + return qt +} + +func (qt *cute) AfterT(fs ...AfterExecuteT) ExpectHTTPBuilder { + qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) + + return qt +} + +func (qt *cute) AfterExecute(fs ...AfterExecute) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) + + return qt +} + +func (qt *cute) AfterExecuteT(fs ...AfterExecuteT) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) + + return qt +} + +func (qt *cute) AfterTestExecute(fs ...AfterExecute) NextTestBuilder { + previousTest := 0 + if qt.countTests != 0 { + previousTest = qt.countTests - 1 + } + + qt.tests[previousTest].Middleware.After = append(qt.tests[previousTest].Middleware.After, fs...) + + return qt +} + +func (qt *cute) AfterTestExecuteT(fs ...AfterExecuteT) NextTestBuilder { + previousTest := 0 + if qt.countTests != 0 { + previousTest = qt.countTests - 1 + } + + qt.tests[previousTest].Middleware.AfterT = append(qt.tests[previousTest].Middleware.AfterT, fs...) + + return qt +} diff --git a/builder_option.go b/builder_option.go new file mode 100644 index 0000000..dbee66d --- /dev/null +++ b/builder_option.go @@ -0,0 +1,75 @@ +package cute + +import ( + "net/http" + "time" +) + +type options struct { + httpClient *http.Client + httpTimeout time.Duration + httpRoundTripper http.RoundTripper + + jsonMarshaler JSONMarshaler + + middleware *Middleware +} + +// Option ... +type Option func(*options) + +// WithHTTPClient is a function for set custom http client +func WithHTTPClient(client *http.Client) Option { + return func(o *options) { + o.httpClient = client + } +} + +// WithJSONMarshaler is a function for set custom json marshaler +func WithJSONMarshaler(m JSONMarshaler) Option { + return func(o *options) { + o.jsonMarshaler = m + } +} + +// WithCustomHTTPTimeout is a function for set custom http client timeout +func WithCustomHTTPTimeout(t time.Duration) Option { + return func(o *options) { + o.httpTimeout = t + } +} + +// WithCustomHTTPRoundTripper is a function for set custom http round tripper +func WithCustomHTTPRoundTripper(r http.RoundTripper) Option { + return func(o *options) { + o.httpRoundTripper = r + } +} + +// WithMiddlewareAfter ... +func WithMiddlewareAfter(after ...AfterExecute) Option { + return func(o *options) { + o.middleware.After = append(o.middleware.After, after...) + } +} + +// WithMiddlewareAfterT ... +func WithMiddlewareAfterT(after ...AfterExecuteT) Option { + return func(o *options) { + o.middleware.AfterT = append(o.middleware.AfterT, after...) + } +} + +// WithMiddlewareBefore ... +func WithMiddlewareBefore(before ...BeforeExecute) Option { + return func(o *options) { + o.middleware.Before = append(o.middleware.Before, before...) + } +} + +// WithMiddlewareBeforeT ... +func WithMiddlewareBeforeT(beforeT ...BeforeExecuteT) Option { + return func(o *options) { + o.middleware.BeforeT = append(o.middleware.BeforeT, beforeT...) + } +} diff --git a/builder_table.go b/builder_table.go new file mode 100644 index 0000000..2c821f7 --- /dev/null +++ b/builder_table.go @@ -0,0 +1,85 @@ +package cute + +import "net/http" + +func (qt *cute) CreateTableTest() MiddlewareTable { + qt.isTableTest = true + + return qt +} + +func (qt *cute) PutNewTest(name string, r *http.Request, expect *Expect) TableTest { + // Validate, that first step is empty + if qt.countTests == 0 { + if qt.tests[0].Request.Base == nil && + len(qt.tests[0].Request.Builders) == 0 { + qt.tests[0].Expect = expect + qt.tests[0].Name = name + qt.tests[0].Request.Base = r + + return qt + } + } + + newTest := createDefaultTest(qt.baseProps) + newTest.Expect = expect + newTest.Name = name + newTest.Request.Base = r + qt.tests = append(qt.tests, newTest) + qt.countTests++ // async? + + return qt +} + +func (qt *cute) PutTests(tests ...*Test) TableTest { + for _, test := range tests { + // Fill common fields + qt.fillBaseProps(test) + + // Validate, that first step is empty + if qt.countTests == 0 { + if qt.tests[0].Request.Base == nil && + len(qt.tests[0].Request.Builders) == 0 { + qt.tests[0] = test + + continue + } + } + + qt.tests = append(qt.tests, test) + qt.countTests++ + } + + return qt +} + +func (qt *cute) fillBaseProps(t *Test) { + if qt.baseProps == nil { + return + } + + if qt.baseProps.httpClient != nil { + t.httpClient = qt.baseProps.httpClient + } + + if qt.baseProps.jsonMarshaler != nil { + t.jsonMarshaler = qt.baseProps.jsonMarshaler + } + + if t.Middleware == nil { + t.Middleware = createMiddlewareFromTemplate(qt.baseProps.middleware) + } else { + t.Middleware.After = append(t.Middleware.After, qt.baseProps.middleware.After...) + t.Middleware.AfterT = append(t.Middleware.AfterT, qt.baseProps.middleware.AfterT...) + t.Middleware.Before = append(t.Middleware.Before, qt.baseProps.middleware.Before...) + t.Middleware.BeforeT = append(t.Middleware.BeforeT, qt.baseProps.middleware.BeforeT...) + } +} + +func (qt *cute) NextTest() NextTestBuilder { + qt.countTests++ // async? + + qt.tests = append(qt.tests, createDefaultTest(qt.baseProps)) + + return qt +} diff --git a/builder_table_test.go b/builder_table_test.go new file mode 100644 index 0000000..0bdbd34 --- /dev/null +++ b/builder_table_test.go @@ -0,0 +1,194 @@ +package cute + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFillBaseProps_WhenBasePropsIsNil(t *testing.T) { + testObj := &Test{} + cuteObj := &cute{} + + cuteObj.fillBaseProps(testObj) + + require.Nil(t, testObj.httpClient) + require.Nil(t, testObj.jsonMarshaler) + require.Nil(t, testObj.Middleware) +} + +func TestFillBaseProps_WhenBasePropsIsNotNil(t *testing.T) { + testObj := &Test{} + cuteObj := &cute{} + + qtBaseProps := &HTTPTestMaker{ + httpClient: &http.Client{}, + jsonMarshaler: &jsonMarshaler{}, + middleware: &Middleware{ + After: []AfterExecute{ + func(*http.Response, []error) error { + return nil + }, + func(*http.Response, []error) error { + return nil + }, + }, + AfterT: []AfterExecuteT{func(T, *http.Response, []error) error { return nil }}, + Before: []BeforeExecute{ + func(*http.Request) error { + return nil + }, + func(*http.Request) error { + return nil + }, + }, + BeforeT: []BeforeExecuteT{ + func(T, *http.Request) error { return nil }, + func(T, *http.Request) error { + return nil + }, + }, + }, + } + cuteObj.baseProps = qtBaseProps + + cuteObj.fillBaseProps(testObj) + + require.Equal(t, qtBaseProps.httpClient, testObj.httpClient) + require.Equal(t, qtBaseProps.jsonMarshaler, testObj.jsonMarshaler) + require.Len(t, testObj.Middleware.After, len(qtBaseProps.middleware.After)) + require.Len(t, testObj.Middleware.AfterT, len(qtBaseProps.middleware.AfterT)) + require.Len(t, testObj.Middleware.Before, len(qtBaseProps.middleware.Before)) + require.Len(t, testObj.Middleware.BeforeT, len(qtBaseProps.middleware.BeforeT)) +} + +func TestFillBaseProps_WhenBasePropsIsNotNil_After(t *testing.T) { + testObj := &Test{ + Middleware: &Middleware{ + After: []AfterExecute{ + func(*http.Response, []error) error { + return nil + }, + }, + }, + } + cuteObj := &cute{} + + qtBaseProps := &HTTPTestMaker{ + httpClient: &http.Client{}, + jsonMarshaler: &jsonMarshaler{}, + middleware: &Middleware{ + After: []AfterExecute{ + func(*http.Response, []error) error { + return nil + }, + func(*http.Response, []error) error { + return nil + }, + }, + BeforeT: []BeforeExecuteT{ + func(T, *http.Request) error { return nil }, + func(T, *http.Request) error { + return nil + }, + }, + }, + } + cuteObj.baseProps = qtBaseProps + + cuteObj.fillBaseProps(testObj) + + require.Equal(t, qtBaseProps.httpClient, testObj.httpClient) + require.Equal(t, qtBaseProps.jsonMarshaler, testObj.jsonMarshaler) + require.Len(t, testObj.Middleware.After, len(qtBaseProps.middleware.After)+1) + require.Len(t, testObj.Middleware.AfterT, len(qtBaseProps.middleware.AfterT)) + require.Len(t, testObj.Middleware.Before, len(qtBaseProps.middleware.Before)) + require.Len(t, testObj.Middleware.BeforeT, len(qtBaseProps.middleware.BeforeT)) +} + +func TestFillBaseProps_WhenBasePropsIsNotNil_Middleware(t *testing.T) { + testObj := &Test{ + Middleware: &Middleware{ + After: []AfterExecute{ + func(*http.Response, []error) error { + return nil + }, + func(*http.Response, []error) error { + return nil + }, + }, + AfterT: []AfterExecuteT{ + func(T, *http.Response, []error) error { + return nil + }, + func(T, *http.Response, []error) error { + return nil + }, + func(T, *http.Response, []error) error { + return nil + }, + }, + Before: []BeforeExecute{ + func(*http.Request) error { + return nil + }, + func(*http.Request) error { + return nil + }, + func(*http.Request) error { + return nil + }, + func(*http.Request) error { + return nil + }, + }, + BeforeT: []BeforeExecuteT{ + func(T, *http.Request) error { + return nil + }, + }, + }, + } + cuteObj := &cute{} + + qtBaseProps := &HTTPTestMaker{ + httpClient: &http.Client{}, + jsonMarshaler: &jsonMarshaler{}, + middleware: &Middleware{ + After: []AfterExecute{ + func(*http.Response, []error) error { + return nil + }, + func(*http.Response, []error) error { + return nil + }, + }, + AfterT: []AfterExecuteT{func(T, *http.Response, []error) error { return nil }}, + Before: []BeforeExecute{ + func(*http.Request) error { + return nil + }, + func(*http.Request) error { + return nil + }, + }, + BeforeT: []BeforeExecuteT{ + func(T, *http.Request) error { return nil }, + func(T, *http.Request) error { + return nil + }, + }, + }, + } + cuteObj.baseProps = qtBaseProps + + cuteObj.fillBaseProps(testObj) + + require.Equal(t, qtBaseProps.httpClient, testObj.httpClient) + require.Equal(t, qtBaseProps.jsonMarshaler, testObj.jsonMarshaler) + require.Len(t, testObj.Middleware.After, len(qtBaseProps.middleware.After)+2) + require.Len(t, testObj.Middleware.AfterT, len(qtBaseProps.middleware.AfterT)+3) + require.Len(t, testObj.Middleware.Before, len(qtBaseProps.middleware.Before)+4) + require.Len(t, testObj.Middleware.BeforeT, len(qtBaseProps.middleware.BeforeT)+1) +}