Skip to content

Instantly share code, notes, and snippets.

@lmansur
Created October 29, 2016 08:24
Show Gist options
  • Save lmansur/f4cd08e00e8821a93f2b8b2a4a42ffe3 to your computer and use it in GitHub Desktop.
Save lmansur/f4cd08e00e8821a93f2b8b2a4a42ffe3 to your computer and use it in GitHub Desktop.
require 'rails_helper'
describe CsvHandler::TotalRevenue do
describe '#total_revenue' do
before do
@import = CsvHandler::ImportData.new('public', 'purchases.csv')
@import.import
@revenue = CsvHandler::TotalRevenue.new('public', 'purchases.csv')
end
context 'given a purchase file' do
it 'calculates total revenue and updates db' do
expect{ @revenue.total_revenue }.to change(Import.last, :total_revenue)
end
end
end
end
@lmansur
Copy link
Author

lmansur commented Oct 29, 2016

module CsvHandler
  class ImportData
    def initialize(file_path, file_name)
      @path = file_path
      @filename = file_name
    end

    def import
      file_path = PathJoiner::PathJoiner.join_path(@path, @filename)
      @import = Import.create
      CSV.foreach(file_path, headers: true) do |row|
        @import.purchases.create(row.to_hash)
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment