Skip to content

Commit

Permalink
Re-including boot back in default lib file, added tests, got Rack::Te…
Browse files Browse the repository at this point in the history
…st working
  • Loading branch information
winton committed Jun 21, 2009
1 parent 68a97ab commit 799f8e0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
1 change: 0 additions & 1 deletion bin/sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

$:.push File.expand_path("#{File.dirname(__FILE__)}/../lib")
require 'sum'
require 'sum/boot'

Application.class_eval do
set :environment, ENV['RACK_ENV'] || :production
Expand Down
1 change: 0 additions & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "#{File.dirname(__FILE__)}/lib/sum"
require "#{File.dirname(__FILE__)}/lib/sum/boot"

run Application
2 changes: 1 addition & 1 deletion lib/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class Application < Sinatra::Base
end

Dir["#{File.dirname(__FILE__)}/sum/**/*.rb"].each do |path|
require path unless path.include?("/boot.rb")
require path
end
3 changes: 2 additions & 1 deletion lib/sum/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

enable :raise_errors

set :environment, $TESTING ? :test : environment
set :root, File.expand_path("#{File.dirname(__FILE__)}/../../")
set :public, "#{root}/public"
set :logging, true
Expand All @@ -12,7 +13,7 @@
$db, $log = ActiveWrapper.setup(
:base => root,
:env => environment,
:stdout => true
:stdout => environment != :test
)
$db.establish_connection

Expand Down
5 changes: 5 additions & 0 deletions lib/sum/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class User < ActiveRecord::Base

serialize :recent_transactions

validates_format_of(
:email,
:with => /^[_a-z0-9\+\.\-]+\@[_a-z0-9\-]+\.[_a-z0-9\.\-]+$/i,
:unless => lambda { |r| r.email.blank? }
)
validates_numericality_of :bills, :income, :savings
validates_presence_of :email

Expand Down
7 changes: 2 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

require 'sum'
require 'pp'
require 'spec/interop/test'
require 'rack/test'

Spec::Runner.configure do |config|
$db, $log = ActiveWrapper.setup(
:base => File.expand_path("#{SPEC}/../"),
:env => 'test'
)
$db.establish_connection
$db.migrate_reset
end

Expand Down
16 changes: 16 additions & 0 deletions spec/sum/controller/front_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe 'Front Controller' do
include Rack::Test::Methods

def app
Application
end

describe '/' do
it "get responds ok" do
get '/'
last_response.should be_ok
end
end
end
47 changes: 45 additions & 2 deletions spec/sum/model/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,51 @@
@user.spent_this_month.should == 0.00
@user.temporary_spending_cut.should == 0.00
@user.timezone_offset.should == -25200
@user.reset_at.to_s.should == 1.month.from_now.utc.to_s
@user.send_at.to_s.should == 1.day.from_now.utc.to_s
@user.send_at.to_s.should == (@user.send(:next_5am, Time.now) - @user.timezone_offset).to_s
@user.reset_at.to_s.should == (@user.send_at + 1.month).to_s
end

it "should make the spending goal equal to income minus bills minus savings" do
@user.spending_goal.should == @user.income - @user.bills - @user.savings
end

it "should make the savings goal equal to savings" do
@user.savings_goal.should == @user.savings
end

it "should send email at next 5am" do
send_at = @user.send_at + @user.timezone_offset
now = Time.now.utc + @user.timezone_offset
[ now, now + 1.day ].include?(send_at.day) == true
send_at.hour.should == 5
send_at.min.should == 0
send_at.should > now
end

it "should reset balances one month from next 5am" do
@user.reset_at.should == @user.send_at + 1.month
end
end

describe "invalid submission" do

before(:each) do
@user = User.create(
:email => "",
:bills => "",
:income => "test",
:savings => "0"
)
end

it "should create errors on the proper attributes" do
@user.errors.on(:email).should == "can't be blank"
@user.errors.on(:bills).should == 'is not a number'
@user.errors.on(:income).should == 'is not a number'
@user.errors.on(:savings).nil?.should == true
@user.email = 'test'
@user.valid?
@user.errors.on(:email).should == 'is invalid'
end
end
end

0 comments on commit 799f8e0

Please sign in to comment.