spec/lib/qreport/connection_spec.rb in qreport-0.0.7 vs spec/lib/qreport/connection_spec.rb in qreport-0.0.8

- old
+ new

@@ -84,33 +84,75 @@ conn.conn_owned.should be_false conn.close conn.instance_variable_get('@conn').should == nil end - describe "#escape_value, #unescape_value" do + describe "#unescape_value" do + it "should not alter undefined types" do + conn.unescape_value(123, :UNDEFINED1).should == 123 + conn.unescape_value("str", :UNDEFINED1).should == "str" + conn.unescape_value(:sym, :UNDEFINED1).should == :sym + end + + it "should handle boolean" do + conn.unescape_value("t", 'boolean').should == true + conn.unescape_value("f", 'boolean').should == false + conn.unescape_value(true, 'boolean').should == true + conn.unescape_value(false, 'boolean').should == false + end + + it "should handle floats" do + conn.unescape_value(123, 'float').should == 123 + conn.unescape_value("123.45", 'float').should == 123.45 + conn.unescape_value(123.45, 'float').should == 123.45 + conn.unescape_value("123.45", 'double precision').should == 123.45 + end + + it "should handle defined types" do + conn.unescape_value_funcs = { 'money' => lambda { | val, type | [ val ] } } + conn.unescape_value("123.00", 'money').should == [ "123.00" ] + end + end + + describe "#escape_value/#unescape_value" do [ [ nil, 'NULL' ], [ true, "'t'::boolean" ], [ false, "'f'::boolean" ], [ 1234, '1234' ], [ -1234, '-1234' ], [ 1234.45, '1234.45' ], + [ :IGNORE, '1234.56::float', 1234.56 ], + [ :IGNORE, '1234.56::float4', 1234.56 ], + [ :IGNORE, '1234.56::float8', 1234.56 ], [ "string with \", \\, and \'", "'string with \", \\, and '''" ], [ :a_symbol!, "'a_symbol!'", :a_symbol!.to_s ], [ Time.parse('2011-04-27T13:23:00.000000Z'), "'2011-04-27T13:23:00.000000Z'::timestamp", Time.parse('2011-04-27T13:23:00.000000') ], [ Time.parse('2011-04-27 13:23:00 -0500'), "'2011-04-27T13:23:00.000000-05:00'::timestamp", Time.parse('2011-04-27 13:23:00 -0500') ], + [ :IGNORE, "'13:23'::time", '13:23:00' ], [ [ 1, "2", :three ], "'[1,\"2\",\"three\"]'", :IGNORE ], [ { :a => 1, "b" => 2 }, "'{\"a\":1,\"b\":2}'", :IGNORE ], - ].each do | value, sql, return_value | + ].each do | value, sql, return_value, sql_expr, sql_value | + if value != :IGNORE it "can handle encoding #{value.class.name} value #{value.inspect} as #{sql.inspect}." do conn.escape_value(value).should == sql end - it "can handle decoding #{value.class.name} value #{value.inspect}." do - pending :if => return_value == :IGNORE - sql_x = conn.escape_value(value) - r = conn.run "SELECT #{sql_x}" + end + + sql_value = return_value + sql_value = nil if sql_value == :IGNORE + sql_value ||= value + if return_value != :IGNORE + it "can handle decoding #{sql.inspect} as #{sql_value.inspect}." do + sql_x = sql # conn.escape_value(sql) + r = conn.run %Q{SELECT #{sql_x} AS "value"} + # PP.pp r.columns + # PP.pp r.ftypes + # PP.pp r.fmods r = r.rows.first.values.first - r.should == (return_value || value) + r.should == sql_value + r.class.should == sql_value.class + end end end it "raises TypeError for other values." do lambda do conn.escape_value(Object.new)