spec/db_spec.rb in knjrbfw-0.0.103 vs spec/db_spec.rb in knjrbfw-0.0.104

- old
+ new

@@ -40,11 +40,13 @@ schema = { "tables" => { "test_table" => { "columns" => [ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true}, - {"name" => "name", "type" => "varchar"} + {"name" => "name", "type" => "varchar"}, + {"name" => "age", "type" => "int"}, + {"name" => "nickname", "type" => "varchar"} ], "indexes" => [ "name" ], "rows" => [ @@ -96,10 +98,29 @@ end raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count + #Test upserting. + data = {:name => "Kasper Johansen"} + sel = {:nickname => "kaspernj"} + + table = db.tables[:test_table] + table.reload + rows_count = table.rows_count + + db.upsert(:test_table, sel, data) + + table.reload + table.rows_count.should eql(rows_count + 1) + + db.upsert(:test_table, sel, data) + + table.reload + table.rows_count.should eql(rows_count + 1) + + #Test dumping. dump = Knj::Db::Dump.new(:db => db, :debug => false) str_io = StringIO.new dump.dump(str_io) str_io.rewind @@ -156,7 +177,30 @@ raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?("new_name") #Delete test-database if everything went well. File.unlink(db_path) if File.exists?(db_path) + end + + it "should generate proper sql" do + require "knj/db" + require "knj/os" + require "rubygems" + require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby" + + db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3" + File.unlink(db_path) if File.exists?(db_path) + + db = Knj::Db.new( + :type => "sqlite3", + :path => db_path, + :return_keys => "symbols", + :index_append_table_name => true + ) + + time = Time.new(1985, 6, 17, 10, 30) + db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')") + + date = Date.new(1985, 6, 17) + db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')") end end \ No newline at end of file