rconomic 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +6 -14
  2. data/.travis.yml +0 -1
  3. data/Gemfile +0 -1
  4. data/lib/economic/account.rb +3 -5
  5. data/lib/economic/cash_book.rb +1 -1
  6. data/lib/economic/cash_book_entry.rb +92 -94
  7. data/lib/economic/creditor.rb +1 -1
  8. data/lib/economic/creditor_contact.rb +15 -17
  9. data/lib/economic/creditor_entry.rb +0 -1
  10. data/lib/economic/current_invoice.rb +32 -35
  11. data/lib/economic/current_invoice_line.rb +14 -2
  12. data/lib/economic/debtor.rb +60 -37
  13. data/lib/economic/debtor_contact.rb +26 -20
  14. data/lib/economic/endpoint.rb +72 -0
  15. data/lib/economic/entity/handle.rb +35 -21
  16. data/lib/economic/entity/mapper.rb +41 -0
  17. data/lib/economic/entity.rb +58 -34
  18. data/lib/economic/invoice.rb +15 -2
  19. data/lib/economic/proxies/account_proxy.rb +2 -3
  20. data/lib/economic/proxies/actions/find_by_ci_number.rb +4 -2
  21. data/lib/economic/proxies/actions/find_by_date_interval.rb +0 -1
  22. data/lib/economic/proxies/actions/find_by_name.rb +5 -1
  23. data/lib/economic/proxies/cash_book_entry_proxy.rb +1 -1
  24. data/lib/economic/proxies/entity_proxy.rb +15 -8
  25. data/lib/economic/session.rb +27 -31
  26. data/lib/rconomic/version.rb +1 -1
  27. data/rconomic.gemspec +1 -5
  28. data/spec/economic/cash_book_entry_spec.rb +27 -5
  29. data/spec/economic/cash_book_spec.rb +6 -6
  30. data/spec/economic/creditor_contact_spec.rb +24 -15
  31. data/spec/economic/creditor_spec.rb +9 -9
  32. data/spec/economic/current_invoice_line_spec.rb +7 -7
  33. data/spec/economic/current_invoice_spec.rb +82 -33
  34. data/spec/economic/debtor_contact_spec.rb +25 -15
  35. data/spec/economic/debtor_entry_spec.rb +3 -3
  36. data/spec/economic/debtor_spec.rb +49 -15
  37. data/spec/economic/endpoint_spec.rb +71 -0
  38. data/spec/economic/entity/handle_spec.rb +42 -49
  39. data/spec/economic/entity/mapper_spec.rb +54 -0
  40. data/spec/economic/entity_spec.rb +78 -72
  41. data/spec/economic/entry_spec.rb +3 -3
  42. data/spec/economic/invoice_spec.rb +37 -18
  43. data/spec/economic/proxies/actions/find_by_name_spec.rb +15 -12
  44. data/spec/economic/proxies/cash_book_entry_proxy_spec.rb +26 -24
  45. data/spec/economic/proxies/cash_book_proxy_spec.rb +23 -21
  46. data/spec/economic/proxies/creditor_contact_proxy_spec.rb +12 -16
  47. data/spec/economic/proxies/creditor_entry_proxy_spec.rb +18 -14
  48. data/spec/economic/proxies/creditor_proxy_spec.rb +23 -26
  49. data/spec/economic/proxies/current_invoice_line_proxy_spec.rb +17 -20
  50. data/spec/economic/proxies/current_invoice_proxy_spec.rb +38 -41
  51. data/spec/economic/proxies/debtor_contact_proxy_spec.rb +12 -15
  52. data/spec/economic/proxies/debtor_entry_proxy_spec.rb +18 -14
  53. data/spec/economic/proxies/debtor_proxy_spec.rb +31 -34
  54. data/spec/economic/proxies/entry_proxy_spec.rb +26 -22
  55. data/spec/economic/proxies/invoice_proxy_spec.rb +18 -21
  56. data/spec/economic/session_spec.rb +57 -44
  57. data/spec/fixtures/{spec_entity_create_from_data → account_create_from_data}/success.xml +5 -5
  58. data/spec/fixtures/{spec_entity_delete → account_delete}/success.xml +1 -1
  59. data/spec/fixtures/{spec_entity_get_data → account_get_data}/success.xml +5 -5
  60. data/spec/fixtures/{spec_entity_update_from_data → account_update_from_data}/success.xml +5 -5
  61. data/spec/fixtures/cash_book_entry_create_creditor_invoice/success.xml +2 -2
  62. data/spec/fixtures/cash_book_entry_create_creditor_payment/success.xml +2 -2
  63. data/spec/fixtures/creditor_contact_create_from_data/success.xml +10 -0
  64. data/spec/fixtures/debtor_contact_create_from_data/success.xml +10 -0
  65. data/spec/fixtures/debtor_create_from_data/success.xml +57 -0
  66. data/spec/spec_helper.rb +10 -104
  67. data/spec/support/api_requests.rb +20 -0
  68. data/spec/support/factories.rb +65 -0
  69. data/spec/support/fixtures.rb +9 -0
  70. metadata +27 -31
