Skip to content

Commit

Permalink
Added tests for InvoiceItem, Invoices, Timeslips, Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrussell committed Oct 26, 2009
1 parent 33689f5 commit f53d1d0
Show file tree
Hide file tree
Showing 18 changed files with 1,125 additions and 142 deletions.
114 changes: 33 additions & 81 deletions lib/freeagent_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TaxTimeline < Base
# :first_name
# :last_name
#
# contact = Contact.new :first_name => 'Joe', :last_name => 'Bloggs'
# contact = Contact.new params
# contact.save
#
# Update contact
Expand Down Expand Up @@ -98,107 +98,59 @@ class Contact < Base
class Project < Base

def invoices
Invoice.find :all, :params => {:project_id => id}
Invoice.find :all, :from => "/projects/#{id}/invoices.xml"
end

def tasks
Task.find :all, :params => {:project_id => id}
end


def timeslips
Timeslip.find :all, :params => {:project_id => id}
Timeslip.find :all, :from => "/projects/#{id}/timeslips.xml"
end

end

# Find invoices
#
# Invoice.find :all, :params => {:project_id => project_id}
# Invoice.find task_id
#
#TODO Create invoice
#
#TODO Update invoice
#
#TODO Delete project
#
##TODO add Change status methods
# /invoices/invoice_id/mark_as_draft
# /invoices/invoice_id/mark_as_sent
# /invoices/invoice_id/mark_as_cancelled

# Tasks - Complete

class Task < Base
self.prefix = '/projects/:project_id/'
end

# Invoices - Complete

class Invoice < Base
self.prefix = '/projects/:project_id/'

