test/unit/relation_spy_tests.rb in ardb-0.28.3 vs test/unit/relation_spy_tests.rb in ardb-0.29.0

- old
+ new

@@ -1,10 +1,9 @@ -require 'assert' -require 'ardb/relation_spy' +require "assert" +require "ardb/relation_spy" class Ardb::RelationSpy - class UnitTests < Assert::Context desc "Ardb::RelationSpy" setup do @relation_spy = Ardb::RelationSpy.new end @@ -49,15 +48,15 @@ subject.select :column_a assert_equal other_relation, subject end should "build a fake sql string for its applied expressions using `to_sql`" do - subject.select 'column' - subject.from 'table' - subject.joins 'my_table.my_column ON my_table.my_column = table.column' + subject.select "column" + subject.from "table" + subject.joins "my_table.my_column ON my_table.my_column = table.column" - expected = subject.applied.map(&:to_sql).join(', ') + expected = subject.applied.map(&:to_sql).join(", ") assert_equal expected, subject.to_sql end should "be able to be reset" do Factory.integer(3).times.each{ subject.applied << Factory.string } @@ -68,11 +67,10 @@ assert_equal [], subject.applied assert_equal [], subject.results assert_nil subject.limit_value assert_nil subject.offset_value end - end class SelectTests < UnitTests desc "select" setup do @@ -83,11 +81,10 @@ should "add a select applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :select, @applied.type assert_equal [ :column_a, :column_b ], @applied.args end - end class FromTests < UnitTests desc "from" setup do @@ -98,11 +95,10 @@ should "add a from applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :from, @applied.type assert_equal [ "some SQL" ], @applied.args end - end class IncludesTests < UnitTests desc "includes" setup do @@ -113,11 +109,10 @@ should "add an includes applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :includes, @applied.type assert_equal [ :table_a, :table_b ], @applied.args end - end class JoinsTests < UnitTests desc "joins" setup do @@ -128,26 +123,24 @@ should "add a joins applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :joins, @applied.type assert_equal [ :table_a, :table_b ], @applied.args end - end class WhereTests < UnitTests desc "where" setup do - @relation_spy.where :column_a => 'some value' + @relation_spy.where :column_a => "some value" @applied = subject.applied.first end should "add a where applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :where, @applied.type - assert_equal [ { :column_a => 'some value' } ], @applied.args + assert_equal [ { :column_a => "some value" } ], @applied.args end - end class OrderTests < UnitTests desc "order" setup do @@ -158,11 +151,10 @@ should "add an order applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :order, @applied.type assert_equal [ :column_a, :column_b ], @applied.args end - end class ReverseOrderTests < UnitTests desc "reverse_order" setup do @@ -172,11 +164,10 @@ should "add a reverse order applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :reverse_order, @applied.type end - end class GroupTests < UnitTests desc "group" setup do @@ -187,26 +178,24 @@ should "add a group applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :group, @applied.type assert_equal [ :column_a, :column_b ], @applied.args end - end class HavingTests < UnitTests desc "having" setup do - @relation_spy.having 'COUNT(column_a) > 0' + @relation_spy.having "COUNT(column_a) > 0" @applied = subject.applied.first end should "add a having applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :having, @applied.type - assert_equal [ 'COUNT(column_a) > 0' ], @applied.args + assert_equal [ "COUNT(column_a) > 0" ], @applied.args end - end class ReadonlyTests < UnitTests desc "readonly" setup do @@ -217,11 +206,10 @@ should "add a readonly applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :readonly, @applied.type assert_equal [ true ], @applied.args end - end class LimitTests < UnitTests desc "limit" setup do @@ -236,11 +224,10 @@ end should "set it's limit value" do assert_equal 100, subject.limit_value end - end class OffsetTests < UnitTests desc "offset" setup do @@ -255,62 +242,58 @@ end should "set it's offset value" do assert_equal 100, subject.offset_value end - end class MergeWithARelationSpyTests < UnitTests desc "merge with a relation spy" setup do - @other_relation_spy = Ardb::RelationSpy.new.select('column').joins('table') + @other_relation_spy = Ardb::RelationSpy.new.select("column").joins("table") @relation_spy.merge @other_relation_spy end should "apply another relation's applied expressions using `merge`" do @other_relation_spy.applied.each do |applied| assert_includes applied, @relation_spy.applied end end - end class MergeWithNonRelationSpyTests < UnitTests desc "merge without a relation spy" setup do - @fake_relation = 'relation' + @fake_relation = "relation" @relation_spy.merge @fake_relation @applied = subject.applied.first end should "add a merge applied expression with the passed args" do assert_instance_of AppliedExpression, @applied assert_equal :merge, @applied.type assert_equal [ @fake_relation ], @applied.args end - end class MergeWithSelfTests < UnitTests desc "merge with itself" setup do - @fake_relation = 'relation' + @fake_relation = "relation" @relation_spy.merge @relation_spy end should "not alter the applied expressions" do assert_empty subject.applied end - end class WithExpressionsTests < UnitTests setup do - @relation_spy.select('column').includes('table').joins('table') - @relation_spy.where(:column => 'value').order('column') - @relation_spy.group('column').having('count(*) > 1') + @relation_spy.select("column").includes("table").joins("table") + @relation_spy.where(:column => "value").order("column") + @relation_spy.group("column").having("count(*) > 1") @relation_spy.limit(1).offset(1) end end class ExceptTests < WithExpressionsTests @@ -343,11 +326,10 @@ relation_spy = subject.except(:select) assert_not_nil relation_spy.offset_value relation_spy = subject.except(:offset) assert_nil relation_spy.offset_value end - end class OnlyTests < WithExpressionsTests desc "only" @@ -378,11 +360,10 @@ relation_spy = subject.only(:offset) assert_not_nil relation_spy.offset_value relation_spy = subject.only(:select) assert_nil relation_spy.offset_value end - end class WithResultsTests < UnitTests setup do @results = [*1..5].map{ |id| Result.new(id) } @@ -399,15 +380,13 @@ end should "raise a not found error if a result can't be found" do assert_raises(NotFoundError){ subject.find(1000) } end - end class FirstTests < WithResultsTests - should "return the first item from `all` using `first`" do assert_equal subject.all.first, subject.first subject.offset 2 assert_equal subject.all.first, subject.first end @@ -416,15 +395,13 @@ "raise an exception if `all` is empty using `first!`" do assert_equal subject.all.first, subject.first! subject.limit 0 assert_raises(NotFoundError){ subject.first! } end - end class LastTests < WithResultsTests - should "return the last item from `all` using `last`" do assert_equal subject.all.last, subject.last subject.limit 2 assert_equal subject.all.last, subject.last end @@ -433,11 +410,10 @@ "raise an exception if `all` is empty using `last!`" do assert_equal subject.all.last, subject.last! subject.limit 0 assert_raises(NotFoundError){ subject.last! } end - end class AllTests < WithResultsTests desc "all" @@ -456,22 +432,20 @@ subject.limit 2 subject.offset 2 assert_equal @results[2, 2], subject.all end - end class CountTests < WithResultsTests desc "count" should "return the size of `all`" do assert_equal subject.all.size, subject.count subject.limit 2 assert_equal subject.all.size, subject.count end - end class PluckTests < WithResultsTests desc "pluck" setup do @@ -482,28 +456,25 @@ should "return a pluck value for every result" do exp = [@column_value] * @results.size assert_equal exp, @relation_spy.pluck(@column_name) end - end class AppliedExpressionTests < UnitTests desc "AppliedExpression" setup do - @applied_expression = AppliedExpression.new(:select, 'column') + @applied_expression = AppliedExpression.new(:select, "column") end subject{ @applied_expression } should have_readers :type, :args should have_imeths :to_sql should "return a string representing the expression using `to_sql`" do expected = "#{subject.type}: #{subject.args.inspect}" assert_equal expected, subject.to_sql end - end Result = Struct.new(:id) - end