@@ -5,7 +5,7 @@ describe Economic::DebtorContact do
5
5
  subject { Economic::DebtorContact.new(:session => session) }
6
6
 
7
7
  it "inherits from Economic::Entity" do
8
- Economic::DebtorContact.ancestors.should include(Economic::Entity)
8
+ expect(Economic::DebtorContact.ancestors).to include(Economic::Entity)
9
9
  end
10
10
 
11
11
  context "when saving" do
@@ -18,17 +18,27 @@ describe Economic::DebtorContact do
18
18
  end
19
19
 
20
20
  it "should send request and let e-conomic return an error" do
21
- session.expects(:request)
21
+ expect(session).to receive(:request)
22
22
  subject.save
23
23
  end
24
24
  end
25
25
  end
26
+
27
+ it "builds and sends data to API" do
28
+ mock_request(
29
+ :debtor_contact_create_from_data,
30
+ {"data" => {"Handle" => {}, "Id" => nil, "Name" => nil, "Number" => 42, "IsToReceiveEmailCopyOfOrder" => false, "IsToReceiveEmailCopyOfInvoice" => false}},
31
+ :success
32
+ )
33
+ subject.number = 42
34
+ subject.save
35
+ end
26
36
  end
27
37
 
28
38
  describe ".debtor" do
29
39
  context "when debtor_handle is not set" do
30
40
  it "returns nil" do
31
- subject.debtor.should be_nil
41
+ expect(subject.debtor).to be_nil
32
42
  end
33
43
  end
34
44
 
@@ -40,13 +50,13 @@ describe Economic::DebtorContact do
40
50
  end
41
51
 
42
52
  it "returns a Debtor" do
43
- session.debtors.expects(:find).with(42).returns(Economic::Debtor.new)
44
- subject.debtor.should be_instance_of(Economic::Debtor)
53
+ expect(session.debtors).to receive(:find).with(42).and_return(Economic::Debtor.new)
54
+ expect(subject.debtor).to be_instance_of(Economic::Debtor)
45
55
  end
46
56
 
47
57
  it "only looks up the debtor the first time" do
48
- session.debtors.expects(:find).with(42).returns(Economic::Debtor.new)
49
- subject.debtor.should === subject.debtor
58
+ expect(session.debtors).to receive(:find).with(42).and_return(Economic::Debtor.new)
59
+ expect(subject.debtor).to equal(subject.debtor)
50
60
  end
51
61
  end
52
62
  end
@@ -55,7 +65,7 @@ describe Economic::DebtorContact do
55
65
  let(:debtor) { make_debtor }
56
66
  it "should set debtor_handle" do
57
67
  subject.debtor = debtor
58
- subject.debtor_handle.should == debtor.handle
68
+ expect(subject.debtor_handle).to eq(debtor.handle)
59
69
  end
60
70
  end
61
71
 
@@ -65,7 +75,7 @@ describe Economic::DebtorContact do
65
75
 
66
76
  it "should set debtor_handle" do
67
77
  subject.debtor_handle = handle
68
- subject.debtor_handle.should == handle
78
+ expect(subject.debtor_handle).to eq(handle)
69
79
  end
70
80
 
71
81
  context "when debtor handle is for a different Debtor" do
@@ -74,9 +84,9 @@ describe Economic::DebtorContact do
74
84
  end
75
85
 
76
86
  it "should clear cached debtor and fetch the new debtor from API" do
77
- savon.stubs('Debtor_GetData').returns(:success)
87
+ stub_request('Debtor_GetData', nil, :success)
78
88
  subject.debtor_handle = Economic::Debtor::Handle.new({:number => 1234})
