From 9bb16869830fd47c3409f0ad564622353cae448a Mon Sep 17 00:00:00 2001 From: Alexis Reigel <mail@koffeinfrei.org> Date: Thu, 31 May 2018 14:12:10 +0200 Subject: [PATCH] use rails / active_record --- .gitignore | 1 + Gemfile.lock | 98 ++++++++++++++++++++++++++++++++++++++++- excelsior.gemspec | 5 ++- lib/excelsior/import.rb | 10 ++--- test/excelsior_test.rb | 33 +++++--------- test/test_helper.rb | 15 +++++++ 6 files changed, 132 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 9106b2a..8db2d1e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /pkg/ /spec/reports/ /tmp/ +/test/test.sqlite3 diff --git a/Gemfile.lock b/Gemfile.lock index 0684b85..9b181ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,32 +2,125 @@ PATH remote: . specs: excelsior (0.1.0) - activesupport (~> 5.0) + activerecord (>= 4.0.0) + rails (>= 4.0.0) simple_xlsx_reader (~> 1.0.2) GEM remote: https://rubygems.org/ specs: + actioncable (5.2.0) + actionpack (= 5.2.0) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.0) + actionpack (= 5.2.0) + actionview (= 5.2.0) + activejob (= 5.2.0) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.2.0) + actionview (= 5.2.0) + activesupport (= 5.2.0) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.0) + activesupport (= 5.2.0) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.2.0) + activesupport (= 5.2.0) + globalid (>= 0.3.6) + activemodel (5.2.0) + activesupport (= 5.2.0) + activerecord (5.2.0) + activemodel (= 5.2.0) + activesupport (= 5.2.0) + arel (>= 9.0) + activestorage (5.2.0) + actionpack (= 5.2.0) + activerecord (= 5.2.0) + marcel (~> 0.3.1) activesupport (5.2.0) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) + arel (9.0.0) + builder (3.2.3) concurrent-ruby (1.0.5) + crass (1.0.4) + erubi (1.7.1) + globalid (0.4.1) + activesupport (>= 4.2.0) i18n (1.0.1) concurrent-ruby (~> 1.0) + loofah (2.2.2) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.0) + mini_mime (>= 0.1.1) + marcel (0.3.2) + mimemagic (~> 0.3.2) + method_source (0.9.0) + mimemagic (0.3.2) + mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.11.3) + nio4r (2.3.1) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) + rack (2.0.5) + rack-test (1.0.0) + rack (>= 1.0, < 3) + rails (5.2.0) + actioncable (= 5.2.0) + actionmailer (= 5.2.0) + actionpack (= 5.2.0) + actionview (= 5.2.0) + activejob (= 5.2.0) + activemodel (= 5.2.0) + activerecord (= 5.2.0) + activestorage (= 5.2.0) + activesupport (= 5.2.0) + bundler (>= 1.3.0) + railties (= 5.2.0) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.4) + loofah (~> 2.2, >= 2.2.2) + railties (5.2.0) + actionpack (= 5.2.0) + activesupport (= 5.2.0) + method_source + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) rake (10.5.0) rubyzip (1.2.1) simple_xlsx_reader (1.0.2) nokogiri rubyzip + sprockets (3.7.1) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.3.13) + thor (0.20.0) thread_safe (0.3.6) tzinfo (1.2.5) thread_safe (~> 0.1) + websocket-driver (0.7.0) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) PLATFORMS ruby @@ -37,6 +130,7 @@ DEPENDENCIES excelsior! minitest (~> 5.0) rake (~> 10.0) + sqlite3 BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/excelsior.gemspec b/excelsior.gemspec index 2ebc529..a449320 100644 --- a/excelsior.gemspec +++ b/excelsior.gemspec @@ -1,4 +1,3 @@ - lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "excelsior/version" @@ -24,7 +23,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest", "~> 5.0" + spec.add_development_dependency "sqlite3" spec.add_runtime_dependency "simple_xlsx_reader", "~> 1.0.2" - spec.add_runtime_dependency "activesupport", "~> 5.0" + spec.add_runtime_dependency "rails", ">= 4.0.0" + spec.add_runtime_dependency "activerecord", ">= 4.0.0" end diff --git a/lib/excelsior/import.rb b/lib/excelsior/import.rb index 23ed605..455ee54 100644 --- a/lib/excelsior/import.rb +++ b/lib/excelsior/import.rb @@ -1,8 +1,8 @@ -require "simple_xlsx_reader" -require "active_support" -require "active_support/core_ext" -require "excelsior/source" -require "excelsior/mapping" +require 'simple_xlsx_reader' +require 'rails' +require 'active_record' +require 'excelsior/source' +require 'excelsior/mapping' module Excelsior class Import diff --git a/test/excelsior_test.rb b/test/excelsior_test.rb index 536cafc..adafe5f 100644 --- a/test/excelsior_test.rb +++ b/test/excelsior_test.rb @@ -4,22 +4,13 @@ require "excelsior/import" class UserImport < Excelsior::Import source "test/files/complete.xlsx" - map "Vorname", to: :firstname - map "Nachname", to: :lastname + map "Vorname", to: :first_name + map "Nachname", to: :last_name map "E-Mail", to: :email end -class User - class << self - def create!(attributes) - @all ||= [] - @all << attributes - end - - def all - @all ||= [] - end - end +class User < ActiveRecord::Base + validates :first_name, presence: true end class ExcelsiorTest < Minitest::Test @@ -54,27 +45,27 @@ class ExcelsiorTest < Minitest::Test def test_mapping assert_equal @import.fields, [ - {attribute: :firstname, header: "Vorname"}, - {attribute: :lastname, header: "Nachname"}, - {attribute: :email, header: "E-Mail"} + { attribute: :first_name, header: "Vorname" }, + { attribute: :last_name, header: "Nachname" }, + { attribute: :email, header: "E-Mail" } ] end def test_import_run - results = @import.run + @import.run assert_equal User.all.size, 2 end def test_import_run_with_block results = @import.run { |v| v } assert_equal results[0], { - firstname: "Hans", - lastname: "Müller", + first_name: "Hans", + last_name: "Müller", email: "hans@mueller.com" } assert_equal results[1], { - firstname: "Jögi", - lastname: "Brunz", + first_name: "Jögi", + last_name: "Brunz", email: "jb@runz.com" } end diff --git a/test/test_helper.rb b/test/test_helper.rb index b65cf89..1bb086f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,18 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require "excelsior" +require 'active_record' require "minitest/autorun" + +ActiveRecord::Base.establish_connection( + adapter: 'sqlite3', + database: File.dirname(__FILE__) + '/test.sqlite3' +) + +ActiveRecord::Schema.define version: 0 do + create_table :users, force: true do |t| + t.column :first_name, :string + t.column :last_name, :string + t.column :email, :string + t.column :admin, :boolean + end +end -- GitLab