aboutsummaryrefslogtreecommitdiffstats
path: root/spec/engines/sql/unit/engine_spec.rb
blob: 85a9dc5bfb104e2c4f2c03c1988b1838823b7ad7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'spec_helper'

module Arel
  FakeAR = Struct.new(:connection)
  class FakeConnection < Struct.new :called
    def initialize c = []; super; end

    def method_missing name, *args, &block
      called << [name, args, block]
    end
  end

  describe Sql::Engine do
    before do
      @users = Table.new(:users)
      @users.delete
    end

    describe "method missing" do
      it "should pass through" do
        conn = FakeConnection.new
        engine = Arel::Sql::Engine.new FakeAR.new conn
        engine.foo
        conn.called.should == [[:foo, [], nil]]
      end

      it "should ask for a connection" do
        conn   = FakeConnection.new
        ar     = FakeAR.new conn
        engine = Arel::Sql::Engine.new ar

        ar.connection = nil
        lambda { engine.foo }.should raise_error
      end
    end

    describe "CRUD" do
      describe "#create" do
        it "inserts into the relation" do
          @users.insert @users[:name] => "Bryan"
          @users.first[@users[:name]].should == "Bryan"
        end
      end

      describe "#read" do
        it "reads from the relation" do
          @users.insert @users[:name] => "Bryan"

          @users.each do |row|
            row[@users[:name]].should == "Bryan"
          end
        end
      end

      describe "#update" do
        it "updates the relation" do
          @users.insert @users[:name] => "Nick"
          @users.update @users[:name] => "Bryan"
          @users.first[@users[:name]].should == "Bryan"
        end
      end

      describe "#delete" do
        it "deletes from the relation" do
          @users.insert @users[:name] => "Bryan"
          @users.delete
          @users.first.should == nil
        end
      end
    end
  end
end