[#9] [Backend] As a user, I can upload a CSV file, [#12] [UI] As a user, I can upload a CSV file#39
[#9] [Backend] As a user, I can upload a CSV file, [#12] [UI] As a user, I can upload a CSV file#39
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
| 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.
| Example has too many lines. [6/5] |
There was a problem hiding this comment.
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.
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.
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.
@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.
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.
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.
Could you please point me out why we remove this validation? 🤔
| @@ -0,0 +1,3 @@ | |||
| 1st keyword | |||
There was a problem hiding this comment.
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.
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) |
Closes
What happened 👀
User can visit
/search_stats/newto upload a CSV file.Insight 📝
Maximum 1,000 keywords per file.
Proof Of Work 📹