Skip to content
Snippets Groups Projects
Verified Commit d04b051d authored by Alexis Reigel's avatar Alexis Reigel
Browse files

convert test to spec syntax

parent 4d59b234
No related branches found
No related tags found
1 merge request!6Convert test to spec syntax
...@@ -13,90 +13,111 @@ class User < ActiveRecord::Base ...@@ -13,90 +13,111 @@ class User < ActiveRecord::Base
validates :first_name, presence: true validates :first_name, presence: true
end end
class ExcelsiorTest < Minitest::Test describe Excelsior do
def setup before do
User.delete_all
@import = UserImport.new @import = UserImport.new
end end
def test_that_it_has_a_version_number it 'has a version number' do
refute_nil ::Excelsior::VERSION refute_nil ::Excelsior::VERSION
end end
def test_class_level_source describe 'source' do
import = UserImport.new it 'allows setting the source on the class' do
assert_equal "test/files/complete.xlsx", import.source import = UserImport.new
end assert_equal "test/files/complete.xlsx", import.source
end
def test_instance_level_source it 'allows setting the source on an instance level' do
import = UserImport.new("test/files/missing-column.xlsx") import = UserImport.new("test/files/missing-column.xlsx")
assert_equal "test/files/missing-column.xlsx", import.source assert_equal "test/files/missing-column.xlsx", import.source
end
end end
def test_import_rows describe '#rows' do
assert_equal 2, @import.rows.length it 'returns the correct number of rows' do
assert_equal 2, @import.rows.length
end
end end
def test_import_columns describe '#columns' do
assert_equal 3, @import.columns.length it 'returns the columns as defined in the excel' do
assert_equal "E-Mail", @import.columns[0] assert_equal 3, @import.columns.length
assert_equal "Vorname", @import.columns[1] assert_equal "E-Mail", @import.columns[0]
assert_equal "Nachname", @import.columns[2] assert_equal "Vorname", @import.columns[1]
assert_equal "Nachname", @import.columns[2]
end
end end
def test_mapping describe '#fields' do
assert_equal @import.fields, [ it 'returns the mapped fields' do
{ attribute: :first_name, header: "Vorname" }, assert_equal @import.fields, [
{ attribute: :last_name, header: "Nachname" }, { attribute: :first_name, header: "Vorname" },
{ attribute: :email, header: "E-Mail" } { attribute: :last_name, header: "Nachname" },
] { attribute: :email, header: "E-Mail" }
]
end
end end
def test_import_run describe '#run' do
@import.run describe 'without a block' do
assert_equal User.all.size, 2 it 'inserts the records' do
end @import.run
assert_equal User.all.size, 2
end
end
def test_import_run_with_block describe 'with a block' do
results = @import.run { |v| v } it 'yields the records to the block' do
assert_equal results[0], { results = @import.run { |v| v }
first_name: "Hans", assert_equal results[0], {
last_name: "Müller", first_name: "Hans",
email: "hans@mueller.com" last_name: "Müller",
} email: "hans@mueller.com"
assert_equal results[1], { }
first_name: "Jögi", assert_equal results[1], {
last_name: "Brunz", first_name: "Jögi",
email: "jb@runz.com" last_name: "Brunz",
} email: "jb@runz.com"
}
end
end
end end
def test_validations describe '#errors' do
import = UserImport.new("test/files/missing-column.xlsx") it 'returns an error when a column is missing in the excel' do
assert import.errors[:missing_column].any? import = UserImport.new("test/files/missing-column.xlsx")
end assert import.errors[:missing_column].any?
end
def test_model_validations it 'returns the model validation errors' do
import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run)
assert import.errors[:model].any? assert import.errors[:model].any?
assert_equal import.errors[:model], [Excelsior::Error.new(3, ["First name can't be blank"])] assert_equal import.errors[:model], [Excelsior::Error.new(3, ["First name can't be blank"])]
end
end end
def test_report describe '#report' do
import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) describe 'without a block' do
assert_equal 2, import.report.inserted it 'returns the inserted, failed and total number of rows' do
assert_equal 1, import.report.failed import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run)
assert_equal 3, import.report.total assert_equal 2, import.report.inserted
end assert_equal 1, import.report.failed
assert_equal 3, import.report.total
end
end
def test_report_with_block describe 'with a block' do
import = UserImport.new("test/files/missing-first-name.xlsx") it 'returns the inserted, failed and total number of rows' do
import.run do |v| import = UserImport.new("test/files/missing-first-name.xlsx")
raise "failure!" if v[:first_name].nil? import.run do |v|
v raise "failure!" if v[:first_name].nil?
v
end
assert_equal 2, import.report.inserted
assert_equal 1, import.report.failed
assert_equal 3, import.report.total
end
end end
assert_equal 2, import.report.inserted
assert_equal 1, import.report.failed
assert_equal 3, import.report.total
end end
end end
...@@ -3,6 +3,12 @@ require "excelsior" ...@@ -3,6 +3,12 @@ require "excelsior"
require 'active_record' require 'active_record'
require "minitest/autorun" require "minitest/autorun"
class MiniTest::Spec
before do
User.delete_all
end
end
ActiveRecord::Base.establish_connection( ActiveRecord::Base.establish_connection(
adapter: 'sqlite3', adapter: 'sqlite3',
database: File.dirname(__FILE__) + '/test.sqlite3' database: File.dirname(__FILE__) + '/test.sqlite3'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment