-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql_spec.rb
165 lines (128 loc) · 4.16 KB
/
sql_spec.rb
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true
require "rspec/sql"
require_relative "../../support/database"
RSpec.describe RSpec::Sql do
include RSpec::Sql
it "expects database queries" do
expect { User.last }.to query_database
end
it "raises without database queries" do
expect {
expect { nil }.to query_database
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it "expects no database queries" do
expect { nil }.to_not query_database
end
it "expects no database queries" do
expect { nil }.to query_database 0
expect {
expect { nil }.to query_database 1
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it "expects a number of database queries" do
expect { User.last }.to query_database 1
expect { User.create! }.to query_database 3.times
expect {
expect { User.last }.to query_database 2
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it "shows which queries were executed" do
expect {
expect { User.connection.execute("SELECT 1") }.to query_database []
}.to raise_error match("SELECT 1")
end
it "expects certain queries" do
expect { User.last }.to query_database ["User Load"]
end
it "expects multiple queries" do
expect { User.create!.update(name: "Jane") }.to query_database [
"TRANSACTION",
"User Create",
"TRANSACTION",
"TRANSACTION",
"User Update",
"TRANSACTION",
]
end
it "expects a summary of queries" do
expect { User.create!.update(name: "Jane") }.to query_database(
{
insert: { users: 1 },
update: { users: 1 },
}
)
end
it "prints user-friendly message expecting nothing" do
message = error_message { expect { User.last }.not_to query_database }
expect(message).to eq <<~TXT
Expected no database queries but observed:
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?
TXT
end
it "prints user-friendly message expecting something" do
message = error_message { expect { nil }.to query_database }
expect(message).to eq(
"Expected at least one database query but observed none."
)
end
it "prints user-friendly message expecting a number" do
message = error_message { expect { User.last }.to query_database 0 }
expect(message).to eq <<~TXT
Expected database queries: 0
Actual database queries: 1
Diff: +1
Full query log:
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?
TXT
end
it "prints user-friendly message expecting x.times" do
message = error_message { expect { User.last }.to query_database 2.times }
expect(message).to eq <<~TXT
Expected database queries: 2
Actual database queries: 1
Diff: -1
Full query log:
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?
TXT
end
it "prints user-friendly message expecting list" do
message = error_message do
expect { User.last }.to query_database ["User Update"]
end
expect(message).to eq <<~TXT
Expected database queries: ["User Update"]
Actual database queries: ["User Load"]
Diff:
@@ -1 +1 @@
-["User Update"]
+["User Load"]
Full query log:
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?
TXT
end
it "prints user-friendly message expecting summary" do
message = error_message do
expect { User.last }.to query_database(
update: { user: 1 }
)
end
# This message could be better but nobody has asked for it yet.
expect(message).to eq <<~TXT
Expected database queries: {:update=>{:user=>1}}
Actual database queries: {:select=>{:users=>1}}
Diff:
@@ -1 +1 @@
-:update => {:user=>1},
+:select => {:users=>1},
Full query log:
User Load SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ?
TXT
end
def error_message
yield
rescue RSpec::Expectations::ExpectationNotMetError => e
# Remove colours and trailing whitespace from message:
e.message.gsub(/\e\[(\d+)m/, "").gsub(/ $/, "")
end
end