-
Notifications
You must be signed in to change notification settings - Fork 2
Add worker tests #22
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
Open
Ricoch
wants to merge
17
commits into
master
Choose a base branch
from
tests/worker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add worker tests #22
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b05e8ae
Add .rspec file
Ricoch f8d47be
Add timecop
Ricoch 126307c
Configure local csv file path
Ricoch 7b56b77
Setup spec_helper
Ricoch d9f8101
Add user
Ricoch 884585f
Update gitignore
Ricoch f39b23d
Add file fixtures
Ricoch 7cfaa57
Add successful worker tests
Ricoch 5401d2b
Fix rubocop issues
Ricoch 66d829c
Replace timecop with activesupport test helpers
Ricoch 032d724
Fix deprecation warning on update_attributes
Ricoch ba857ef
Update csv folder location and name
Ricoch 9207902
Remove ActiveStorage::Blob.service from spec_helper
Ricoch e98fd6f
Fix tests
Ricoch b8a5017
Move pry requirement
Ricoch 62bca0d
Fix code analysis
Ricoch 88bf10c
Remove unnecesary db configuration for tests
Ricoch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| Gemfile.lock | ||
| *.gem | ||
| .rspec_status | ||
| /coverage/ | ||
| /spec/dummy/log/ | ||
| /spec/dummy/tmp/ | ||
| /spec/dummy/csv/ | ||
| /spec/dummy/db/development.sqlite3 | ||
| /spec/dummy/db/test.sqlite3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --format documentation | ||
| --color | ||
| --require spec_helper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class CreateUser < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :users do |t| | ||
| t.string :username, null: false | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # This file is auto-generated from the current state of the database. Instead | ||
| # of editing this file, please use the migrations feature of Active Record to | ||
| # incrementally modify your database, and then regenerate this schema definition. | ||
| # | ||
| # Note that this schema.rb definition is the authoritative source for your | ||
| # database schema. If you need to create the application database on another | ||
| # system, you should be using db:schema:load, not running all the migrations | ||
| # from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
| # you'll amass, the slower it'll run and the greater likelihood for issues). | ||
| # | ||
| # It's strongly recommended that you check this file into your version control system. | ||
|
|
||
| ActiveRecord::Schema.define(version: 2020_09_25_172213) do | ||
|
|
||
| create_table "admin_reports", force: :cascade do |t| | ||
| t.integer "author_id" | ||
| t.string "entity", null: false | ||
| t.string "format", default: "csv", null: false | ||
| t.integer "status", null: false | ||
| t.string "location_url" | ||
| t.datetime "created_at", null: false | ||
| t.datetime "updated_at", null: false | ||
| t.index ["author_id"], name: "index_admin_reports_on_author_id" | ||
| end | ||
|
|
||
| create_table "users", force: :cascade do |t| | ||
| t.string "username", null: false | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| test_block | ||
| The username is: user_1 | ||
| The username is: user_2 | ||
| The username is: user_3 | ||
| The username is: AdminUser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| username | ||
| user_1 | ||
| user_2 | ||
| user_3 | ||
| AdminUser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| username,test_block | ||
| user_1,The username is: user_1 | ||
| user_2,The username is: user_2 | ||
| user_3,The username is: user_3 | ||
| AdminUser,The username is: AdminUser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'active_support/testing/time_helpers' | ||
|
|
||
| RSpec.configure do |config| | ||
| config.include ActiveSupport::Testing::TimeHelpers | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| RSpec.describe ActiveAdmin::AsyncExporter::Worker do | ||
| let!(:users) do | ||
| %w[user_1 user_2 user_3].map do |username| | ||
| User.find_or_create_by!(username: username) | ||
| end | ||
| end | ||
| let(:controller) { Admin::UsersController.new } | ||
| let(:decorate_model) { false } | ||
| let(:file_name) { 'file_name' } | ||
| let(:query) { nil } | ||
| let(:csv_path) { Rails.root.join('tmp/activeadmin-async_exporter') } | ||
| let(:admin) { User.find_or_create_by!(username: 'AdminUser') } | ||
| let(:csv_fixture_file_path) { "./spec/fixtures/files/#{csv_fixture_file_name}.csv" } | ||
| let(:csv_fixture_file_name) { 'username' } | ||
|
|
||
| let(:admin_report) do | ||
| AdminReport.create!(author_id: admin.id, | ||
| entity: User.name, | ||
| status: :pending) | ||
| end | ||
|
|
||
| let(:columns) { { username: { block: false, value: 'username' } } } | ||
|
|
||
| let(:options) do | ||
| { | ||
| admin_report_id: admin_report.id, | ||
| controller: controller.class.name, | ||
| columns: columns, | ||
| decorate_model: decorate_model, | ||
| file_name: file_name, | ||
| query: query | ||
| } | ||
| end | ||
|
|
||
| subject do | ||
| described_class.perform_now(options) | ||
| end | ||
|
|
||
| context 'with valid options' do | ||
| let(:test_date) { Time.current } | ||
| let(:full_file_name) { [file_name, test_date.to_i].join('_') + '.csv' } | ||
| let(:file_path) { "#{csv_path}/#{full_file_name}" } | ||
|
|
||
| before do | ||
| travel_to(test_date) | ||
| end | ||
|
|
||
| after do | ||
| FileUtils.rm(file_path) | ||
| travel_back | ||
| end | ||
|
|
||
| shared_examples 'creates the physical file with data' do | ||
| it 'creates the physical file at the given location_url' do | ||
| expect { File.open(admin_report.location_url.to_s) }.to raise_exception(Errno::ENOENT) | ||
| subject | ||
| expect(File.open(admin_report.reload.location_url)).to be | ||
Ricoch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| it 'physical path is the expected' do | ||
| subject | ||
| expect(admin_report.reload.location_url).to eq(file_path) | ||
| end | ||
|
|
||
| it 'physical file contains data' do | ||
| subject | ||
| expect(File.open(admin_report.reload.location_url).read).to be | ||
| end | ||
|
|
||
| it 'generated csv file contains the expected results' do | ||
| subject | ||
|
|
||
| generated_file_data = File.open(admin_report.reload.location_url).read | ||
|
|
||
| expect(File.open(csv_fixture_file_path).read).to eq(generated_file_data) | ||
| end | ||
| end | ||
|
|
||
| it 'changes admin_report status to ready' do | ||
| expect { subject }.to change { admin_report.reload.status }.from('pending').to('ready') | ||
| end | ||
|
|
||
| it 'changes admin_report location_url' do | ||
| expect { subject }.to change { admin_report.reload.location_url }.from(nil).to(file_path) | ||
| end | ||
|
|
||
| context 'when the file_name is given' do | ||
| it 'full file name is the correct file name' do | ||
| subject | ||
| expect(admin_report.reload.location_url.split('/').last).to eq(full_file_name) | ||
| end | ||
|
|
||
| it_behaves_like 'creates the physical file with data' | ||
| end | ||
|
|
||
| context 'when the file_name is not given' do | ||
| let(:file_name) { nil } | ||
| let(:full_file_name) do | ||
| [controller.current_collection.name.pluralize.downcase, test_date.to_i].join('_') + '.csv' | ||
| end | ||
|
|
||
| it 'full file name is the correct file name' do | ||
| subject | ||
| expect(admin_report.reload.location_url.split('/').last).to eq(full_file_name) | ||
| end | ||
|
|
||
| it_behaves_like 'creates the physical file with data' | ||
| end | ||
|
|
||
| context 'when the column is an attribute' do | ||
| it_behaves_like 'creates the physical file with data' | ||
| end | ||
|
|
||
| context 'when the column is a block' do | ||
| let(:block) do | ||
| proc { |user| "The username is: #{user.username}" } | ||
| end | ||
| let(:csv_fixture_file_name) { 'block' } | ||
| let(:columns) { { test_block: { block: true, value: block.source } } } | ||
|
|
||
| it_behaves_like 'creates the physical file with data' | ||
| end | ||
|
|
||
| context 'when we have multiple columns' do | ||
| let(:block) do | ||
| proc { |user| "The username is: #{user.username}" } | ||
| end | ||
|
|
||
| let(:csv_fixture_file_name) { 'username_and_block' } | ||
| let(:columns) do | ||
| { | ||
| username: { block: false, value: 'username' }, | ||
| test_block: { block: true, value: block.source } | ||
| } | ||
| end | ||
|
|
||
| it_behaves_like 'creates the physical file with data' | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
maybe it's a good idea to add
expect(File.open(admin_report.reload.location_url)).to_not bebefore running thesubjectto make sure that the subject is actually creating it. Same for the other testsUh oh!
There was an error while loading. Please reload this page.
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.
Cannot do
File.openof a file that does not exist. I can try to do something likeexpect { File.open(admin_report.location_url.to_s) }.to raise_exception(Errno::ENOENT), wdyt?