diff --git a/.gitignore b/.gitignore index ac13607..f627f9e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,5 @@ config/database.yml config/config.yml - +PocketMoneyDB.sql **/.DS_Store diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..4e1e0d2 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color diff --git a/Gemfile b/Gemfile index f58c20f..015dec7 100644 --- a/Gemfile +++ b/Gemfile @@ -33,6 +33,7 @@ gem 'haml' gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git' gem 'less-rails' gem "therubyracer" +gem "faker" group :doc do # bundle exec rake doc:rails generates the API under doc/api. @@ -46,6 +47,16 @@ gem 'pry-rails' gem 'ruby-progressbar' +group :test do + gem 'database_cleaner' + gem 'rspec' + gem 'rspec-rails' + gem 'capybara' + gem 'factory_girl' + gem 'factory_girl_rails' + gem 'shoulda-matchers' +end + # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index cb0d74c..b453bd4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,6 +38,12 @@ GEM arel (4.0.0) atomic (1.1.9) builder (3.1.4) + capybara (2.1.0) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) coderay (1.0.9) coffee-rails (4.0.0) coffee-script (>= 2.2.0) @@ -47,9 +53,18 @@ GEM execjs coffee-script-source (1.6.2) commonjs (0.2.6) + database_cleaner (1.2.0) + diff-lcs (1.2.4) erubis (2.7.0) execjs (1.4.0) multi_json (~> 1.0) + factory_girl (4.2.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.2.1) + factory_girl (~> 4.2.0) + railties (>= 3.0.0) + faker (1.2.0) + i18n (~> 0.5) haml (4.0.3) tilt hike (1.2.2) @@ -71,8 +86,11 @@ GEM treetop (~> 1.4.8) method_source (0.8.1) mime-types (1.23) + mini_portile (0.5.1) minitest (4.7.4) multi_json (1.7.4) + nokogiri (1.6.0) + mini_portile (~> 0.5.0) pg (0.15.1) polyglot (0.3.3) pry (0.9.12.2) @@ -104,6 +122,21 @@ GEM rdoc (3.12.2) json (~> 1.4) ref (1.0.5) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.5) + rspec-expectations (2.14.3) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.3) + rspec-rails (2.14.0) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) ruby-progressbar (1.1.1) sass (3.2.9) sass-rails (4.0.0.rc1) @@ -114,6 +147,8 @@ GEM sdoc (0.3.20) json (>= 1.1.3) rdoc (~> 3.10) + shoulda-matchers (2.4.0) + activesupport (>= 3.0.0) slop (3.4.5) sprockets (2.10.0) hike (~> 1.2) @@ -141,13 +176,20 @@ GEM uglifier (2.1.1) execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) + xpath (2.0.0) + nokogiri (~> 1.3) yard (0.8.7) PLATFORMS ruby DEPENDENCIES + capybara coffee-rails (~> 4.0.0) + database_cleaner + factory_girl + factory_girl_rails + faker haml jbuilder (~> 1.0.1) jquery-rails @@ -157,9 +199,12 @@ DEPENDENCIES pry-doc pry-rails rails (= 4.0.0.rc1) + rspec + rspec-rails ruby-progressbar sass-rails (~> 4.0.0.rc1) sdoc + shoulda-matchers sqlite3 therubyracer turbolinks diff --git a/app/models/transaction.rb b/app/models/transaction.rb index ee2cc2b..4258869 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -2,14 +2,13 @@ class Transaction < ActiveRecord::Base # pm_types # 0 Withdrawal # 1 Deposit - # 2 Transfer - # 3 + # 2 Transfer (-) + # 3 Transfer (+) # 4 # 5 Dunno but removed it for now # scope :search, ->(q) { where "payee_name like ? OR uuid = ?", "%#{q}%", q } - scope :uuid, ->(uuid) { where(uuid:uuid).first } scope :active, -> { where(deleted:false).where('transactions.pm_type <> 5') } scope :order_date, -> { order('date desc') } scope :cleared, -> { where(cleared:true) } @@ -23,7 +22,8 @@ class Transaction < ActiveRecord::Base belongs_to :account has_many :splits - + validates :account_id, presence: true + validates :amount, presence: true def split? @split ||= splits.size > 1 diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erb index 9258302..984c17f 100644 --- a/app/views/transactions/_form.html.erb +++ b/app/views/transactions/_form.html.erb @@ -31,14 +31,14 @@ <%= f.label :category_id %>
<%= f.number_field :category_id %> -
- <%= f.label :class_id %>
- <%= f.number_field :class_id %> -
-
- <%= f.label :memo %>
- <%= f.text_area :memo %> -
+ <%#
%> + <%#<%= f.label :class_id %>
%> + <%#<%= f.number_field :class_id %>%> + <%#
%> + <%#
%> + <%#<%= f.label :memo %>
%> + <%#<%= f.text_area :memo %>%> + <%#
%>
<%= f.label :amount %>
<%= f.text_field :amount %> @@ -47,18 +47,18 @@ <%= f.label :cleared %>
<%= f.check_box :cleared %>
-
- <%= f.label :currency_id %>
- <%= f.text_field :currency_id %> -
-
- <%= f.label :currency_exchange_rate %>
- <%= f.text_field :currency_exchange_rate %> -
-
- <%= f.label :balance %>
- <%= f.text_field :balance %> -
+ <%#
%> + <%#<%= f.label :currency_id %>
%> + <%#<%= f.text_field :currency_id %>%> + <%#
%> + <%#
%> + <%#<%= f.label :currency_exchange_rate %>
%> + <%#<%= f.text_field :currency_exchange_rate %>%> + <%#
%> + <%#
%> + <%#<%= f.label :balance %>
%> + <%#<%= f.text_field :balance %>%> + <%#
%>
<%= f.submit %>
diff --git a/app/views/transactions/_transaction.haml b/app/views/transactions/_transaction.haml index b1adf79..b4a566b 100644 --- a/app/views/transactions/_transaction.haml +++ b/app/views/transactions/_transaction.haml @@ -3,7 +3,7 @@ %td= link_to transaction.date.to_s(:short_date), transaction %td= transaction.account.name %td= transaction.payee_name - %td= transaction.category_name + %td= transaction.category_name %td.currency= money transaction.amount - if show_balance diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erb index 88f1c1a..98f3edb 100644 --- a/app/views/transactions/show.html.erb +++ b/app/views/transactions/show.html.erb @@ -25,15 +25,6 @@ <%= @transaction.category_id %>

