Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ Naming/FileName:
Style/EvalWithLocation:
Exclude:
- lib/active_admin/async_exporter/reports/worker.rb

Style/StringLiterals:
Exclude:
- spec/dummy/db/schema.rb

Style/NumericLiterals:
Exclude:
- spec/dummy/db/schema.rb

Layout/EmptyLinesAroundBlockBody:
Exclude:
- spec/dummy/db/schema.rb
2 changes: 1 addition & 1 deletion activeadmin-async_exporter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Gem::Specification.new do |s|
s.add_dependency 'rails', '>= 1.0.0'

# Development dependencies
s.add_development_dependency 'pry-rails', '~> 0.3.9'
s.add_development_dependency 'rspec', '~> 3.9.0'
s.add_development_dependency 'rubocop-rails', '~> 2.3'
s.add_development_dependency 'rubocop-rootstrap', '~> 0.1.0'
s.add_development_dependency 'simplecov', '~> 0.17.1'
s.add_development_dependency 'sqlite3', '1.4.1'
s.add_development_dependency 'pry-rails'
end
2 changes: 1 addition & 1 deletion lib/active_admin/async_exporter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Config
def initialize
@aws_bucket_name = nil
@aws_bucket_folder_path = nil
@disk_folder_path = nil
@disk_folder_path = Rails.root.join('tmp/activeadmin-async_exporter')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/async_exporter/reports/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def perform(options = {})
end

file = Services::StorageService.call(path: path, name: file_name).store
AdminReport.find(options[:admin_report_id]).update_attributes!(
AdminReport.find(options[:admin_report_id]).update!(
status: :ready,
location_url: file.url
)
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'action_view/railtie'
require 'sprockets/railtie'
require 'active_storage/engine'
require 'pry'

Bundler.require(*Rails.groups)

Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/db/migrate/20200925172213_create_user.rb
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
30 changes: 30 additions & 0 deletions spec/dummy/db/schema.rb
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
5 changes: 5 additions & 0 deletions spec/fixtures/files/block.csv
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
5 changes: 5 additions & 0 deletions spec/fixtures/files/username.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
username
user_1
user_2
user_3
AdminUser
5 changes: 5 additions & 0 deletions spec/fixtures/files/username_and_block.csv
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
13 changes: 11 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'activeadmin-async_exporter'
ENV['RAILS_ENV'] ||= 'test'
require 'dummy/config/environment.rb'
require 'active_admin'
require 'support/time_helpers.rb'

require 'simplecov'
SimpleCov.start

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

require 'tmpdir'

require 'simplecov'
SimpleCov.start
Expand Down
5 changes: 5 additions & 0 deletions spec/support/time_helpers.rb
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
140 changes: 140 additions & 0 deletions spec/worker_spec.rb
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
Copy link
Member

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 be before running the subject to make sure that the subject is actually creating it. Same for the other tests

Copy link
Contributor Author

@Ricoch Ricoch Oct 16, 2020

Choose a reason for hiding this comment

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

Cannot do File.open of a file that does not exist. I can try to do something like expect { File.open(admin_report.location_url.to_s) }.to raise_exception(Errno::ENOENT), wdyt?

expect(File.open(admin_report.reload.location_url)).to be
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