-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
148 lines (130 loc) · 4.61 KB
/
db_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This file is part of the eliona project.
// Copyright © 2022 LEICOM iTEC AG. All Rights Reserved.
// ______ _ _
// | ____| (_)
// | |__ | |_ ___ _ __ __ _
// | __| | | |/ _ \| '_ \ / _` |
// | |____| | | (_) | | | | (_| |
// |______|_|_|\___/|_| |_|\__,_|
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package db
import (
"fmt"
"github.com/eliona-smart-building-assistant/go-utils/log"
"github.com/pashagolub/pgxmock"
"github.com/stretchr/testify/assert"
"testing"
)
type Temperature struct {
Value int
Unit string
}
type Weather struct {
Temperature *Temperature
Remark *string
DayOfWeek *int
}
func TestConnectionString(t *testing.T) {
t.Setenv("CONNECTION_STRING", "postgres://user:secret@localhost:5432/database")
assert.Equal(t, "localhost", Hostname())
assert.Equal(t, "user", Username())
assert.Equal(t, "secret", Password())
assert.Equal(t, 5432, Port())
assert.Equal(t, "database", DatabaseName())
}
func TestEmptyConnectionString(t *testing.T) {
t.Setenv("CONNECTION_STRING", "")
assert.Equal(t, "", Hostname())
assert.Equal(t, "", Username())
assert.Equal(t, "", Password())
assert.Equal(t, 0, Port())
assert.Equal(t, "", DatabaseName())
}
func TestPointer(t *testing.T) {
mock := connectionMock()
rows := mock.NewRows([]string{"temperature", "remark", "day_of_week"}).
AddRow(nil, "Cloudy", 4)
mock.ExpectQuery("select (.+) from weather").WillReturnRows(rows)
weather, err := QuerySingleRow[Weather](mock, "select value, unit from weather")
assert.Nil(t, err)
assert.Equal(t, "", weather.Temperature.Unit)
assert.Equal(t, "Cloudy", *weather.Remark)
assert.Equal(t, 4, *weather.DayOfWeek)
}
func TestQuery(t *testing.T) {
mock := connectionMock()
rows := mock.NewRows([]string{"value", "unit"}).
AddRow(25, "Celsius").
AddRow(67, "Fah").
AddRow(40, "Celsius")
mock.ExpectQuery("select (.+) from temperatures").WillReturnRows(rows)
resultsChan := make(chan Temperature)
go func() {
err := Query(mock, "select value, unit from temperatures", resultsChan)
if err != nil {
assert.Nil(t, err)
}
}()
var results []Temperature
for result := range resultsChan {
results = append(results, result)
}
assert.Equal(t, results[0], Temperature{25, "Celsius"})
assert.Equal(t, results[1], Temperature{67, "Fah"})
assert.Equal(t, results[2], Temperature{40, "Celsius"})
}
func TestQuerySingleRowError(t *testing.T) {
mock := connectionMock()
mock.ExpectQuery("select.+from temperatures").WillReturnError(fmt.Errorf("error"))
result, err := QuerySingleRow[Temperature](mock, "select value, unit from temperatures")
assert.Equal(t, fmt.Errorf("error"), err)
assert.Equal(t, result.Value, 0)
}
func TestQuerySingleRow(t *testing.T) {
mock := connectionMock()
rows := mock.NewRows([]string{"value", "unit"}).
AddRow(25, "Celsius")
mock.ExpectQuery("select (.+) from temperatures").WillReturnRows(rows)
result, err := QuerySingleRow[Temperature](mock, "select value, unit from temperatures")
assert.Nil(t, err)
assert.Equal(t, result, Temperature{25, "Celsius"})
}
func TestQueryError(t *testing.T) {
mock := connectionMock()
mock.ExpectQuery("select.+from temperatures").WillReturnError(fmt.Errorf("error"))
resultsChan := make(chan Temperature)
go func() {
err := Query(mock, "select value, unit from temperatures", resultsChan)
if err != nil {
assert.Equal(t, fmt.Errorf("error"), err)
}
}()
assert.Equal(t, (<-resultsChan).Value, 0)
}
func TestExecuteFile(t *testing.T) {
mock := connectionMock()
mock.ExpectExec("create table test").WillReturnResult(pgxmock.NewResult("CREATE", 0))
mock.ExpectExec("insert into test").WillReturnResult(pgxmock.NewResult("INSERT", 1))
err := ExecFile(mock, "test.sql")
assert.Nil(t, err)
}
func TestInsert(t *testing.T) {
mock := connectionMock()
mock.ExpectExec("insert into temperatures").
WithArgs(pgxmock.AnyArg(), pgxmock.AnyArg()).
WillReturnResult(pgxmock.NewResult("INSERT", 1))
err := Exec(mock, "insert into temperatures (value, unit) values (?, ?)", 25, "Celsius")
assert.Nil(t, err)
}
func connectionMock() pgxmock.PgxConnIface {
mock, err := pgxmock.NewConn()
if err != nil {
log.Fatal("database", "An error '%s' was not expected when opening a mocked database connection", err)
}
return mock
}