PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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