From d04b051df458aae81efc971ffc8c2923c8193859 Mon Sep 17 00:00:00 2001 From: Alexis Reigel <mail@koffeinfrei.org> Date: Fri, 1 Jun 2018 09:41:54 +0200 Subject: [PATCH] convert test to spec syntax --- test/excelsior_test.rb | 145 +++++++++++++++++++++++------------------ test/test_helper.rb | 6 ++ 2 files changed, 89 insertions(+), 62 deletions(-) diff --git a/test/excelsior_test.rb b/test/excelsior_test.rb index 60f8efb..946db56 100644 --- a/test/excelsior_test.rb +++ b/test/excelsior_test.rb @@ -13,90 +13,111 @@ class User < ActiveRecord::Base validates :first_name, presence: true end -class ExcelsiorTest < Minitest::Test - def setup - User.delete_all +describe Excelsior do + before do @import = UserImport.new end - def test_that_it_has_a_version_number + it 'has a version number' do refute_nil ::Excelsior::VERSION end - def test_class_level_source - import = UserImport.new - assert_equal "test/files/complete.xlsx", import.source - end + describe 'source' do + it 'allows setting the source on the class' do + import = UserImport.new + assert_equal "test/files/complete.xlsx", import.source + end - def test_instance_level_source - import = UserImport.new("test/files/missing-column.xlsx") - assert_equal "test/files/missing-column.xlsx", import.source + it 'allows setting the source on an instance level' do + import = UserImport.new("test/files/missing-column.xlsx") + assert_equal "test/files/missing-column.xlsx", import.source + end end - def test_import_rows - assert_equal 2, @import.rows.length + describe '#rows' do + it 'returns the correct number of rows' do + assert_equal 2, @import.rows.length + end end - def test_import_columns - assert_equal 3, @import.columns.length - assert_equal "E-Mail", @import.columns[0] - assert_equal "Vorname", @import.columns[1] - assert_equal "Nachname", @import.columns[2] + describe '#columns' do + it 'returns the columns as defined in the excel' do + assert_equal 3, @import.columns.length + assert_equal "E-Mail", @import.columns[0] + assert_equal "Vorname", @import.columns[1] + assert_equal "Nachname", @import.columns[2] + end end - def test_mapping - assert_equal @import.fields, [ - { attribute: :first_name, header: "Vorname" }, - { attribute: :last_name, header: "Nachname" }, - { attribute: :email, header: "E-Mail" } - ] + describe '#fields' do + it 'returns the mapped fields' do + assert_equal @import.fields, [ + { attribute: :first_name, header: "Vorname" }, + { attribute: :last_name, header: "Nachname" }, + { attribute: :email, header: "E-Mail" } + ] + end end - def test_import_run - @import.run - assert_equal User.all.size, 2 - end + describe '#run' do + describe 'without a block' do + it 'inserts the records' do + @import.run + assert_equal User.all.size, 2 + end + end - def test_import_run_with_block - results = @import.run { |v| v } - assert_equal results[0], { - first_name: "Hans", - last_name: "Müller", - email: "hans@mueller.com" - } - assert_equal results[1], { - first_name: "Jögi", - last_name: "Brunz", - email: "jb@runz.com" - } + describe 'with a block' do + it 'yields the records to the block' do + results = @import.run { |v| v } + assert_equal results[0], { + first_name: "Hans", + last_name: "Müller", + email: "hans@mueller.com" + } + assert_equal results[1], { + first_name: "Jögi", + last_name: "Brunz", + email: "jb@runz.com" + } + end + end end - def test_validations - import = UserImport.new("test/files/missing-column.xlsx") - assert import.errors[:missing_column].any? - end + describe '#errors' do + it 'returns an error when a column is missing in the excel' do + import = UserImport.new("test/files/missing-column.xlsx") + assert import.errors[:missing_column].any? + end - def test_model_validations - import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) - assert import.errors[:model].any? - assert_equal import.errors[:model], [Excelsior::Error.new(3, ["First name can't be blank"])] + it 'returns the model validation errors' do + import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) + assert import.errors[:model].any? + assert_equal import.errors[:model], [Excelsior::Error.new(3, ["First name can't be blank"])] + end end - def test_report - import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) - assert_equal 2, import.report.inserted - assert_equal 1, import.report.failed - assert_equal 3, import.report.total - end + describe '#report' do + describe 'without a block' do + it 'returns the inserted, failed and total number of rows' do + import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run) + assert_equal 2, import.report.inserted + assert_equal 1, import.report.failed + assert_equal 3, import.report.total + end + end - def test_report_with_block - import = UserImport.new("test/files/missing-first-name.xlsx") - import.run do |v| - raise "failure!" if v[:first_name].nil? - v + describe 'with a block' do + it 'returns the inserted, failed and total number of rows' do + import = UserImport.new("test/files/missing-first-name.xlsx") + import.run do |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 - assert_equal 2, import.report.inserted - assert_equal 1, import.report.failed - assert_equal 3, import.report.total end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 1bb086f..59bb421 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,6 +3,12 @@ require "excelsior" require 'active_record' require "minitest/autorun" +class MiniTest::Spec + before do + User.delete_all + end +end + ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: File.dirname(__FILE__) + '/test.sqlite3' -- GitLab