File: joiner_spec.rb

package info (click to toggle)
ruby-joiner 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 204 kB
  • sloc: ruby: 303; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,473 bytes parent folder | download | duplicates (5)
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
73
74
75
76
77
require 'spec_helper'

describe 'Joiner' do
  it "handles has many associations" do
    joiner = Joiner::Joins.new User
    joiner.add_join_to [:articles]

    sql = User.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"articles\"/)
  end

  it "handles multiple has many associations separately" do
    joiner = Joiner::Joins.new User
    joiner.add_join_to [:articles]
    joiner.add_join_to [:articles, :comments]

    sql = User.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"articles\"/)
    expect(sql).to match(/LEFT OUTER JOIN \"comments\"/)
  end

  it "handles multiple has many associations together" do
    joiner = Joiner::Joins.new User
    joiner.add_join_to [:articles, :comments]

    sql = User.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"articles\"/)
    expect(sql).to match(/LEFT OUTER JOIN \"comments\"/)
  end

  it "handles a belongs to association" do
    joiner = Joiner::Joins.new Comment
    joiner.add_join_to [:article]

    sql = Comment.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"articles\"/)
  end

  it "handles both belongs to and has many associations separately" do
    joiner = Joiner::Joins.new Article
    joiner.add_join_to [:user]
    joiner.add_join_to [:comments]

    sql = Article.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"users\"/)
    expect(sql).to match(/LEFT OUTER JOIN \"comments\"/)
  end

  it "handles both belongs to and has many associations together" do
    joiner = Joiner::Joins.new Article
    joiner.add_join_to [:user, :comments]

    sql = Article.joins(joiner.join_values).to_sql
    expect(sql).to match(/LEFT OUTER JOIN \"users\"/)
    expect(sql).to match(/LEFT OUTER JOIN \"comments\"/)
  end

  it "distinguishes joins via different relationships" do
    joiner = Joiner::Joins.new Article
    joiner.add_join_to [:comments]
    joiner.add_join_to [:user, :comments]

    expect(joiner.alias_for([:comments])).to eq('comments')
    expect(joiner.alias_for([:user, :comments])).to eq('comments_users')
  end

  it 'handles simple and deep chains' do
    joiner = Joiner::Joins.new Article
    joiner.add_join_to [:comments]
    joiner.add_join_to [:comments, :user, :articles]

    expect(joiner.alias_for([:comments])).to eq('comments')
    expect(joiner.alias_for([:comments, :user, :articles])).to eq(
      'articles_users'
    )
  end
end