@@ -4,6 +4,8 @@ defmodule QueryTest do
4
4
import ExUnit.CaptureLog
5
5
alias Postgrex , as: P
6
6
7
+ Postgrex.Types . define ( Postgrex.ElixirDurationTypes , [ ] , interval_decode_type: Duration )
8
+
7
9
setup context do
8
10
opts = [
9
11
database: "postgrex_test" ,
@@ -133,6 +135,66 @@ defmodule QueryTest do
133
135
query ( "SELECT interval '10240000 microseconds'" , [ ] )
134
136
end
135
137
138
+ if Version . match? ( System . version ( ) , ">= 1.17.0" ) do
139
+ test "decode interval with Elixir Duration" do
140
+ opts = [ database: "postgrex_test" , backoff_type: :stop , types: Postgrex.ElixirDurationTypes ]
141
+ { :ok , pid } = P . start_link ( opts )
142
+
143
+ assert [ [ % Duration { microsecond: { 0 , 6 } } ] ] =
144
+ P . query! ( pid , "SELECT interval '0'" , [ ] ) . rows
145
+
146
+ assert [ [ % Duration { year: 100 , microsecond: { 0 , 6 } } ] ] =
147
+ P . query! ( pid , "SELECT interval '100 years'" , [ ] ) . rows
148
+
149
+ assert [ [ % Duration { month: 10 , microsecond: { 0 , 6 } } ] ] =
150
+ P . query! ( pid , "SELECT interval '10 months'" , [ ] ) . rows
151
+
152
+ assert [ [ % Duration { week: 100 , microsecond: { 0 , 6 } } ] ] =
153
+ P . query! ( pid , "SELECT interval '100 weeks'" , [ ] ) . rows
154
+
155
+ assert [ [ % Duration { day: 5 , microsecond: { 0 , 6 } } ] ] =
156
+ P . query! ( pid , "SELECT interval '5 days'" , [ ] ) . rows
157
+
158
+ assert [ [ % Duration { hour: 100 , microsecond: { 0 , 6 } } ] ] =
159
+ P . query! ( pid , "SELECT interval '100 hours'" , [ ] ) . rows
160
+
161
+ assert [ [ % Duration { minute: 10 , microsecond: { 0 , 6 } } ] ] =
162
+ P . query! ( pid , "SELECT interval '10 minutes'" , [ ] ) . rows
163
+
164
+ assert [ [ % Duration { second: 10 , microsecond: { 0 , 6 } } ] ] =
165
+ P . query! ( pid , "SELECT interval '10 secs'" , [ ] ) . rows
166
+
167
+ assert [
168
+ [
169
+ % Duration {
170
+ year: 1 ,
171
+ month: 2 ,
172
+ week: 5 ,
173
+ day: 5 ,
174
+ hour: 3 ,
175
+ minute: 2 ,
176
+ second: 1 ,
177
+ microsecond: { 0 , 6 }
178
+ }
179
+ ]
180
+ ] =
181
+ P . query! (
182
+ pid ,
183
+ "SELECT interval '1 year 2 months 40 days 3 hours 2 minutes 1 seconds'" ,
184
+ [ ]
185
+ ) . rows
186
+
187
+ assert [ [ % Duration { second: 53 , microsecond: { 204_800 , 6 } } ] ] =
188
+ P . query! ( pid , "SELECT interval '53 secs 204800 microseconds'" , [ ] ) . rows
189
+
190
+ assert [ [ % Duration { second: 10 , microsecond: { 240_000 , 6 } } ] ] =
191
+ P . query! ( pid , "SELECT interval '10240000 microseconds'" , [ ] ) . rows
192
+
193
+ assert [ [ [ % Duration { second: 10 , microsecond: { 240_000 , 6 } } ] ] ] =
194
+ P . query! ( pid , "SELECT ARRAY[interval '10240000 microseconds']" , [ ] ) . rows
195
+ end
196
+ end
197
+
136
198
test "decode point" , context do
137
199
assert [ [ % Postgrex.Point { x: - 97.5 , y: 100.1 } ] ] ==
138
200
query ( "SELECT point(-97.5, 100.1)::point" , [ ] )
@@ -896,6 +958,44 @@ defmodule QueryTest do
896
958
] )
897
959
end
898
960
961
+ if Version . match? ( System . version ( ) , ">= 1.17.0" ) do
962
+ test "encode interval with Elixir duration" , context do
963
+ assert [ [ % Postgrex.Interval { months: 0 , days: 0 , secs: 0 , microsecs: 0 } ] ] =
964
+ query ( "SELECT $1::interval" , [ Duration . new! ( [ ] ) ] )
965
+
966
+ assert [ [ % Postgrex.Interval { months: 100 , days: 0 , secs: 0 , microsecs: 0 } ] ] =
967
+ query ( "SELECT $1::interval" , [ Duration . new! ( month: 100 ) ] )
968
+
969
+ assert [ [ % Postgrex.Interval { months: 0 , days: 100 , secs: 0 , microsecs: 0 } ] ] =
970
+ query ( "SELECT $1::interval" , [ Duration . new! ( day: 100 ) ] )
971
+
972
+ assert [ [ % Postgrex.Interval { months: 0 , days: 0 , secs: 100 , microsecs: 0 } ] ] =
973
+ query ( "SELECT $1::interval" , [ Duration . new! ( second: 100 ) ] )
974
+
975
+ assert [ [ % Postgrex.Interval { months: 14 , days: 40 , secs: 10920 , microsecs: 0 } ] ] =
976
+ query ( "SELECT $1::interval" , [ Duration . new! ( month: 14 , day: 40 , second: 10920 ) ] )
977
+
978
+ assert [ [ % Postgrex.Interval { months: 14 , days: 40 , secs: 10921 , microsecs: 24000 } ] ] =
979
+ query ( "SELECT $1::interval" , [
980
+ Duration . new! ( month: 14 , day: 40 , second: 10920 , microsecond: { 1_024_000 , 0 } )
981
+ ] )
982
+
983
+ assert [ [ % Postgrex.Interval { months: 74 , days: 54 , secs: 46921 , microsecs: 24000 } ] ] =
984
+ query ( "SELECT $1::interval" , [
985
+ Duration . new! (
986
+ year: 5 ,
987
+ month: 14 ,
988
+ week: 2 ,
989
+ day: 40 ,
990
+ hour: 9 ,
991
+ minute: 60 ,
992
+ second: 10920 ,
993
+ microsecond: { 1_024_000 , 0 }
994
+ )
995
+ ] )
996
+ end
997
+ end
998
+
899
999
test "encode arrays" , context do
900
1000
assert [ [ [ ] ] ] = query ( "SELECT $1::integer[]" , [ [ ] ] )
901
1001
assert [ [ [ 1 ] ] ] = query ( "SELECT $1::integer[]" , [ [ 1 ] ] )
0 commit comments