-
Notifications
You must be signed in to change notification settings - Fork 0
[#9] [Backend] As a user, I can upload a CSV file, [#12] [UI] As a user, I can upload a CSV file #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Release - 0.1.0
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>
Release/0.2.0
| render :new, locals: { search_stat: search_stat } | ||
| end | ||
|
|
||
| def create |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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. 💭
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
| 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] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
Closes
What happened 👀
User can visit
/search_stats/newto upload a CSV file.Insight 📝
Maximum 1,000 keywords per file.
Proof Of Work 📹