forked from pier-oliviert/irrigation
/
irrigation_test.go
95 lines (74 loc) · 1.7 KB
/
irrigation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
_ "github.com/mattn/go-sqlite3"
"os"
"testing"
"time"
"github.com/pothibo/irrigation/db"
"github.com/pothibo/irrigation/models"
"github.com/pothibo/irrigation/scheduler"
)
func TestStart(t *testing.T) {
db.Init("test")
models.RegisterEntry()
models.RegisterValve()
models.RegisterSchedule()
err := db.Create()
if err != nil {
t.Fail()
t.Log(err)
}
}
func TestValveCreation(t *testing.T) {
valve := models.FirstValveOrCreate(17)
if valve.RelayId != 17 {
t.Fail()
}
}
func TestValvesAreUnique(t *testing.T) {
query := "select v.RelayId, v.Name " +
"from valves v " +
"where v.RelayId = 17"
valves, err := db.Orm().Select(models.Valve{}, query)
if err != nil || len(valves) != 1 {
t.Fail()
t.Log(valves)
t.Log(err)
}
}
func TestSchedulesCreation(t *testing.T) {
valve, _ := models.GetValveByRelayId(17)
schedule := &models.Schedule{
ValveId: valve.Id,
Length: 3,
Interval: 2,
Active: true,
}
schedule.SetStart(time.Now().Add(time.Second * 5).String())
db.Orm().Insert(schedule)
}
func TestSchedulerIsRunning(t *testing.T) {
scheduler.Run()
if !scheduler.IsRunning() {
t.Fail()
t.Log("Scheduler is not running")
}
}
func TestSchedulerManageSchedules(t *testing.T) {
query := "select s.Id, s.ValveId, s.Active, s.Start, s.Interval, s.Length " +
"from schedules s " +
"where s.Active = 1 "
instances, err := db.Orm().Select(models.Schedule{}, query)
if err != nil {
t.Fail()
t.Log(err)
}
schedules := make([]*models.Schedule, len(instances))
for i, instance := range instances {
schedules[i] = instance.(*models.Schedule)
}
time.Sleep(20 * time.Second)
}
func TestEnded(t *testing.T) {
os.Remove("irrigation-test.db")
}