-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest_camera_event_logger_test.go
84 lines (73 loc) · 2.13 KB
/
nest_camera_event_logger_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
package main
import (
"encoding/json"
_ "github.com/jinzhu/gorm/dialects/postgres"
"io/ioutil"
"os"
"testing"
"time"
)
func TestMain(m *testing.M) {
err := os.Setenv("NEST_DB_NAME", "postgres")
if err != nil {
panic(err)
}
os.Exit(m.Run())
}
func Test_logNestCamEvent(t *testing.T) {
jsonExample, err := ioutil.ReadFile("fixtures/nest_camera_response.json")
if err != nil {
panic(err)
}
var exampleNestCameraResponse NestCameraResponse
json.Unmarshal(jsonExample, &exampleNestCameraResponse)
logNestCamEvent(exampleNestCameraResponse)
db, err := getDbConnection()
if err != nil {
t.Error(err)
}
defer db.Close()
defer db.Exec("TRUNCATE TABLE nest_camera_events;")
var firstNestCamEvent NestCameraEvent
db.First(&firstNestCamEvent)
var count int
db.Model(&firstNestCamEvent).Count(&count)
if 1 != count {
t.Errorf("Expected 1 camera event, got %v", count)
}
timeLayout := "2006-01-02T15:04:05.000Z"
timeStr := "2017-08-30T19:43:37.000Z"
expectedEventTime, err := time.Parse(timeLayout, timeStr)
if err != nil {
t.Error(err)
}
if firstNestCamEvent.HasSound != false {
t.Errorf("Expected %v got %v", false, firstNestCamEvent.HasSound)
}
if firstNestCamEvent.HasMotion != true {
t.Errorf("Expected %v got %v", true, firstNestCamEvent.HasMotion)
}
if firstNestCamEvent.HasPerson != false {
t.Errorf("Expected %v got %v", false, firstNestCamEvent.HasPerson)
}
if !firstNestCamEvent.StartTime.Equal(expectedEventTime) {
t.Errorf("Expected %v got %v", expectedEventTime, firstNestCamEvent.StartTime)
}
if firstNestCamEvent.WebURL != "www.nest.com" {
t.Errorf("Expected %v got %v", "www.nest.com", firstNestCamEvent.WebURL)
}
// It should not log duplicate events
logNestCamEvent(exampleNestCameraResponse)
db.Model(&firstNestCamEvent).Count(&count)
if 1 != count {
t.Errorf("Expected 1 camera event, got %v", count)
}
// Unique by StartDate
exampleNestCameraResponse.NestCameraEvent.StartTime = time.Now()
logNestCamEvent(exampleNestCameraResponse)
var secondNestCamEvent NestCameraEvent
db.Model(&secondNestCamEvent).Count(&count)
if 2 != count {
t.Errorf("Expected 2 camera events, got %v", count)
}
}