-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding resource loader interface and default implement (#95)
- Loading branch information
1 parent
5a3b114
commit d564f69
Showing
8 changed files
with
231 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package testing | ||
|
||
// Loader is an interface for test cases loader | ||
type Loader interface { | ||
HasMore() bool | ||
Load() ([]byte, error) | ||
Put(string) (err error) | ||
GetContext() string | ||
GetCount() int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package testing | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/util" | ||
) | ||
|
||
type fileLoader struct { | ||
paths []string | ||
index int | ||
} | ||
|
||
// NewFileLoader creates the instance of file loader | ||
func NewFileLoader() Loader { | ||
return &fileLoader{index: -1} | ||
} | ||
|
||
// HasMore returns if there are more test cases | ||
func (l *fileLoader) HasMore() bool { | ||
l.index++ | ||
return l.index < len(l.paths) | ||
} | ||
|
||
// Load returns the test case content | ||
func (l *fileLoader) Load() (data []byte, err error) { | ||
data, err = os.ReadFile(l.paths[l.index]) | ||
return | ||
} | ||
|
||
// Put adds the test case path | ||
func (l *fileLoader) Put(item string) (err error) { | ||
for _, pattern := range util.Expand(item) { | ||
var files []string | ||
if files, err = filepath.Glob(pattern); err == nil { | ||
l.paths = append(l.paths, files...) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// GetContext returns the context of current test case | ||
func (l *fileLoader) GetContext() string { | ||
return path.Dir(l.paths[l.index]) | ||
} | ||
|
||
// GetCount returns the count of test cases | ||
func (l *fileLoader) GetCount() int { | ||
return len(l.paths) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package testing_test | ||
|
||
import ( | ||
"testing" | ||
|
||
atest "github.com/linuxsuren/api-testing/pkg/testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileLoader(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
items []string | ||
verify func(t *testing.T, loader atest.Loader) | ||
}{{ | ||
name: "empty", | ||
items: []string{}, | ||
verify: func(t *testing.T, loader atest.Loader) { | ||
assert.False(t, loader.HasMore()) | ||
assert.Empty(t, loader.GetCount()) | ||
}, | ||
}, { | ||
name: "brace expansion path", | ||
items: []string{"testdata/{invalid-,}testcase.yaml"}, | ||
verify: defaultVerify, | ||
}, { | ||
name: "glob path", | ||
items: []string{"testdata/*testcase.yaml"}, | ||
verify: defaultVerify, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
loader := atest.NewFileLoader() | ||
for _, item := range tt.items { | ||
loader.Put(item) | ||
} | ||
tt.verify(t, loader) | ||
}) | ||
} | ||
} | ||
|
||
func defaultVerify(t *testing.T, loader atest.Loader) { | ||
assert.True(t, loader.HasMore()) | ||
data, err := loader.Load() | ||
assert.Nil(t, err) | ||
assert.Equal(t, invalidTestCaseContent, string(data)) | ||
assert.Equal(t, "testdata", loader.GetContext()) | ||
|
||
assert.True(t, loader.HasMore()) | ||
data, err = loader.Load() | ||
assert.Nil(t, err) | ||
assert.Equal(t, testCaseContent, string(data)) | ||
assert.Equal(t, "testdata", loader.GetContext()) | ||
|
||
assert.False(t, loader.HasMore()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.