Browse files

customer specs and run validations on create/update

  • Loading branch information...
1 parent 4a0bb06 commit fc38732835661bc1e12a903eb7ce3e6e28b39171 @ruckus committed Feb 20, 2012
View
8 lib/quickeebooks/model/account.rb
@@ -25,6 +25,14 @@ def to_xml_ns(options = {})
to_xml_inject_ns('Account', options)
end
+ def valid_for_update?
+ if sync_token.nil?
+ errors.add(:sync_token, "Missing required attribute SyncToken for update")
+ end
+ valid?
+ errors.empty?
+ end
+
# To delete an account Intuit requires we provide Id and SyncToken fields
def valid_for_deletion?
return false if(id.nil? || sync_token.nil?)
View
17 lib/quickeebooks/model/customer.rb
@@ -11,6 +11,8 @@
module Quickeebooks
module Model
class Customer < IntuitType
+ include ActiveModel::Validations
+
xml_convention :camelcase
xml_accessor :id, :from => 'Id'
xml_accessor :sync_token, :from => 'SyncToken', :as => Integer
@@ -31,6 +33,15 @@ class Customer < IntuitType
xml_accessor :sales_term_id, :from => 'SalesTermId'
xml_accessor :paymethod_method_id, :from => 'PaymentMethodId'
xml_accessor :open_balance, :from => 'OpenBalance', :as => Quickeebooks::Model::OpenBalance
+
+ validates_length_of :name, :minimum => 1
+
+ def valid_for_update?
+ if sync_token.nil?
+ errors.add(:sync_token, "Missing required attribute SyncToken for update")
+ end
+ errors.empty?
+ end
def to_xml_ns(options = {})
to_xml_inject_ns('Customer', options)
@@ -40,6 +51,12 @@ def email_address=(email_address)
self.email = Quickeebooks::Model::Email.new(email_address)
end
+ # To delete an account Intuit requires we provide Id and SyncToken fields
+ def valid_for_deletion?
+ return false if(id.nil? || sync_token.nil?)
+ id.to_i > 0 && !sync_token.to_s.empty? && sync_token.to_i >= 0
+ end
+
end
end
View
16 lib/quickeebooks/service/account.rb
@@ -20,8 +20,20 @@ def create(account)
end
end
- def fetch_by_id(id)
- response = do_http_get("#{url_for_resource("account")}/#{id}")
+ def update(account)
+ raise InvalidModelException unless account.valid_for_update?
+ xml = account.to_xml_ns
+ url = "#{url_for_resource("account")}/#{account.id}"
+ response = do_http_post(url, valid_xml_document(xml))
+ if response && response.code.to_i == 200
+ Quickeebooks::Model::Account.from_xml(response.body)
+ else
+ nil
+ end
+ end
+
+ def fetch_by_id(account_id)
+ response = do_http_get("#{url_for_resource("account")}/#{account_id}")
Quickeebooks::Model::Account.from_xml(response.body)
end
View
3 lib/quickeebooks/service/customer.rb
@@ -6,6 +6,7 @@ module Service
class Customer < ServiceBase
def create(customer)
+ raise InvalidModelException unless customer.valid?
xml = customer.to_xml_ns
response = do_http_post(url_for_resource("customer"), valid_xml_document(xml))
if response.code.to_i == 200
@@ -26,6 +27,7 @@ def fetch_by_id(id)
end
def update(customer)
+ raise InvalidModelException.new("Missing required parameters for update") unless customer.valid_for_update?
url = "#{url_for_resource("customer")}/#{customer.id}"
xml = customer.to_xml_ns
response = do_http_post(url, valid_xml_document(xml))
@@ -37,6 +39,7 @@ def update(customer)
end
def delete(customer)
+ raise InvalidModelException.new("Missing required parameters for delete") unless customer.valid_for_deletion?
xml = valid_xml_document(customer.to_xml_ns(:fields => ['Id', 'SyncToken']))
url = "#{url_for_resource("customer")}/#{customer.id}"
response = do_http_post(url, xml, {:methodx => "delete"})
View
16 spec/quickeebooks/customer_spec.rb
@@ -4,7 +4,7 @@
describe "Quickeebooks::Model::Customer" do
- describe "parse customer from XML" do
+ it "parse customer from XML" do
xml = File.read(File.dirname(__FILE__) + "/../xml/customer.xml")
customer = Quickeebooks::Model::Customer.from_xml(xml)
customer.sync_token.should == 1
@@ -29,4 +29,18 @@
customer.open_balance.amount.should == 6200.0
end
+ it "can assign an email address" do
+ customer = Quickeebooks::Model::Customer.new
+ the_email = "foo@example.org"
+ customer.email_address = the_email
+ customer.email.is_a?(Quickeebooks::Model::Email).should == true
+ customer.email.address.should == the_email
+ end
+
+ it "cannot update an invalid model" do
+ customer = Quickeebooks::Model::Customer.new
+ customer.valid_for_update?.should == false
+ customer.errors.keys.include?(:sync_token).should == true
+ end
+
end
View
105 spec/quickeebooks/services/customer_spec.rb
@@ -39,48 +39,69 @@
accounts.entries.count.should == 3
accounts.entries.first.name.should == "John Doe"
end
+
+ it "can create a customer" do
+ xml = File.read(File.dirname(__FILE__) + "/../../xml/customer.xml")
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ FakeWeb.register_uri(:post, service.url_for_resource("customer"), :status => ["200", "OK"], :body => xml)
+ customer = Quickeebooks::Model::Customer.new
+ customer.name = "Billy Bob"
+ result = service.create(customer)
+ result.id.to_i.should > 0
+ end
+
+ it "can delete a customer" do
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ url = "#{service.url_for_resource("customer")}/99?methodx=delete"
+ FakeWeb.register_uri(:post, url, :status => ["200", "OK"])
+ customer = Quickeebooks::Model::Customer.new
+ customer.id = 99
+ customer.sync_token = 0
+ result = service.delete(customer)
+ result.should == true
+ end
+ it "cannot delete a customer with missing required fields for deletion" do
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ customer = Quickeebooks::Model::Customer.new
+ lambda { service.delete(customer) }.should raise_error(InvalidModelException, "Missing required parameters for delete")
+ end
- #
- # it "can create an account" do
- # service = Quickeebooks::Service::Account.new(@oauth, @realm_id, @base_uri)
- # FakeWeb.register_uri(:post, service.url_for_resource("account"), :status => ["200", "OK"])
- # account = Quickeebooks::Model::Account.new
- # account.name = "Billy Bob"
- # account.sub_type = "AccountsPayable"
- # result = service.create(account)
- # end
- #
- # it "can delete an account" do
- # service = Quickeebooks::Service::Account.new(@oauth, @realm_id, @base_uri)
- # url = "#{service.url_for_resource("account")}/99?methodx=delete"
- # FakeWeb.register_uri(:post, url, :status => ["200", "OK"])
- # account = Quickeebooks::Model::Account.new
- # account.id = 99
- # account.sync_token = 0
- # result = service.delete(account)
- # result.code.should == "200"
- # end
- #
- # it "cannot delete an account with missing required fields for deletion" do
- # service = Quickeebooks::Service::Account.new(@oauth, @realm_id, @base_uri)
- # account = Quickeebooks::Model::Account.new
- # lambda { service.delete(account) }.should raise_error(InvalidModelException, "Missing required parameters for delete")
- # end
- #
- # it "exception is raised when we try to create an invalid account" do
- # account = Quickeebooks::Model::Account.new
- # service = Quickeebooks::Service::Account.new(@oauth, @realm_id, @base_uri)
- # lambda { service.create(account) }.should raise_error(InvalidModelException)
- # end
- #
- # it "can fetch an account by id" do
- # xml = File.read(File.dirname(__FILE__) + "/../../xml/account.xml")
- # service = Quickeebooks::Service::Account.new(@oauth, @realm_id, @base_uri)
- # url = "#{service.url_for_resource("account")}/99"
- # FakeWeb.register_uri(:get, url, :status => ["200", "OK"], :body => xml)
- # account = service.fetch_by_id(99)
- # account.name.should == "Billy Bob"
- # end
- #
+ it "exception is raised when we try to create an invalid account" do
+ customer = Quickeebooks::Model::Customer.new
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ lambda { service.create(customer) }.should raise_error(InvalidModelException)
+ end
+
+ it "cannot update an invalid customer" do
+ customer = Quickeebooks::Model::Customer.new
+ customer.name = "John Doe"
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ lambda { service.update(customer) }.should raise_error(InvalidModelException)
+ end
+
+ it "can fetch an customer by id" do
+ xml = File.read(File.dirname(__FILE__) + "/../../xml/customer.xml")
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ url = "#{service.url_for_resource("customer")}/99"
+ FakeWeb.register_uri(:get, url, :status => ["200", "OK"], :body => xml)
+ customer = service.fetch_by_id(99)
+ customer.name.should == "John Doe"
+ end
+
+ it "can update a customer" do
+ xml2 = File.read(File.dirname(__FILE__) + "/../../xml/customer2.xml")
+ customer = Quickeebooks::Model::Customer.new
+ customer.name = "John Doe"
+ customer.id = 1
+ customer.sync_token = 2
+
+ service = Quickeebooks::Service::Customer.new(@oauth, @realm_id, @base_uri)
+ url = "#{service.url_for_resource("customer")}/#{customer.id}"
+ FakeWeb.register_uri(:post, url, :status => ["200", "OK"], :body => xml2)
+ updated = service.update(customer)
+ updated.name.should == "Billy Bob"
+ end
+
+
end
View
63 spec/xml/customer2.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Customer>
+ <Id idDomain="QBO">1</Id>
+ <SyncToken>1</SyncToken>
+ <MetaData>
+ <CreateTime>2011-09-29T15:45:12-07:00</CreateTime>
+ <LastUpdatedTime>2012-01-22T20:35:57-08:00</LastUpdatedTime>
+ </MetaData>
+ <Name>Billy Bob</Name>
+ <Address>
+ <Line1>123 Main St.</Line1>
+ <Line2>Suite 400</Line2>
+ <City>San Diego</City>
+ <Country>USA</Country>
+ <CountrySubDivisionCode>CA</CountrySubDivisionCode>
+ <PostalCode>96009</PostalCode>
+ <Tag>Billing</Tag>
+ </Address>
+ <Address>
+ <Line1>123 Main St.</Line1>
+ <Line2>Suite 400</Line2>
+ <City>San Diego</City>
+ <Country>USA</Country>
+ <CountrySubDivisionCode>CA</CountrySubDivisionCode>
+ <PostalCode>96009</PostalCode>
+ <Tag>Shipping</Tag>
+ </Address>
+ <Phone>
+ <DeviceType>Primary</DeviceType>
+ <FreeFormNumber>(408) 555-1212</FreeFormNumber>
+ </Phone>
+ <Phone>
+ <DeviceType>Mobile</DeviceType>
+ <FreeFormNumber>(831) 334-0987</FreeFormNumber>
+ </Phone>
+ <WebSite/>
+ <Email>
+ <Address>johndoe@gmail.com</Address>
+ </Email>
+ <GivenName>John</GivenName>
+ <FamilyName>Doe</FamilyName>
+ <DBAName>John's Wine Shop</DBAName>
+ <Notes>
+ <Content>Likes chocolate and horses</Content>
+ </Notes>
+ <CustomField xsi:type="BooleanTypeCustomField">
+ <DefinitionId>Bill With Parent</DefinitionId>
+ <Value>false</Value>
+ </CustomField>
+ <CustomField xsi:type="StringTypeCustomField">
+ <DefinitionId>Resale Number</DefinitionId>
+ <Value>ABC-123</Value>
+ </CustomField>
+ <CustomField xsi:type="StringTypeCustomField">
+ <DefinitionId>Preferred Delivery Method</DefinitionId>
+ <Value>PRINT</Value>
+ </CustomField>
+ <SalesTermId idDomain="QBO">3</SalesTermId>
+ <PaymentMethodId idDomain="QBO">5</PaymentMethodId>
+ <OpenBalance>
+ <Amount>6200</Amount>
+ </OpenBalance>
+</Customer>

0 comments on commit fc38732

Please sign in to comment.