def mark_as_draft
connection.put("/invoices/#{id}/mark_as_draft.xml", encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
def mark_as_sent
connection.put("/invoices/#{id}/mark_as_sent.xml", encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
def mark_as_cancelled
connection.put("/invoices/#{id}/mark_as_cancelled.xml", encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end

end

# Find invoice items
#
# InvoiceItem.find :all, :params => {:invoice_id => invoice_id}
# InvoiceItem.find invoice_item_id, :params => {:invoice_id => invoice_id}
#
#TODO Create invoice item
#
# Invoice items - Complete

class InvoiceItem < Base
self.prefix = '/invoices/:invoice_id/'
end

# Timeslips

# Find tasks
#
# Task.find :all
# Task.find :all, :params => {:project_id => project_id}
# Task.find task_id
#
#TODO Create task
#
#TODO Update task
#
#TODO Delete project
#

class Task < Base

self.prefix = '/projects/:project_id/'
class Timeslip < Base

# def self.find(*args)
# opts = args.slice!(1) || {}
# opts = args.slice(1) || {}
# self.prefix = "/projects/#{opts[:params][:project_id]}/" if opts[:params] && opts[:params][:project_id]
# super
# end

end

# Find timeslips
#
# Timeslip.find :all, :params => {:view => '2009-01-01_2009-10-01'}
# Timeslip.find :all, :params => {:project_id => project_id}
# Timeslip.find :timeslip_id


class Timeslip < Base

def self.find(*args)
opts = args.slice!(1) || {}
self.prefix = "/projects/#{opts[:params][:project_id]}/" if opts[:params] && opts[:params][:project_id]
super
end

end

####################################################################################


class ActiveResource::Connection
def http
http = Net::HTTP.new(@site.host, @site.port)
http.use_ssl = @site.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
http.read_timeout = @timeout if @timeout
#Here's the addition that allows you to see the output
http.set_debug_output $stderr
return http
end
end

end
4 changes: 2 additions & 2 deletions test/contact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class ContactTest < Test::Unit::TestCase
:first_name => 'Fred',
:last_name => 'Bloggs'
}
@new_contact = Contact.new params
@contact = Contact.new params
end
should "validate and save" do
assert @new_contact.save_with_validation
assert @contact.save_with_validation
end
end

Expand Down
66 changes: 66 additions & 0 deletions test/invoice_item_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'test_helper'

class InvoiceItemTest < Test::Unit::TestCase

fake_it_all

context "InvoiceItem class" do
should "has correct collection path" do
assert_equal '/invoices/1000/invoice_items.xml', InvoiceItem.collection_path(:invoice_id => 1000)
end
should "has correct element path" do
assert_equal '/invoices/1000/invoice_items/first.xml', InvoiceItem.element_path(:first, :invoice_id => 1000)
assert_equal '/invoices/1000/invoice_items/1.xml', InvoiceItem.element_path(1, :invoice_id => 1000)
end
end

context "Invoice Items" do
setup do
@invoice_items = InvoiceItem.find :all, :params => {:invoice_id => 73867}
end
should "return an array" do
assert @invoice_items.is_a? Array
end
should "return Invoices" do
assert_equal 3, @invoice_items.size
assert @invoice_items.first.is_a? InvoiceItem
end
end

context "Invoice Item" do
setup do
@invoice_item = InvoiceItem.find 169399, :params => {:invoice_id => 73867}
end
should "return a Invoice Item" do
assert @invoice_item.is_a? InvoiceItem
end
should "update and save" do
@invoice_item.description = 'Create wireframe templates'
assert @invoice_item.save
end
should "be destroyed" do
assert @invoice_item.destroy
end
end

#TODO - Add test for invalid resource
# Need support from fakeweb in order to achieve this

context "New Invoice Item" do
setup do
params = {
:item_type => 'Hours',
:description => 'Create wireframe templates',
:quantity => '12',
:price => '50',
:sales_tax_rate => '15',
:invoice_id => '73867'
}
@invoice_item = InvoiceItem.new params
end
should "validate and save" do
assert @invoice_item.save_with_validation
end
end

end
92 changes: 50 additions & 42 deletions test/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,65 @@ class InvoiceTest < Test::Unit::TestCase

context "Invoice class" do
should "has correct collection path" do
assert_equal '/projects/1000/invoices.xml', Invoice.collection_path(:project_id => 1000)
assert_equal '/invoices.xml', Invoice.collection_path
end
should "has correct element path" do
assert_equal '/projects/1000/invoices/first.xml', Invoice.element_path(:first, :project_id => 1000)
assert_equal '/projects/1000/invoices/1.xml', Invoice.element_path(1, :project_id => 1000)
assert_equal '/invoices/first.xml', Invoice.element_path(:first)
assert_equal '/invoices/1.xml', Invoice.element_path(1)
end
end

# context "Contacts" do
# setup do
# @contacts = Contact.find :all
# end
# should "return an array" do
# assert @contacts.is_a? Array
# end
# should "return Contacts" do
# assert_equal 16, @contacts.size
# assert @contacts.first.is_a? Contact
# end
# end
context "Invoices" do
setup do
@invoices = Invoice.find :all
end
should "return an array" do
assert @invoices.is_a? Array
end
should "return Invoices" do
assert_equal 7, @invoices.size
assert @invoices.first.is_a? Invoice
end
end

# context "Contact" do
# setup do
# @contact = Contact.find 27309
# end
# should "return a Contact" do
# assert @contact.is_a? Contact
# end
# should "update and save" do
# @contact.last_name = 'Roberts'
# assert @contact.save
# end
# should "be destroyed" do
# assert @contact.destroy
# end
# end
context "Invoice" do
setup do
@invoice = Invoice.find 73867
end
should "return a Invoice" do
assert @invoice.is_a? Invoice
end
should "update and save" do
@invoice.last_name = 'Roberts'
assert @invoice.save
end
should "be destroyed" do
assert @invoice.destroy
end
should "change status" do
assert @invoice.mark_as_draft
assert @invoice.mark_as_sent
assert @invoice.mark_as_cancelled
end
end

#TODO - Add test for invalid resource
# Need support from fakeweb in order to achieve this

# context "New Contact" do
# setup do
# params = {
# :first_name => 'Fred',
# :last_name => 'Bloggs'
# }
# @new_contact = Contact.new params
# end
# should "validate and save" do
# assert @new_contact.save_with_validation
# end
# end
context "New Invoice" do
setup do
params = {
:contact_id => '29899',
:project_id => '21445',
:dated_on => '2009-10-26T00:00:00Z',
:reference => 'INV100',
:status => 'Draft'
}
@invoice = Invoice.new params
end
should "validate and save" do
assert @invoice.save_with_validation
end
end

end
39 changes: 39 additions & 0 deletions test/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,44 @@ class ProjectTest < Test::Unit::TestCase
end
end

context "Nested Invoices" do
setup do
@project = Project.find 17820
@invoices = @project.invoices
@invoice = @invoices.first
end
should "be Invoices" do
assert @invoices.is_a? Array
assert_equal 1, @invoices.size
assert @invoices.first.is_a? Invoice
end
should "be updateable" do
@invoice.comments = "This is a test comment"
assert @invoice.save
end
should "be deletable" do
assert @invoice.destroy
end
end

context "Nested Timeslips" do
setup do
@project = Project.find 17820
@timeslips = @project.timeslips
@timeslip = @timeslips.first
end
should "be Timeslips" do
assert @timeslips.is_a? Array
assert_equal 24, @timeslips.size
assert @timeslips.first.is_a? Timeslip
end
should "be updateable" do
@timeslip.hours = '1'
assert @timeslip.save
end
should "be deletable" do
assert @timeslip.destroy
end
end

end
Loading

0 comments on commit f53d1d0

Please sign in to comment.