diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-16 16:14:02 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-16 16:14:02 -0700 |
commit | af3d7c5193eee90ad17b1ba8aaf5d76a2874c0a5 (patch) | |
tree | c3eaf1cfdc0ed1f1d977937fb04848566e964c32 /spec | |
parent | ed9c8d4d828fe4e28c1e37ed8921acbee54450f2 (diff) | |
download | rails-af3d7c5193eee90ad17b1ba8aaf5d76a2874c0a5.tar.gz rails-af3d7c5193eee90ad17b1ba8aaf5d76a2874c0a5.tar.bz2 rails-af3d7c5193eee90ad17b1ba8aaf5d76a2874c0a5.zip |
formatting insert and update statements
- values need to be coerced to the type corresponding to the column
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/unit/predicates/binary_spec.rb | 22 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/insertion_spec.rb | 51 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/update_spec.rb | 61 |
3 files changed, 104 insertions, 30 deletions
diff --git a/spec/active_relation/unit/predicates/binary_spec.rb b/spec/active_relation/unit/predicates/binary_spec.rb index 62d894b0fa..b63472a836 100644 --- a/spec/active_relation/unit/predicates/binary_spec.rb +++ b/spec/active_relation/unit/predicates/binary_spec.rb @@ -54,19 +54,25 @@ module ActiveRelation end describe 'when relating to an array' do - describe 'when given an arry of elements of the same type of the attribute' do - it 'manufactures sql with a list' do - array = [1, 2, 3] - ConcreteBinary.new(@attribute1, array.bind(@relation)).to_sql.should be_like(" + describe 'when the array\'s elements are the same type as the attribute' do + before do + @array = [1, 2, 3] + end + + it 'manufactures sql with a comma separated list' do + ConcreteBinary.new(@attribute1, @array.bind(@relation)).to_sql.should be_like(" `users`.`id` <=> (1, 2, 3) ") end end - describe 'when given an array, the elements of which are not the same type as the attribute' do - it 'formats values in the array in the type of the attribute' do - array = ['1-asdf', 2, 3] - ConcreteBinary.new(@attribute1, array.bind(@relation)).to_sql.should be_like(" + describe 'when the array\'s elements are not same type as the attribute' do + before do + @array = ['1-asdf', 2, 3] + end + + it 'formats values in the array as the type of the attribute' do + ConcreteBinary.new(@attribute1, @array.bind(@relation)).to_sql.should be_like(" `users`.`id` <=> (1, 2, 3) ") end diff --git a/spec/active_relation/unit/relations/insertion_spec.rb b/spec/active_relation/unit/relations/insertion_spec.rb index f081743c50..b74ec6c7cb 100644 --- a/spec/active_relation/unit/relations/insertion_spec.rb +++ b/spec/active_relation/unit/relations/insertion_spec.rb @@ -4,20 +4,57 @@ module ActiveRelation describe Insertion do before do @relation = Table.new(:users) - @insertion = Insertion.new(@relation, @relation[:name] => "nick".bind(@relation)) end describe '#to_sql' do - it 'manufactures sql inserting the data for one item' do - @insertion.to_sql.should be_like(" - INSERT - INTO `users` - (`users`.`name`) VALUES ('nick') - ") + describe 'when given values whose types correspond to the types of the attributes' do + before do + @insertion = Insertion.new(@relation, @relation[:name] => "nick".bind(@relation)) + end + + it 'manufactures sql inserting data' do + @insertion.to_sql.should be_like(" + INSERT + INTO `users` + (`users`.`name`) VALUES ('nick') + ") + end + end + + describe 'when given values whose types differ from from the types of the attributes' do + before do + @insertion = Insertion.new(@relation, @relation[:id] => '1-asdf'.bind(@relation)) + end + + it 'manufactures sql inserting data' do + @insertion.to_sql.should be_like(" + INSERT + INTO `users` + (`users`.`id`) VALUES (1) + ") + end + end + + describe 'when given values whose types correspond to the type of the attribtues' do + before do + @insertion = Insertion.new(@relation, @relation[:name] => "nick".bind(@relation)) + end + + it 'manufactures sql inserting data' do + @insertion.to_sql.should be_like(" + INSERT + INTO `users` + (`users`.`name`) VALUES ('nick') + ") + end end end describe '#call' do + before do + @insertion = Insertion.new(@relation, @relation[:name] => "nick".bind(@relation)) + end + it 'executes an insert on the connection' do mock(connection = Object.new).insert(@insertion.to_sql) @insertion.call(connection) diff --git a/spec/active_relation/unit/relations/update_spec.rb b/spec/active_relation/unit/relations/update_spec.rb index 848760de83..485c86372d 100644 --- a/spec/active_relation/unit/relations/update_spec.rb +++ b/spec/active_relation/unit/relations/update_spec.rb @@ -7,27 +7,58 @@ module ActiveRelation end describe '#to_sql' do - it 'manufactures sql updating attributes' do - Update.new(@relation, @relation[:name] => "nick".bind(@relation)).to_sql.should be_like(" - UPDATE `users` - SET `users`.`name` = 'nick' - ") + describe 'when given values whose types correspond to the types of the attributes' do + before do + @update = Update.new(@relation, @relation[:name] => "nick".bind(@relation)) + end + + it 'manufactures sql updating attributes' do + @update.to_sql.should be_like(" + UPDATE `users` + SET `users`.`name` = 'nick' + ") + end end - - it 'manufactures sql updating a selection relation' do - Update.new(@relation.select(@relation[:id].equals(1)), @relation[:name] => "nick".bind(@relation)).to_sql.should be_like(" - UPDATE `users` - SET `users`.`name` = 'nick' - WHERE `users`.`id` = 1 - ") + + describe 'when given values whose types differ from from the types of the attributes' do + before do + @update = Update.new(@relation, @relation[:id] => '1-asdf'.bind(@relation)) + end + + it 'manufactures sql updating attributes' do + @update.to_sql.should be_like(" + UPDATE `users` + SET `users`.`id` = 1 + ") + end + end + + describe 'when the relation is a selection' do + before do + @update = Update.new( + @relation.select(@relation[:id].equals(1)), + @relation[:name] => "nick".bind(@relation) + ) + end + + it 'manufactures sql updating a selection relation' do + @update.to_sql.should be_like(" + UPDATE `users` + SET `users`.`name` = 'nick' + WHERE `users`.`id` = 1 + ") + end end end describe '#call' do + before do + @update = Update.new(@relation, @relation[:name] => "nick".bind(@relation)) + end + it 'executes an update on the connection' do - update = Update.new(@relation, @relation[:name] => "nick".bind(@relation)) - mock(connection = Object.new).update(update.to_sql) - update.call(connection) + mock(connection = Object.new).update(@update.to_sql) + @update.call(connection) end end |