From 960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 30 Dec 2007 11:35:44 -0800 Subject: initial import --- spec/extensions/range_spec.rb | 1 + spec/predicates/binary_predicate_spec.rb | 27 ++++++++++ spec/predicates/equality_predicate_spec.rb | 25 +++++++++ .../relation_inclusion_predicate_spec.rb | 16 ++++++ spec/relations/attribute_spec.rb | 60 ++++++++++++++++++++++ spec/relations/join_operation_spec.rb | 29 +++++++++++ spec/relations/join_relation_spec.rb | 20 ++++++++ spec/relations/relation_spec.rb | 52 +++++++++++++++++++ spec/relations/table_relation_spec.rb | 7 +++ spec/spec_helper.rb | 3 ++ spec/sql/select_spec.rb | 19 +++++++ 11 files changed, 259 insertions(+) create mode 100644 spec/extensions/range_spec.rb create mode 100644 spec/predicates/binary_predicate_spec.rb create mode 100644 spec/predicates/equality_predicate_spec.rb create mode 100644 spec/predicates/relation_inclusion_predicate_spec.rb create mode 100644 spec/relations/attribute_spec.rb create mode 100644 spec/relations/join_operation_spec.rb create mode 100644 spec/relations/join_relation_spec.rb create mode 100644 spec/relations/relation_spec.rb create mode 100644 spec/relations/table_relation_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/sql/select_spec.rb (limited to 'spec') diff --git a/spec/extensions/range_spec.rb b/spec/extensions/range_spec.rb new file mode 100644 index 0000000000..26ca8978ca --- /dev/null +++ b/spec/extensions/range_spec.rb @@ -0,0 +1 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') diff --git a/spec/predicates/binary_predicate_spec.rb b/spec/predicates/binary_predicate_spec.rb new file mode 100644 index 0000000000..1be7dd067d --- /dev/null +++ b/spec/predicates/binary_predicate_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe BinaryPredicate do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @attribute1 = Attribute.new(@relation1, :attribute_name) + @attribute2 = Attribute.new(@relation2, :attribute_name) + end + + describe BinaryPredicate, '==' do + before do + class ConcreteBinaryPredicate < BinaryPredicate + end + end + + it "obtains if attribute1 and attribute2 are identical" do + BinaryPredicate.new(@attribute1, @attribute2).should == BinaryPredicate.new(@attribute1, @attribute2) + BinaryPredicate.new(@attribute1, @attribute2).should_not == BinaryPredicate.new(@attribute1, @attribute1) + end + + it "obtains if the concrete type of the BinaryPredicates are identical" do + ConcreteBinaryPredicate.new(@attribute1, @attribute2).should == ConcreteBinaryPredicate.new(@attribute1, @attribute2) + BinaryPredicate.new(@attribute1, @attribute2).should_not == ConcreteBinaryPredicate.new(@attribute1, @attribute2) + end + end +end \ No newline at end of file diff --git a/spec/predicates/equality_predicate_spec.rb b/spec/predicates/equality_predicate_spec.rb new file mode 100644 index 0000000000..18d3399193 --- /dev/null +++ b/spec/predicates/equality_predicate_spec.rb @@ -0,0 +1,25 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe EqualityPredicate do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @attribute1 = Attribute.new(@relation1, :attribute_name) + @attribute2 = Attribute.new(@relation2, :attribute_name) + end + + describe EqualityPredicate, '==' do + it "obtains if attribute1 and attribute2 are identical" do + EqualityPredicate.new(@attribute1, @attribute2).should == EqualityPredicate.new(@attribute1, @attribute2) + EqualityPredicate.new(@attribute1, @attribute2).should_not == EqualityPredicate.new(@attribute1, @attribute1) + end + + it "obtains if the concrete type of the predicates are identical" do + EqualityPredicate.new(@attribute1, @attribute2).should_not == BinaryPredicate.new(@attribute1, @attribute2) + end + + it "is commutative on the attributes" do + EqualityPredicate.new(@attribute1, @attribute2).should == EqualityPredicate.new(@attribute2, @attribute1) + end + end +end \ No newline at end of file diff --git a/spec/predicates/relation_inclusion_predicate_spec.rb b/spec/predicates/relation_inclusion_predicate_spec.rb new file mode 100644 index 0000000000..6cd37fafa8 --- /dev/null +++ b/spec/predicates/relation_inclusion_predicate_spec.rb @@ -0,0 +1,16 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe RelationInclusionPredicate do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @attribute = @relation1[:baz] + end + + describe RelationInclusionPredicate, '==' do + it "obtains if attribute1 and attribute2 are identical" do + RelationInclusionPredicate.new(@attribute, @relation1).should == RelationInclusionPredicate.new(@attribute, @relation1) + RelationInclusionPredicate.new(@attribute, @relation1).should_not == RelationInclusionPredicate.new(@attribute, @relation2) + end + end +end \ No newline at end of file diff --git a/spec/relations/attribute_spec.rb b/spec/relations/attribute_spec.rb new file mode 100644 index 0000000000..5f2d70ec48 --- /dev/null +++ b/spec/relations/attribute_spec.rb @@ -0,0 +1,60 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Attribute do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + end + + describe Attribute, '#eql?' do + it "obtains if the relation and attribute name are identical" do + Attribute.new(@relation1, :attribute_name).should be_eql(Attribute.new(@relation1, :attribute_name)) + Attribute.new(@relation1, :attribute_name).should_not be_eql(Attribute.new(@relation1, :another_attribute_name)) + Attribute.new(@relation1, :attribute_name).should_not be_eql(Attribute.new(@relation2, :attribute_name)) + end + end + + describe Attribute, 'predications' do + before do + @attribute1 = Attribute.new(@relation1, :attribute_name) + @attribute2 = Attribute.new(@relation2, :attribute_name) + end + + describe Attribute, '==' do + it "manufactures an equality predicate" do + (@attribute1 == @attribute2).should == EqualityPredicate.new(@attribute1, @attribute2) + end + end + + describe Attribute, '<' do + it "manufactures a less-than predicate" do + (@attribute1 < @attribute2).should == LessThanPredicate.new(@attribute1, @attribute2) + end + end + + describe Attribute, '<=' do + it "manufactures a less-than or equal-to predicate" do + (@attribute1 <= @attribute2).should == LessThanOrEqualToPredicate.new(@attribute1, @attribute2) + end + end + + describe Attribute, '>' do + it "manufactures a greater-than predicate" do + (@attribute1 > @attribute2).should == GreaterThanPredicate.new(@attribute1, @attribute2) + end + end + + describe Attribute, '>=' do + it "manufactures a greater-than or equal to predicate" do + (@attribute1 >= @attribute2).should == GreaterThanOrEqualToPredicate.new(@attribute1, @attribute2) + end + end + + describe Attribute, '=~' do + it "manufactures a match predicate" do + (@attribute1 =~ /.*/).should == MatchPredicate.new(@attribute1, @attribute2) + end + end + + end +end diff --git a/spec/relations/join_operation_spec.rb b/spec/relations/join_operation_spec.rb new file mode 100644 index 0000000000..13e50e057e --- /dev/null +++ b/spec/relations/join_operation_spec.rb @@ -0,0 +1,29 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe JoinOperation, 'between two relations' do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + end + + describe JoinOperation, '==' do + it "obtains if the relations of both joins are identical" do + JoinOperation.new(@relation1, @relation2).should == JoinOperation.new(@relation1, @relation2) + JoinOperation.new(@relation1, @relation2).should_not == JoinOperation.new(@relation1, @relation1) + end + + it "is commutative on the relations" do + JoinOperation.new(@relation1, @relation2).should == JoinOperation.new(@relation2, @relation1) + end + end + + describe JoinOperation, 'on' do + before do + @predicate = Predicate.new + end + + it "manufactures a JoinRelation" do + JoinOperation.new(@relation1, @relation2).on(@predicate).should == JoinRelation.new(@relation1, @relation2, @predicate) + end + end +end \ No newline at end of file diff --git a/spec/relations/join_relation_spec.rb b/spec/relations/join_relation_spec.rb new file mode 100644 index 0000000000..7309563cd5 --- /dev/null +++ b/spec/relations/join_relation_spec.rb @@ -0,0 +1,20 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe JoinRelation, 'between two relations' do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @predicate = Predicate.new + end + + describe JoinRelation, '==' do + it "obtains if the two relations and the predicate are identical" do + JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation1, @relation2, @predicate) + JoinRelation.new(@relation1, @relation2, @predicate).should_not == JoinRelation.new(@relation1, @relation1, @predicate) + end + + it "is commutative on the relations" do + JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation2, @relation1, @predicate) + end + end +end \ No newline at end of file diff --git a/spec/relations/relation_spec.rb b/spec/relations/relation_spec.rb new file mode 100644 index 0000000000..c482eb4a01 --- /dev/null +++ b/spec/relations/relation_spec.rb @@ -0,0 +1,52 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Relation do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + end + + describe Relation, '*' do + it "manufactures a JoinOperation between those two relations" do + (@relation1 * @relation2).should == JoinOperation.new(@relation1, @relation2) + end + end + + describe Relation, 'attributes' do + end + + describe Relation, '[]' do + it "manufactures a attribute" do + @relation1[:id].should be_eql(Attribute.new(@relation1, :id)) + end + + it "raises an error if the named attribute is not part of the relation" do + end + end + + describe Relation, 'include?' do + before do + @attribute = Attribute.new(@relation1, :id) + end + + it "manufactures an inclusion predicate" do + @relation1.include?(@attribute).should == RelationInclusionPredicate.new(@attribute, @relation1) + end + end + + describe Relation, 'project' do + before do + @attribute1 = Attribute.new(@relation1, :id) + @attribute2 = Attribute.new(@relation1, :name) + end + + it "only allows projecting attributes in the relation" do + end + + it "collapses identical projections" do + end + end + + describe Relation, 'select' do + end +end \ No newline at end of file diff --git a/spec/relations/table_relation_spec.rb b/spec/relations/table_relation_spec.rb new file mode 100644 index 0000000000..dd86f83294 --- /dev/null +++ b/spec/relations/table_relation_spec.rb @@ -0,0 +1,7 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe TableRelation, '#to_sql' do + it "returns a simple SELECT query" do + TableRelation.new(:users).to_sql.should == Select.new(:*).from(:users) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000000..30c1f65f57 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,3 @@ +require 'rubygems' +require 'spec' +require File.join(File.dirname(__FILE__), '..', 'lib', 'sql_algebra') \ No newline at end of file diff --git a/spec/sql/select_spec.rb b/spec/sql/select_spec.rb new file mode 100644 index 0000000000..7d40a20a5b --- /dev/null +++ b/spec/sql/select_spec.rb @@ -0,0 +1,19 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe Select, '==' do + it "obtains for queries with identical attributes" do + Select.new(:foo).should == Select.new(:foo) + Select.new(:bar).should_not == Select.new(:foo) + end + + it "obtains for queries with identical tables" do + Select.new(:foo).from(:bar).should == Select.new(:foo).from(:bar) + Select.new(:foo).from(:bar).should_not == Select.new(:foo).from(:foo) + end + + it "obtains for queries with identical predicates" do + Select.new(:foo).from(:bar).where(:baz).should == Select.new(:foo).from(:bar).where(:baz) + Select.new(:foo).from(:bar).where(:baz).should_not == Select.new(:foo).from(:bar).where(:foo) + end + +end \ No newline at end of file -- cgit v1.2.3