79
- subject.debtor.should be_instance_of(Economic::Debtor)
89
+ expect(subject.debtor).to be_instance_of(Economic::Debtor)
80
90
  end
81
91
  end
82
92
 
@@ -86,20 +96,20 @@ describe Economic::DebtorContact do
86
96
  end
87
97
 
88
98
  it "should not clear cached debtor nor fetch the debtor from API" do
89
- savon.stubs('Debtor_GetData').never
99
+ expect(session).to receive(:request).never
90
100
  subject.debtor_handle = handle
91
- subject.debtor.should be_instance_of(Economic::Debtor)
101
+ expect(subject.debtor).to be_instance_of(Economic::Debtor)
92
102
  end
93
103
  end
94
104
  end
95
105
 
96
106
  describe ".proxy" do
97
107
  it "should return a DebtorContactProxy" do
98
- subject.proxy.should be_instance_of(Economic::DebtorContactProxy)
108
+ expect(subject.proxy).to be_instance_of(Economic::DebtorContactProxy)
99
109
  end
100
110
 
101
111
  it "should return a proxy owned by session" do
102
- subject.proxy.session.should == session
112
+ expect(subject.proxy.session).to eq(session)
103
113
  end
104
114
  end
105
115
 
@@ -5,16 +5,16 @@ describe Economic::DebtorEntry do
5
5
  subject { Economic::DebtorEntry.new(:session => session) }
6
6
 
7
7
  it "inherits from Economic::Entity" do
8
- Economic::DebtorEntry.ancestors.should include(Economic::Entity)
8
+ expect(Economic::DebtorEntry.ancestors).to include(Economic::Entity)
9
9
  end
10
10
 
11
11
  describe ".proxy" do
12
12
  it "should return a DebtorEntryProxy" do
13
- subject.proxy.should be_instance_of(Economic::DebtorEntryProxy)
13
+ expect(subject.proxy).to be_instance_of(Economic::DebtorEntryProxy)
14
14
  end
15
15
 
16
16
  it "should return a proxy owned by session" do
17
- subject.proxy.session.should == session
17
+ expect(subject.proxy.session).to eq(session)
18
18
  end
19
19
  end
20
20
  end
@@ -5,7 +5,7 @@ describe Economic::Debtor do
5
5
  subject { Economic::Debtor.new(:session => session) }
6
6
 
7
7
  it "inherits from Economic::Entity" do
8
- Economic::Debtor.ancestors.should include(Economic::Entity)
8
+ expect(Economic::Debtor.ancestors).to include(Economic::Entity)
9
9
  end
10
10
 
11
11
  describe "class methods" do
@@ -13,13 +13,13 @@ describe Economic::Debtor do
13
13
 
14
14
  describe ".proxy" do
15
15
  it "should return DebtorProxy" do
16
- subject.proxy.should == Economic::DebtorProxy
16
+ expect(subject.proxy).to eq(Economic::DebtorProxy)
17
17
  end
18
18
  end
19
19
 
20
20
  describe ".key" do
21
21
  it "should == :debtor" do
22
- Economic::Debtor.key.should == :debtor
22
+ expect(Economic::Debtor.key).to eq(:debtor)
23
23
  end
24
24
  end
25
25
  end
@@ -34,7 +34,7 @@ describe Economic::Debtor do
34
34
  end
35
35
 
36
36
  it "should send request and let e-conomic return an error" do
37
- session.expects(:request)
37
+ expect(session).to receive(:request)
38
38
  subject.save
39
39
  end
40
40
  end
@@ -43,41 +43,41 @@ describe Economic::Debtor do
43
43
 
44
44
  describe ".current_invoices" do
45
45
  it "returns an CurrentInvoiceProxy" do
46
- subject.current_invoices.should be_instance_of(Economic::CurrentInvoiceProxy)
46
+ expect(subject.current_invoices).to be_instance_of(Economic::CurrentInvoiceProxy)
47
47
  end
48
48
 
49
49
  it "memoizes the proxy" do
50
- subject.current_invoices.should === subject.current_invoices
50
+ expect(subject.current_invoices).to equal(subject.current_invoices)
51
51
  end
52
52
 
53
53
  it "should store the session" do
54
- subject.session.should_not be_nil
55
- subject.current_invoices.session.should == subject.session
54
+ expect(subject.session).to_not be_nil
55
+ expect(subject.current_invoices.session).to eq(subject.session)
56
56
  end
57
57
  end
58
58
 
59
59
  describe ".contacts" do
