realize/realize_test.go

149 lines
2.8 KiB
Go
Raw Normal View History

2017-11-20 13:16:07 +00:00
package main
2017-11-20 15:36:48 +00:00
import (
2017-11-20 15:48:06 +00:00
"bytes"
"github.com/go-siris/siris/core/errors"
"github.com/tockins/realize/realize"
"log"
2017-11-20 15:36:48 +00:00
"os"
"strings"
"testing"
)
2017-11-20 13:16:07 +00:00
var mockResponse interface{}
type mockRealize struct {
2017-11-20 15:48:06 +00:00
Settings realize.Settings `yaml:"settings" json:"settings"`
Server realize.Server `yaml:"server" json:"server"`
realize.Schema `yaml:",inline"`
sync chan string
exit chan os.Signal
2017-11-20 13:16:07 +00:00
}
2017-11-20 15:36:48 +00:00
2017-11-20 15:48:06 +00:00
func (m *mockRealize) add() error {
2017-11-20 15:36:48 +00:00
if mockResponse != nil {
return mockResponse.(error)
}
2017-11-20 15:48:06 +00:00
m.Projects = append(m.Projects, realize.Project{Name: "One"})
2017-11-20 15:36:48 +00:00
return nil
}
2017-11-20 15:48:06 +00:00
func (m *mockRealize) setup() error {
2017-11-20 15:36:48 +00:00
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
2017-11-20 15:48:06 +00:00
func (m *mockRealize) start() error {
2017-11-20 15:36:48 +00:00
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
2017-11-20 15:48:06 +00:00
func (m *mockRealize) clean() error {
2017-11-20 15:36:48 +00:00
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
2017-11-20 15:48:06 +00:00
func (m *mockRealize) remove() error {
2017-11-20 15:36:48 +00:00
if mockResponse != nil {
return mockResponse.(error)
}
2017-11-20 15:48:06 +00:00
m.Projects = []realize.Project{}
2017-11-20 15:36:48 +00:00
return nil
}
func TestRealize_add(t *testing.T) {
m := mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.add(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) <= 0 {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
m = mockRealize{}
2017-11-20 15:48:06 +00:00
m.Projects = []realize.Project{{Name: "Default"}}
2017-11-20 15:36:48 +00:00
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.add(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 2 {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
m = mockRealize{}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Expected error")
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 0 {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
}
func TestRealize_start(t *testing.T) {
m := mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.add(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
}
func TestRealize_setup(t *testing.T) {
m := mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.setup(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
}
func TestRealize_clean(t *testing.T) {
m := mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.clean(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Expected error")
}
}
func TestRealize_remove(t *testing.T) {
m := mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
if err := m.remove(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
m = mockRealize{}
mockResponse = nil
2017-11-20 15:48:06 +00:00
m.Projects = []realize.Project{{Name: "Default"}, {Name: "Default"}}
if err := m.remove(); err != nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 0 {
2017-11-20 15:36:48 +00:00
t.Fatal("Unexpected error")
}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-20 15:36:48 +00:00
t.Fatal("Expected error")
}
}
func TestRealize_version(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
version()
2017-11-20 15:48:06 +00:00
if !strings.Contains(buf.String(), realize.RVersion) {
t.Fatal("Version expted", realize.RVersion)
2017-11-20 15:36:48 +00:00
}
2017-11-20 15:48:06 +00:00
}