-

- Class: - <%= @transaction.class_id %> -

- -

- Memo: - <%= @transaction.memo %> -

Amount: @@ -45,20 +36,7 @@ <%= @transaction.cleared %>

-

- Currency: - <%= @transaction.currency_id %> -

- -

- Currency exchange rate: - <%= @transaction.currency_exchange_rate %> -

-

- Balance: - <%= @transaction.balance %> -

<%= link_to 'Edit', edit_transaction_path(@transaction) %> | <%= link_to 'Back', transactions_path %> diff --git a/db/migrate/20130531005809_create_transactions.rb b/db/migrate/20130531005809_create_transactions.rb index 9ca3316..8e02e5b 100644 --- a/db/migrate/20130531005809_create_transactions.rb +++ b/db/migrate/20130531005809_create_transactions.rb @@ -19,7 +19,7 @@ def change t.integer :payee_id t.integer :category_id t.integer :department_id - t.decimal :amount + t.decimal :amount, :precision => 10, :scale => 2 t.boolean :cleared t.string :uuid diff --git a/db/schema.rb b/db/schema.rb index af83078..3965c60 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,9 +13,6 @@ ActiveRecord::Schema.define(version: 20130718222102) do - # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" - create_table "accounts", force: true do |t| t.boolean "deleted" t.datetime "updated_at" @@ -134,7 +131,7 @@ t.integer "payee_id" t.integer "category_id" t.integer "department_id" - t.decimal "amount" + t.decimal "amount", precision: 10, scale: 2 t.boolean "cleared" t.string "uuid" t.datetime "created_at" diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..34aafb4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,133 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). -# -# Examples: -# -# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) -# Mayor.create(name: 'Emanuel', city: cities.first) +boolean = [false] #To have inactive transactions, add true at array [true, false] +currency = ["AED", "ALL", "CAD", "CNY", "MXN", "IRR", "JPY", "USD", "UYU"] +#Seed to Account table +15.times do + Account.create( + deleted: false, + pm_id: rand(0..8), + pm_account_type: rand(0..8), + display_order: rand(1..4), + name: Faker::Company.name, + balance_overall: rand*(5), + balance_cleared: rand*(5), + number: Faker::Number.number(2), + institution: Faker::Company.suffix, + phone: Faker::PhoneNumber.cell_phone, + expiration_date: Faker::Business.credit_card_expiry_date.strftime("%d,%m,%Y"), + check_number: Faker::Number.digit, + notes: Faker::Lorem.paragraph, + pm_icon: "image", + url: Faker::Internet.url, + of_x_id: "dummy", + of_x_url: Faker::Internet.domain_word, + password: Faker::Internet.password, + fee: rand*(100), + fixed_percent: rand*(20), + limit_amount: 1000 + rand*(9_000), + limit: boolean.sample, + total_worth: boolean.sample, + exchange_rate: rand*(10), + currency_code: currency.sample, + last_sync_time: (Time.now - rand(60).days).to_date, + routing_number: rand(100), + overdraft_account_id: rand(30).to_s, + keep_the_change_account_id: rand(30).to_s, + heek_change_round_to: rand*(5), + uuid: Faker::Code.isbn(64) + ) + #seed to category table + Category.create( + name: Faker::Commerce.department, + deleted: boolean.sample, + pm_id: rand(0..8), + pm_type: rand(0..2), + budget_period: rand(100..10000), + budget_limit: rand(500..5000), + include_subcategories: boolean.sample, + rollover: boolean.sample, + uuid: Faker::Code.isbn + ) + #seed to department table + Department.create( + name: Faker::Commerce.product_name, + pm_id: rand(0..8), + uuid: Faker::Code.isbn, + deleted: boolean.sample + ) + #seed to Payee table + Payee.create( + name: Faker::Name.name, + deleted: boolean.sample, + pm_id: rand(0..8), + latitude: Faker::Address.latitude, + longitude: Faker::Address.longitude, + uuid: Faker::Code.isbn + ) +end + + +40.times do + payee = Payee.all.sample + account = Account.all.sample + amount = 10 + rand*(10_000) + + #seed to transaction table + Transaction.create( + pm_type: rand(0..2), + pm_id: rand(0..8), + account_id: account.id, + pm_account_id: account.pm_id, + pm_payee: payee.pm_id, + pm_sub_total: rand*(1_000), + pm_of_x_id: "dummy", + pm_image: "dummy", + pm_overdraft_id: rand(30).to_s, + date: Time.now - rand(0..60).days, + deleted: boolean.sample, + check_number: rand(10).to_s, + payee_name: payee.name, + payee_id: payee.id, + category_id: Category.all.map(&:id).sample, + department_id: Department.all.map(&:id).sample, + amount: amount, + cleared: boolean.sample, + uuid: Faker::Code.isbn + ) + #create split to transaction + transaction = Transaction.last + num_of_split = rand(1..5) + max_amount_split = amount/(num_of_split - 1) + sum_split_amount = 0 + (num_of_split - 1).times do + split_amount = 1 + rand(max_amount_split) + sum_split_amount += split_amount + Split.create( + pm_id: transaction.pm_id, + transaction_id: transaction.id, + amount: split_amount, + xrate: rand*(5), + category_id: transaction.category_id, + class_id: rand(0..10), + memo: Faker::Lorem.paragraph, + transfer_to_account_id: rand(100), + currency_code: currency.sample, + of_x_id: "dummy" + ) + end + last_split_amount = amount - sum_split_amount + Split.create( + pm_id: transaction.pm_id, + transaction_id: transaction.id, + amount: last_split_amount, + xrate: rand*(5), + category_id: transaction.category_id, + class_id: rand(0..10), + memo: Faker::Lorem.paragraph, + transfer_to_account_id: rand(100), + currency_code: currency.sample, + of_x_id: "dummy" + ) + +end + + diff --git a/lib/pocket_money/tables.rb b/lib/pocket_money/tables.rb index 9de66b2..950c76f 100644 --- a/lib/pocket_money/tables.rb +++ b/lib/pocket_money/tables.rb @@ -1,4 +1,3 @@ - class PocketMoney module BaseTable def self.included(main) @@ -10,7 +9,6 @@ def self.included(main) class Accounts < ActiveRecord::Base include BaseTable - end class Categories < ActiveRecord::Base include BaseTable diff --git a/spec/PocketMoneyDBtest.sql b/spec/PocketMoneyDBtest.sql new file mode 100644 index 0000000..1af63a8 Binary files /dev/null and b/spec/PocketMoneyDBtest.sql differ diff --git a/spec/controllers/transactions_controller_spec.rb b/spec/controllers/transactions_controller_spec.rb new file mode 100644 index 0000000..3407b85 --- /dev/null +++ b/spec/controllers/transactions_controller_spec.rb @@ -0,0 +1,114 @@ +require 'spec_helper' +describe TransactionsController do + let(:transaction) { Transaction.first } + #spec to GET methods + describe "Get index" do + it "should render the index templates" do + get :index + expect(response).to render_template("index") + end + it "should response with 200 http code" do + get :index + response.should be_succes + end + end + + describe 'GET #new' do + it 'should render new template' do + get :new + response.should render_template :new + end + it 'should response with 200 code' do + get :new + response.should be_success + end + end + + describe 'GET #show' do + it 'should redirect to #show view' do + get :show, id: transaction + response.should render_template :show + end + it 'should response with 200 http code' do + get :show, id: transaction + response.should be_success + end + end + + describe 'GET #edit' do + it 'should render #edit view' do + get :edit, id: transaction + response.should render_template :edit + end + it 'should response with 200 http code' do + get :edit, id: transaction + response.should be_success + end + end + #spec to post methods + describe 'POST #create' do + context "valid attributes" do + it 'should create a new transaction' do + expect{ + post :create, transaction: attributes_for(:transaction) + }.to change(Transaction, :count).by(1) + end + it 'should redirect to #show view' do + post :create, transaction: attributes_for(:transaction) + response.should redirect_to Transaction.last + end + end + context "invalid attributes" do + it 'should not create a new transaction' do + expect{ + post :create, transaction: attributes_for(:transaction, account_id: nil, amount: nil) + }.to_not change(Transaction, :count) + end + it 'should render to #new view' do + post :create, transaction: attributes_for(:transaction, account_id: nil, amount: nil) + response.should render_template :new + end + + end + end + + #spec to put method + describe 'PUT #update' do + new_uuid = "12yag172-1721nh-12" + new_amount = 99_999 + context 'valid attributes' do + it 'should located the requested transaction' do + put :update, id: transaction, transaction: FactoryGirl.attributes_for(:transaction) + assigns(:transaction).should eq(transaction) + end + it "should change the transaction's attributes" do + put :update, id: transaction, + transaction: FactoryGirl.attributes_for(:transaction, amount: new_amount) + transaction.reload + transaction.amount.should eq(new_amount) + end + it "should redirect to #show view" do + put :update, id: transaction, transaction: FactoryGirl.attributes_for(:transaction) + response.should redirect_to transaction + end + end + context 'invalid attributes' do + it 'should not located the requested boook' do + put :update, id: transaction, transaction: FactoryGirl.attributes_for(:transaction, uuid: nil, amount: nil) + assigns(:transaction2).should_not eq(transaction) + end + it "should not change the transaction's attributes" do + put :update, id: transaction, + transaction: FactoryGirl.attributes_for(:transaction, uuid: nil, amount: nil) + transaction.reload + transaction.amount.should_not eq(new_amount) + transaction.uuid.should_not eq(new_uuid) + end + it "should redirect to #edit view" do + put :update, id: transaction, + transaction: attributes_for(:transaction, uuid: nil, amount: nil) + response.should render_template :edit + end + end + end +end diff --git a/spec/factories/account.rb b/spec/factories/account.rb new file mode 100644 index 0000000..f52af00 --- /dev/null +++ b/spec/factories/account.rb @@ -0,0 +1,39 @@ +require 'faker' + +boolean = [true, false] +currency = ["AED", "ALL", "CAD", "CNY", "MXN", "IRR", "JPY", "USD", "UYU"] +FactoryGirl.define do + factory :account do + deleted false + pm_id (0..2).to_a.sample + pm_account_type (0..8).to_a.sample + display_order (1..9).to_a.sample + name Faker::Company.name + balance_overall (1..9).to_a.sample + balance_cleared (1..9).to_a.sample + number Faker::Number.number(2) + institution Faker::Company.suffix + phone Faker::PhoneNumber.cell_phone + expiration_date Faker::Business.credit_card_expiry_date.strftime("%d%m%Y") + check_number (1..9).to_a.sample + notes Faker::Lorem.paragraph + pm_icon "image" + url Faker::Internet.url + of_x_id "dummy" + of_x_url Faker::Internet.domain_word + password Faker::Internet.password + fee (1..9).to_a.sample + fixed_percent (1..9).to_a.sample + limit_amount (1..9).to_a.sample + limit boolean.sample + total_worth boolean.sample + exchange_rate (1..9).to_a.sample + currency_code currency.sample + last_sync_time Time.now + routing_number (1..9).to_a.sample + overdraft_account_id (1..9).to_a.sample + keep_the_change_account_id (1..9).to_a.sample + heek_change_round_to (1..9).to_a.sample + uuid Faker::Code.isbn(64) + end +end diff --git a/spec/factories/category.rb b/spec/factories/category.rb new file mode 100644 index 0000000..018c146 --- /dev/null +++ b/spec/factories/category.rb @@ -0,0 +1,15 @@ +require 'faker' + +FactoryGirl.define do + factory :category do + name Faker::Commerce.department + deleted false + pm_id (0..8).to_a.sample + pm_type (0..2).to_a.sample + budget_period (100..1000).to_a.sample + budget_limit (50..500).to_a.sample + include_subcategories false + rollover false + uuid Faker::Code.isbn + end +end diff --git a/spec/factories/department.rb b/spec/factories/department.rb new file mode 100644 index 0000000..a3b4e5d --- /dev/null +++ b/spec/factories/department.rb @@ -0,0 +1,11 @@ +require 'faker' + +FactoryGirl.define do + factory :department do + name Faker::Commerce.product_name + pm_id (0..8).to_a.sample + uuid Faker::Code.isbn + deleted false + end +end + diff --git a/spec/factories/payee.rb b/spec/factories/payee.rb new file mode 100644 index 0000000..e967919 --- /dev/null +++ b/spec/factories/payee.rb @@ -0,0 +1,13 @@ +require 'faker' + +FactoryGirl.define do + factory :payee do + name Faker::Name.name + deleted false + pm_id (0..8).to_a.sample + latitude Faker::Address.latitude + longitude Faker::Address.longitude + uuid Faker::Code.isbn + + end +end diff --git a/spec/factories/split.rb b/spec/factories/split.rb new file mode 100644 index 0000000..41e5390 --- /dev/null +++ b/spec/factories/split.rb @@ -0,0 +1,18 @@ +#require 'faker' +#currency = ["AED", "ALL", "CAD", "CNY", "MXN", "IRR", "JPY", "USD", "UYU"] +#transaction = FactoryGirl.create(:transaction) +#FactoryGirl.define do + #factory :split do + #pm_id transaction.pm_id + #transaction_id transaction.id + #amount transaction.amount + #xrate (1..10).to_a.sample + #category_id transaction.category_id + #class_id (0..10).to_a.sample + #memo Faker::Lorem.paragraph + #transfer_to_account_id (1..100).to_a.sample + #currency_code currency.sample + #of_x_id "dummy" + #end +#end + diff --git a/spec/factories/transaction.rb b/spec/factories/transaction.rb new file mode 100644 index 0000000..60afb1a --- /dev/null +++ b/spec/factories/transaction.rb @@ -0,0 +1,50 @@ +require 'faker' + +currency = ["AED", "ALL", "CAD", "CNY", "MXN", "IRR", "JPY", "USD", "UYU"] +account = FactoryGirl.create(:account) +payee = FactoryGirl.create(:payee) +department = FactoryGirl.create(:department) +category = FactoryGirl.create(:category) + + +FactoryGirl.define do + factory :transaction do + pm_type (0..2).to_a.sample + pm_id (0..8).to_a.sample + account_id account.id + pm_account_id account.pm_id + pm_payee payee.name + pm_sub_total (10..10_000).to_a.sample + pm_of_x_id "dummy" + pm_image "dummy" + pm_overdraft_id (1..30).to_a.sample.to_s + date Time.now + deleted false + check_number (1..10).to_a.sample + payee_name payee.name + payee_id payee.pm_id + category_id category.id + department_id department.id + amount Faker::Number.number(2) + cleared true + uuid Faker::Code.isbn + end +end + +transaction = FactoryGirl.create(:transaction) + +FactoryGirl.define do + factory :split do + pm_id transaction.pm_id + transaction_id transaction.id + amount transaction.amount + xrate (1..10).to_a.sample + category_id transaction.category_id + class_id (0..10).to_a.sample + memo Faker::Lorem.paragraph + transfer_to_account_id (1..100).to_a.sample + currency_code currency.sample + of_x_id "dummy" + end +end + diff --git a/spec/features/see_total_amount_per_account_spec.rb b/spec/features/see_total_amount_per_account_spec.rb new file mode 100644 index 0000000..fa34ca0 --- /dev/null +++ b/spec/features/see_total_amount_per_account_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' +feature "check total amount per account" do + + before(:each) do + visit accounts_path + end + + scenario "cheack all categories" do + page.should have_content("Efectivo") + page.should have_content("$550.00") + within ('table') do + page.should have_content("$4,490.00") + page.should have_content("Tarjeta") + page.should have_content("-$250") + page.should have_content("$3,050.00") + page.should have_content("Cuenta Ahorro") + page.should have_content("$0.00") + page.should have_content("$950.00") + end + end + + scenario "Check Efectivo" do + click_link "Efectivo" + page.should have_content("(17)") + page.should have_content("$4,490.00") + within('table') do + page.should have_content("10/16/13") + page.should have_content("$1,750.00") + page.should have_content("10/17/13") + page.should have_content("$640.00") + page.should have_content("10/18/13") + page.should have_content("$150.00") + page.should have_content("10/23/13") + page.should have_content("$500.00") + page.should have_content("10/31/13") + page.should have_content("$1,750.00") + end + end + + scenario "Check Tarjeta" do + click_link "Tarjeta" + page.should have_content("(11)") + page.should have_content("$3,050.00") + within ('table') do + page.should have_content("10/07/13") + page.should have_content("-$2,500.00") + page.should have_content("10/17/13") + page.should have_content("$1,250.00") + page.should have_content("10/18/13") + page.should have_content("$4,300.00") + end + end + + scenario "Cuenta Ahorro" do + click_link "Cuenta Ahorro" + page.should have_content("(9)") + page.should have_content("$950.00") + within ('table') do + page.should have_content("10/18/13") + page.should have_content("$1,450.00") + page.should have_content("10/23/13") + page.should have_content("-$500.00") + end + end +end diff --git a/spec/features/see_total_amount_per_category_spec.rb b/spec/features/see_total_amount_per_category_spec.rb new file mode 100644 index 0000000..1c45576 --- /dev/null +++ b/spec/features/see_total_amount_per_category_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' +feature 'check total amount per category' do + before(:each) do + visit root_path + click_link "This Year" + end + + scenario 'Per category' do + page.should have_content('Salario') + page.should have_content('$3,500.00') + page.should have_content('Personal') + page.should have_content('$8,900.00') + page.should have_content("Automóvil") + page.should have_content('-$200.00') + page.should have_content('Entretenimiento') + page.should have_content('$0.00') + end + + scenario "Salario Category" do + click_link "Salario" + page.should have_content("Deposit") + page.should have_content("$3,500.00") + within('table') do + page.should have_content("10/16/13") + page.should have_content("10/31/13") + page.should have_content("$1,750.00") + end + end + + scenario "Personal Category" do + click_link "Personal" + page.should have_content("Deposit") + page.should have_content("$9,000.00") + page.should have_content("Widthdrawals") + page.should have_content("$100.00") + within('table') do + page.should have_content("10/18/13") + page.should have_content("$6,500.00") + page.should have_content("10/17/13") + page.should have_content("$2,400.00") + end + end + + scenario "Automovil Category" do + click_link "Automóvil" + page.should have_content("Widthdrawals") + page.should have_content("$200.00") + within('table') do + page.should have_content("10/17/13") + page.should have_content("-$200.00") + end + end + + scenario "Caridad Category" do + click_link "Caridad" + page.should have_content("Transfer") + page.should have_content("$1,800.00") + page.should have_content("Widthdrawals") + page.should have_content("$1,000.00") + within('table') do + page.should have_content("10/18/13") + page.should have_content("-$1,000.00") + end + end + +end diff --git a/spec/features/see_total_amount_per_transaction_type_spec.rb b/spec/features/see_total_amount_per_transaction_type_spec.rb new file mode 100644 index 0000000..6179d7d --- /dev/null +++ b/spec/features/see_total_amount_per_transaction_type_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' +feature 'check total amount per transaction type' do + + before(:each) do + visit root_path + click_link "This Year" + end + + scenario "Deposit Transaction" do + click_link "Deposit" + page.should have_content("$12,600.00") + within('table') do + page.should have_content("10/16/13") + page.should have_content("$1,750.00") + page.should have_content("10/17/13") + page.should have_content("$2,500.00") + page.should have_content("10/18/13") + page.should have_content("$6,600.00") + page.should have_content("10/31/13") + page.should have_content("$1,750.00") + end + end + + scenario "Transfer Transaction" do + click_link "Transfer" + page.should have_content("$5,000.00") + within('table') do + page.should have_content("10/17/13") + page.should have_content("-$1,250.00") + page.should have_content("10/18/13") + page.should have_content("-$3,250.00") + page.should have_content("10/23/13") + page.should have_content("-$500.00") + end + end + + scenario "Widthdrawals Transaction" do + click_link "Widthdrawals" + page.should have_content("$4,110.00") + within('table') do + page.should have_content("10/07/13") + page.should have_content("-$2,500.00") + page.should have_content("10/17/13") + page.should have_content("-$610.00") + page.should have_content("10/18/13") + page.should have_content("-$1,000.00") + end + end + +end diff --git a/spec/import_pocket_money.rb b/spec/import_pocket_money.rb new file mode 100644 index 0000000..c936616 --- /dev/null +++ b/spec/import_pocket_money.rb @@ -0,0 +1,6 @@ +Bernard::Application.load_tasks +Rake::Task['db:drop'].invoke +Rake::Task['db:create'].invoke +Rake::Task['db:migrate'].invoke +PocketMoney.import + diff --git a/spec/lib/pocket_money_spec.rb b/spec/lib/pocket_money_spec.rb new file mode 100644 index 0000000..60f4eff --- /dev/null +++ b/spec/lib/pocket_money_spec.rb @@ -0,0 +1,186 @@ +require 'spec_helper' +require 'active_support/core_ext/date/acts_like' +require 'rake' +require 'pocket_money' + +describe PocketMoney do + context "import data for a known database" do + let(:accounts) { Account.all } + let(:transactions) { Transaction.all } + let(:categories) { Category.all } + let(:payees) { Payee.all } + let(:departments) { Department.all } + let(:splits) { Split.all } + let(:repeating_transactions) { RepeatingTransaction.all } + + #Table accounts + it "should have 3 accounts" do + (accounts.count).should eq(3) + end + + it "should have 3 knowed account" do + (accounts.map(&:name).sort).should eq(["Cuenta Ahorro", "Efectivo", "Tarjeta"]) + end + + it "should have $1,650.00 current balance in 'cuenta ahorro'" do + (accounts.find_by_name("Cuenta Ahorro").current_balance.to_f).should eq(950) + end + + it "the second account should have a limit of $100" do + (accounts[1].limit_amount.to_f).should eq(100) + end + + it "all limit amount sum $600" do + (accounts.map(&:limit_amount).sum.to_f).should eq(600) + end + + it "should have $740 current balance in 'efectivo'" do + (accounts.find_by_name("Efectivo").current_balance.to_f).should eq(2740) + end + + it "should have $350 current balance in 'tarjeta'" do + (accounts.find_by_name("Tarjeta").current_balance.to_f).should eq(3050) + end + + #table transactions + it "should have 37 transactions" do + (transactions.count).should eq(37) + end + + it "the first transaction should have $1750 of amount" do + (transactions.first.amount.to_f).should eq(1750) + end + + it "should have transactions with transaction type" do + types = transactions.map(&:pm_type) + types.include?(nil).should eq(false) + types.include?("").should + end + + it "should have transactions with valid account id" do + transactions_account_id = transactions.map(&:account_id) + ids_accounts = accounts.map(&:id) + transactions_account_id.each do |account_id| + (ids_accounts.include?(account_id)).should eq(true) + end + end + + it "should have transactions with amount" do + amount_array = transactions.map(&:amount) + amount_array.include?(nil).should eq(false) + amount_array.include?("").should eq(false) + end + + it "should have transactions with valid date" do + dates = transactions.map(&:date) + dates.each do |date| + (date.acts_like?(:time)).should eq(true) + end + end + + it "first transaction should have category id 16" do + transaction = transactions.first + (transaction.category_id).should eq(16) + end + + it "first transaction should have class personal" do + transaction = transactions.first + (transaction.department_id).should eq(1) + end + + #table split + it "all aplits should have a transaction id" do + all_splits = splits.map(&:transaction_id) + all_splits.include?(nil).should eq(false) + all_splits.include?("").should eq(false) + end + + it "all splits should have a transaction with currency code" do + currency_codes = splits.map(&:currency_code) + currency_codes.include?(nil).should eq(false) + currency_codes.include?("").should eq(false) + end + + it "the sum of the splits of a transactions should have same amount that transaction " do + 35.times do |count| + transaction = transactions[count] + splits = transaction.splits + (splits.map(&:amount).sum.to_f).should eq(transaction.amount) + end + end + + it "first split should have a class id 1" do + split = splits.first + (split.class_id).should eq(1) + end + + it "amount of transaction 2 should equal at transaction 37" do + transaction_2 = transactions[1].amount.to_f + transaction_37 = transactions[36].amount.to_f + transaction_2.should eq(transaction_37) + end + + it "amount of transaction 4 should equal at transaction 36" do + transaction_4 = transactions[3].amount.to_f + transaction_36 = transactions[35].amount.to_f + transaction_4.should eq(transaction_36) + end + + #table department <--- class + it "should have personal and negocios classes" do + (departments.map(&:name)).should eq(["Personal", "Negocios"]) + end + + it "all class should have a uuid" do + departments_uuids = departments.map(&:uuid) + departments_uuids.include?(nil).should eq(false) + departments_uuids.include?("").should eq(false) + end + + #table category + it "should have 20 Categories" do + (categories.count).should eq(20) + end + + it "all categories should have a uuid" do + uuids = categories.map(&:uuid) + uuids.include?(nil).should eq(false) + uuids.include?("").should eq(false) + end + + it "should include 'Salario' category" do + names = categories.map(&:name) + names.include?("Salario").should eq(true) + end + + #table Payee + it "should have 9 payees" do + (payees.count).should eq(9) + end + + it "should include 'TangoSource' payee" do + names = payees.map(&:name) + names.include?("TangoSource").should eq(true) + end + + it "Last payee should be 'Ahorro' " do + (payees.last.name).should eq("Ahorro") + end + + #table repeating_transactions + it "should have 2 repeating transactions" do + (repeating_transactions.count).should eq(2) + end + + it "last repeating transaction should have a valid date" do + transaction = repeating_transactions.last + (transaction.end_date.acts_like?(:time)).should eq(true) + end + + it "transaction 36 should have pm_type 5" do + transaction = transactions[35] + (transaction.pm_type).should eq(5) + end + + end +end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb new file mode 100644 index 0000000..55e3e25 --- /dev/null +++ b/spec/models/account_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe Account do + let(:account) {Account.first} + + it "has a valid account" do + account.should be_valid + end + + #scopes + it "should return 14 transaction for account id 1" do + (Account.find(1).active_transactions.count).should eq(14) + end + + it "should return 9 transaction for account id 2" do + (Account.find(2).active_transactions.count).should eq(9) + end + + it "should return 7 transaction for account id 3" do + (Account.find(3).active_transactions.count).should eq(7) + end +end + diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 0000000..ad00902 --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe Category do + let(:category) {Category.first} + + it "has a valid category" do + category.should be_valid + end +end diff --git a/spec/models/department_spec.rb b/spec/models/department_spec.rb new file mode 100644 index 0000000..5331ad8 --- /dev/null +++ b/spec/models/department_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe Department do + let(:department) { Department.first } + + it "has avalid department" do + department.should be_valid + end +end diff --git a/spec/models/payee_spec.rb b/spec/models/payee_spec.rb new file mode 100644 index 0000000..3f3a157 --- /dev/null +++ b/spec/models/payee_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe Payee do + let(:payee) { Payee.first } + + it "has a valid payee" do + payee.should be_valid + end +end diff --git a/spec/models/split_spec.rb b/spec/models/split_spec.rb new file mode 100644 index 0000000..ba2b473 --- /dev/null +++ b/spec/models/split_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe Split do + let(:split) { Split.first } + + it "has a valid split" do + split.should be_valid + end +end + diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb new file mode 100644 index 0000000..4cfb4dd --- /dev/null +++ b/spec/models/transaction_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe Transaction do + + let(:transactions) { Transaction.all } + + it { should belong_to(:account) } + + it { should have_many(:splits) } + + it "has a valid transaction" do + (transactions.sample).should be_valid + end + + #scopes + it "should have 33 active transactions" do + transactions.active.count.should eq(33) + end + + it "should find a transaction given a payee name" do + (transactions.search("TangoSource").count).should eq(2) + end + + it "should return records with cleared value true " do + (transactions.cleared.count).should eq(6) + end + + it "should return records valid given two dates" do + (transactions.interval("20131016", "20131021").count).should eq(32) + end + + it "not should return records given two dates known" do + (transactions.interval("19991016", "20021021").count).should eq(0) + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..cf0c582 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,22 @@ +ENV["RAILS_ENV"] ||= 'test' +require File.expand_path("../../config/environment", __FILE__) +require 'import_pocket_money' +require 'rspec/rails' +require 'rspec/autorun' +require 'factory_girl_rails' + +Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } + +ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) + +RSpec.configure do |config| + config.include FactoryGirl::Syntax::Methods + config.treat_symbols_as_metadata_keys_with_true_values = true + config.use_transactional_fixtures = true + + config.infer_base_class_for_anonymous_controllers = false + + config.order = "random" + + +end