From e13420c86afb5c31e90cff800f121bd49255b939 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 12 Mar 2010 14:46:37 -0800 Subject: We're obviously writing specs for arel. No need for a sub directory. --- spec/engines/memory/unit/relations/array_spec.rb | 32 ++++++++++++++++++ spec/engines/memory/unit/relations/insert_spec.rb | 28 ++++++++++++++++ spec/engines/memory/unit/relations/join_spec.rb | 31 +++++++++++++++++ spec/engines/memory/unit/relations/order_spec.rb | 27 +++++++++++++++ spec/engines/memory/unit/relations/project_spec.rb | 27 +++++++++++++++ spec/engines/memory/unit/relations/skip_spec.rb | 26 +++++++++++++++ spec/engines/memory/unit/relations/take_spec.rb | 26 +++++++++++++++ spec/engines/memory/unit/relations/where_spec.rb | 39 ++++++++++++++++++++++ 8 files changed, 236 insertions(+) create mode 100644 spec/engines/memory/unit/relations/array_spec.rb create mode 100644 spec/engines/memory/unit/relations/insert_spec.rb create mode 100644 spec/engines/memory/unit/relations/join_spec.rb create mode 100644 spec/engines/memory/unit/relations/order_spec.rb create mode 100644 spec/engines/memory/unit/relations/project_spec.rb create mode 100644 spec/engines/memory/unit/relations/skip_spec.rb create mode 100644 spec/engines/memory/unit/relations/take_spec.rb create mode 100644 spec/engines/memory/unit/relations/where_spec.rb (limited to 'spec/engines/memory/unit/relations') 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 -- cgit v1.2.3