realize/realize_test.go

149 lines
2.8 KiB
Go

package main
import (
"bytes"
"github.com/go-siris/siris/core/errors"
"github.com/tockins/realize/realize"
"log"
"os"
"strings"
"testing"
)
var mockResponse interface{}
type mockRealize struct {
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
}
func (m *mockRealize) add() error {
if mockResponse != nil {
return mockResponse.(error)
}
m.Projects = append(m.Projects, realize.Project{Name: "One"})
return nil
}
func (m *mockRealize) setup() error {
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
func (m *mockRealize) start() error {
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
func (m *mockRealize) clean() error {
if mockResponse != nil {
return mockResponse.(error)
}
return nil
}
func (m *mockRealize) remove() error {
if mockResponse != nil {
return mockResponse.(error)
}
m.Projects = []realize.Project{}
return nil
}
func TestRealize_add(t *testing.T) {
m := mockRealize{}
mockResponse = nil
if err := m.add(); err != nil {
t.Fatal("Unexpected error")
}
if len(m.Projects) <= 0 {
t.Fatal("Unexpected error")
}
m = mockRealize{}
m.Projects = []realize.Project{{Name: "Default"}}
mockResponse = nil
if err := m.add(); err != nil {
t.Fatal("Unexpected error")
}
if len(m.Projects) != 2 {
t.Fatal("Unexpected error")
}
m = mockRealize{}
mockResponse = errors.New("error")
if err := m.clean(); err == nil {
t.Fatal("Expected error")
}
if len(m.Projects) != 0 {
t.Fatal("Unexpected error")
}
}
func TestRealize_start(t *testing.T) {
m := mockRealize{}
mockResponse = nil
if err := m.add(); err != nil {
t.Fatal("Unexpected error")
}
}
func TestRealize_setup(t *testing.T) {
m := mockRealize{}
mockResponse = nil
if err := m.setup(); err != nil {
t.Fatal("Unexpected error")
}
}
func TestRealize_clean(t *testing.T) {
m := mockRealize{}
mockResponse = nil
if err := m.clean(); err != nil {
t.Fatal("Unexpected error")
}
mockResponse = errors.New("error")
if err := m.clean(); err == nil {
t.Fatal("Expected error")
}
}
func TestRealize_remove(t *testing.T) {
m := mockRealize{}
mockResponse = nil
if err := m.remove(); err != nil {
t.Fatal("Unexpected error")
}
m = mockRealize{}
mockResponse = nil
m.Projects = []realize.Project{{Name: "Default"}, {Name: "Default"}}
if err := m.remove(); err != nil {
t.Fatal("Unexpected error")
}
if len(m.Projects) != 0 {
t.Fatal("Unexpected error")
}
mockResponse = errors.New("error")
if err := m.clean(); err == nil {
t.Fatal("Expected error")
}
}
func TestRealize_version(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
version()
if !strings.Contains(buf.String(), realize.RVersion) {
t.Fatal("Version expted", realize.RVersion)
}
}