60
60
  it "returns a DebtorContactProxy" do
61
- subject.contacts.should be_instance_of(Economic::DebtorContactProxy)
61
+ expect(subject.contacts).to be_instance_of(Economic::DebtorContactProxy)
62
62
  end
63
63
 
64
64
  it "memoizes the proxy" do
65
- subject.contacts.should === subject.contacts
65
+ expect(subject.contacts).to equal(subject.contacts)
66
66
  end
67
67
 
68
68
  it "should store the session" do
69
- subject.session.should_not be_nil
70
- subject.contacts.session.should == subject.session
69
+ expect(subject.session).to_not be_nil
70
+ expect(subject.contacts.session).to eq(subject.session)
71
71
  end
72
72
  end
73
73
 
74
74
  describe ".proxy" do
75
75
  it "should return a DebtorProxy" do
76
- subject.proxy.should be_instance_of(Economic::DebtorProxy)
76
+ expect(subject.proxy).to be_instance_of(Economic::DebtorProxy)
77
77
  end
78
78
 
79
79
  it "should return a proxy owned by session" do
80
- subject.proxy.session.should == session
80
+ expect(subject.proxy.session).to eq(session)
81
81
  end
82
82
  end
83
83
 
@@ -87,10 +87,44 @@ describe Economic::Debtor do
87
87
  let(:other) { Economic::Invoice.new(:session => session, :handle => subject.handle) }
88
88
 
89
89
  it "should return false" do
90
- subject.should_not == other
90
+ expect(subject).not_to eq(other)
91
91
  end
92
92
  end
93
93
  end
94
94
  end
95
95
 
96
+ describe "#save" do
97
+ it 'should save it' do
98
+ stub_request('Debtor_CreateFromData', nil, :success)
99
+ subject.save
100
+ end
101
+
102
+ it "builds and sends data to API" do
103
+ mock_request(
104
+ :debtor_create_from_data, {
105
+ "data" => {
106
+ "Handle" => {},
107
+ "Number" => nil,
108
+ "DebtorGroupHandle" => {"Number" => 42},
109
+ "Name" => nil,
110
+ "VatZone" => nil,
111
+ "CurrencyHandle" => {"Code" => "BTC"},
112
+ "PriceGroupHandle" => {"Number" => 37},
113
+ "IsAccessible" => nil,
114
+ "TermOfPaymentHandle" => {"Id" => 314},
115
+ "LayoutHandle" => {"Id" => 21}
116
+ }
117
+ },
118
+ :success
119
+ )
120
+
121
+ subject.debtor_group_handle = Economic::Entity::Handle.new({:number => 42})
122
+ subject.currency_handle = Economic::Entity::Handle.new({:code => 'BTC'})
123
+ subject.price_group_handle = Economic::Entity::Handle.new({:number => 37})
124
+ subject.term_of_payment_handle = Economic::Entity::Handle.new({:id => 314})
125
+ subject.layout_handle = Economic::Entity::Handle.new({:id => 21})
126
+
127
+ subject.save
128
+ end
129
+ end
96
130
  end
