From df0ef69e0e85f324d178a433b1765cc0fff47311 Mon Sep 17 00:00:00 2001 From: Quobix Date: Mon, 28 Aug 2023 17:26:17 -0400 Subject: [PATCH] Renamed schema_renderer and added more tests Ensures coverage across the board Signed-off-by: Quobix --- .../{example_renderer.go => schema_renderer.go} | 13 ++++++------- ...le_renderer_test.go => schema_renderer_test.go} | 14 +++++++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) rename renderer/{example_renderer.go => schema_renderer.go} (99%) rename renderer/{example_renderer_test.go => schema_renderer_test.go} (98%) diff --git a/renderer/example_renderer.go b/renderer/schema_renderer.go similarity index 99% rename from renderer/example_renderer.go rename to renderer/schema_renderer.go index 713cd245..70824f64 100644 --- a/renderer/example_renderer.go +++ b/renderer/schema_renderer.go @@ -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 @@ -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 @@ -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) diff --git a/renderer/example_renderer_test.go b/renderer/schema_renderer_test.go similarity index 98% rename from renderer/example_renderer_test.go rename to renderer/schema_renderer_test.go index 32c5cbe3..50eaa07c 100644 --- a/renderer/example_renderer_test.go +++ b/renderer/schema_renderer_test.go @@ -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)