Skip to content

Commit

Permalink
Fix issues and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Aug 28, 2022
1 parent 54cb229 commit 8581238
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 51 deletions.
22 changes: 11 additions & 11 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewHTTPTestMaker(opts ...Option) *HTTPTestMaker {

// NewTestBuilder is a function for initialization foundation for cute
func (m *HTTPTestMaker) NewTestBuilder() AllureBuilder {
tests := make([]*Test, 1, 1)
tests := make([]*Test, 1)
tests[0] = createDefaultTest(m.httpClient)

return &cute{
Expand All @@ -104,7 +104,7 @@ func createDefaultTest(httpClient *http.Client) *Test {
Request: &Request{
Repeat: new(RequestRepeatPolitic),
},
Expect: new(Expect),
Expect: &Expect{JSONSchema: new(ExpectJSONSchema)},
}
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func (it *cute) Create() MiddlewareRequest {
}

func (it *cute) CreateStep(name string) MiddlewareRequest {
it.tests[it.countTests].AllureStep.name = name
it.tests[it.countTests].AllureStep.Name = name

return it
}
Expand All @@ -247,19 +247,19 @@ func (it *cute) CreateRequest() RequestHTTPBuilder {
}

func (it *cute) StepName(name string) MiddlewareRequest {
it.tests[it.countTests].AllureStep.name = name
it.tests[it.countTests].AllureStep.Name = name

return it
}

func (it *cute) BeforeExecute(fs ...BeforeExecute) MiddlewareRequest {
it.tests[it.countTests].Middleware.before = append(it.tests[it.countTests].Middleware.before, fs...)
it.tests[it.countTests].Middleware.Before = append(it.tests[it.countTests].Middleware.Before, fs...)

return it
}

func (it *cute) BeforeExecuteT(fs ...BeforeExecuteT) MiddlewareRequest {
it.tests[it.countTests].Middleware.beforeT = append(it.tests[it.countTests].Middleware.beforeT, fs...)
it.tests[it.countTests].Middleware.BeforeT = append(it.tests[it.countTests].Middleware.BeforeT, fs...)

return it
}
Expand All @@ -271,7 +271,7 @@ func (it *cute) AfterExecute(fs ...AfterExecute) MiddlewareRequest {
}

func (it *cute) AfterExecuteT(fs ...AfterExecuteT) MiddlewareRequest {
it.tests[it.countTests].Middleware.afterT = append(it.tests[it.countTests].Middleware.afterT, fs...)
it.tests[it.countTests].Middleware.AfterT = append(it.tests[it.countTests].Middleware.AfterT, fs...)

return it
}
Expand All @@ -283,7 +283,7 @@ func (it *cute) AfterTestExecute(fs ...AfterExecute) NextTestBuilder {
}

func (it *cute) AfterTestExecuteT(fs ...AfterExecuteT) NextTestBuilder {
it.tests[it.countTests].Middleware.afterT = append(it.tests[it.countTests].Middleware.afterT, fs...)
it.tests[it.countTests].Middleware.AfterT = append(it.tests[it.countTests].Middleware.AfterT, fs...)

return it
}
Expand Down Expand Up @@ -325,19 +325,19 @@ func (it *cute) ExpectStatus(code int) ExpectHTTPBuilder {
}

func (it *cute) ExpectJSONSchemaString(schema string) ExpectHTTPBuilder {
it.tests[it.countTests].Expect.JSONSchemaString = schema
it.tests[it.countTests].Expect.JSONSchema.String = schema

return it
}

func (it *cute) ExpectJSONSchemaByte(schema []byte) ExpectHTTPBuilder {
it.tests[it.countTests].Expect.JSONSchemaByte = schema
it.tests[it.countTests].Expect.JSONSchema.Byte = schema

return it
}

func (it *cute) ExpectJSONSchemaFile(filePath string) ExpectHTTPBuilder {
it.tests[it.countTests].Expect.JSONSchemaFile = filePath
it.tests[it.countTests].Expect.JSONSchema.File = filePath

return it
}
Expand Down
14 changes: 8 additions & 6 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ func TestHTTPTestMaker(t *testing.T) {
require.Equal(t, desc, resHt.allureInfo.description)
require.Equal(t, feature, resHt.allureLabels.feature)
require.Equal(t, epic, resHt.allureLabels.epic)
require.Equal(t, stepName, resHt.tests[0].AllureStep.name)
require.Equal(t, stepName, resHt.tests[0].AllureStep.Name)
require.Equal(t, req, resHt.tests[0].Request.Base)
require.Equal(t, executeTime, resHt.tests[0].Expect.ExecuteTime)
require.Equal(t, status, resHt.tests[0].Expect.Code)
require.Equal(t, schemaBt, resHt.tests[0].Expect.JSONSchemaByte)
require.Equal(t, schemaStg, resHt.tests[0].Expect.JSONSchemaString)
require.Equal(t, schemaFile, resHt.tests[0].Expect.JSONSchemaFile)
require.Equal(t, schemaBt, resHt.tests[0].Expect.JSONSchema.Byte)
require.Equal(t, schemaStg, resHt.tests[0].Expect.JSONSchema.String)
require.Equal(t, schemaFile, resHt.tests[0].Expect.JSONSchema.File)
require.Equal(t, id, resHt.allureLabels.id)
require.Equal(t, addSuiteLabel, resHt.allureLabels.suiteLabel)
require.Equal(t, addSubSuite, resHt.allureLabels.subSuite)
Expand Down Expand Up @@ -199,7 +199,9 @@ func TestCreateDefaultTest(t *testing.T) {
Request: &Request{
Repeat: new(RequestRepeatPolitic),
},
Expect: new(Expect),
Expect: &Expect{
JSONSchema: new(ExpectJSONSchema),
},
}, resTest)
}

Expand All @@ -211,7 +213,7 @@ func TestCreateTableTest(t *testing.T) {
}

func TestPutNewTest(t *testing.T) {
tests := make([]*Test, 1, 1)
tests := make([]*Test, 1)
tests[0] = createDefaultTest(http.DefaultClient)

var (
Expand Down
2 changes: 1 addition & 1 deletion cute.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (it *cute) ExecuteTest(ctx context.Context, t testing.TB) []ResultsHTTPBuil
tOriginal, ok := t.(*testing.T)
if ok {
newT := createAllureT(tOriginal)
defer newT.FinishTest()
defer newT.FinishTest() //nolint

internalT = newT
}
Expand Down
10 changes: 5 additions & 5 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ type MiddlewareTable interface {
AfterTest
}

// MiddlewareRequest is function for create requests or add After/before functions
// MiddlewareRequest is function for create requests or add After/Before functions
type MiddlewareRequest interface {
RequestHTTPBuilder

BeforeTest
AfterTest
}

// BeforeTest is function for processing request before execute
// BeforeTest is function for processing request Before execute
type BeforeTest interface {
// BeforeExecute is function for processing request before createRequest Request
// BeforeExecute is function for processing request Before createRequest Request
BeforeExecute(...BeforeExecute) MiddlewareRequest
// BeforeExecuteT is function for processing request before createRequest Request
// BeforeExecuteT is function for processing request Before createRequest Request
BeforeExecuteT(...BeforeExecuteT) MiddlewareRequest
}

Expand Down Expand Up @@ -239,7 +239,7 @@ type ResultsHTTPBuilder interface {
// GetErrors is a function, which returns all errors from test
GetErrors() []error

// GetName is a function, which returns name Test
// GetName is a function, which returns Name Test
GetName() string
}

Expand Down
12 changes: 6 additions & 6 deletions jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func (it *Test) validateJSONSchema(t internalT, body []byte) []error {
)

switch {
case it.Expect.JSONSchemaString != "":
expect = gojsonschema.NewStringLoader(it.Expect.JSONSchemaString)
case it.Expect.JSONSchemaByte != nil:
expect = gojsonschema.NewBytesLoader(it.Expect.JSONSchemaByte)
case it.Expect.JSONSchemaFile != "":
expect = gojsonschema.NewReferenceLoader(it.Expect.JSONSchemaFile)
case it.Expect.JSONSchema.String != "":
expect = gojsonschema.NewStringLoader(it.Expect.JSONSchema.String)
case it.Expect.JSONSchema.Byte != nil:
expect = gojsonschema.NewBytesLoader(it.Expect.JSONSchema.Byte)
case it.Expect.JSONSchema.File != "":
expect = gojsonschema.NewReferenceLoader(it.Expect.JSONSchema.File)
default:
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestValidateJSONSchemaFromString(t *testing.T) {
}
`)

tBuilder.Expect.JSONSchemaString = `
tBuilder.Expect.JSONSchema.String = `
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestValidateJSONSchemaFromStringWithError(t *testing.T) {
}
`)

tBuilder.Expect.JSONSchemaString = `
tBuilder.Expect.JSONSchema.String = `
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestValidateJSONSchemaFromByteWithTwoError(t *testing.T) {
}
`)

tBuilder.Expect.JSONSchemaString = `
tBuilder.Expect.JSONSchema.String = `
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down
48 changes: 30 additions & 18 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
errorRequestURLEmpty = errors.New("url request must be not empty")
)

// Test TODO
type Test struct {
httpClient *http.Client

Expand All @@ -38,35 +39,39 @@ type Test struct {
Expect *Expect
}

// Request TODO
type Request struct {
Base *http.Request
Builders []RequestBuilder
Repeat *RequestRepeatPolitic
}

// RequestRepeatPolitic TODO
type RequestRepeatPolitic struct {
Count int
Delay time.Duration
}

// Middleware TODO
type Middleware struct {
After []AfterExecute
afterT []AfterExecuteT
before []BeforeExecute
beforeT []BeforeExecuteT
AfterT []AfterExecuteT
Before []BeforeExecute
BeforeT []BeforeExecuteT
}

// AllureStep TODO
type AllureStep struct {
name string
Name string
}

// Expect TODO
type Expect struct {
ExecuteTime time.Duration

Code int
JSONSchemaString string
JSONSchemaByte []byte
JSONSchemaFile string
Code int

JSONSchema *ExpectJSONSchema

AssertBody []AssertBody
AssertHeaders []AssertHeaders
Expand All @@ -77,6 +82,13 @@ type Expect struct {
AssertResponseT []AssertResponseT
}

// ExpectJSONSchema TODO
type ExpectJSONSchema struct {
String string
Byte []byte
File string
}

func (it *Test) Execute(ctx context.Context, t testing.TB) ResultsHTTPBuilder {
var (
internalT allureProvider
Expand All @@ -86,7 +98,7 @@ func (it *Test) Execute(ctx context.Context, t testing.TB) ResultsHTTPBuilder {
tOriginal, ok := t.(*testing.T)
if ok {
newT := createAllureT(tOriginal)
defer newT.FinishTest()
defer newT.FinishTest() //nolint

internalT = newT
}
Expand Down Expand Up @@ -132,9 +144,9 @@ func (it *Test) execute(ctx context.Context, allureProvider allureProvider) Resu
name = allureProvider.Name() + "_" + it.Name
)

if it.AllureStep.name != "" {
if it.AllureStep.Name != "" {
// Test with step
name = it.AllureStep.name
name = it.AllureStep.Name

// Execute Test
allureProvider.Logf("Start step %v", name)
Expand Down Expand Up @@ -191,7 +203,7 @@ func (it *Test) startTestWithStep(ctx context.Context, t internalT) (*http.Respo
errs []error
)

t.WithNewStep(it.AllureStep.name, func(stepCtx provider.StepCtx) {
t.WithNewStep(it.AllureStep.Name, func(stepCtx provider.StepCtx) {
resp, errs = it.startTest(ctx, stepCtx)

if len(errs) != 0 {
Expand Down Expand Up @@ -222,7 +234,7 @@ func (it *Test) startTest(ctx context.Context, t internalT) (*http.Response, []e
return resp, []error{err}
}

// Execute before
// Execute Before
if errs := it.beforeTest(t, req); len(errs) > 0 {
return nil, errs
}
Expand Down Expand Up @@ -253,7 +265,7 @@ func (it *Test) startTest(ctx context.Context, t internalT) (*http.Response, []e
}

func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []error {
if len(it.Middleware.After) == 0 && len(it.Middleware.afterT) == 0 {
if len(it.Middleware.After) == 0 && len(it.Middleware.AfterT) == 0 {
return nil
}

Expand All @@ -266,7 +278,7 @@ func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []erro
}
}

for _, executeSuite := range it.Middleware.afterT {
for _, executeSuite := range it.Middleware.AfterT {
if err := executeSuite(t, resp, errs); err != nil {
scope = append(scope, err)
}
Expand All @@ -277,20 +289,20 @@ func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []erro
}

func (it *Test) beforeTest(t internalT, req *http.Request) []error {
if len(it.Middleware.before) == 0 && len(it.Middleware.beforeT) == 0 {
if len(it.Middleware.Before) == 0 && len(it.Middleware.BeforeT) == 0 {
return nil
}

return executeWithStep(t, "Before", func(t T) []error {
scope := make([]error, 0)

for _, execute := range it.Middleware.before {
for _, execute := range it.Middleware.Before {
if err := execute(req); err != nil {
scope = append(scope, err)
}
}

for _, executeT := range it.Middleware.beforeT {
for _, executeT := range it.Middleware.BeforeT {
if err := executeT(t, req); err != nil {
scope = append(scope, err)
}
Expand Down
3 changes: 2 additions & 1 deletion test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func TestValidateResponseWithErrors(t *testing.T) {
var (
ht = &Test{
Expect: &Expect{
Code: 200,
Code: 200,
JSONSchema: new(ExpectJSONSchema),
AssertHeaders: []AssertHeaders{
func(headers http.Header) error {
return errors.New("two error")
Expand Down

0 comments on commit 8581238

Please sign in to comment.