Skip to content

Commit

Permalink
Renamed schema_renderer and added more tests
Browse files Browse the repository at this point in the history
Ensures coverage across the board

Signed-off-by: Quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Aug 28, 2023
1 parent 8adc2ac commit df0ef69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 6 additions & 7 deletions renderer/example_renderer.go → renderer/schema_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}

func CreateRendererUsingDictionary(dictionaryLocation string) *SchemaRenderer {
// try and read in the dictionary file
words := ReadDictionary(dictionaryLocation)
return &SchemaRenderer{words: words}
}

// RenderSchema takes a schema and renders it into a map[string]any, ready to be converted to JSON or YAML.
func (wr *SchemaRenderer) RenderSchema(schema *base.Schema) map[string]any {
// dive into the schema and render it
Expand Down Expand Up @@ -63,7 +57,6 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
} else {

// generate a random value based on the schema format, pattern and length values.

var minLength int64 = 3
var maxLength int64 = 10

Expand Down Expand Up @@ -301,6 +294,12 @@ type SchemaRenderer struct {
words []string
}

func CreateRendererUsingDictionary(dictionaryLocation string) *SchemaRenderer {
// try and read in the dictionary file
words := ReadDictionary(dictionaryLocation)
return &SchemaRenderer{words: words}
}

// CreateRendererUsingDefaultDictionary will create a new SchemaRenderer using the default dictionary file.
func CreateRendererUsingDefaultDictionary() *SchemaRenderer {
wr := new(SchemaRenderer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,12 +994,24 @@ func TestCreateRendererUsingDefaultDictionary(t *testing.T) {
}

func TestReadDictionary(t *testing.T) {
if _, err := os.Stat("/usr/share/dict/words"); !os.IsNotExist(err) {
if _, err := os.Stat("/usr/share/dict/wddords"); !os.IsNotExist(err) {
words := ReadDictionary("/usr/share/dict/words")
assert.Greater(t, len(words), 500)
}
}

func TestCreateFakeDictionary(t *testing.T) {
// create a temp file and create a simple temp dictionary
tmpFile, _ := os.CreateTemp("", "pb33f")
tmpFile.Write([]byte("one\nfive\nthree"))
words := ReadDictionary(tmpFile.Name())
renderer := CreateRendererUsingDictionary(tmpFile.Name())
assert.Len(t, words, 3)
assert.Equal(t, "five", renderer.RandomWord(4, 4, 0))
assert.Equal(t, "one", renderer.RandomWord(2, 3, 0))
assert.Equal(t, "three", renderer.RandomWord(5, 5, 0))
}

func TestReadDictionary_BadReadFile(t *testing.T) {
words := ReadDictionary("/do/not/exist")
assert.LessOrEqual(t, len(words), 0)
Expand Down

0 comments on commit df0ef69

Please sign in to comment.