Skip to content
Browse files

refactor spec files and add let

  • Loading branch information...
1 parent c529db8 commit d3c834665122962ff580fb56d8d59d779aa621b3 @gitlaura committed Jul 15, 2014
View
6 lib/transaction_parser.rb
@@ -13,9 +13,9 @@ def parse(csv_file)
end
def create_transaction(line)
- @store, @sku, @amount = line.chomp.split(",")
- @original_amount, @original_currency = @amount.split(" ")
- OpenStruct.new(:store => @store, :sku => @sku, :original_amount => @original_amount, :original_currency => @original_currency)
+ store, sku, amount = line.chomp.split(",")
+ original_amount, original_currency = amount.split(" ")
+ OpenStruct.new(:store => store, :sku => sku, :original_amount => original_amount, :original_currency => original_currency)
end
end
View
34 spec/calculator_spec.rb
@@ -2,38 +2,36 @@
require 'ostruct'
describe "Calculator interface" do
- before(:each) do
- @rates_file = "SAMPLE_RATES.xml"
- @trans_file = "SAMPLE_TRANS.csv"
- trans_001 = OpenStruct.new(:store => "Yonkers", :sku => "DM1182", :original_amount => "19.68", :original_currency => "AUD")
- trans_002 = OpenStruct.new(:store => "Nashua", :sku => "DM1182", :original_amount => "58.58", :original_currency => "AUD")
- trans_003 = OpenStruct.new(:store => "Camden", :sku => "DM1182", :original_amount => "54.64", :original_currency => "USD")
- @transactions = [trans_001, trans_002, trans_003]
- rate_one = OpenStruct.new(:from_currency => "AUD", :to_currency => "CAD", :rate => 1.0079)
- rate_two = OpenStruct.new(:from_currency => "CAD", :to_currency => "USD", :rate => 1.0090)
- rate_three = OpenStruct.new(:from_currency => "USD", :to_currency => "CAD", :rate => 0.9911)
- @rates = [rate_one, rate_two, rate_three]
- @sku = "DM1182"
- @to_currency = "USD"
- end
+ let(:rates_file) {"SAMPLE_RATES.xml"}
+ let(:trans_file) {"SAMPLE_TRANS.csv"}
+ let(:trans_001) {OpenStruct.new(:store => "Yonkers", :sku => "DM1182", :original_amount => "19.68", :original_currency => "AUD")}
+ let(:trans_002) {OpenStruct.new(:store => "Nashua", :sku => "DM1182", :original_amount => "58.58", :original_currency => "AUD")}
+ let(:trans_003) {OpenStruct.new(:store => "Camden", :sku => "DM1182", :original_amount => "54.64", :original_currency => "USD")}
+ let(:rate_one) {OpenStruct.new(:from_currency => "AUD", :to_currency => "CAD", :rate => 1.0079)}
+ let(:rate_two) {OpenStruct.new(:from_currency => "CAD", :to_currency => "USD", :rate => 1.0090)}
+ let(:rate_three) {OpenStruct.new(:from_currency => "USD", :to_currency => "CAD", :rate => 0.9911)}
+ let(:sku) {"DM1182"}
+ let(:to_currency) {"USD"}
+ let(:transactions) {[trans_001, trans_002, trans_003]}
+ let(:rates) {[rate_one, rate_two, rate_three]}
it "gets a list of rates" do
- rates = Calculator.get_conversion_rates(@rates_file)
+ rates = Calculator.get_conversion_rates(rates_file)
expect(rates.size).to eq(3)
end
it "gets a list of transactions" do
- transactions = Calculator.get_transactions(@trans_file)
+ transactions = Calculator.get_transactions(trans_file)
expect(transactions.size).to eq(6)
end
it "sorts transactions by SKU" do
- sorted_transactions = Calculator.sort_transactions(@sku, @transactions)
+ sorted_transactions = Calculator.sort_transactions(sku, transactions)
expect(sorted_transactions.size).to eq(3)
end
it "converts sorted transactions" do
- converted_amounts = Calculator.convert_transactions(@to_currency, @rates, @transactions)
+ converted_amounts = Calculator.convert_transactions(to_currency, rates, transactions)
expect(converted_amounts).to eq([20.01,59.57,54.64])
end
View
40 spec/currency_converter_spec.rb
@@ -2,51 +2,49 @@
require 'ostruct'
describe "Currency Converter" do
- before(:each) do
- to_currency = "USD"
- rate_one = OpenStruct.new(:from_currency => "AUD", :to_currency => "CAD", :rate => 1.0079)
- rate_two = OpenStruct.new(:from_currency => "CAD", :to_currency => "USD", :rate => 1.0090)
- rate_three = OpenStruct.new(:from_currency => "USD", :to_currency => "CAD", :rate => 0.9911)
- rates = [rate_one, rate_two, rate_three]
- @trans_one = OpenStruct.new(:store => "Nashua", :sku => "DM1182", :original_amount => "58.58", :original_currency => "AUD")
- @trans_two = OpenStruct.new(:store => "Camden", :sku => "DM1182", :original_amount => "54.64", :original_currency => "USD")
- @sorted_transactions = [@trans_one, @trans_two]
- @currency_converter = CurrencyConverter.new(to_currency, rates)
- end
+ let(:to_currency) {"USD"}
+ let(:rate_one) {OpenStruct.new(:from_currency => "AUD", :to_currency => "CAD", :rate => 1.0079)}
+ let(:rate_two) {OpenStruct.new(:from_currency => "CAD", :to_currency => "USD", :rate => 1.0090)}
+ let(:rate_three) {OpenStruct.new(:from_currency => "USD", :to_currency => "CAD", :rate => 0.9911)}
+ let(:rates) {[rate_one, rate_two, rate_three]}
+ let(:trans_one) {OpenStruct.new(:store => "Nashua", :sku => "DM1182", :original_amount => "58.58", :original_currency => "AUD")}
+ let(:trans_two) {OpenStruct.new(:store => "Camden", :sku => "DM1182", :original_amount => "54.64", :original_currency => "USD")}
+ let(:sorted_transactions) {[trans_one, trans_two]}
+ let(:currency_converter) {CurrencyConverter.new(to_currency, rates)}
it "converts transactions into converted amounts" do
- amounts = @currency_converter.convert(@sorted_transactions)
+ amounts = currency_converter.convert(sorted_transactions)
expect(amounts).to eq([59.57,54.64])
end
it "determines if a conversion is not needed" do
- expect(@currency_converter.conversion_unnecessary?(@trans_one)).to eq(false)
- expect(@currency_converter.conversion_unnecessary?(@trans_two)).to eq(true)
+ expect(currency_converter.conversion_unnecessary?(trans_one)).to eq(false)
+ expect(currency_converter.conversion_unnecessary?(trans_two)).to eq(true)
end
it "converts amounts" do
- expect(@currency_converter).to receive(:get_conversion_rate) {1.0090}
- amount = @currency_converter.convert_amount("AUD", 50.00)
+ expect(currency_converter).to receive(:get_conversion_rate) {1.0090}
+ amount = currency_converter.convert_amount("AUD", 50.00)
expect(amount).to eq(50.45)
end
it "checks for an exact match of currencies" do
- expect(@currency_converter.exact_match?("CAD")).to eq(true)
- expect(@currency_converter.exact_match?("AUD")).to eq(false)
+ expect(currency_converter.exact_match?("CAD")).to eq(true)
+ expect(currency_converter.exact_match?("AUD")).to eq(false)
end
it "gets exact matching rate object" do
- rate = @currency_converter.get_exact_matching_rate("CAD")
+ rate = currency_converter.get_exact_matching_rate("CAD")
expect(rate.rate).to eq(1.0090)
end
it "returns rates that match to and from currency" do
- rates = @currency_converter.rates_with_exact_matches("CAD")
+ rates = currency_converter.rates_with_exact_matches("CAD")
expect(rates.size).to eq(1)
end
it "returns rates that just the from currency" do
- rates = @currency_converter.rate_with_same_from_currency("AUD")
+ rates = currency_converter.rate_with_same_from_currency("AUD")
expect(rates).to be_a(Object)
end
end
View
3 spec/runner_spec.rb
@@ -2,12 +2,11 @@
describe "Runner" do
it "runs international calc" do
- @runner = Runner.new
expect(Calculator).to receive(:get_conversion_rates)
expect(Calculator).to receive(:get_transactions)
expect(Calculator).to receive(:sort_transactions)
expect(Calculator).to receive(:convert_transactions)
expect(Calculator).to receive(:add_sorted_transactions)
- @runner.run_international_calculator("DM1210", "SAMPLE_TRANS.csv", "SAMPLE_RATES.xml", "USD")
+ Runner.new.run_international_calculator("DM1210", "SAMPLE_TRANS.csv", "SAMPLE_RATES.xml", "USD")
end
end
View
1 spec/sort_by_sku_spec.rb
@@ -8,7 +8,6 @@
trans_003 = OpenStruct.new(:store => "Camden", :sku => "DM1182", :original_amount => "54.64", :original_currency => "USD")
transactions = [trans_001, trans_002, trans_003]
sku = "DM1182"
-
expect(SortBySku.sort(sku, transactions)).to eq([trans_002, trans_003])
end
end
View
6 spec/transaction_parser_spec.rb
@@ -1,13 +1,13 @@
require 'transaction_parser'
describe "Transaction parser" do
+ let(:file) {"SAMPLE_TRANS.csv"}
+
it "parses a csv trans file" do
- file = "SAMPLE_TRANS.csv"
expect(TransactionParser.parse(file).size).to eq(6)
end
- it "creates a transaction" do
- file = "SAMPLE_TRANS.csv"
+ it "creates a transaction" do
result = TransactionParser.create_transaction("Yonkers,DM1210,70.00 USD")
expect(result.store).to eq("Yonkers")
expect(result.sku).to eq("DM1210")

0 comments on commit d3c8346

Please sign in to comment.
Something went wrong with that request. Please try again.