[NEW] Plugin to strip leading and trailing blanks from attributes.
[redhillonrails.git] / test / unit / customer_test.rb
blobc5c2f8247a0192702b9b8862e6c1f5f0f4289a34
1 require File.dirname(__FILE__) + '/../test_helper'
3 class CustomerTest < Test::Unit::TestCase
4         def test_is_invalid_when_blank
5     customer = Customer.new
6     assert !customer.valid?
7     errors = customer.errors
8     assert_equal 3, errors.size
9     assert_equal "can't be blank", errors[:name]
10     assert_equal "can't be blank", errors[:street]
11     assert_equal "can't be blank", errors[:city_id]
12         end
14         def test_is_valid_when_fully_populated
15     customer = Customer.new(:name => 'ThoughtWorks, Inc.', :street => 'Level 11, 155 Queen Street', :city => cities(:melbourne))
16     assert customer.valid?
17     assert customer.errors.empty?
18   end
20   def test_has_many_orders
21     orders = customers(:redhill).orders
23     assert orders.include?(orders(:redhill_lamps))
24     assert orders.include?(orders(:redhill_pens))
26     assert_equal 2, orders.size
27   end
29   def test_status_is_required_on_update_even_though_it_has_a_default
30     customer = customers(:redhill)
31     customer.status = nil
32     assert !customer.valid?
33     errors = customer.errors
34     assert_equal 1, errors.size
35     assert_equal "can't be blank", errors[:status]
36   end
37   
38   def test_has_parent
39     assert_equal customers(:redhill), customers(:redhill_usa).parent
40   end
41   
42   def test_invalid_fax_format
43     customer = Customer.new
44     customer.fax = "abc"
45     assert !customer.valid?
46     assert_equal "is invalid", customer.errors[:fax]
47   end
48   
49   def test_invalid_phone_format
50     customer = Customer.new
51     customer.phone = "abc"
52     assert !customer.valid?
53     assert_equal "is invalid", customer.errors[:phone]
54   end
56   def test_blanks_are_stripped_from_name_using_setter
57     customer = Customer.new
58     customer.name = "    this is a name    "
59     assert_equal "this is a name", customer.name
60   end
61   
62   def test_blanks_are_stripped_from_name_using_indexed_setter
63     customer = Customer.new
64     customer["name"] = "    this is a name    "
65     assert_equal "this is a name", customer.name
66   end
68   def test_blanks_are_stripped_from_name_using_write_attribute
69     customer = Customer.new
70     customer.write_attribute("name", "    this is a name    ")
71     assert_equal "this is a name", customer.name
72   end
74   def test_blanks_are_stripped_from_name_using_bulk_setter
75     customer = Customer.new
76     customer.attributes = { :name => "    this is a name    " }
77     assert_equal "this is a name", customer.name
78   end
79 end