@@ -0,0 +1,71 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Economic::Endpoint do
4
+ subject { Economic::Endpoint.new }
5
+
6
+ describe "call" do
7
+ let(:client) {
8
+ subject.client
9
+ }
10
+
11
+ it "uses the SOAP client to invoke a SOAP action on the API" do
12
+ expect(client).to receive(:call).with(
13
+ :foo_bar,
14
+ :message => {:baz => 'qux'}
15
+ ).and_return({})
16
+ subject.call(:foo_bar, {:baz => 'qux'})
17
+ end
18
+
19
+ it "sends an actual request" do
20
+ mock_request('Connect', nil, :success)
21
+ subject.call(:connect)
22
+ end
23
+
24
+ it "returns a hash with data" do
25
+ stub_request('CurrentInvoice_GetAll', nil, :single)
26
+ expect(subject.call(:current_invoice_get_all)).to eq({:current_invoice_handle => {:id => "1"}})
27
+ end
28
+
29
+ it "returns an empty hash if no data returned" do
30
+ stub_request('CurrentInvoice_GetAll', nil, :none)
31
+ expect(subject.call(:current_invoice_get_all)).to be_empty
32
+ end
33
+
34
+ it "yields a Savon response" do
35
+ stub_request('CurrentInvoice_GetAll', nil, :single)
36
+ @yielded_value = nil
37
+ subject.call(:current_invoice_get_all) do |response|
38
+ @yielded_value = response
39
+ end
40
+ expect(@yielded_value).to be_instance_of(Savon::Response)
41
+ end
42
+
43
+ it "adds a cookie header" do
44
+ expect(client).to receive(:call).with(
45
+ :current_invoice_get_all,
46
+ :cookies => "omnomnom"
47
+ ).and_return({})
48
+ subject.call(:current_invoice_get_all, {}, "omnomnom")
49
+ end
50
+ end
51
+
52
+ describe "#client" do
53
+ it "returns a Savon::Client" do
54
+ expect(subject.client).to be_instance_of(::Savon::Client)
55
+ end
56
+
57
+ it "returns the same Savon::Client" do
58
+ expect(subject.client).to equal(subject.client)
59
+ end
60
+ end
61
+
62
+ describe "soap_action_name" do
63
+ it "returns full action name for the given class and soap action" do
64
+ expect(subject.soap_action_name(Economic::Debtor, :get_data)).to eq(:debtor_get_data)
65
+ end
66
+
67
+ it "returns full action name for a class given as strings" do
68
+ expect(subject.soap_action_name("FooBar", "Stuff")).to eq(:foo_bar_stuff)
69
+ end
70
+ end
71
+ end
@@ -8,24 +8,24 @@ describe Economic::Entity::Handle do
8
8
  let(:handle_d) { Economic::Entity::Handle.new(:id => 1, :number => 3) }
9
9
 
10
10
  it "should be equal when both id and number are equal" do
11
- handle_a.should == handle_b
11
+ expect(handle_a).to eq(handle_b)
12
12
  end
13
13
 
14
14
  it "should not be equal when id or number is missing" do
15
- handle_a.should_not == handle_c
15
+ expect(handle_a).not_to eq(handle_c)
16
16
  end
17
17
 
18
18
  it "should not be equal when id or number is equal and the other isn't" do
19
- handle_a.should_not == handle_d
19
+ expect(handle_a).not_to eq(handle_d)
20
20
  end
21
21
 
22
22
  it "should not equal if both are empty" do
23
- Economic::Entity::Handle.new({}).should_not == Economic::Entity::Handle.new({})
23
+ expect(Economic::Entity::Handle.new({})).not_to eq(Economic::Entity::Handle.new({}))
24
24
  end
25
25
 
26
26
  it "should be equal if both are the same object" do
27
27
  handle = Economic::Entity::Handle.new({})
28
- handle.should == handle
28
+ expect(handle).to eq(handle)
29
29
  end
30
30
 
31
31
  describe "CashBookEntry handles" do
@@ -35,7 +35,7 @@ describe Economic::Entity::Handle do
35
35
  let(:handle_b) { Economic::Entity::Handle.new(:id1 => 1, :id2 => 2) }
36
36
 
37
37
  it "should be equal" do
38
- handle_a.should == handle_b
38
+ expect(handle_a).to eq(handle_b)
39
39
  end
40
40
  end
41
41
 
@@ -43,7 +43,7 @@ describe Economic::Entity::Handle do
43
43
  let(:handle_b) { Economic::Entity::Handle.new(:id1 => 11, :id2 => 12) }
44
44
 
45
45
  it "should_not be equal" do
46
- handle_a.should_not == handle_b
46
+ expect(handle_a).not_to eq(handle_b)
47
47
  end
48
48
  end
49
49
  end
@@ -51,137 +51,130 @@ describe Economic::Entity::Handle do
51
51
 
52
52
  describe ".new" do
53
53
  it "should raise error if argument isn't supported" do
54
- lambda do
54
+ expect(lambda do
55
55
  Economic::Entity::Handle.new(true)
56
- end.should raise_error(ArgumentError)
56
+ end).to raise_error(ArgumentError)
57
57
  end
58
58
 
59
59
  it "should assume :id if argument is numeric" do
60
60
  handle = Economic::Entity::Handle.new(12)
61
- handle.id.should == 12
62
- handle.number.should be_nil
61
+ expect(handle.id).to eq(12)
62
+ expect(handle.number).to be_nil
63
63
  end
64
64
 
65
65
  it "should use to_i on numeric argument" do
66
66
  handle = Economic::Entity::Handle.new("42")
67
- handle.id.should == 42
67
+ expect(handle.id).to eq(42)
68
68
  end
69
69
 
70
70
  it "should raise error if argument is nil" do
