ydim 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+ # TestDebitor -- ydim -- 10.01.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'ydim/debitor'
8
+ require 'flexmock'
9
+ require 'date'
10
+
11
+ module YDIM
12
+ class TestDebitor < Test::Unit::TestCase
13
+ include FlexMock::TestCase
14
+ def setup
15
+ @debitor = Debitor.new(1)
16
+ end
17
+ def test_address
18
+ @debitor.name = 'Name'
19
+ @debitor.contact = 'Contact'
20
+ @debitor.contact_firstname = 'Firstname'
21
+ @debitor.contact_title = 'Title'
22
+ @debitor.address_lines = %w{Line1 Line2}
23
+ @debitor.emails = ['Email', 'OtherEmail']
24
+ expected = [
25
+ 'Name',
26
+ 'z.H. Firstname Contact',
27
+ 'Title',
28
+ 'Line1',
29
+ 'Line2',
30
+ 'To: Email',
31
+ 'Cc: OtherEmail',
32
+ ]
33
+ assert_equal(expected, @debitor.address)
34
+ end
35
+ def test_add_invoice
36
+ invoice = FlexMock.new()
37
+ invoice.should_receive(:unique_id).and_return { 17 }
38
+ retval = @debitor.add_invoice(invoice)
39
+ assert_equal(invoice, retval)
40
+ assert_equal([invoice], @debitor.invoices)
41
+ assert_equal(invoice, @debitor.invoice(17))
42
+ end
43
+ def test_delete_invoice
44
+ invoice = FlexMock.new()
45
+ invoice.should_receive(:unique_id).and_return { 17 }
46
+ @debitor.invoices.push(invoice)
47
+ retval = @debitor.delete_invoice(invoice)
48
+ assert_equal(invoice, retval)
49
+ assert_equal([], @debitor.invoices)
50
+ assert_nil(@debitor.delete_invoice(invoice))
51
+ end
52
+ def test_add_autoinvoice
53
+ invoice = flexmock('autoinvoice')
54
+ invoice.should_receive(:unique_id).and_return(17)
55
+ retval = @debitor.add_autoinvoice(invoice)
56
+ assert_equal(invoice, retval)
57
+ assert_equal([invoice], @debitor.autoinvoices)
58
+ assert_equal(invoice, @debitor.autoinvoice(17))
59
+ end
60
+ def test_delete_autoinvoice
61
+ invoice = flexmock('autoinvoice')
62
+ invoice.should_receive(:unique_id).and_return(17)
63
+ @debitor.autoinvoices.push(invoice)
64
+ retval = @debitor.delete_autoinvoice(invoice)
65
+ assert_equal(invoice, retval)
66
+ assert_equal([], @debitor.autoinvoices)
67
+ assert_nil(@debitor.delete_autoinvoice(invoice))
68
+ end
69
+ def test_autoinvoice_infos
70
+ invoice = flexmock('autoinvoice')
71
+ invoice.should_receive(:info).and_return('info')
72
+ @debitor.autoinvoices.push(invoice)
73
+ assert_equal(['info'], @debitor.autoinvoice_infos)
74
+ end
75
+ def test_invoice_infos
76
+ inv1 = flexmock('invoice')
77
+ inv1.should_receive(:info).and_return('info1')
78
+ inv1.should_receive(:status).and_return('selectable')
79
+ inv2 = flexmock('invoice')
80
+ inv2.should_receive(:info).and_return('info2')
81
+ inv2.should_receive(:status).and_return('not selectable')
82
+ @debitor.invoices.push(inv1, inv2)
83
+ assert_equal(['info1'], @debitor.invoice_infos('selectable'))
84
+ end
85
+ def test_next_autoinvoice_date
86
+ inv1 = flexmock('autoinvoice')
87
+ inv1.should_receive(:date).and_return(Date.today >> 1)
88
+ inv2 = flexmock('autoinvoice')
89
+ inv2.should_receive(:date).and_return(Date.today >> 2)
90
+ inv3 = flexmock('autoinvoice')
91
+ inv3.should_receive(:date)
92
+ @debitor.autoinvoices.push(inv1, inv2, inv3)
93
+ assert_equal(Date.today >> 1, @debitor.next_invoice_date)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env ruby
2
+ # TestFactory -- ydim -- 16.01.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.dirname(__FILE__)
5
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
6
+
7
+ require 'test/unit'
8
+ require 'stub/odba'
9
+ require 'flexmock'
10
+ require 'ydim/factory'
11
+
12
+ module YDIM
13
+ class TestFactory < Test::Unit::TestCase
14
+ include FlexMock::TestCase
15
+ def setup
16
+ @id_server = flexmock('IdServer')
17
+ @config = flexmock('Config')
18
+ @serv = flexmock('Registry')
19
+ @serv.should_receive(:id_server).and_return(@id_server)
20
+ @serv.should_receive(:config).and_return(@config)
21
+ @factory = Factory.new(@serv)
22
+ end
23
+ def test_create_invoice
24
+ @config.should_receive(:invoice_number_start).and_return(13)
25
+ @id_server.should_receive(:next_id).and_return { |key, start|
26
+ assert_equal(13, start)
27
+ assert_equal(:invoice, key)
28
+ 24
29
+ }
30
+ debitor = FlexMock.new
31
+ debitor.should_receive(:foreign?).and_return(false)
32
+ debitor.should_receive(:add_invoice).and_return { |inv|
33
+ assert_instance_of(Invoice, inv)
34
+ }
35
+ dinvs = nil
36
+ debitor.should_receive(:invoices).and_return {
37
+ dinvs = FlexMock.new
38
+ dinvs.should_receive(:odba_store, 1).and_return
39
+ dinvs
40
+ }
41
+ invoice = nil
42
+ assert_nothing_raised { invoice = @factory.create_invoice(debitor) }
43
+ assert_instance_of(Invoice, invoice)
44
+ end
45
+ def test_create_autoinvoice
46
+ @config.should_receive(:invoice_number_start).and_return(13)
47
+ @id_server.should_receive(:next_id).and_return { |key, start|
48
+ assert_equal(13, start)
49
+ assert_equal(:autoinvoice, key)
50
+ 24
51
+ }
52
+ debitor = FlexMock.new
53
+ debitor.should_receive(:foreign?).and_return(false)
54
+ debitor.should_receive(:add_autoinvoice).and_return { |inv|
55
+ assert_instance_of(AutoInvoice, inv)
56
+ }
57
+ dinvs = nil
58
+ debitor.should_receive(:autoinvoices).and_return {
59
+ dinvs = FlexMock.new
60
+ dinvs.should_receive(:odba_store, 1).and_return
61
+ dinvs
62
+ }
63
+ invoice = nil
64
+ assert_nothing_raised {
65
+ invoice = @factory.create_autoinvoice(debitor) }
66
+ assert_instance_of(AutoInvoice, invoice)
67
+ end
68
+ def test_generate_invoice
69
+ @config.should_receive(:vat_rate).and_return('current_vat_rate')
70
+ invs = flexmock('Invoices')
71
+ invs.should_receive(:odba_store).times(1)
72
+ debitor = flexmock('Debitor')
73
+ debitor.should_receive(:foreign?).and_return(false)
74
+ debitor.should_receive(:invoices).and_return(invs)
75
+ debitor.should_receive(:add_invoice).and_return { |invoice|
76
+ assert_equal(24, invoice.unique_id)
77
+ assert_equal(1, invoice.items.size)
78
+ item = invoice.items.first
79
+ assert_equal('current_vat_rate', item.vat_rate)
80
+ }
81
+ auto = AutoInvoice.new(1)
82
+ auto.invoice_interval = 3
83
+ item = Item.new
84
+ auto.add_item(item)
85
+
86
+ auto.instance_variable_set('@debitor', debitor)
87
+ flexstub(auto).should_receive(:odba_store).and_return {
88
+ assert_equal(Date.today >> 3, auto.date)
89
+ }
90
+ @config.should_receive(:invoice_number_start).and_return(13)
91
+ @id_server.should_receive(:next_id).and_return { |key, start|
92
+ assert_equal(13, start)
93
+ assert_equal(:invoice, key)
94
+ 24
95
+ }
96
+ @factory.generate_invoice(auto)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env ruby
2
+ # TestInvoice -- ydim -- 11.01.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'flexmock'
8
+ require 'ydim/invoice'
9
+
10
+ module YDIM
11
+ class TestInvoice < Test::Unit::TestCase
12
+ include FlexMock::TestCase
13
+ def setup
14
+ @invoice = Invoice.new(23)
15
+ end
16
+ def test_add_item
17
+ assert_equal([], @invoice.items)
18
+ item_id = 0
19
+ item = FlexMock.new
20
+ item.should_receive(:index=, 2).and_return { |idx|
21
+ assert_equal(item_id, idx)
22
+ item_id += 1
23
+ }
24
+ retval = @invoice.add_item(item)
25
+ assert_equal([item], @invoice.items)
26
+ assert_equal(item, retval)
27
+ retval = @invoice.add_item(item)
28
+ assert_equal([item, item], @invoice.items)
29
+ assert_equal(item, retval)
30
+ end
31
+ def test_item
32
+ item = flexmock('item')
33
+ item.should_receive(:index).and_return(4)
34
+ @invoice.items.push(item)
35
+ assert_nil(@invoice.item(0))
36
+ assert_equal(item, @invoice.item(4))
37
+ end
38
+ def test_debitor_writer
39
+ debitor = FlexMock.new
40
+ debitor.should_receive(:add_invoice, 1).and_return { |arg|
41
+ assert_equal(@invoice, arg)
42
+ }
43
+ @invoice.debitor = debitor
44
+ debitor2 = FlexMock.new
45
+ debitor.should_receive(:delete_invoice, 1).and_return { |arg|
46
+ assert_equal(@invoice, arg)
47
+ }
48
+ debitor2.should_receive(:add_invoice, 1).and_return { |arg|
49
+ assert_equal(@invoice, arg)
50
+ }
51
+ @invoice.debitor = debitor2
52
+ end
53
+ def test_due_date
54
+ @invoice.payment_period = nil
55
+ assert_nil(@invoice.due_date)
56
+ today = Date.today
57
+ @invoice.date = today
58
+ assert_equal(today, @invoice.due_date)
59
+ @invoice.payment_period = 10
60
+ assert_equal(today + 10, @invoice.due_date)
61
+ @invoice.payment_received = true
62
+ assert_nil(@invoice.due_date)
63
+ end
64
+ def test_pdf_invoice
65
+ debitor = FlexMock.new
66
+ debitor.should_receive(:add_invoice, 1).and_return { |arg|
67
+ assert_equal(@invoice, arg)
68
+ }
69
+ debitor.should_receive(:address).and_return { ['address'] }
70
+ @invoice.debitor = debitor
71
+ @invoice.description = 'description'
72
+ pdf = @invoice.pdf_invoice
73
+ assert_instance_of(PdfInvoice::Invoice, pdf)
74
+ assert_equal(['address'], pdf.debitor_address)
75
+ assert_equal(23, pdf.invoice_number)
76
+ assert_equal('description', pdf.description)
77
+ end
78
+ def test_status
79
+ @invoice.date = Date.today
80
+ assert_equal('is_open', @invoice.status)
81
+ @invoice.date -= 2
82
+ @invoice.payment_period = 1
83
+ assert_equal('is_due', @invoice.status)
84
+ @invoice.payment_received = true
85
+ assert_equal('is_paid', @invoice.status)
86
+ @invoice.deleted = true
87
+ assert_equal('is_trash', @invoice.status)
88
+ end
89
+ def test_info
90
+ info = @invoice.info
91
+ assert_instance_of(Invoice::Info, info)
92
+ end
93
+ def test_empty
94
+ assert_equal(true, @invoice.empty?)
95
+ item = flexmock('item')
96
+ @invoice.items.push(item)
97
+ assert_equal(false, @invoice.empty?)
98
+ end
99
+ end
100
+ class TestAutoInvoice < Test::Unit::TestCase
101
+ def setup
102
+ @invoice = AutoInvoice.new(23)
103
+ end
104
+ def test_advance
105
+ assert_equal 10, @invoice.payment_period
106
+ today = Date.today
107
+ subj = 'Reminder for <year>2008, 2009</year> and <year>2010</year>'
108
+ @invoice.reminder_subject = subj
109
+ retval = @invoice.advance(today)
110
+ assert_equal(today, @invoice.date)
111
+ assert_equal(@invoice.date, retval)
112
+ assert_equal subj, @invoice.reminder_subject
113
+ @invoice.invoice_interval = "inv_3"
114
+ retval = @invoice.advance(today)
115
+ assert_equal(today >> 3, @invoice.date)
116
+ assert_equal(@invoice.date, retval)
117
+ assert_equal subj, @invoice.reminder_subject
118
+ @invoice.invoice_interval = "inv_12"
119
+ retval = @invoice.advance(today - 2)
120
+ assert_equal((today - 2) >> 12, @invoice.date)
121
+ assert_equal(@invoice.date, retval)
122
+ subj = 'Reminder for <year>2009, 2010</year> and <year>2011</year>'
123
+ assert_equal subj, @invoice.reminder_subject
124
+ assert_equal 10, @invoice.payment_period
125
+ @invoice.invoice_interval = "inv_24"
126
+ subj = 'Reminder for <year>2011, 2012</year> and <year>2013</year>'
127
+ retval = @invoice.advance(today - 2)
128
+ assert_equal subj, @invoice.reminder_subject
129
+ end
130
+ end
131
+ end
data/test/test_item.rb ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # TestItem -- ydim -- 12.01.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'ydim/item'
8
+
9
+ module YDIM
10
+ class TestItem < Test::Unit::TestCase
11
+ def test_initialize
12
+ time = Time.now
13
+ data = {
14
+ :quantity => 1, :unit => 'St�ck', :text => 'Item 1', :price => 10.00,
15
+ :vat_rate => 7.6, :data => {}, :time => time, :expiry_time => nil
16
+ }
17
+ item = Item.new(data)
18
+ assert_equal(1, item.quantity)
19
+ assert_equal('St�ck', item.unit)
20
+ assert_equal('Item 1', item.text)
21
+ assert_equal(10.0, item.price)
22
+ assert_equal(7.6, item.vat_rate)
23
+ assert_equal({}, item.data)
24
+ assert_equal(time, item.time)
25
+ assert_equal(nil, item.expiry_time)
26
+ end
27
+ def test_total_netto
28
+ item = Item.new
29
+ item.quantity = 3.5
30
+ item.price = 2.0
31
+ assert_equal(7.0, item.total_netto)
32
+ end
33
+ def test_total_brutto
34
+ item = Item.new
35
+ item.quantity = 3.5
36
+ item.price = 2.0
37
+ item.vat_rate = 10
38
+ assert_equal(7.7, item.total_brutto)
39
+ end
40
+ end
41
+ end
data/test/test_mail.rb ADDED
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+ # TestMail -- ydim -- 01.02.2007 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'ydim/mail'
8
+ require 'flexmock'
9
+
10
+ module YDIM
11
+ class TestMail < Test::Unit::TestCase
12
+ include FlexMock::TestCase
13
+ def setup_config
14
+ config = flexmock('Config')
15
+ config.should_receive(:mail_body).and_return <<-MAIL
16
+ %s %s
17
+ %s
18
+ MAIL
19
+ config.should_receive(:mail_charset).and_return('utf-8')
20
+ config.should_receive(:mail_from).and_return('from@ywesee.com')
21
+ config.should_receive(:mail_recipients)\
22
+ .and_return(['cc@ywesee.com'])
23
+ config.should_receive(:salutation).and_return {
24
+ {
25
+ '' => 'Sehr geehrter Herr',
26
+ 'Herr' => 'Sehr geehrter Herr',
27
+ 'Frau' => 'Sehr geehrte Frau',
28
+ }
29
+ }
30
+ config.should_receive(:smtp_from).and_return('smtp@ywesee.com')
31
+ config.should_receive(:smtp_server).and_return('localhost')
32
+ config
33
+ end
34
+ def test_body
35
+ debitor = flexmock('Debitor')
36
+ debitor.should_receive(:salutation).and_return('Frau')
37
+ debitor.should_receive(:contact).and_return('Melanie Esterhazy')
38
+ invoice = flexmock('Invoice')
39
+ invoice.should_receive(:description).and_return('Description')
40
+ body = Mail.body(setup_config, debitor, invoice)
41
+ assert_equal <<-EXPECTED, body
42
+ Sehr geehrte Frau Melanie Esterhazy
43
+ Description
44
+ EXPECTED
45
+ end
46
+ def test_send_invoice
47
+ debitor = flexmock('Debitor')
48
+ debitor.should_receive(:email).and_return('test@ywesee.com')
49
+ debitor.should_receive(:emails_cc).and_return(['test.cc@ywesee.com'])
50
+ debitor.should_receive(:salutation).and_return('Herr')
51
+ debitor.should_receive(:name).and_return('Company-Name')
52
+ debitor.should_receive(:contact).and_return('Contact-Name')
53
+ invoice = flexmock('Invoice')
54
+ invoice.should_receive(:debitor).and_return(debitor)
55
+ invoice.should_receive(:unique_id).and_return(12345)
56
+ invoice.should_receive(:description).and_return('Description')
57
+ invoice.should_receive(:to_pdf).times(1).and_return('pdf-document')
58
+ smtp = flexmock('SMTP')
59
+ flexstub(Net::SMTP).should_receive(:new).and_return(smtp)
60
+ smtp.should_receive(:start).and_return { |block| block.call }
61
+ smtp.should_receive(:sendmail).with(String,
62
+ 'smtp@ywesee.com', 'cc@ywesee.com').and_return { assert(true) }
63
+ smtp.should_receive(:sendmail).with(String,
64
+ 'smtp@ywesee.com', 'test@ywesee.com').and_return { assert(true) }
65
+ smtp.should_receive(:sendmail).with(String,
66
+ 'smtp@ywesee.com', 'test.cc@ywesee.com').and_return { assert(true) }
67
+ Mail.send_invoice(setup_config, invoice)
68
+ end
69
+ def test_send_reminder
70
+ debitor = flexmock('Debitor')
71
+ debitor.should_receive(:email).and_return('test@ywesee.com')
72
+ debitor.should_receive(:emails_cc).and_return(['test.cc@ywesee.com'])
73
+ debitor.should_receive(:salutation).and_return('Herr')
74
+ debitor.should_receive(:name).and_return('Company-Name')
75
+ debitor.should_receive(:contact).and_return('Contact-Name')
76
+ invoice = flexmock('Invoice')
77
+ invoice.should_receive(:debitor).and_return(debitor)
78
+ invoice.should_receive(:unique_id).and_return(12345)
79
+ invoice.should_receive(:description).and_return('Description')
80
+ invoice.should_receive(:reminder_subject).and_return('Reminder')
81
+ invoice.should_receive(:reminder_body).and_return('Reminder Body')
82
+ smtp = flexmock('SMTP')
83
+ flexstub(Net::SMTP).should_receive(:new).and_return(smtp)
84
+ smtp.should_receive(:start).and_return { |block| block.call }
85
+ smtp.should_receive(:sendmail).with(String,
86
+ 'smtp@ywesee.com', 'cc@ywesee.com').and_return { assert(true) }
87
+ smtp.should_receive(:sendmail).with(String,
88
+ 'smtp@ywesee.com', 'test@ywesee.com').and_return { assert(true) }
89
+ smtp.should_receive(:sendmail).with(String,
90
+ 'smtp@ywesee.com', 'test.cc@ywesee.com').and_return { assert(true) }
91
+ Mail.send_reminder(setup_config, invoice)
92
+ end
93
+ end
94
+ end