diff options
| author | Zog | 2018-03-02 11:16:33 +0100 | 
|---|---|---|
| committer | Zog | 2018-03-02 11:16:33 +0100 | 
| commit | 4e0ef1700c8c105f5e5a7e4c3af20f6065bef461 (patch) | |
| tree | 759bdc67544dc3d657ce05ef2bcb8dac0c3e1f63 | |
| parent | 4acb5a53da2995aacc7b6ce2c802ad16fa563bf8 (diff) | |
| download | chouette-core-4e0ef1700c8c105f5e5a7e4c3af20f6065bef461.tar.bz2 | |
Refs #6068; Create a SimpleExporter model
This is an STI model, inheriting from SimpleInterface
SimpleImporter has been updated to work likewise
| -rw-r--r-- | app/models/simple_exporter.rb | 126 | ||||
| -rw-r--r-- | app/models/simple_importer.rb | 287 | ||||
| -rw-r--r-- | app/models/simple_interface.rb | 270 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 2 | ||||
| -rw-r--r-- | db/migrate/20180301142531_create_simple_exporters.rb | 7 | ||||
| -rw-r--r-- | db/schema.rb | 7 | ||||
| -rw-r--r-- | spec/models/simple_exporter_spec.rb | 72 | ||||
| -rw-r--r-- | spec/models/simple_importer_spec.rb | 4 | 
8 files changed, 502 insertions, 273 deletions
| diff --git a/app/models/simple_exporter.rb b/app/models/simple_exporter.rb new file mode 100644 index 000000000..f9b1c0a80 --- /dev/null +++ b/app/models/simple_exporter.rb @@ -0,0 +1,126 @@ +class SimpleExporter < SimpleInterface +  def export opts={} +    configuration.validate! +    @verbose = opts.delete :verbose + +    @resolution_queue = Hash.new{|h,k| h[k] = []} +    @errors = [] +    @messages = [] +    @number_of_lines = 0 +    @padding = 1 +    @current_line = -1 +    @number_of_lines = collection.size +    @padding = [1, Math.log(@number_of_lines, 10).ceil()].max + +    @csv = nil +    fail_with_error "Unable to write in file: #{self.filepath}" do +      @csv = CSV.open(self.filepath, 'w', self.configuration.csv_options) +    end + +    self.configuration.before_actions(:parsing).each do |action| action.call self end + +    @statuses = "" + +    if ENV["NO_TRANSACTION"] +      process_collection +    else +      ActiveRecord::Base.transaction do +        process_collection +      end +    end +    self.status ||= :success +  rescue SimpleInterface::FailedOperation +    self.status = :failed +  ensure +    @csv&.close +    self.save! +  end + +  def collection +    @collection ||= begin +      coll = configuration.collection +      coll = coll.call() if coll.is_a?(Proc) +      coll +    end +  end + +  def encode_string s +    s.encode("utf-8").force_encoding("utf-8") +  end + +  protected +  def process_collection +    self.configuration.before_actions(:all).each do |action| action.call self end +    log "Starting export ...", color: :green +    log "Export will be written in #{filepath}", color: :green +    @csv << self.configuration.columns.map(&:name) +    ids = collection.pluck :id +    ids.in_groups_of(configuration.batch_size).each do |batch_ids| +      collection.where(id: batch_ids).each do |item| +        @current_row = item.attributes +        @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present? +        row = [] +        @new_status = nil +        self.configuration.columns.each do |col| +          val = col[:value] +          if val.nil? || val.is_a?(Proc) +            if item.respond_to? col.attribute +              if val.is_a?(Proc) +                val = instance_exec(item.send(col.attribute), &val) +              else +                val = item.send(col.attribute) +              end +            else +              push_in_journal({event: :attribute_not_found, message: "Attribute not found: #{col.attribute}", kind: :warning}) +              self.status ||= :success_with_warnings +            end +          end + +          if val.nil? && col.required? +            @new_status = colorize("x", :red) +            raise "MISSING VALUE FOR COLUMN #{col.name}" +          end +          @new_status ||= colorize("✓", :green) +          val = encode_string(val) if val.is_a?(String) +          row << val +        end +        push_in_journal({event: :success, kind: :log}) +        @statuses += @new_status +        print_state if @current_line % 20 == 0 +        @current_line += 1 +        @csv << row +      end +    end +    print_state +  end + +  class Configuration < SimpleInterface::Configuration +    attr_accessor :collection +    attr_accessor :batch_size +    attr_accessor :logged_attributes + +    def initialize import_name, opts={} +      super import_name, opts +      @collection = opts[:collection] +      @batch_size = opts[:batch_size] || 1000 +      @logged_attributes = opts[:logged_attributes] +    end + +    def options +      super.update({ +        collection: collection, +        batch_size: batch_size, +        logged_attributes: logged_attributes, +      }) +    end + +    def add_column name, opts={} +      raise "Column already defined: #{name}" if @columns.any?{|c| c.name == name.to_s} +      super name, opts +    end + +    def validate! +      raise "Incomplete configuration, missing collection for #{@import_name}" if collection.nil? +    end +  end +end diff --git a/app/models/simple_importer.rb b/app/models/simple_importer.rb index d6ba64494..6db71797a 100644 --- a/app/models/simple_importer.rb +++ b/app/models/simple_importer.rb @@ -1,37 +1,4 @@ -class SimpleImporter < ActiveRecord::Base -  attr_accessor :configuration - -  def self.define name -    @importers ||= {} -    configuration = Configuration.new name -    yield configuration -    configuration.validate! -    @importers[name.to_sym] = configuration -  end - -  def self.find_configuration name -    @importers ||= {} -    configuration = @importers[name.to_sym] -    raise "Importer not found: #{name}" unless configuration -    configuration -  end - -  def initialize *args -    super *args -    self.configuration = self.class.find_configuration self.configuration_name -    self.journal ||= [] -  end - -  def configure -    new_config = configuration.duplicate -    yield new_config -    new_config.validate! -    self.configuration = new_config -  end - -  def context -    self.configuration.context -  end +class SimpleImporter < SimpleInterface    def resolve col_name, value, &block      val = block.call(value) @@ -41,9 +8,9 @@ class SimpleImporter < ActiveRecord::Base    end    def import opts={} +    configuration.validate!      @verbose = opts.delete :verbose -      @resolution_queue = Hash.new{|h,k| h[k] = []}      @errors = []      @messages = [] @@ -55,7 +22,6 @@ class SimpleImporter < ActiveRecord::Base        @padding = [1, Math.log(@number_of_lines, 10).ceil()].max      end -      self.configuration.before_actions(:parsing).each do |action| action.call self end      @statuses = "" @@ -68,28 +34,12 @@ class SimpleImporter < ActiveRecord::Base        end      end      self.status ||= :success -  rescue FailedImport +  rescue SimpleInterface::FailedOperation      self.status = :failed    ensure      self.save!    end -  def fail_with_error msg=nil, opts={} -    begin -      yield -    rescue => e -      msg = msg.call if msg.is_a?(Proc) -      custom_print "\nFAILED: \n errors: #{msg}\n exception: #{e.message}\n#{e.backtrace.join("\n")}", color: :red unless self.configuration.ignore_failures -      push_in_journal({message: msg, error: e.message, event: :error, kind: :error}) -      @new_status = colorize("x", :red) -      if self.configuration.ignore_failures -        raise FailedRow if opts[:abort_row] -      else -        raise FailedImport -      end -    end -  end -    def encode_string s      s.encode("utf-8").force_encoding("utf-8")    end @@ -109,16 +59,6 @@ class SimpleImporter < ActiveRecord::Base      log "CSV file dumped in #{filepath}"    end -  def log msg, opts={} -    msg = colorize msg, opts[:color] if opts[:color] -    if opts[:append] -      @messages[-1] = (@messages[-1] || "") + msg -    else -      @messages << msg -    end -    print_state -  end -    protected    def process_csv_file @@ -154,23 +94,23 @@ class SimpleImporter < ActiveRecord::Base              action.call self, @current_record            end          end -      rescue FailedRow +      rescue SimpleInterface::FailedRow          @new_status = colorize("x", :red)        end        push_in_journal({event: @event, kind: :log}) if @current_record&.valid?        @statuses += @new_status        self.configuration.columns.each do |col| -      if @current_record && col.name && @resolution_queue.any? -        val = @current_record.send col[:attribute] -        (@resolution_queue.delete([col.name, val]) || []).each do |res| -          record = res[:record] -          attribute = res[:attribute] -          value = res[:block].call(val, record) -          record.send "#{attribute}=", value -          record.save! +        if @current_record && col.name && @resolution_queue.any? +          val = @current_record.send col[:attribute] +          (@resolution_queue.delete([col.name, val]) || []).each do |res| +            record = res[:record] +            attribute = res[:attribute] +            value = res[:block].call(val, record) +            record.send "#{attribute}=", value +            record.save! +          end          end        end -    end        print_state        @current_line += 1      end @@ -179,7 +119,7 @@ class SimpleImporter < ActiveRecord::Base        self.configuration.after_actions(:all).each do |action|          action.call self        end -    rescue FailedRow +    rescue SimpleInterface::FailedRow      end    end @@ -215,211 +155,20 @@ class SimpleImporter < ActiveRecord::Base      end    end -  def push_in_journal data -    line = @current_line + 1 -    line += 1 if configuration.headers -    self.journal.push data.update(line: line, row: @current_row) -    if data[:kind] == :error || data[:kind] == :warning -      @errors.push data -    end -  end - -  def colorize txt, color -    color = { -      red: "31", -      green: "32", -      orange: "33", -    }[color] || "33" -    "\e[#{color}m#{txt}\e[0m" -  end - -  def print_state -    return unless @verbose - -    @status_width ||= begin -      term_width = %x(tput cols).to_i -      term_width - @padding - 10 -    rescue -      100 -    end - -    @status_height ||= begin -      term_height = %x(tput lines).to_i -      term_height - 3 -    rescue -      50 -    end - -    full_status = @statuses || "" -    full_status = full_status.last(@status_width*10) || "" -    padding_size = [(@number_of_lines - @current_line - 1), (@status_width - full_status.size/10)].min -    full_status = "#{full_status}#{"."*[padding_size, 0].max}" - -    msg = "#{"%#{@padding}d" % (@current_line + 1)}/#{@number_of_lines}: #{full_status}" - -    lines_count = [(@status_height / 2) - 3, 1].max - -    if @messages.any? -      msg += "\n\n" -      msg += colorize "=== MESSAGES (#{@messages.count}) ===\n", :green -      msg += "[...]\n" if @messages.count > lines_count -      msg += @messages.last(lines_count).map{|m| m.truncate(@status_width)}.join("\n") -      msg += "\n"*[lines_count-@messages.count, 0].max -    end - -    if @errors.any? -      msg += "\n\n" -      msg += colorize "=== ERRORS (#{@errors.count}) ===\n", :red -      msg += "[...]\n" if @errors.count > lines_count -      msg += @errors.last(lines_count).map do |j| -        kind = j[:kind] -        kind = colorize(kind, kind == :error ? :red : :orange) -        kind = "[#{kind}]" -        kind += " "*(25 - kind.size) -        encode_string("#{kind}L#{j[:line]}\t#{j[:error]}\t\t#{j[:message]}").truncate(@status_width) -      end.join("\n") -    end -    custom_print msg, clear: true -  end - -  def custom_print msg, opts={} -    return unless @verbose -    out = "" -    msg = colorize(msg, opts[:color]) if opts[:color] -    puts "\e[H\e[2J" if opts[:clear] -    out += msg -    print out -  end - -  class FailedImport < RuntimeError -  end - -  class FailedRow < RuntimeError -  end - -  class Configuration -    attr_accessor :model, :headers, :separator, :key, :context, :encoding, :ignore_failures, :scope -    attr_reader :columns +  class Configuration < SimpleInterface::Configuration +    attr_accessor :model      def initialize import_name, opts={} -      @import_name = import_name -      @key = opts[:key] || "id" -      @headers = opts.has_key?(:headers) ? opts[:headers] : true -      @separator = opts[:separator] || "," -      @encoding = opts[:encoding] -      @columns = opts[:columns] || [] +      super import_name, opts        @model = opts[:model] -      @custom_handler = opts[:custom_handler] -      @before = opts[:before] -      @after = opts[:after] -      @ignore_failures = opts[:ignore_failures] -      @context = opts[:context] || {} -      @scope = opts[:scope] -    end - -    def duplicate -      Configuration.new @import_name, self.options      end      def options -      { -        key: @key, -        headers: @headers, -        separator: @separator, -        encoding: @encoding, -        columns: @columns.map(&:duplicate), -        model: model, -        custom_handler: @custom_handler, -        before: @before, -        after: @after, -        ignore_failures: @ignore_failures, -        context: @context, -        scope: @scope -      } +      super.update({model: model})      end      def validate!        raise "Incomplete configuration, missing model for #{@import_name}" unless model.present?      end - -    def attribute_for_col col_name -      column = self.columns.find{|c| c.name == col_name} -      column && column[:attribute] || col_name -    end - -    def record_scope -      _scope = @scope -      _scope = instance_exec(&_scope) if _scope.is_a?(Proc) -      _scope || model -    end - -    def find_record attrs -      record_scope.find_or_initialize_by(attribute_for_col(@key) => attrs[@key.to_s]) -    end - -    def csv_options -      { -        headers: self.headers, -        col_sep: self.separator, -        encoding: self.encoding -      } -    end - -    def add_column name, opts={} -      @columns.push Column.new({name: name.to_s}.update(opts)) -    end - -    def add_value attribute, value -      @columns.push Column.new({attribute: attribute, value: value}) -    end - -    def before group=:all, &block -      @before ||= Hash.new{|h, k| h[k] = []} -      @before[group].push block -    end - -    def after group=:all, &block -      @after ||= Hash.new{|h, k| h[k] = []} -      @after[group].push block -    end - -    def before_actions group=:all -      @before ||= Hash.new{|h, k| h[k] = []} -      @before[group] -    end - -    def after_actions group=:all -      @after ||= Hash.new{|h, k| h[k] = []} -      @after[group] -    end - -    def custom_handler &block -      @custom_handler = block -    end - -    def get_custom_handler -      @custom_handler -    end - -    class Column -      attr_accessor :name -      def initialize opts={} -        @name = opts[:name] -        @options = opts -        @options[:attribute] ||= @name -      end - -      def duplicate -        Column.new @options.dup -      end - -      def required? -        !!@options[:required] -      end - -      def [](key) -        @options[key] -      end -    end    end  end diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb new file mode 100644 index 000000000..3d5027bf1 --- /dev/null +++ b/app/models/simple_interface.rb @@ -0,0 +1,270 @@ +class SimpleInterface < ActiveRecord::Base +  attr_accessor :configuration + +  class << self +    def configuration_class +      "#{self.name}::Configuration".constantize +    end + +    def define name +      @importers ||= {} +      configuration = configuration_class.new name +      yield configuration if block_given? +      @importers[name.to_sym] = configuration +    end + +    def find_configuration name +      @importers ||= {} +      configuration = @importers[name.to_sym] +      raise "Importer not found: #{name}" unless configuration +      configuration +    end +  end + +  def initialize *args +    super *args +    self.configuration = self.class.find_configuration self.configuration_name +    self.journal ||= [] +  end + +  def configure +    new_config = configuration.duplicate +    yield new_config +    self.configuration = new_config +  end + +  def context +    self.configuration.context +  end + +  def fail_with_error msg=nil, opts={} +    begin +      yield +    rescue => e +      msg = msg.call if msg.is_a?(Proc) +      custom_print "\nFAILED: \n errors: #{msg}\n exception: #{e.message}\n#{e.backtrace.join("\n")}", color: :red unless self.configuration.ignore_failures +      push_in_journal({message: msg, error: e.message, event: :error, kind: :error}) +      @new_status = colorize("x", :red) +      if self.configuration.ignore_failures +        raise SimpleInterface::FailedRow if opts[:abort_row] +      else +        raise FailedOperation +      end +    end +  end + +  def log msg, opts={} +    msg = msg.to_s +    msg = colorize msg, opts[:color] if opts[:color] +    if opts[:append] +      @messages[-1] = (@messages[-1] || "") + msg +    else +      @messages << msg +    end +    print_state +  end + +  protected + +  def push_in_journal data +    line = @current_line + 1 +    line += 1 if configuration.headers +    self.journal.push data.update(line: line, row: @current_row) +    if data[:kind] == :error || data[:kind] == :warning +      @errors.push data +    end +  end + +  def colorize txt, color +    color = { +      red: "31", +      green: "32", +      orange: "33", +    }[color] || "33" +    "\e[#{color}m#{txt}\e[0m" +  end + +  def print_state +    return unless @verbose + +    @status_width ||= begin +      term_width = %x(tput cols).to_i +      term_width - @padding - 10 +    rescue +      100 +    end + +    @status_height ||= begin +      term_height = %x(tput lines).to_i +      term_height - 3 +    rescue +      50 +    end + +    full_status = @statuses || "" +    full_status = full_status.last(@status_width*10) || "" +    padding_size = [(@number_of_lines - @current_line - 1), (@status_width - full_status.size/10)].min +    full_status = "#{full_status}#{"."*[padding_size, 0].max}" + +    msg = "#{"%#{@padding}d" % (@current_line + 1)}/#{@number_of_lines}: #{full_status}" + +    lines_count = [(@status_height / 2) - 3, 1].max + +    if @messages.any? +      msg += "\n\n" +      msg += colorize "=== MESSAGES (#{@messages.count}) ===\n", :green +      msg += "[...]\n" if @messages.count > lines_count +      msg += @messages.last(lines_count).map{|m| m.truncate(@status_width)}.join("\n") +      msg += "\n"*[lines_count-@messages.count, 0].max +    end + +    if @errors.any? +      msg += "\n\n" +      msg += colorize "=== ERRORS (#{@errors.count}) ===\n", :red +      msg += "[...]\n" if @errors.count > lines_count +      msg += @errors.last(lines_count).map do |j| +        kind = j[:kind] +        kind = colorize(kind, kind == :error ? :red : :orange) +        kind = "[#{kind}]" +        kind += " "*(25 - kind.size) +        encode_string("#{kind}L#{j[:line]}\t#{j[:error]}\t\t#{j[:message]}").truncate(@status_width) +      end.join("\n") +    end +    custom_print msg, clear: true +  end + +  def custom_print msg, opts={} +    return unless @verbose +    out = "" +    msg = colorize(msg, opts[:color]) if opts[:color] +    puts "\e[H\e[2J" if opts[:clear] +    out += msg +    print out +  end + +  class FailedRow < RuntimeError +  end + +  class FailedOperation < RuntimeError +  end + +  class Configuration +    attr_accessor :headers, :separator, :key, :context, :encoding, :ignore_failures, :scope +    attr_reader :columns + +    def initialize import_name, opts={} +      @import_name = import_name +      @key = opts[:key] || "id" +      @headers = opts.has_key?(:headers) ? opts[:headers] : true +      @separator = opts[:separator] || "," +      @encoding = opts[:encoding] +      @columns = opts[:columns] || [] +      @custom_handler = opts[:custom_handler] +      @before = opts[:before] +      @after = opts[:after] +      @ignore_failures = opts[:ignore_failures] +      @context = opts[:context] || {} +      @scope = opts[:scope] +    end + +    def duplicate +      self.class.new @import_name, self.options +    end + +    def options +      { +        key: @key, +        headers: @headers, +        separator: @separator, +        encoding: @encoding, +        columns: @columns.map(&:duplicate), +        custom_handler: @custom_handler, +        before: @before, +        after: @after, +        ignore_failures: @ignore_failures, +        context: @context, +        scope: @scope +      } +    end + +    def attribute_for_col col_name +      column = self.columns.find{|c| c.name == col_name} +      column && column[:attribute] || col_name +    end + +    def record_scope +      _scope = @scope +      _scope = instance_exec(&_scope) if _scope.is_a?(Proc) +      _scope || model +    end + +    def find_record attrs +      record_scope.find_or_initialize_by(attribute_for_col(@key) => attrs[@key.to_s]) +    end + +    def csv_options +      { +        headers: self.headers, +        col_sep: self.separator, +        encoding: self.encoding +      } +    end + +    def add_column name, opts={} +      @columns.push Column.new({name: name.to_s}.update(opts)) +    end + +    def add_value attribute, value +      @columns.push Column.new({attribute: attribute, value: value}) +    end + +    def before group=:all, &block +      @before ||= Hash.new{|h, k| h[k] = []} +      @before[group].push block +    end + +    def after group=:all, &block +      @after ||= Hash.new{|h, k| h[k] = []} +      @after[group].push block +    end + +    def before_actions group=:all +      @before ||= Hash.new{|h, k| h[k] = []} +      @before[group] +    end + +    def after_actions group=:all +      @after ||= Hash.new{|h, k| h[k] = []} +      @after[group] +    end + +    def custom_handler &block +      @custom_handler = block +    end + +    def get_custom_handler +      @custom_handler +    end + +    class Column +      attr_accessor :name, :attribute +      def initialize opts={} +        @name = opts[:name] +        @options = opts +        @attribute = @options[:attribute] ||= @name +      end + +      def duplicate +        Column.new @options.dup +      end + +      def required? +        !!@options[:required] +      end + +      def [](key) +        @options[key] +      end +    end +  end +end diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index a996549fd..f5fb8cd5e 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -81,7 +81,9 @@ Apartment.configure do |config|      'ComplianceCheckMessage',      'Merge',      'CustomField', +    'SimpleInterface',      'SimpleImporter', +    'SimpleExporter',    ]    # use postgres schemas? diff --git a/db/migrate/20180301142531_create_simple_exporters.rb b/db/migrate/20180301142531_create_simple_exporters.rb new file mode 100644 index 000000000..c007546c2 --- /dev/null +++ b/db/migrate/20180301142531_create_simple_exporters.rb @@ -0,0 +1,7 @@ +class CreateSimpleExporters < ActiveRecord::Migration +  def change +    rename_table :simple_importers, :simple_interfaces +    add_column :simple_interfaces, :type, :string +    SimpleInterface.update_all type: :SimpleImporter +  end +end diff --git a/db/schema.rb b/db/schema.rb index 045fc658d..d6f3cdbe0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@  #  # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180227151937) do +ActiveRecord::Schema.define(version: 20180301142531) do    # These are extensions that must be enabled in order to support this database    enable_extension "plpgsql" @@ -90,9 +90,9 @@ ActiveRecord::Schema.define(version: 20180227151937) do      t.integer   "organisation_id", limit: 8      t.datetime  "created_at"      t.datetime  "updated_at" +    t.integer   "workgroup_id",    limit: 8      t.integer   "int_day_types"      t.date      "excluded_dates",                            array: true -    t.integer   "workgroup_id",    limit: 8    end    add_index "calendars", ["organisation_id"], name: "index_calendars_on_organisation_id", using: :btree @@ -725,11 +725,12 @@ ActiveRecord::Schema.define(version: 20180227151937) do      t.integer "line_id",      limit: 8    end -  create_table "simple_importers", id: :bigserial, force: :cascade do |t| +  create_table "simple_interfaces", id: :bigserial, force: :cascade do |t|      t.string "configuration_name"      t.string "filepath"      t.string "status"      t.json   "journal" +    t.string "type"    end    create_table "stop_area_referential_memberships", id: :bigserial, force: :cascade do |t| diff --git a/spec/models/simple_exporter_spec.rb b/spec/models/simple_exporter_spec.rb new file mode 100644 index 000000000..395cb1034 --- /dev/null +++ b/spec/models/simple_exporter_spec.rb @@ -0,0 +1,72 @@ +RSpec.describe SimpleExporter do +  describe "#define" do +    context "with an incomplete configuration" do +      it "should raise an error" do +        SimpleExporter.define :foo +        expect do +          SimpleExporter.new(configuration_name: :test).export +        end.to raise_error +      end +    end +    context "with a complete configuration" do +      before do +        SimpleExporter.define :foo do |config| +          config.collection = Chouette::StopArea.all +        end +      end + +      it "should define an exporter" do +        expect{SimpleExporter.find_configuration(:foo)}.to_not raise_error +        expect{SimpleExporter.new(configuration_name: :foo, filepath: "").export}.to_not raise_error +        expect{SimpleExporter.find_configuration(:bar)}.to raise_error +        expect{SimpleExporter.new(configuration_name: :bar, filepath: "")}.to_not raise_error +        expect{SimpleExporter.new(configuration_name: :bar, filepath: "").export}.to raise_error +        expect{SimpleExporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleExporter.count}.by 1 +      end +    end + +    context "when defining the same col twice" do +      it "should raise an error" do +        expect do +          SimpleExporter.define :foo do |config| +            config.collection = Chouette::StopArea.all +            config.add_column :name +            config.add_column :name +          end +        end.to raise_error +      end +    end +  end + +  describe "#export" do +    let(:exporter){ importer = SimpleExporter.new(configuration_name: :test, filepath: filepath) } +    let(:filepath){ Rails.root + "tmp/" + filename } +    let(:filename){ "stop_area.csv" } +    # let(:stop_area_referential){ create(:stop_area_referential, objectid_format: :stif_netex) } + +    before(:each) do +      @stop_area = create :stop_area +      SimpleExporter.define :test do |config| +        config.collection = ->{ Chouette::StopArea.all } +        config.separator = ";" +        config.add_column :name +        config.add_column :lat, attribute: :latitude +        config.add_column :lng, attribute: :latitude, value: ->(raw){ raw.to_f + 1 } +        config.add_column :type, attribute: :area_type +        config.add_column :street_name, value: "Lil Exporter" +      end +    end + +    it "should export the given file" do +      expect{exporter.export verbose: true}.to_not raise_error +      expect(exporter.status).to eq "success" +      expect(File.exists?(filepath)).to be_truthy +      csv = CSV.read(filepath, headers: true, col_sep: ";") +      row = csv.by_row.values_at(0).last +      expect(row["name"]).to eq @stop_area.name +      expect(row["lat"]).to eq @stop_area.latitude.to_s +      expect(row["lng"]).to eq (@stop_area.latitude.to_f + 1).to_s +      expect(row["street_name"]).to eq "Lil Exporter" +    end +  end +end diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb index 60d7b7882..231a699a3 100644 --- a/spec/models/simple_importer_spec.rb +++ b/spec/models/simple_importer_spec.rb @@ -3,8 +3,9 @@ RSpec.describe SimpleImporter do      context "with an incomplete configuration" do        it "should raise an error" do +        SimpleImporter.define :foo          expect do -          SimpleImporter.define :foo +          SimpleImporter.new(configuration_name: :foo, filepath: "").import          end.to raise_error        end      end @@ -18,6 +19,7 @@ RSpec.describe SimpleImporter do        it "should define an importer" do          expect{SimpleImporter.find_configuration(:foo)}.to_not raise_error          expect{SimpleImporter.new(configuration_name: :foo, filepath: "")}.to_not raise_error +        expect{SimpleImporter.new(configuration_name: :foo, filepath: "").import}.to_not raise_error          expect{SimpleImporter.find_configuration(:bar)}.to raise_error          expect{SimpleImporter.new(configuration_name: :bar, filepath: "")}.to raise_error          expect{SimpleImporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleImporter.count}.by 1 | 