71
- lambda do
71
+ expect(lambda do
72
72
  Economic::Entity::Handle.new(nil)
73
- end.should raise_error(ArgumentError)
73
+ end).to raise_error(ArgumentError)
74
74
  end
75
75
 
76
76
  it "should raise error if argument contains invalid key" do
77
- lambda do
77
+ expect(lambda do
78
78
  Economic::Entity::Handle.new(:Numeric => 12)
79
- end.should raise_error(ArgumentError)
80
- end
81
-
82
- it "should raise error if argument contains invalid values" do
83
- lambda do
84
- Economic::Entity::Handle.new(:number => {:number => 12})
85
- end.should raise_error(ArgumentError)
79
+ end).to raise_error(ArgumentError)
86
80
  end
87
81
 
88
82
  it "should set id" do
89
83
  handle = Economic::Entity::Handle.new(:id => 12)
90
- handle.id.should == 12
84
+ expect(handle.id).to eq(12)
91
85
  end
92
86
 
93
87
  it "should set number" do
94
88
  handle = Economic::Entity::Handle.new(:number => 12)
95
- handle.number.should == 12
89
+ expect(handle.number).to eq(12)
96
90
  end
97
91
 
98
92
  it "should set both id and number" do
99
93
  handle = Economic::Entity::Handle.new(:id => 37, :number => 42)
100
- handle.id.should == 37
101
- handle.number.should == 42
94
+ expect(handle.id).to eq(37)
95
+ expect(handle.number).to eq(42)
102
96
  end
103
97
 
104
98
  it 'should set id1 and id2' do
105
99
  handle = Economic::Entity::Handle.new(:id1 => 37, :id2 => 42)
106
- handle.id1.should == 37
107
- handle.id2.should == 42
100
+ expect(handle.id1).to eq(37)
101
+ expect(handle.id2).to eq(42)
108
102
  end
109
103
 
110
104
  it "should to_i values" do
111
105
  handle = Economic::Entity::Handle.new(:id => "37", :number => "42")
112
- handle.id.should == 37
113
- handle.number.should == 42
106
+ expect(handle.id).to eq(37)
107
+ expect(handle.number).to eq(42)
114
108
  end
115
109
 
116
110
  it "should not to_i nil values" do
117
111
  handle = Economic::Entity::Handle.new(:id => "37")
118
- handle.id.should == 37
119
- handle.number.should be_nil
112
+ expect(handle.id).to eq(37)
113
+ expect(handle.number).to be_nil
120
114
  end
121
115
 
122
116
  it "should accept a Hash with capitalized keys" do
123
117
  handle = Economic::Entity::Handle.new({"Id" => 37, "Number" => 42})
124
- handle.id.should == 37
125
- handle.number.should == 42
118
+ expect(handle.id).to eq(37)
119
+ expect(handle.number).to eq(42)
126
120
  end
127
121
 
128
122
  it "should accept another Handle" do
129
123
  original_handle = Economic::Entity::Handle.new(:id => 37)
130
124
  handle = Economic::Entity::Handle.new(original_handle)
131
- handle.id.should == 37
132
- handle.number.should be_nil
133
- handle.should == original_handle
125
+ expect(handle.id).to eq(37)
126
+ expect(handle.number).to be_nil
127
+ expect(handle).to eq(original_handle)
134
128
  end
135
129
  end
136
130
 
137
131
  describe ".build" do
138
132
  it "returns nil when given nil" do
139
- Economic::Entity::Handle.build(nil).should be_nil
133
+ expect(Economic::Entity::Handle.build(nil)).to be_nil
140
134
  end
141
135
 
142
136
  it "returns empty handle when hash is empty" do
143
- Economic::Entity::Handle.build({}).should be_empty
137
+ expect(Economic::Entity::Handle.build({})).to be_empty
144
138
  end
145
139
 
146
140
  it "returns nil when hash has no values" do
147
- Economic::Entity::Handle.build({:id => nil, :number => nil}).should be_empty
141
+ expect(Economic::Entity::Handle.build({:id => nil, :number => nil})).to be_empty
148
142
  end
149
143
 
150
144
  it "returns handle when hash has values" do
151
- Economic::Entity::Handle.build({:id2 => 42}).should == Economic::Entity::Handle.new({:id2 => 42})
145
+ expect(Economic::Entity::Handle.build({:id2 => 42})).to eq(Economic::Entity::Handle.new({:id2 => 42}))
152
146
  end
