|
| 1 | +require 'spec_helper' |
| 2 | +require 'baby_squeel/dsl' |
| 3 | + |
| 4 | +describe BabySqueel::DSL do |
| 5 | + subject(:dsl) { BabySqueel::DSL.new(Post) } |
| 6 | + |
| 7 | + describe '#respond_to?' do |
| 8 | + it 'resolves attributes' do |
| 9 | + expect(dsl.respond_to?(:id)).to eq(true) |
| 10 | + end |
| 11 | + |
| 12 | + it 'resolves associations' do |
| 13 | + expect(dsl.respond_to?(:author)).to eq(true) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + describe '#method_missing' do |
| 18 | + it 'resolves attributes' do |
| 19 | + expect(dsl.id).to be_an(Arel::Attribute) |
| 20 | + end |
| 21 | + |
| 22 | + it 'resolves associations' do |
| 23 | + expect(dsl.author).to eq(Author.arel_table) |
| 24 | + end |
| 25 | + |
| 26 | + it 'does not resolve when arguments are given' do |
| 27 | + expect { dsl.id(:arg) }.to raise_error(NameError) |
| 28 | + end |
| 29 | + |
| 30 | + it 'does not resolve when a block is given' do |
| 31 | + expect { dsl.id { 'block' } }.to raise_error(NameError) |
| 32 | + end |
| 33 | + |
| 34 | + it 'does not resolve for non-existent columns' do |
| 35 | + expect { dsl.non_existent_column }.to raise_error(NameError) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe '#evaluate' do |
| 40 | + context 'when an arity is given' do |
| 41 | + it 'yields itself' do |
| 42 | + dsl.evaluate do |table| |
| 43 | + expect(table).to be_a(BabySqueel::DSL) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it 'does not change self' do |
| 48 | + this = self |
| 49 | + that = nil |
| 50 | + dsl.evaluate { |t| that = self } |
| 51 | + expect(that).to equal(this) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when no arity is given' do |
| 56 | + it 'changes self' do |
| 57 | + this = self |
| 58 | + that = nil |
| 59 | + dsl.evaluate { that = self } |
| 60 | + expect(that).not_to equal(this) |
| 61 | + end |
| 62 | + |
| 63 | + it 'resolves attributes without a receiver' do |
| 64 | + resolution = nil |
| 65 | + dsl.evaluate { resolution = title } |
| 66 | + expect(resolution).to eq(Post.arel_table[:title]) |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe '#[]' do |
| 72 | + it 'returns an arel attribute' do |
| 73 | + expect(dsl[:title]).to be_an(Arel::Attribute) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe '#func' do |
| 78 | + it 'constructs a named function' do |
| 79 | + expect(dsl.func(:coalesce, 0, 1).to_sql).to eq('coalesce(0, 1)') |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments