2024-05-08 20:28:36 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
2024-05-08 21:08:10 +00:00
|
|
|
|
2024-05-08 20:28:36 +00:00
|
|
|
package mockdata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"log"
|
|
|
|
"fmt"
|
2024-05-08 21:32:31 +00:00
|
|
|
"gitea.rosskeen.house/rosskeen.house/testing/fixture"
|
2024-05-08 20:28:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Return a structure to load the mock data into
|
|
|
|
type MockObjectFn func() interface{}
|
|
|
|
|
|
|
|
type MockParam struct {
|
|
|
|
param fixture.Param
|
|
|
|
mock MockObjectFn
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parameters struct {
|
|
|
|
*fixture.Parameters
|
|
|
|
name string
|
|
|
|
basepath string
|
|
|
|
mockdata MockObjectFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func P(name string, mockdata MockObjectFn) *Parameters { return &Parameters{ name: name, mockdata: mockdata } }
|
|
|
|
|
|
|
|
// mockdata.BasePath('./testdata/mock_{testname}_')
|
|
|
|
func (p *Parameters) BasePath() string {
|
|
|
|
if p.basepath == "" {
|
|
|
|
g := fmt.Sprintf("mock_%s_", p.name)
|
|
|
|
p.basepath = filepath.Join("./testdata", g)
|
|
|
|
}
|
|
|
|
return p.basepath
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockdata.Paths(base string)
|
|
|
|
func (p *Parameters) Paths() []string {
|
|
|
|
df, e := filepath.Glob(p.BasePath() + "*")
|
|
|
|
if e != nil {
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
|
|
|
return df
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parameters) Values() []fixture.Param {
|
|
|
|
// return TestData files
|
|
|
|
paths := p.Paths()
|
|
|
|
r := make([]fixture.Param, len(paths))
|
|
|
|
for i := range(paths) {
|
|
|
|
r[i] = MockParam { param: paths[i], mock: p.mockdata }
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func Read(path string, mockdata interface{}) interface{} {
|
|
|
|
d,e := ioutil.ReadFile(path)
|
|
|
|
if e != nil {
|
|
|
|
log.Fatalf("Error reading fixture data from %s: %s", path, e)
|
|
|
|
}
|
|
|
|
err := yaml.Unmarshal([]byte(d), mockdata)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s", err)
|
|
|
|
}
|
|
|
|
return mockdata
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadRaw(path string) interface{} {
|
|
|
|
d,e := ioutil.ReadFile(path)
|
|
|
|
if e != nil {
|
|
|
|
log.Fatalf("Error reading fixture data from %s: %s", path, e)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
|