153
147
 
154
148
  it "returns a given handle" do
155
149
  handle = Economic::Entity::Handle.new({})
156
- Economic::Entity::Handle.build(handle).should === handle
150
+ expect(Economic::Entity::Handle.build(handle)).to equal(handle)
157
151
  end
158
152
  end
159
153
 
160
154
  describe "#empty?" do
161
155
  it "returns true when handle has no values" do
162
- Economic::Entity::Handle.new({}).should be_empty
156
+ expect(Economic::Entity::Handle.new({})).to be_empty
163
157
  end
164
158
 
165
159
  it "returns false when handle has a value" do
166
- Economic::Entity::Handle.new({:serial_number => 12}).should_not be_empty
160
+ expect(Economic::Entity::Handle.new({:serial_number => 12})).to_not be_empty
167
161
  end
168
162
  end
169
163
 
170
164
  describe ".to_hash" do
171
- subject { Economic::Entity::Handle.new({:id => 42, :number => 37, :serial_number => 7}) }
165
+ subject { Economic::Entity::Handle.new({:id => 42, :number => 37, :serial_number => 7, :code => "USD", :vat_code => 1}) }
172
166
 
173
167
  it "should return a handle for putting into the body of a SOAP request" do
174
- subject.to_hash.should == {'Id' => 42, 'Number' => 37, 'SerialNumber' => 7}
168
+ expect(subject.to_hash).to eq({'Id' => 42, 'Number' => 37, 'SerialNumber' => 7, 'Code' => 'USD', 'VatCode' => 1})
175
169
  end
176
170
 
177
171
  it "includes only the named value in the hash" do
178
- subject.to_hash(:id).should == {'Id' => 42}
172
+ expect(subject.to_hash(:id)).to eq({'Id' => 42})
179
173
  end
180
174
 
181
175
  it "includes only the named values in the hash" do
182
- subject.to_hash([:id, :serial_number]).should == {'Id' => 42, 'SerialNumber' => 7}
176
+ expect(subject.to_hash([:id, :serial_number])).to eq({'Id' => 42, 'SerialNumber' => 7})
183
177
  end
184
178
  end
185
-
186
179
  end
187
180
 
@@ -0,0 +1,54 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Economic::Entity::Mapper do
4
+ let(:entity) { double("Entity", {
5
+ :handle => Economic::Entity::Handle.new(:id => 42),
6
+ :creditor_handle => Economic::Entity::Handle.new(:number => 37),
7
+ :name => "David Brent",
8
+ :is_to_receive_email_copy_of_order => true
9
+ }) }
10
+
11
+ let(:fields) {
12
+ [
13
+ ["Handle", :handle, Proc.new { |v| v.to_hash }, :required],
14
+ ["CreditorHandle", :creditor_handle, Proc.new { |v| {"Number" => v[:number] }}],
15
+ ["Id", :handle, Proc.new { |v| v.id }, :required],
16
+ ["Name", :name],
17
+ ["IsToReceiveEmailCopyOfOrder", :is_to_receive_email_copy_of_order, Proc.new { |v| v || false }, :required],
18
+ ]
19
+ }
20
+
21
+ subject { Economic::Entity::Mapper.new(entity, fields) }
22
+
23
+ describe "#to_hash" do
24
+ it "returns a Hash with fields as per the field descriptions" do
25
+ subject.to_hash.should == {
26
+ "Handle" => {"Id" => 42},
27
+ "CreditorHandle" => {"Number" => 37},
28
+ "Id" => 42,
29
+ "Name" => "David Brent",
30
+ "IsToReceiveEmailCopyOfOrder" => true
31
+ }
32
+ end
33
+ end
34
+
35
+ describe "when entity has no values" do
36
+ let(:entity) { double("Entity", {
37
+ :handle => Economic::Entity::Handle.new({}),
38
+ :creditor_handle => nil,
39
+ :name => nil,
40
+ :number => nil,
41
+ :is_to_receive_email_copy_of_order => nil
42
+ }) }
43
+
44
+ subject { Economic::Entity::Mapper.new(entity, fields) }
45
+
46
+ it "returns the minimal set of required fields" do
47
+ subject.to_hash.should == {
48
+ "Handle" => {},
49
+ "Id" => nil,
50
+ "IsToReceiveEmailCopyOfOrder" => false
51
+ }
52
+ end
53
+ end
54
+ end