-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_export.go
108 lines (91 loc) · 2.35 KB
/
db_export.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
//
// Copyright (c) 2018 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//
package test
import (
"testing"
"github.com/Circutor/edgex/internal/export"
"github.com/Circutor/edgex/pkg/models"
)
func TestExportDB(t *testing.T, db export.DBClient) {
// Remove previous registrations
db.ScrubAllRegistrations()
regs, err := db.Registrations()
if err != nil {
t.Fatalf("Error getting registrations %v", err)
}
if len(regs) != 0 {
t.Fatalf("Error: expected 0 registrations; found %d", len(regs))
}
r := models.Registration{}
r.Name = "name"
id, err := db.AddRegistration(r)
if err != nil {
t.Fatalf("Error adding registration %v: %v", r, err)
}
regs, err = db.Registrations()
if err != nil {
t.Fatalf("Error getting registrations %v", err)
}
if len(regs) != 1 {
t.Fatalf("There should be only one registration instead of %d", len(regs))
}
r2, err := db.RegistrationById(id)
if err != nil {
t.Fatalf("Error getting registrations by id %v", err)
}
if r2.ID != id {
t.Fatalf("Id does not match %s - %s", r2.ID, id)
}
_, err = db.RegistrationById("INVALID")
if err == nil {
t.Fatalf("Registration should not be found")
}
r3, err := db.RegistrationByName(r.Name)
if err != nil {
t.Fatalf("Error getting registrations by name %v", err)
}
if r3.Name != r.Name {
t.Fatalf("Id does not match %s - %s", r2.ID, id)
}
_, err = db.RegistrationByName("INVALID")
if err == nil {
t.Fatalf("Registration should not be found")
}
err = db.DeleteRegistrationById("INVALID")
if err == nil {
t.Fatalf("Registration should not be deleted")
}
err = db.DeleteRegistrationById(id)
if err != nil {
t.Fatalf("Registration should be deleted: %v", err)
}
id, err = db.AddRegistration(r)
if err != nil {
t.Fatalf("Error adding registration %v: %v", r, err)
}
r.ID = id
r.Name = "name2"
err = db.UpdateRegistration(r)
if err != nil {
t.Fatalf("Error updating registration %v", err)
}
err = db.DeleteRegistrationByName("INVALID")
if err == nil {
t.Fatalf("Registration should not be deleted")
}
err = db.DeleteRegistrationByName(r.Name)
if err != nil {
t.Fatalf("Registration should be deleted: %v", err)
}
err = db.UpdateRegistration(r)
if err == nil {
t.Fatalf("Update should return error")
}
db.CloseSession()
// Calling CloseSession twice to test that there is no panic when closing an
// already closed db
db.CloseSession()
}