realize/realize_test.go

142 lines
2.6 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"
2017-11-26 23:01:08 +00:00
"errors"
2018-04-14 14:15:03 +00:00
"github.com/oxequa/realize/realize"
2017-11-20 15:48:06 +00:00
"log"
2017-11-20 15:36:48 +00:00
"strings"
"testing"
)
2017-11-20 13:16:07 +00:00
var mockResponse interface{}
2017-11-23 12:45:49 +00:00
type mockRealize realize.Realize
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) <= 0 {
2017-11-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 2 {
2017-11-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
m = mockRealize{}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-22 16:01:05 +00:00
t.Error("Expected error")
2017-11-20 15:36:48 +00:00
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 0 {
2017-11-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-22 16:01:05 +00:00
t.Error("Expected error")
2017-11-20 15:36:48 +00:00
}
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
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-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
2017-11-20 15:48:06 +00:00
if len(m.Projects) != 0 {
2017-11-22 16:01:05 +00:00
t.Error("Unexpected error")
2017-11-20 15:36:48 +00:00
}
mockResponse = errors.New("error")
2017-11-20 15:48:06 +00:00
if err := m.clean(); err == nil {
2017-11-22 16:01:05 +00:00
t.Error("Expected error")
2017-11-20 15:36:48 +00:00
}
}
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) {
2017-11-22 16:01:05 +00:00
t.Error("Version expted", realize.RVersion)
2017-11-20 15:36:48 +00:00
}
2017-11-26 23:01:08 +00:00
}