redmine_crm 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,292 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ module RedmineCrm
4
+ class CurrencyTest < ActiveSupport::TestCase
5
+ FOO = '{ "priority": 1, "iso_code": "FOO", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 100, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }'
6
+ def register_foo(opts={})
7
+ foo_attrs = JSON.parse(FOO, :symbolize_names => true)
8
+ # Pass an array of attribute names to 'skip' to remove them from the 'FOO'
9
+ # json before registering foo as a currency.
10
+ Array(opts[:skip]).each { |attr| foo_attrs.delete(attr) }
11
+ RedmineCrm::Currency.register(foo_attrs)
12
+ end
13
+
14
+ def unregister_foo
15
+ Currency.unregister(JSON.parse(FOO, :symbolize_names => true))
16
+ end
17
+
18
+ def test_unknown_currency
19
+ assert_equal true, (Currency::UnknownCurrency < ArgumentError)
20
+ end
21
+
22
+ def test_matching_by_id
23
+ register_foo
24
+ foo = Currency.new(:foo)
25
+ assert_equal Currency.find(:foo), foo
26
+ assert_equal Currency.find(:FOO), foo
27
+ assert_equal Currency.find("foo"), foo
28
+ assert_equal Currency.find("FOO"), foo
29
+ unregister_foo
30
+ end
31
+
32
+ def test_nil_unless_matching_given_id
33
+ assert_equal nil, Currency.find("ZZZ")
34
+ end
35
+
36
+ class Mock
37
+ def to_s
38
+ '208'
39
+ end
40
+ end
41
+
42
+ def test_matching_by_given_numeric_code
43
+ assert_equal Currency.find_by_iso_numeric(978), Currency.new(:eur)
44
+ assert_not_equal Currency.find_by_iso_numeric(208), Currency.new(:eur)
45
+ assert_equal Currency.find_by_iso_numeric('840'), Currency.new(:usd)
46
+
47
+ assert_equal Currency.find_by_iso_numeric(Mock.new), Currency.new(:dkk)
48
+ assert_not_equal Currency.find_by_iso_numeric(Mock.new), Currency.new(:usd)
49
+ end
50
+
51
+ def test_nil_if_no_currency_has_the_given_num_code
52
+ assert_equal Currency.find_by_iso_numeric("non iso 4217 numeric code"), nil
53
+ assert_equal Currency.find_by_iso_numeric(0), nil
54
+ end
55
+
56
+ # .all
57
+ def test_array_of_currencies
58
+ assert Currency.all.include?(Currency.new(:usd))
59
+ end
60
+
61
+ def test_include_register_currencies
62
+ register_foo
63
+ assert Currency.all.include?(Currency.new(:foo))
64
+ unregister_foo
65
+ end
66
+
67
+ def test_sort_by_priority
68
+ assert_equal Currency.all.first.priority, 1
69
+ end
70
+
71
+ def test_raises_missing_attributes_error_if_no_priority
72
+ register_foo(:skip => :priority)
73
+ assert_raises Currency::MissingAttributeError do
74
+ Currency.all
75
+ end
76
+ unregister_foo
77
+ end
78
+
79
+ #.register
80
+ def test_register_new_currency
81
+ Currency.register(
82
+ iso_code: "XXX",
83
+ name: "Golden Doubloon",
84
+ symbol: "%",
85
+ subunit_to_unit: 100
86
+ )
87
+ new_currency = Currency.find("XXX")
88
+ assert_not_equal nil, new_currency
89
+ assert_equal "Golden Doubloon", new_currency.name
90
+ assert_equal "%", new_currency.symbol
91
+ Currency.unregister(iso_code: "XXX")
92
+ end
93
+
94
+ def test_present_iso_code
95
+ assert_raises KeyError do
96
+ Currency.register(name: "New currency")
97
+ end
98
+ end
99
+
100
+ # .unregister
101
+ def test_unregister_currency
102
+ Currency.register(iso_code: "XXX")
103
+ assert_not_equal nil, Currency.find("XXX")
104
+ Currency.unregister(iso_code: "XXX")
105
+ assert_equal nil, Currency.find("XXX")
106
+ end
107
+
108
+ def test_exitred_currency
109
+ Currency.register(iso_code: "XXX")
110
+ assert_equal true, Currency.unregister(iso_code: "XXX")
111
+ assert_equal false, Currency.unregister(iso_code: "XXX")
112
+ end
113
+
114
+ def test_passed_iso_code
115
+ Currency.register(iso_code: "XXX")
116
+ Currency.register(iso_code: "YYZ")
117
+ #test with string
118
+ Currency.unregister("XXX")
119
+ assert_equal nil, Currency.find("XXX")
120
+ #test with symbol
121
+ Currency.unregister(:yyz)
122
+ assert_equal nil, Currency.find(:yyz)
123
+ end
124
+
125
+ # .each
126
+ def test_each_currency_to_block
127
+ assert_equal true, Currency.respond_to?(:each)
128
+ currencies = []
129
+ Currency.each do |currency|
130
+ currencies.push(currency)
131
+ end
132
+
133
+ assert_equal currencies[0], Currency.all[0]
134
+ assert_equal currencies[1], Currency.all[1]
135
+ assert_equal currencies[-1], Currency.all[-1]
136
+ end
137
+
138
+ # enumerable
139
+ def test_implemants_enumerable
140
+ assert_equal true, Currency.respond_to?(:all?)
141
+ assert_equal true, Currency.respond_to?(:each_with_index)
142
+ assert_equal true, Currency.respond_to?(:map)
143
+ assert_equal true, Currency.respond_to?(:select)
144
+ assert_equal true, Currency.respond_to?(:reject)
145
+ end
146
+
147
+ # #initialize
148
+ def test_lookups_data_from_loading_config
149
+ currency = Currency.new("USD")
150
+ assert_equal :usd, currency.id
151
+ assert_equal 1, currency.priority
152
+ assert_equal "USD", currency.iso_code
153
+ assert_equal "840", currency.iso_numeric
154
+ assert_equal "United States Dollar", currency.name
155
+ assert_equal ".", currency.decimal_mark
156
+ assert_equal ".", currency.separator
157
+ assert_equal ",", currency.thousands_separator
158
+ assert_equal ",", currency.delimiter
159
+ assert_equal 1, currency.smallest_denomination
160
+ end
161
+
162
+ def test_raises_with_unknown_currency
163
+ assert_raises Currency::UnknownCurrency do
164
+ Currency.new("xxx")
165
+ end
166
+ end
167
+
168
+ # #<=>
169
+ def test_compare_by_priority
170
+ assert Currency.new(:cad) > Currency.new(:usd)
171
+ assert Currency.new(:usd) < Currency.new(:eur)
172
+ end
173
+
174
+ def test_compares_by_id_with_same_priority
175
+ Currency.register(iso_code: "ABD", priority: 15)
176
+ Currency.register(iso_code: "ABC", priority: 15)
177
+ Currency.register(iso_code: "ABE", priority: 15)
178
+ abd = Currency.find("ABD")
179
+ abc = Currency.find("ABC")
180
+ abe = Currency.find("ABE")
181
+ assert abd > abc
182
+ assert abe > abd
183
+ Currency.unregister("ABD")
184
+ Currency.unregister("ABC")
185
+ Currency.unregister("ABE")
186
+ end
187
+
188
+ # when one of the currencies has no 'priority' set
189
+ def test_compare_by_id
190
+ Currency.register(iso_code: "ABD") # No priority
191
+ abd = Currency.find(:abd)
192
+ usd = Currency.find(:usd)
193
+ assert abd < usd
194
+ Currency.unregister(iso_code: "ABD")
195
+ end
196
+
197
+ # "#=="
198
+ def test_strong_equal
199
+ eur = Currency.new(:eur)
200
+ assert eur === eur
201
+ end
202
+
203
+ def test_equal_id_in_different_case
204
+ assert_equal Currency.new(:eur), Currency.new(:eur)
205
+ assert_equal Currency.new(:eur), Currency.new(:EUR)
206
+ assert_not_equal Currency.new(:eur), Currency.new(:usd)
207
+ end
208
+
209
+ def test_direct_comparison_currency_and_symbol_string
210
+ assert_equal Currency.new(:eur), 'eur'
211
+ assert_equal Currency.new(:eur), 'EUR'
212
+ assert_equal Currency.new(:eur), :eur
213
+ assert_equal Currency.new(:eur), :EUR
214
+ assert_not_equal Currency.new(:eur), 'usd'
215
+ end
216
+
217
+ def test_comparison_with_nil
218
+ assert_not_equal nil, Currency.new(:eur)
219
+ end
220
+
221
+ #eql?
222
+ def test_eql
223
+ assert Currency.new(:eur).eql?(Currency.new(:eur))
224
+ assert !Currency.new(:eur).eql?(Currency.new(:usd))
225
+ end
226
+
227
+ # hash
228
+ def test_return_same_value_for_equal_objects
229
+ assert_equal Currency.new(:eur).hash, Currency.new(:eur).hash
230
+ assert_not_equal Currency.new(:eur).hash, Currency.new(:usd).hash
231
+ end
232
+
233
+ def test_return_intersection_for_array_of_object
234
+ intersection = [Currency.new(:eur), Currency.new(:usd)] & [Currency.new(:eur)]
235
+ assert_equal intersection, [Currency.new(:eur)]
236
+ end
237
+
238
+ # inspect
239
+ def test_work_as_documented
240
+ assert_equal Currency.new(:usd).inspect, %Q{#<RedmineCrm::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, exponent: 2.0, iso_code: USD, iso_numeric: 840, subunit: Cent, smallest_denomination: 1>}
241
+ end
242
+
243
+ # to_s
244
+ def test_to_s
245
+ assert_equal Currency.new(:usd).to_s, "USD"
246
+ assert_equal Currency.new(:eur).to_s, "EUR"
247
+ end
248
+
249
+ def test_to_sym
250
+ assert_equal Currency.new(:usd).to_sym, :USD
251
+ assert_equal Currency.new(:eur).to_sym, :EUR
252
+ end
253
+
254
+ # to_currency
255
+ def test_to_currency
256
+ usd = Currency.new(:usd)
257
+ assert_equal usd.to_currency, usd
258
+ end
259
+
260
+ def test_doesnt_create_new_symbol_indefiniteily
261
+ assert_raises Currency::UnknownCurrency do
262
+ Currency.new("bogus")
263
+ end
264
+ assert !Symbol.all_symbols.map{|s| s.to_s}.include?("bogus")
265
+ end
266
+
267
+ # code
268
+ def test_code_as_documented
269
+ assert_equal Currency.new(:usd).code, "$"
270
+ assert_equal Currency.new(:azn).code, "\u20BC"
271
+ end
272
+
273
+ # exponent
274
+ def test_conform_to_iso_4217
275
+ assert Currency.new(:jpy).exponent == 0
276
+ assert Currency.new(:usd).exponent == 2
277
+ assert Currency.new(:iqd).exponent == 3
278
+ end
279
+
280
+ # decimal_places
281
+ def test_proper_place_for_know_currency
282
+ assert Currency.new(:mro).decimal_places == 1
283
+ assert Currency.new(:usd).decimal_places == 2
284
+ end
285
+
286
+ def test_proper_place_for_custom_currency
287
+ register_foo
288
+ assert_equal 2, Currency.new(:foo).decimal_places
289
+ unregister_foo
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,17 @@
1
+ mysql:
2
+ :adapter: mysql2
3
+ :host: localhost
4
+ :username: root
5
+ :password: 1
6
+ :database: rails_plugin_test
7
+
8
+ sqlite3:
9
+ :adapter: sqlite3
10
+ :database: ':memory:'
11
+
12
+ postgresql:
13
+ :adapter: postgresql
14
+ :database: rails_plugin_test
15
+ :user: container
16
+ :password: 1
17
+ :host: localhost
@@ -0,0 +1,9 @@
1
+ class Issue < ActiveRecord::Base
2
+ rcrm_acts_as_taggable
3
+ # default_scope {where(:closed => false)}
4
+ belongs_to :user
5
+ validates_presence_of :description
6
+ end
7
+
8
+ class SpecialIssue < Issue
9
+ end
@@ -0,0 +1,12 @@
1
+ first_issue:
2
+ description: Some description for the issue
3
+ user: jonathan
4
+ second_issue:
5
+ description: Second issue
6
+ user: jonathan
7
+ third_issue:
8
+ description: Third issue
9
+ user: sam
10
+ fourth_issue:
11
+ description: Fourth issue
12
+ user: sam
@@ -0,0 +1,32 @@
1
+ tag_for_error:
2
+ tag: error
3
+ taggable: first_issue (Issue)
4
+ created_at: 2014-01-01
5
+ tag_for_error1:
6
+ tag: error
7
+ taggable: second_issue (Issue)
8
+ created_at: 2015-01-01
9
+ tag_for_error2:
10
+ tag: error
11
+ taggable: third_issue (Issue)
12
+ created_at: 2015-01-01
13
+ tag_for_feature:
14
+ tag: feature
15
+ taggable: second_issue (Issue)
16
+ created_at: 2015-01-01
17
+ tag_for_bug:
18
+ tag: bug
19
+ taggable: second_issue (Issue)
20
+ created_at: 2015-01-01
21
+ tag_for_question1:
22
+ tag: question
23
+ taggable: third_issue (Issue)
24
+ created_at: 2015-01-01
25
+ tag_for_question2:
26
+ tag: question
27
+ taggable: first_issue (Issue)
28
+ created_at: 2015-01-01
29
+ tag_for_question3:
30
+ tag: question
31
+ taggable: second_issue (Issue)
32
+ created_at: 2015-02-01
@@ -0,0 +1,11 @@
1
+ error:
2
+ name: error
3
+
4
+ feature:
5
+ name: New feature
6
+
7
+ bug:
8
+ name: bug
9
+
10
+ question:
11
+ name: question
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :issues
3
+ end
@@ -0,0 +1,5 @@
1
+ jonathan:
2
+ name: Jonathan
3
+
4
+ sam:
5
+ name: Sam
@@ -0,0 +1,25 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+
3
+ create_table "tags", :force => true do |t|
4
+ t.column "name", :string
5
+ end
6
+
7
+ create_table "taggings", :force => true do |t|
8
+ t.column "tag_id", :integer
9
+ t.column "taggable_id", :integer
10
+ t.column "taggable_type", :string
11
+ t.column "created_at", :datetime
12
+ end
13
+
14
+
15
+ create_table "users", :force => true do |t|
16
+ t.column "name", :string
17
+ end
18
+
19
+ create_table "issues", :force => true do |t|
20
+ t.column "description", :string
21
+ t.column "closed", :boolean
22
+ t.column "cached_tag_list", :string
23
+ t.column "user_id", :integer
24
+ end
25
+ end
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ module RedmineCrm
4
+ class TagTest < ActiveSupport::TestCase
5
+ def test_name_required
6
+ t = Tag.create
7
+ assert_match /blank/, t.errors[:name].to_s
8
+ end
9
+
10
+ def test_name_unique
11
+ t = Tag.create!(:name => "My tag")
12
+ duplicate = t.dup
13
+ assert !duplicate.save
14
+ assert_match /taken/, duplicate.errors[:name].to_s
15
+ end
16
+
17
+ def test_taggings
18
+ assert_equivalent [taggings(:tag_for_error), taggings(:tag_for_error1), taggings(:tag_for_error2)
19
+ ], tags(:error).taggings
20
+ assert_equivalent [taggings(:tag_for_question1), taggings(:tag_for_question2), taggings(:tag_for_question3)], tags(:question).taggings
21
+ end
22
+
23
+ def test_to_s
24
+ assert_equal tags(:error).name, tags(:error).to_s
25
+ end
26
+
27
+ def test_equality
28
+ assert_equal tags(:error), tags(:error)
29
+ assert_equal Tag.find(tags(:error).id), Tag.find(tags(:error).id)
30
+ assert_equal Tag.new(:name => 'A'), Tag.new(:name => 'A')
31
+ assert_not_equal Tag.new(:name => 'A'), Tag.new(:name => 'B')
32
+ end
33
+
34
+ def test_taggings_removed_when_tag_destroyed
35
+ assert_difference("Tagging.count", -Tagging.where(:tag_id => tags(:error).id).count) do
36
+ assert tags(:error).destroy
37
+ end
38
+ end
39
+
40
+ def test_all_counts
41
+ assert_tag_counts Tag.counts, :error => 3, :feature => 1, :bug => 1, :question => 3
42
+ end
43
+
44
+ def test_all_counts_with_string_conditions
45
+ assert_tag_counts Tag.counts(:conditions => 'taggings.created_at >= \'2015-01-01\''),
46
+ :question => 3, :error => 2, :feature => 1, :bug => 1
47
+ end
48
+
49
+ def test_all_counts_with_array_conditions
50
+ assert_tag_counts Tag.counts(:conditions => ['taggings.created_at >= ?', '2015-01-01']),
51
+ :question => 3, :error => 2, :feature => 1, :bug => 1
52
+ end
53
+
54
+ def test_all_counts_with_hash_conditions
55
+ tag_counts = Tag.counts(
56
+ :conditions => {
57
+ :taggings => { :created_at => (DateTime.parse('2014-12-31 23:59') .. DateTime.parse('4000-01-01')) }
58
+ }
59
+ )
60
+
61
+ assert_tag_counts tag_counts, :question => 3, :error => 2, :feature => 1, :bug => 1
62
+ end
63
+ end
64
+ end