realize/realize_test.go

148 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 (
"os"
"strings"
"testing"
rc "github.com/tockins/realize/realize"
"github.com/go-siris/siris/core/errors"
"bytes"
"log"
)
2017-11-20 13:16:07 +00:00
var mockResponse interface{}
type mockRealize struct {
2017-11-20 15:36:48 +00:00
Settings rc.Settings `yaml:"settings" json:"settings"`
Server rc.Server `yaml:"server" json:"server"`
rc.Schema `yaml:",inline"`
2017-11-20 13:16:07 +00:00
sync chan string
exit chan os.Signal
}
2017-11-20 15:36:48 +00:00
func (m *mockRealize) add() error{
if mockResponse != nil {
return mockResponse.(error)
}
m.Projects = append(m.Projects, rc.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 = []rc.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 = []rc.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 = []rc.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(), rc.RVersion) {
t.Fatal("Version expted", rc.RVersion)
}
}