Skip to content

Commit

Permalink
Increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivaylo Marinkov committed Mar 22, 2017
1 parent b7bf207 commit 3cecde7
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 22 deletions.
88 changes: 71 additions & 17 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (

func TestBuildConfig(t *testing.T) {
testCases := []struct {
tmpl string
srcs []map[string]interface{}
out string
ok bool
createFile bool
tmpl string
validate bool
srcs []map[string]interface{}
out string
ok bool
}{
{
true,
"key1: {{.key1}}, key2: {{.key2}}",
true,
[]map[string]interface{}{
{
"type": "config",
Expand All @@ -33,7 +37,19 @@ func TestBuildConfig(t *testing.T) {
true,
},
{
true,
"key1: {{.key1}}, key2: {{.key2}}",
true,
[]map[string]interface{}{
{
"type": "bad",
},
},
"",
false,
},
{
false, "", true,
[]map[string]interface{}{
{
"type": "bad",
Expand All @@ -42,26 +58,64 @@ func TestBuildConfig(t *testing.T) {
"",
false,
},
{
true,
"key1: {{test}}, key2: {{test2}}",
true,
[]map[string]interface{}{
{
"type": "config",
"vals": map[interface{}]interface{}{
"key1": "var1",
"key2": "var2",
},
},
},
"",
false,
},
{
true,
"key1: {{test}}, key2: {{test2}}",
false,
[]map[string]interface{}{
{
"type": "config",
"vals": map[interface{}]interface{}{
"key1": "var1",
"key2": "var2",
},
},
},
"key1: {{test}}, key2: {{test2}}",
true,
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
// Prepare config file
tmlpFile, err := ioutil.TempFile("", "TestBuild")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmlpFile.Name()) // clean up
filename := ""

if _, err := tmlpFile.Write([]byte(tc.tmpl)); err != nil {
t.Fatal(err)
}
if err := tmlpFile.Close(); err != nil {
t.Fatal(err)
if tc.createFile {
// Prepare config file
tmlpFile, err := ioutil.TempFile("", "TestBuild")
if err != nil {
log.Fatal(err)
}
filename = tmlpFile.Name()

defer os.Remove(filename) // clean up

if _, err := tmlpFile.Write([]byte(tc.tmpl)); err != nil {
t.Fatal(err)
}
if err := tmlpFile.Close(); err != nil {
t.Fatal(err)
}
}

// Build
out, err := buildConfig(tmlpFile.Name(), true, tc.srcs)
out, err := buildConfig(filename, tc.validate, tc.srcs)
if tc.ok != (err == nil) {
if err != nil {
t.Fatal("Failed with", err)
Expand All @@ -71,7 +125,7 @@ func TestBuildConfig(t *testing.T) {
}

if tc.ok && string(out) != tc.out {
t.Errorf("Got '%v'; want '%v'", out, tc.out)
t.Errorf("Got '%s'; want '%s'", out, tc.out)
}
})
}
Expand Down
82 changes: 82 additions & 0 deletions cmd/push_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"

"github.com/miracl/casper/lib/caspertest"
Expand Down Expand Up @@ -195,3 +197,83 @@ func TestPush(t *testing.T) {
})
}
}

func TestGenerateBackupFilename(t *testing.T) {
t.Run("Case0", func(t *testing.T) {
expression := regexp.MustCompile("\\d{10}_backup.txt")
generated := generateBackupFilename()

if !expression.MatchString(generated) {
t.Errorf("Generated filename %s did not match the expected format", generated)
}
})
}

func TestSaveBackup(t *testing.T) {
testCases := []string{
"test1234",
}

for i := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
filename, err := saveBackup(&testCases[i])
defer os.Remove(filename)

result, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Could not read file %s", filename)
}

if bytes.Compare(result, []byte(testCases[i])) != 0 {
t.Errorf("Wrong content: Expected \"%s\", got \"%s\"", testCases[i], string(result))
}
})
}
}

func TestBackup(t *testing.T) {
testCases := []struct {
storage string
}{
{"test: 1234abc"},
}

backupFormat := "Backup has been saved as (?P<Filename>\\d{10}_backup.txt).*"
expression := regexp.MustCompile(backupFormat)

for i, tc := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
// Prepare config
testFilename := fmt.Sprintf("Case%vConfig.yaml", i)
storageFile, err := caspertest.PrepareTmpFile(testFilename, []byte(tc.storage))
if err != nil {
t.Fatal(err)
}
defer os.Remove(storageFile.Name())

sourcesList, _ := getSliceStringMapIface("")
pc := &pushConfig{
"", false, tc.storage, "file", "",
false, true, &sourcesList, false, true,
}
conf := map[string]interface{}{"path": storageFile.Name()}

out := caspertest.GetStdout(t, func() {
err = backup(pc, conf)

if err != nil {
t.Errorf("Backup failed: %v", err)
}
})

if !expression.MatchString(out) {
t.Errorf("Output \"%s\" did not match "+
"the expected format: \"%s\"",
out, backupFormat)
} else {
backupFile := expression.FindStringSubmatch(out)[1]
defer os.Remove(backupFile)
}
})
}
}
80 changes: 80 additions & 0 deletions cmd/sources_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -273,3 +274,82 @@ func TestToStringMapString(t *testing.T) {
})
}
}

func TestSourceFormatError(t *testing.T) {
testCases := []struct {
inputMessage string
inputError error
expected string
}{
{
"test1234", nil,
"Invalid source definition: test1234",
},
{
"test1234", errors.New("test444"),
"Invalid source definition: test1234 (Err:test444)",
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
output := sourceFormatError{
tc.inputMessage, tc.inputError,
}.Error()

if output != tc.expected {
t.Errorf("Got \"%s\"; want \"%s\"", output, tc.expected)
}
})
}
}

func TestConvertError(t *testing.T) {
testCases := []struct {
inputInterface interface{}
inputType string
expected string
}{
{
"test string", "int32",
"Unable to convert \"test string\" to int32",
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
output := convertError{
tc.inputInterface, tc.inputType,
}.Error()

if output != tc.expected {
t.Errorf("Got \"%s\"; want \"%s\"", output, tc.expected)
}
})
}
}

func TestToString(t *testing.T) {
testCases := []struct {
input interface{}
expectedStr string
expectedOK bool
}{
{"24abc", "24abc", true},
{24, "24", true},
{true, "true", true},
{false, "false", true},
{-150.29, "", false},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) {
str, ok := toString(tc.input)

if str != tc.expectedStr || ok != tc.expectedOK {
t.Errorf("Got \"%s\", %t; want \"%s\", %t",
str, ok, tc.expectedStr, tc.expectedOK)
}
})
}
}
9 changes: 9 additions & 0 deletions cmd/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func TestGetStorage(t *testing.T) {
}
}

func TestError(t *testing.T) {
t.Run("Case0", func(t *testing.T) {
err := storageError("test")
if err.Error() != "Invalid storage type" {
t.Errorf("Incorrect error message")
}
})
}

type testStorage struct{}

func (testStorage) String(string) (string, error) {
Expand Down
Loading

0 comments on commit 3cecde7

Please sign in to comment.