-
Notifications
You must be signed in to change notification settings - Fork 2
/
timefor_test.go
131 lines (114 loc) · 3.06 KB
/
timefor_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"text/template"
"github.com/google/go-cmp/cmp"
"github.com/jmoiron/sqlx"
"gopkg.in/yaml.v2"
)
var db *sqlx.DB
func TestMain(m *testing.M) {
err := exec.Command("sh", "-c", "go build").Run()
if err != nil {
panic(err)
}
os.Exit(m.Run())
}
func TestSchema(t *testing.T) {
db = sqlx.MustOpen("sqlite3", ":memory:")
defer db.Close()
initDb(db)
err := Start(db, "test", 0)
if err != nil {
t.Fatal(err)
}
var count int
_ = db.QueryRow(`SELECT count(*) FROM log`).Scan(&count)
if count != 1 {
t.Errorf("log table should have 1 row, but it has %v", count)
}
initDb(db)
_ = db.QueryRow(`SELECT count(*) FROM log`).Scan(&count)
if count != 1 {
t.Errorf("log table should have 1 row, but it has %v", count)
}
err = Start(db, "test", 0)
if diff := cmp.Diff(err.Error(), "Keep tracking existing activity"); diff != "" {
t.Errorf("expected different error: %v", diff)
}
_, err = db.Exec("INSERT INTO log (name, started) VALUES ('test', strftime('%s', 'now'))")
if err == nil {
t.Error("insert should not succeed")
}
if diff := cmp.Diff(err.Error(), "UNIQUE constraint failed: log.current"); diff != "" {
t.Errorf("expected different error: %v", diff)
}
_, err = db.Exec("INSERT INTO log (name, started, current) VALUES ('test', strftime('%s', 'now'), NULL)")
if err == nil {
t.Error("insert should not succeed")
}
if diff := cmp.Diff(err.Error(), "UNIQUE constraint failed: log.started"); diff != "" {
t.Errorf("expected different error: %v", diff)
}
err = Start(db, "test2", 0)
if diff := cmp.Diff(err.Error(), "cannot insert new activity into database: UNIQUE constraint failed: log.started"); diff != "" {
t.Errorf("expected different error: %v", diff)
}
}
func TestCmd(t *testing.T) {
file, err := os.CreateTemp("", "logtest")
if err != nil {
t.Fatal(err)
}
defer os.Remove(file.Name())
db = sqlx.MustOpen("sqlite3", file.Name())
defer db.Close()
data, err := os.ReadFile("testcmd.yaml")
if err != nil {
t.Fatal(err)
}
var cases []struct {
Name string
Cmd string
Code int
Output string
Error string
Active bool
}
err = yaml.Unmarshal(data, &cases)
if err != nil {
t.Fatal(err)
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
line := fmt.Sprintf("DBFILE=%v ./timefor %v", file.Name(), c.Cmd)
cmd := exec.Command("sh", "-c", line)
out, err := cmd.CombinedOutput()
var exiterr *exec.ExitError
if c.Code == 0 && err != nil {
t.Fatal(err)
} else if errors.As(err, &exiterr) && exiterr.ExitCode() != c.Code {
t.Errorf("expected code %v got %v", c.Code, exiterr.ExitCode())
}
latest, err := Latest(db)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
err = template.Must(template.New("tpl").Parse(c.Output)).Execute(&buf, latest)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(strings.TrimSpace(string(out)), strings.TrimSpace(buf.String())); diff != "" {
t.Logf("Got:\n%v", string(out))
t.Errorf("expected different output: %v", diff)
}
})
}
}