/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb

http://github.com/flyerhzm/rails_best_practices · Ruby · 248 lines · 222 code · 25 blank · 1 comment · 21 complexity · 63d94e353fc929efd023f7059f0b267c MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. module RailsBestPractices
  4. module Reviews
  5. describe UseQueryAttributeReview do
  6. let(:runner) do
  7. Core::Runner.new(
  8. prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new], reviews: described_class.new
  9. )
  10. end
  11. before do
  12. content = <<-EOF
  13. class User < ActiveRecord::Base
  14. has_many :projects
  15. belongs_to :location
  16. has_one :phone
  17. belongs_to :category, class_name: 'IssueCategory', foreign_key: 'category_id'
  18. end
  19. EOF
  20. runner.prepare('app/models/user.rb', content)
  21. content = <<-EOF
  22. ActiveRecord::Schema.define(version: 20110216150853) do
  23. create_table "users", force => true do |t|
  24. t.string :login
  25. t.integer :age
  26. end
  27. end
  28. EOF
  29. runner.prepare('db/schema.rb', content)
  30. end
  31. it 'uses query attribute by blank call' do
  32. content = <<-EOF
  33. <% if @user.login.blank? %>
  34. <%= link_to 'login', new_session_path %>
  35. <% end %>
  36. EOF
  37. runner.review('app/views/users/show.html.erb', content)
  38. expect(runner.errors.size).to eq(1)
  39. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  40. end
  41. it 'uses query attribute by blank call with if in one line' do
  42. content = <<-EOF
  43. <%= link_to 'login', new_session_path if @user.login.blank? %>
  44. EOF
  45. runner.review('app/views/users/show.html.erb', content)
  46. expect(runner.errors.size).to eq(1)
  47. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  48. end
  49. it "uses query attribute by blank call with '? :'" do
  50. content = <<-EOF
  51. <%= @user.login.blank? ? link_to('login', new_session_path) : '' %>
  52. EOF
  53. runner.review('app/views/users/show.html.erb', content)
  54. expect(runner.errors.size).to eq(1)
  55. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  56. end
  57. it 'uses query attribute by comparing empty string' do
  58. content = <<-EOF
  59. <% if @user.login == "" %>
  60. <%= link_to 'login', new_session_path %>
  61. <% end %>
  62. EOF
  63. runner.review('app/views/users/show.html.erb', content)
  64. expect(runner.errors.size).to eq(1)
  65. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  66. end
  67. it 'uses query attribute by nil call' do
  68. content = <<-EOF
  69. <% if @user.login.nil? %>
  70. <%= link_to 'login', new_session_path %>
  71. <% end %>
  72. EOF
  73. runner.review('app/views/users/show.html.erb', content)
  74. expect(runner.errors.size).to eq(1)
  75. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  76. end
  77. it 'uses query attribute by present call' do
  78. content = <<-EOF
  79. <% if @user.login.present? %>
  80. <%= @user.login %>
  81. <% end %>
  82. EOF
  83. runner.review('app/views/users/show.html.erb', content)
  84. expect(runner.errors.size).to eq(1)
  85. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  86. end
  87. it 'uses query attribute within and conditions' do
  88. content = <<-EOF
  89. <% if @user.active? && @user.login.present? %>
  90. <%= @user.login %>
  91. <% end %>
  92. EOF
  93. runner.review('app/views/users/show.html.erb', content)
  94. expect(runner.errors.size).to eq(1)
  95. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  96. end
  97. it 'uses query attribute within or conditions' do
  98. content = <<-EOF
  99. <% if @user.active? or @user.login != "" %>
  100. <%= @user.login %>
  101. <% end %>
  102. EOF
  103. runner.review('app/views/users/show.html.erb', content)
  104. expect(runner.errors.size).to eq(1)
  105. expect(runner.errors[0].to_s).to eq('app/views/users/show.html.erb:1 - use query attribute (@user.login?)')
  106. end
  107. it 'does not use query attribute' do
  108. content = <<-EOF
  109. <% if @user.login? %>
  110. <%= @user.login %>
  111. <% end %>
  112. EOF
  113. runner.review('app/views/users/show.html.erb', content)
  114. expect(runner.errors.size).to eq(0)
  115. end
  116. it 'does not use query attribute for number' do
  117. content = <<-EOF
  118. <% unless @user.age.blank? %>
  119. <%= @user.age %>
  120. <% end %>
  121. EOF
  122. runner.review('app/views/users/show.html.erb', content)
  123. expect(runner.errors.size).to eq(0)
  124. end
  125. it 'does not review for pluralize attribute' do
  126. content = <<-EOF
  127. <% if @user.roles.blank? %>
  128. <%= @user.login %>
  129. <% end %>
  130. EOF
  131. runner.review('app/views/users/show.html.erb', content)
  132. expect(runner.errors.size).to eq(0)
  133. end
  134. it 'does not review non model class' do
  135. content = <<-EOF
  136. <% if @person.login.present? %>
  137. <%= @person.login %>
  138. <% end %>
  139. EOF
  140. runner.review('app/views/users/show.html.erb', content)
  141. expect(runner.errors.size).to eq(0)
  142. end
  143. context 'association' do
  144. it 'does not review belongs_to association' do
  145. content = <<-EOF
  146. <% if @user.location.present? %>
  147. <%= @user.location.name %>
  148. <% end %>
  149. EOF
  150. runner.review('app/views/users/show.html.erb', content)
  151. expect(runner.errors.size).to eq(0)
  152. end
  153. it 'does not review belongs_to category' do
  154. content = <<-EOF
  155. <% if @user.category.present? %>
  156. <%= @user.category.name %>
  157. <% end %>
  158. EOF
  159. runner.review('app/views/users/show.html.erb', content)
  160. expect(runner.errors.size).to eq(0)
  161. end
  162. it 'does not review has_one association' do
  163. content = <<-EOF
  164. <% if @user.phone.present? %>
  165. <%= @user.phone.number %>
  166. <% end %>
  167. EOF
  168. runner.review('app/views/users/show.html.erb', content)
  169. expect(runner.errors.size).to eq(0)
  170. end
  171. it 'does not review has_many association' do
  172. content = <<-EOF
  173. <% if @user.projects.present? %>
  174. <%= @user.projects.first.name %>
  175. <% end %>
  176. EOF
  177. runner.review('app/views/users/show.html.erb', content)
  178. expect(runner.errors.size).to eq(0)
  179. end
  180. end
  181. it 'does not review for class method' do
  182. content = <<-EOF
  183. <% if User.name.present? %>
  184. <%= User.name %>
  185. <% end %>
  186. EOF
  187. runner.review('app/views/users/show.html.erb', content)
  188. expect(runner.errors.size).to eq(0)
  189. end
  190. it 'does not review for non attribute call' do
  191. content = <<-EOF
  192. if @user.login(false).nil?
  193. puts @user.login(false)
  194. end
  195. EOF
  196. runner.review('app/models/users_controller.rb', content)
  197. expect(runner.errors.size).to eq(0)
  198. end
  199. it 'does not raise error for common conditional statement' do
  200. content = <<-EOF
  201. if voteable.is_a? Answer
  202. puts voteable.title
  203. end
  204. EOF
  205. expect { runner.review('app/models/users_controller.rb', content) }.not_to raise_error
  206. end
  207. it 'does not check ignored files' do
  208. runner =
  209. Core::Runner.new(
  210. prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new],
  211. reviews: described_class.new(ignored_files: %r{users/show})
  212. )
  213. content = <<-EOF
  214. <% if @user.login.blank? %>
  215. <%= link_to 'login', new_session_path %>
  216. <% end %>
  217. EOF
  218. runner.review('app/views/users/show.html.erb', content)
  219. expect(runner.errors.size).to eq(0)
  220. end
  221. end
  222. end
  223. end