Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented May 30, 2023

Closes

What happened 👀

User can visit /search_stats/new to upload a CSV file.

Insight 📝

Maximum 1,000 keywords per file.

Proof Of Work 📹

Screenshot 2023-06-01 at 18-45-24 GoogleScrapperRuby

mosharaf13 and others added 29 commits May 22, 2023 17:15
Co-authored-by: Long Nguyen <nguyenduclong@msn.com>
Co-authored-by: Sang Huynh Thanh <63148598+sanG-github@users.noreply.github.com>
Co-authored-by: Sang Huynh Thanh <63148598+sanG-github@users.noreply.github.com>
Co-authored-by: Sang Huynh Thanh <63148598+sanG-github@users.noreply.github.com>
Co-authored-by: Sang Huynh Thanh <63148598+sanG-github@users.noreply.github.com>
@ghost ghost self-assigned this May 30, 2023
@ghost ghost changed the base branch from main to backend/list-keywords June 1, 2023 09:50
@ghost ghost marked this pull request as ready for review June 1, 2023 11:41
@ghost ghost requested review from longnd, mosharaf13 and sanG-github as code owners June 1, 2023 11:41
@ghost ghost added the feature label Jun 1, 2023
render :new, locals: { search_stat: search_stat }
end

def create
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Assignment Branch Condition size for create is too high. [<5, 21, 6> 22.41/17]

render :new, locals: { search_stat: search_stat }
end

def create
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Method has too many lines. [11/10]

Copy link
Contributor

@longnd longnd Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@topnimble don't just skip the warnings from the linter, the checks are imposed for a reason

render :new, locals: { search_stat: search_stat }
end

def create
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Has approx 10 statements

end

def create
csv_file = params[:csv_file]
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Calls 'params[:csv_file]' 2 times


raise 'Invalid file type' unless csv_file.content_type == ALLOWED_MIME_TYPE

csv_file_content = params[:csv_file].read
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Calls 'params[:csv_file]' 2 times

RSpec.describe 'Search Stats', type: :request do
describe 'POST #create' do
context 'given a valid file' do
it 'inserts keywords to the database and redirects to search stat index' do
Copy link

@github-actions github-actions bot Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Example has too many lines. [6/5]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the rule of this one, many tests will take more than 5 lines. 💭

@ghost ghost added the @0.3.0 label Jun 1, 2023
@ghost ghost added this to the 0.3.0 milestone Jun 1, 2023
Base automatically changed from backend/list-keywords to develop June 2, 2023 10:05
Comment on lines +21 to +29
csv_file = params[:csv_file]

raise 'Invalid file type' unless csv_file.content_type == ALLOWED_MIME_TYPE

csv_file_content = params[:csv_file].read
keywords = CSV.parse(csv_file_content).flatten.compact_blank

raise 'Invalid file data' unless keywords.any?
raise 'Too many keywords' unless keywords.count <= MAXIMUM_KEYWORDS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file validation is not the reponsibility of the controller, it usually handled in the model.
In this case, we can use create a form to handle both validation and parsing the file.
https://nimblehq.co/compass/development/code-conventions/ruby/ruby-on-rails/#forms

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, the validations here can be extracted to a separated validator to used in the form
https://nimblehq.co/compass/development/code-conventions/ruby/ruby-on-rails/#validators

render :new, locals: { search_stat: search_stat }
end

def create
Copy link
Contributor

@longnd longnd Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@topnimble don't just skip the warnings from the linter, the checks are imposed for a reason

end
end

context 'given too many keywords' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how many is too many? Let's be specific

Suggested change
context 'given too many keywords' do
context 'Given that the file contains more keywords than allowed' do

end

def create
csv_file = params[:csv_file]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Long suggested, we should have a dedicated from/model/service to handle the uploaded file.


class SearchStat < ApplicationRecord
validates :keyword, presence: true
validates :raw_response, presence: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please point me out why we remove this validation? 🤔

@@ -0,0 +1,3 @@
1st keyword
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the invalid type, we can specify it when attaching the file to the Fabricator instead of using a dedicated file. 😉
But it's an additional option, you can use which way is more convenient.

Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'files', 'keywords.csv'), 'plain/text')

RSpec.describe 'Search Stats', type: :request do
describe 'POST #create' do
context 'given a valid file' do
it 'inserts keywords to the database and redirects to search stat index' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the rule of this one, many tests will take more than 5 lines. 💭


expect { post search_stats_path, params: params }.to raise_error(RuntimeError, 'Invalid file data')

expect(SearchStat.all.count).to eq(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With count, no need for .all anymore. (ref)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants