aboutsummaryrefslogtreecommitdiffstats
path: root/spec/engines/memory
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2010-03-12 14:46:37 -0800
committerCarl Lerche <carllerche@mac.com>2010-03-12 14:46:37 -0800
commite13420c86afb5c31e90cff800f121bd49255b939 (patch)
tree7089d188061b2a676143add52227e107a5cf9b45 /spec/engines/memory
parent0b8b87fb947a746d4e58d11ea73ef20cfb23f576 (diff)
downloadrails-e13420c86afb5c31e90cff800f121bd49255b939.tar.gz
rails-e13420c86afb5c31e90cff800f121bd49255b939.tar.bz2
rails-e13420c86afb5c31e90cff800f121bd49255b939.zip
We're obviously writing specs for arel. No need for a sub directory.
Diffstat (limited to 'spec/engines/memory')
-rw-r--r--spec/engines/memory/integration/joins/cross_engine_spec.rb52
-rw-r--r--spec/engines/memory/unit/relations/array_spec.rb32
-rw-r--r--spec/engines/memory/unit/relations/insert_spec.rb28
-rw-r--r--spec/engines/memory/unit/relations/join_spec.rb31
-rw-r--r--spec/engines/memory/unit/relations/order_spec.rb27
-rw-r--r--spec/engines/memory/unit/relations/project_spec.rb27
-rw-r--r--spec/engines/memory/unit/relations/skip_spec.rb26
-rw-r--r--spec/engines/memory/unit/relations/take_spec.rb26
-rw-r--r--spec/engines/memory/unit/relations/where_spec.rb39
9 files changed, 288 insertions, 0 deletions
diff --git a/spec/engines/memory/integration/joins/cross_engine_spec.rb b/spec/engines/memory/integration/joins/cross_engine_spec.rb
new file mode 100644
index 0000000000..606f3154c7
--- /dev/null
+++ b/spec/engines/memory/integration/joins/cross_engine_spec.rb
@@ -0,0 +1,52 @@
+require 'spec_helper'
+
+module Arel
+ describe Join do
+ before do
+ @users = Array.new([
+ [1, 'bryan' ],
+ [2, 'emilio' ],
+ [3, 'nick']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ @photos = Table.new(:photos)
+ @photos.delete
+ @photos.insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6)
+ @photos.insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42)
+ # Oracle adapter returns database integers as Ruby integers and not strings
+ @adapter_returns_integer = false
+ adapter_is :oracle do
+ @adapter_returns_integer = true
+ end
+ end
+
+ describe 'when the in memory relation is on the left' do
+ it 'joins across engines' do
+ @users \
+ .join(@photos) \
+ .on(@users[:id].eq(@photos[:user_id])) \
+ .project(@users[:name], @photos[:camera_id]) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, ['bryan', @adapter_returns_integer ? 6 : '6']),
+ Row.new(relation, ['emilio', @adapter_returns_integer ? 42 : '42'])
+ ]
+ end
+ end
+ end
+
+ describe 'when the in memory relation is on the right' do
+ it 'joins across engines' do
+ @photos \
+ .join(@users) \
+ .on(@users[:id].eq(@photos[:user_id])) \
+ .project(@users[:name], @photos[:camera_id]) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, ['bryan', @adapter_returns_integer ? 6 : '6']),
+ Row.new(relation, ['emilio', @adapter_returns_integer ? 42 : '42'])
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/array_spec.rb b/spec/engines/memory/unit/relations/array_spec.rb
new file mode 100644
index 0000000000..dcec2afa19
--- /dev/null
+++ b/spec/engines/memory/unit/relations/array_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+module Arel
+ describe Array do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#attributes' do
+ it 'manufactures attributes corresponding to the names given on construction' do
+ @relation.attributes.should == [
+ Attribute.new(@relation, :id),
+ Attribute.new(@relation, :name)
+ ]
+ end
+ end
+
+ describe '#call' do
+ it "manufactures an array of hashes of attributes to values" do
+ @relation.call.should == [
+ Row.new(@relation, [1, 'duck']),
+ Row.new(@relation, [2, 'duck']),
+ Row.new(@relation, [3, 'goose'])
+ ]
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/insert_spec.rb b/spec/engines/memory/unit/relations/insert_spec.rb
new file mode 100644
index 0000000000..987e708e0b
--- /dev/null
+++ b/spec/engines/memory/unit/relations/insert_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+module Arel
+ describe Insert do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it "manufactures an array of hashes of attributes to values" do
+ @relation \
+ .insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \
+ do |relation|
+ relation.should == [
+ Row.new(relation, [1, 'duck']),
+ Row.new(relation, [2, 'duck']),
+ Row.new(relation, [3, 'goose']),
+ Row.new(relation, [4, 'guinea fowl'])
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/join_spec.rb b/spec/engines/memory/unit/relations/join_spec.rb
new file mode 100644
index 0000000000..ed5fe89ef0
--- /dev/null
+++ b/spec/engines/memory/unit/relations/join_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+module Arel
+ describe Join do
+ before do
+ @relation1 = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ @relation2 = @relation1.alias
+ end
+
+ describe InnerJoin do
+ describe '#call' do
+ it 'combines the two tables where the predicate obtains' do
+ @relation1 \
+ .join(@relation2) \
+ .on(@relation1[:id].eq(@relation2[:id])) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [1, 'duck', 1, 'duck' ]),
+ Row.new(relation, [2, 'duck', 2, 'duck' ]),
+ Row.new(relation, [3, 'goose', 3, 'goose'])
+ ]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/order_spec.rb b/spec/engines/memory/unit/relations/order_spec.rb
new file mode 100644
index 0000000000..9546449bfb
--- /dev/null
+++ b/spec/engines/memory/unit/relations/order_spec.rb
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+module Arel
+ describe Order do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it 'sorts the relation with the provided ordering' do
+ @relation \
+ .order(@relation[:id].desc) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [3, 'goose']),
+ Row.new(relation, [2, 'duck' ]),
+ Row.new(relation, [1, 'duck' ])
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/project_spec.rb b/spec/engines/memory/unit/relations/project_spec.rb
new file mode 100644
index 0000000000..92ed9fa74b
--- /dev/null
+++ b/spec/engines/memory/unit/relations/project_spec.rb
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+module Arel
+ describe Project do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it 'retains only the attributes that are provided' do
+ @relation \
+ .project(@relation[:id]) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [1]),
+ Row.new(relation, [2]),
+ Row.new(relation, [3])
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/skip_spec.rb b/spec/engines/memory/unit/relations/skip_spec.rb
new file mode 100644
index 0000000000..089db24cea
--- /dev/null
+++ b/spec/engines/memory/unit/relations/skip_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+module Arel
+ describe Skip do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it 'removes the first n rows' do
+ @relation \
+ .skip(1) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [2, 'duck']),
+ Row.new(relation, [3, 'goose']),
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/take_spec.rb b/spec/engines/memory/unit/relations/take_spec.rb
new file mode 100644
index 0000000000..16b99872c5
--- /dev/null
+++ b/spec/engines/memory/unit/relations/take_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+module Arel
+ describe Take do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it 'removes the rows after the first n' do
+ @relation \
+ .take(2) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [1, 'duck']),
+ Row.new(relation, [2, 'duck']),
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/engines/memory/unit/relations/where_spec.rb b/spec/engines/memory/unit/relations/where_spec.rb
new file mode 100644
index 0000000000..b45c009d83
--- /dev/null
+++ b/spec/engines/memory/unit/relations/where_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+module Arel
+ describe Where do
+ before do
+ @relation = Array.new([
+ [1, 'duck' ],
+ [2, 'duck' ],
+ [3, 'goose']
+ ], [[:id, Attributes::Integer], [:name, Attributes::String]])
+ end
+
+ describe '#call' do
+ it 'filters the relation with the provided predicate' do
+ @relation \
+ .where(@relation[:id].lt(3)) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [1, 'duck']),
+ Row.new(relation, [2, 'duck']),
+ ]
+ end
+ end
+
+ describe 'when filtering a where relation' do
+ it 'further filters the already-filtered relation with the provided predicate' do
+ @relation \
+ .where(@relation[:id].gt(1)) \
+ .where(@relation[:id].lt(3)) \
+ .let do |relation|
+ relation.call.should == [
+ Row.new(relation, [2, 'duck'])
+ ]
+ end
+ end
+ end
+ end
+ end
+end