test_table.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. require 'helper'
  2. module Arel
  3. describe Table do
  4. before do
  5. @relation = Table.new(:users)
  6. end
  7. it 'should create join nodes' do
  8. join = @relation.create_string_join 'foo'
  9. assert_kind_of Arel::Nodes::StringJoin, join
  10. assert_equal 'foo', join.left
  11. end
  12. it 'should create join nodes' do
  13. join = @relation.create_join 'foo', 'bar'
  14. assert_kind_of Arel::Nodes::InnerJoin, join
  15. assert_equal 'foo', join.left
  16. assert_equal 'bar', join.right
  17. end
  18. it 'should create join nodes with a klass' do
  19. join = @relation.create_join 'foo', 'bar', Arel::Nodes::OuterJoin
  20. assert_kind_of Arel::Nodes::OuterJoin, join
  21. assert_equal 'foo', join.left
  22. assert_equal 'bar', join.right
  23. end
  24. it 'should return an insert manager' do
  25. im = @relation.compile_insert 'VALUES(NULL)'
  26. assert_kind_of Arel::InsertManager, im
  27. assert_equal 'INSERT INTO NULL VALUES(NULL)', im.to_sql
  28. end
  29. it 'should return IM from insert_manager' do
  30. im = @relation.insert_manager
  31. assert_kind_of Arel::InsertManager, im
  32. assert_equal im.engine, @relation.engine
  33. end
  34. describe 'skip' do
  35. it 'should add an offset' do
  36. sm = @relation.skip 2
  37. sm.to_sql.must_be_like "SELECT FROM \"users\" OFFSET 2"
  38. end
  39. end
  40. describe 'select_manager' do
  41. it 'should return an empty select manager' do
  42. sm = @relation.select_manager
  43. sm.to_sql.must_be_like 'SELECT'
  44. end
  45. end
  46. describe 'having' do
  47. it 'adds a having clause' do
  48. mgr = @relation.having @relation[:id].eq(10)
  49. mgr.to_sql.must_be_like %{
  50. SELECT FROM "users" HAVING "users"."id" = 10
  51. }
  52. end
  53. end
  54. describe 'backwards compat' do
  55. describe 'join' do
  56. it 'noops on nil' do
  57. mgr = @relation.join nil
  58. mgr.to_sql.must_be_like %{ SELECT FROM "users" }
  59. end
  60. it 'takes a second argument for join type' do
  61. right = @relation.alias
  62. predicate = @relation[:id].eq(right[:id])
  63. mgr = @relation.join(right, Nodes::OuterJoin).on(predicate)
  64. mgr.to_sql.must_be_like %{
  65. SELECT FROM "users"
  66. LEFT OUTER JOIN "users" "users_2"
  67. ON "users"."id" = "users_2"."id"
  68. }
  69. end
  70. end
  71. end
  72. describe 'group' do
  73. it 'should create a group' do
  74. manager = @relation.group @relation[:id]
  75. manager.to_sql.must_be_like %{
  76. SELECT FROM "users" GROUP BY "users"."id"
  77. }
  78. end
  79. end
  80. describe 'alias' do
  81. it 'should create a node that proxies to a table' do
  82. @relation.aliases.must_equal []
  83. node = @relation.alias
  84. @relation.aliases.must_equal [node]
  85. node.name.must_equal 'users_2'
  86. node[:id].relation.must_equal node
  87. end
  88. end
  89. describe 'new' do
  90. it 'should accept an engine' do
  91. rel = Table.new :users, 'foo'
  92. rel.engine.must_equal 'foo'
  93. end
  94. it 'should accept a hash' do
  95. rel = Table.new :users, :engine => 'foo'
  96. rel.engine.must_equal 'foo'
  97. end
  98. it 'ignores as if it equals name' do
  99. rel = Table.new :users, :as => 'users'
  100. rel.table_alias.must_be_nil
  101. end
  102. end
  103. describe 'order' do
  104. it "should take an order" do
  105. manager = @relation.order "foo"
  106. manager.to_sql.must_be_like %{ SELECT FROM "users" ORDER BY foo }
  107. end
  108. end
  109. describe 'take' do
  110. it "should add a limit" do
  111. manager = @relation.take 1
  112. manager.project SqlLiteral.new '*'
  113. manager.to_sql.must_be_like %{ SELECT * FROM "users" LIMIT 1 }
  114. end
  115. end
  116. describe 'project' do
  117. it 'can project' do
  118. manager = @relation.project SqlLiteral.new '*'
  119. manager.to_sql.must_be_like %{ SELECT * FROM "users" }
  120. end
  121. it 'takes multiple parameters' do
  122. manager = @relation.project SqlLiteral.new('*'), SqlLiteral.new('*')
  123. manager.to_sql.must_be_like %{ SELECT *, * FROM "users" }
  124. end
  125. end
  126. describe 'where' do
  127. it "returns a tree manager" do
  128. manager = @relation.where @relation[:id].eq 1
  129. manager.project @relation[:id]
  130. manager.must_be_kind_of TreeManager
  131. manager.to_sql.must_be_like %{
  132. SELECT "users"."id"
  133. FROM "users"
  134. WHERE "users"."id" = 1
  135. }
  136. end
  137. end
  138. it "should have a name" do
  139. @relation.name.must_equal 'users'
  140. end
  141. it "should have a table name" do
  142. @relation.table_name.must_equal 'users'
  143. end
  144. it "should have an engine" do
  145. @relation.engine.must_equal Table.engine
  146. end
  147. describe '[]' do
  148. describe 'when given a Symbol' do
  149. it "manufactures an attribute if the symbol names an attribute within the relation" do
  150. column = @relation[:id]
  151. column.name.must_equal :id
  152. end
  153. end
  154. end
  155. end
  156. end