@@ -956,13 +956,75 @@ def expect_deprecated_coder_init
956956
957957 expect ( binaryenc_text_array . encode ( [ [ [ 5 , 6 ] ] , [ [ "6\" " , 7 ] ] , [ [ nil , 5 ] ] ] ) ) . to eq ( exp )
958958 end
959+
960+ let! ( :binaryenc_array_array ) { PG ::BinaryEncoder ::Array . new elements_type : PG ::BinaryEncoder ::Array . new ( elements_type : PG ::BinaryEncoder ::Int4 . new ( oid : 0x17 ) , dimensions : 1 ) , dimensions : 2 }
961+
962+ it 'encodes an array in an array of int4' do
963+ exp = [ "00000002" + "00000001" + "00000000" +
964+ "00000003" + "00000001" + "00000001" + "00000001" +
965+
966+ "00000024" +
967+ "00000001" + "00000001" + "00000017" +
968+ "00000002" + "00000001" +
969+ "00000004" + "00000005" +
970+ "00000004" + "00000006" +
971+
972+ "00000024" +
973+ "00000001" + "00000001" + "00000017" +
974+ "00000002" + "00000001" +
975+ "00000004" + "00000006" +
976+ "00000004" + "00000007" +
977+
978+ "00000020" +
979+ "00000001" + "00000001" + "00000017" +
980+ "00000002" + "00000001" +
981+ "ffffffff" +
982+ "00000004" + "00000005"
983+ ] . pack ( "H*" )
984+
985+ expect ( binaryenc_array_array . encode ( [ [ [ 5 , 6 ] ] , [ [ 6 , 7 ] ] , [ [ nil , 5 ] ] ] ) ) . to eq ( exp )
986+ end
959987 end
960988
961989 context 'two dimensional arrays' do
962990 it 'encodes an array of timestamps with sub arrays' do
963991 expect ( textenc_timestamp_array . encode ( [ Time . new ( 2014 , 12 , 31 ) , [ nil , Time . new ( 2016 , 01 , 02 , 23 , 23 , 59.99 ) ] ] ) ) .
964992 to eq ( %[{2014-12-31 00:00:00.000000000,{NULL,2016-01-02 23:23:59.990000000}}] )
965993 end
994+
995+ context 'with dimensions' do
996+ let! ( :textenc_array_2dim ) { textenc_string_array . dup . tap { |a | a . dimensions = 2 } }
997+ let! ( :binaryenc_array_2dim ) { binaryenc_array . dup . tap { |a | a . dimensions = 2 } }
998+
999+ it 'encodes binary int array' do
1000+ binaryenc_array_2dim . encode ( [ [ 1 ] ] )
1001+ end
1002+ it 'encodes text int array' do
1003+ expect ( textenc_array_2dim . encode ( [ [ 1 ] ] ) ) . to eq ( "{{1}}" )
1004+ end
1005+ it 'encodes empty array' do
1006+ binaryenc_array_2dim . encode ( [ [ ] ] )
1007+ end
1008+ it 'encodes text empty array' do
1009+ expect ( textenc_array_2dim . encode ( [ [ ] ] ) ) . to eq ( "{{}}" )
1010+ end
1011+ it 'raises an error on 1 dim binary array input to int4' do
1012+ expect { binaryenc_array_2dim . encode ( [ 1 ] ) } . to raise_error ( ArgumentError , /less array dimensions.*1.*2/ )
1013+ end
1014+ it 'raises an error on 1 dim text array input to int4' do
1015+ expect { textenc_array_2dim . encode ( [ 1 ] ) } . to raise_error ( ArgumentError , /less array dimensions.*1.*2/ )
1016+ end
1017+
1018+ it 'raises an error on 0 dim array input to int4' do
1019+ expect { binaryenc_array_2dim . encode ( [ ] ) } . to raise_error ( ArgumentError , /less array dimensions.*0.*2/ )
1020+ end
1021+ it 'raises an error on 0 dim text array input to int4' do
1022+ expect { textenc_array_2dim . encode ( [ ] ) } . to raise_error ( ArgumentError , /less array dimensions.*1.*2/ )
1023+ end
1024+ it 'raises an error on 1 dim text array nil input' do
1025+ expect { textenc_array_2dim . encode ( [ nil ] ) } . to raise_error ( ArgumentError , /less array dimensions.*1.*2/ )
1026+ end
1027+ end
9661028 end
9671029
9681030 context 'one dimensional array' do
@@ -986,6 +1048,37 @@ def expect_deprecated_coder_init
9861048
9871049 expect ( binaryenc_array . encode ( [ nil , "6\" " ] ) ) . to eq ( exp )
9881050 end
1051+
1052+ context 'with dimensions' do
1053+ let! ( :textenc_array_1dim ) { textenc_int_array . dup . tap { |a | a . dimensions = 1 } }
1054+ let! ( :binaryenc_array_1dim ) { binaryenc_array . dup . tap { |a | a . dimensions = 1 } }
1055+
1056+ it 'encodes an array' do
1057+ exp = [ "00000001" + "00000001" + "00000000" +
1058+ "00000002" + "00000001" +
1059+ "ffffffff" +
1060+ "00000002" + "3622"
1061+ ] . pack ( "H*" )
1062+
1063+ expect ( binaryenc_array_1dim . encode ( [ nil , "6\" " ] ) ) . to eq ( exp )
1064+ end
1065+ it 'encodes an empty binary array' do
1066+ exp = [ "00000000" + "00000001" + "00000000"
1067+ ] . pack ( "H*" )
1068+ expect ( binaryenc_array_1dim . encode ( [ ] ) ) . to eq ( exp )
1069+ end
1070+ it 'encodes an empty text array' do
1071+ expect ( textenc_array_1dim . encode ( [ ] ) ) . to eq ( "{}" )
1072+ end
1073+
1074+ let! ( :binaryenc_int4_array_1dim ) { PG ::BinaryEncoder ::Array . new elements_type : PG ::BinaryEncoder ::Int4 . new , dimensions : 1 }
1075+ it 'raises an error on binary array input to int4' do
1076+ expect { binaryenc_int4_array_1dim . encode ( [ [ 1 ] ] ) } . to raise_error ( NoMethodError , /to_i/ )
1077+ end
1078+ it 'raises an error on text array input to int4' do
1079+ expect { textenc_array_1dim . encode ( [ [ 1 ] ] ) } . to raise_error ( NoMethodError , /to_i/ )
1080+ end
1081+ end
9891082 end
9901083
9911084 context 'other dimensional array' do
@@ -1091,7 +1184,8 @@ def expect_deprecated_coder_init
10911184 it "should respond to to_h" do
10921185 expect ( textenc_int_array . to_h ) . to eq ( {
10931186 name : nil , oid : 0 , format : 0 , flags : 0 ,
1094- elements_type : textenc_int , needs_quotation : false , delimiter : ','
1187+ elements_type : textenc_int , needs_quotation : false , delimiter : ',' ,
1188+ dimensions : nil
10951189 } )
10961190 end
10971191
@@ -1107,6 +1201,7 @@ def expect_deprecated_coder_init
11071201 expect ( t . needs_quotation? ) . to eq ( true )
11081202 expect ( t . delimiter ) . to eq ( ',' )
11091203 expect ( t . elements_type ) . to be_nil
1204+ expect ( t . dimensions ) . to be_nil
11101205 end
11111206
11121207 it "should deny changes when frozen" do
@@ -1117,6 +1212,7 @@ def expect_deprecated_coder_init
11171212 expect { t . needs_quotation = true } . to raise_error ( FrozenError )
11181213 expect { t . delimiter = "," } . to raise_error ( FrozenError )
11191214 expect { t . elements_type = nil } . to raise_error ( FrozenError )
1215+ expect { t . dimensions = 1 } . to raise_error ( FrozenError )
11201216 end
11211217
11221218 it "should be shareable for Ractor" , :ractor do
0 commit comments