Commit a1d51c769d422c460728c3eae4bd24d8560e8edd
1 parent
92f71ed5
Exists in
master
and in
28 other branches
[bsc-contract] Models and migrations
(ActionItem2079)
Showing
17 changed files
with
515 additions
and
50 deletions
Show diff stats
app/helpers/dates_helper.rb
... | ... | @@ -42,11 +42,11 @@ module DatesHelper |
42 | 42 | end |
43 | 43 | end |
44 | 44 | |
45 | - def show_period(date1, date2 = nil) | |
45 | + def show_period(date1, date2 = nil, use_numbers = false) | |
46 | 46 | if (date1 == date2) || (date2.nil?) |
47 | - show_date(date1) | |
47 | + show_date(date1, use_numbers) | |
48 | 48 | else |
49 | - _('from %{date1} to %{date2}') % {:date1 => show_date(date1), :date2 => show_date(date2)} | |
49 | + _('from %{date1} to %{date2}') % {:date1 => show_date(date1, use_numbers), :date2 => show_date(date2, use_numbers)} | |
50 | 50 | end |
51 | 51 | end |
52 | 52 | ... | ... |
plugins/bsc/db/migrate/20111018201143_create_bsc_plugin_sale.rb
0 → 100644
... | ... | @@ -0,0 +1,15 @@ |
1 | +class CreateBscPluginSale < ActiveRecord::Migration | |
2 | + def self.up | |
3 | + create_table :bsc_plugin_sales do |t| | |
4 | + t.references :product, :null => false | |
5 | + t.references :contract, :null => false | |
6 | + t.integer :quantity, :null => false | |
7 | + t.decimal :price | |
8 | + t.timestamps | |
9 | + end | |
10 | + end | |
11 | + | |
12 | + def self.down | |
13 | + drop_table :bsc_plugin_sales | |
14 | + end | |
15 | +end | ... | ... |
plugins/bsc/db/migrate/20111018201220_create_bsc_plugin_contracts_enterprises.rb
0 → 100644
... | ... | @@ -0,0 +1,12 @@ |
1 | +class CreateBscPluginContractsEnterprises < ActiveRecord::Migration | |
2 | + def self.up | |
3 | + create_table :bsc_plugin_contracts_enterprises, :id => false do |t| | |
4 | + t.references :contract | |
5 | + t.references :enterprise | |
6 | + end | |
7 | + end | |
8 | + | |
9 | + def self.down | |
10 | + drop_table :bsc_plugin_contracts_enterprises | |
11 | + end | |
12 | +end | ... | ... |
plugins/bsc/db/migrate/20111018201239_create_bsc_plugin_contract.rb
0 → 100644
... | ... | @@ -0,0 +1,22 @@ |
1 | +class CreateBscPluginContract < ActiveRecord::Migration | |
2 | + def self.up | |
3 | + create_table :bsc_plugin_contracts do |t| | |
4 | + t.string :client_name | |
5 | + t.integer :client_type | |
6 | + t.integer :business_type | |
7 | + t.string :state | |
8 | + t.string :city | |
9 | + t.integer :status, :default => 0 | |
10 | + t.integer :number_of_producers, :default => 0 | |
11 | + t.datetime :supply_start | |
12 | + t.datetime :supply_end | |
13 | + t.text :annotations | |
14 | + t.references :bsc | |
15 | + t.timestamps | |
16 | + end | |
17 | + end | |
18 | + | |
19 | + def self.down | |
20 | + drop_table :bsc_plugin_contracts | |
21 | + end | |
22 | +end | ... | ... |
plugins/bsc/lib/bsc_plugin/bsc.rb
... | ... | @@ -3,6 +3,7 @@ class BscPlugin::Bsc < Enterprise |
3 | 3 | has_many :enterprises |
4 | 4 | has_many :enterprise_requests, :class_name => 'BscPlugin::AssociateEnterprise' |
5 | 5 | has_many :products, :finder_sql => 'select * from products where enterprise_id in (#{enterprises.map(&:id).join(",")})' |
6 | + has_many :contracts, :class_name => 'BscPlugin::Contract' | |
6 | 7 | |
7 | 8 | validates_presence_of :nickname |
8 | 9 | validates_presence_of :company_name |
... | ... | @@ -19,8 +20,8 @@ class BscPlugin::Bsc < Enterprise |
19 | 20 | enterprise_requests.pending.map(&:enterprise).include?(enterprise) |
20 | 21 | end |
21 | 22 | |
22 | - def enterprises_to_json | |
23 | - enterprises.map { |enterprise| {:id => enterprise.id, :name => enterprise.name} }.to_json | |
23 | + def enterprises_to_token_input | |
24 | + enterprises.map { |enterprise| {:id => enterprise.id, :name => enterprise.name} } | |
24 | 25 | end |
25 | 26 | |
26 | 27 | def control_panel_settings_button | ... | ... |
... | ... | @@ -0,0 +1,75 @@ |
1 | +module BscPlugin::BscHelper | |
2 | + include ActionView::Helpers::FormTagHelper | |
3 | + | |
4 | + def token_input_field_tag(name, element_id, search_action, options = {}, text_field_options = {}, html_options = {}) | |
5 | + options[:min_chars] ||= 3 | |
6 | + options[:hint_text] ||= _("Type in a search term") | |
7 | + options[:no_results_text] ||= _("No results") | |
8 | + options[:searching_text] ||= _("Searching...") | |
9 | + options[:search_delay] ||= 1000 | |
10 | + options[:prevent_duplicates] ||= true | |
11 | + options[:backspace_delete_item] ||= false | |
12 | + options[:focus] ||= false | |
13 | + options[:avoid_enter] ||= true | |
14 | + options[:on_result] ||= 'null' | |
15 | + options[:on_add] ||= 'null' | |
16 | + options[:on_delete] ||= 'null' | |
17 | + options[:on_ready] ||= 'null' | |
18 | + | |
19 | + result = text_field_tag(name, nil, text_field_options.merge(html_options.merge({:id => element_id}))) | |
20 | + result += | |
21 | + " | |
22 | + <script type='text/javascript'> | |
23 | + jQuery('##{element_id}') | |
24 | + .tokenInput('#{url_for(search_action)}', { | |
25 | + minChars: #{options[:min_chars].to_json}, | |
26 | + prePopulate: #{options[:pre_populate].to_json}, | |
27 | + hintText: #{options[:hint_text].to_json}, | |
28 | + noResultsText: #{options[:no_results_text].to_json}, | |
29 | + searchingText: #{options[:searching_text].to_json}, | |
30 | + searchDelay: #{options[:serach_delay].to_json}, | |
31 | + preventDuplicates: #{options[:prevent_duplicates].to_json}, | |
32 | + backspaceDeleteItem: #{options[:backspace_delete_item].to_json}, | |
33 | + queryParam: #{name.to_json}, | |
34 | + tokenLimit: #{options[:token_limit].to_json}, | |
35 | + onResult: #{options[:on_result]}, | |
36 | + onAdd: #{options[:on_add]}, | |
37 | + onDelete: #{options[:on_delete]}, | |
38 | + onReady: #{options[:on_ready]}, | |
39 | + }) | |
40 | + " | |
41 | + result += options[:focus] ? ".focus();" : ";" | |
42 | + if options[:avoid_enter] | |
43 | + result += "jQuery('#token-input-#{element_id}') | |
44 | + .live('keydown', function(event){ | |
45 | + if(event.keyCode == '13') return false; | |
46 | + });" | |
47 | + end | |
48 | + result += "</script>" | |
49 | + result | |
50 | + end | |
51 | + | |
52 | + def product_display_name(product) | |
53 | + "#{product.name} (#{product.enterprise.name})" | |
54 | + end | |
55 | + | |
56 | + def display_text_field(name, value, options={:display_nil => false, :nil_symbol => '---'}) | |
57 | + value = value.to_s | |
58 | + if !value.blank? || options[:display_nil] | |
59 | + value = value.blank? ? options[:nil_symbol] : value | |
60 | + content_tag('tr', content_tag('td', name+': ', :class => 'bsc-field-label') + content_tag('td', value, :class => 'bsc-field-value')) | |
61 | + end | |
62 | + end | |
63 | + | |
64 | + def display_list_field(list, options={:nil_symbol => '---'}) | |
65 | + list.map do |item| | |
66 | + item = item.blank? ? options[:nil_symbol] : item | |
67 | + content_tag('tr', content_tag('td', item, :class => 'bsc-field-value')) | |
68 | + end.join | |
69 | + end | |
70 | + | |
71 | + def short_text(name, chars = 40) | |
72 | + truncate name, chars, '...' | |
73 | + end | |
74 | + | |
75 | +end | ... | ... |
... | ... | @@ -0,0 +1,84 @@ |
1 | +class BscPlugin::Contract < Noosfero::Plugin::ActiveRecord | |
2 | + validates_presence_of :bsc, :client_name | |
3 | + | |
4 | + has_many :sales, :class_name => 'BscPlugin::Sale' | |
5 | + has_many :products, :through => :sales | |
6 | + has_and_belongs_to_many :enterprises, :join_table => 'bsc_plugin_contracts_enterprises' | |
7 | + | |
8 | + belongs_to :bsc, :class_name => 'BscPlugin::Bsc' | |
9 | + | |
10 | + named_scope :status, lambda { |status_list| status_list.blank? ? {} : {:conditions => ['status in (?)', status_list]} } | |
11 | + named_scope :sorted_by, lambda { |sorter, direction| {:order => "#{sorter} #{direction}"} } | |
12 | + | |
13 | + before_create do |contract| | |
14 | + contract.created_at ||= Time.now.utc | |
15 | + contract.updated_at ||= Time.now.utc | |
16 | + end | |
17 | + | |
18 | + before_update do |contract| | |
19 | + contract.updated_at ||= Time.now.utc | |
20 | + end | |
21 | + | |
22 | + module Status | |
23 | + OPENED = 0 | |
24 | + NEGOTIATING = 1 | |
25 | + EXECUTING = 2 | |
26 | + CLOSED = 3 | |
27 | + | |
28 | + def self.types | |
29 | + [OPENED, NEGOTIATING, EXECUTING, CLOSED] | |
30 | + end | |
31 | + | |
32 | + def self.names | |
33 | + [_('Opened'), _('Negotiating'), _('Executing'), _('Closed')] | |
34 | + end | |
35 | + end | |
36 | + | |
37 | + module ClientType | |
38 | + STATE = 0 | |
39 | + FEDERAL = 1 | |
40 | + | |
41 | + def self.types | |
42 | + [STATE, FEDERAL] | |
43 | + end | |
44 | + | |
45 | + def self.names | |
46 | + [_('State'), _('Federal')] | |
47 | + end | |
48 | + end | |
49 | + | |
50 | + module BusinessType | |
51 | + PROJECTA = 0 | |
52 | + PROJECTB = 1 | |
53 | + | |
54 | + def self.types | |
55 | + [PROJECTA, PROJECTB] | |
56 | + end | |
57 | + | |
58 | + def self.names | |
59 | + [_('ProjectA'), _('ProjectB')] | |
60 | + end | |
61 | + end | |
62 | + | |
63 | + def enterprises_to_token_input | |
64 | + enterprises.map { |enterprise| {:id => enterprise.id, :name => enterprise.name} } | |
65 | + end | |
66 | + | |
67 | + def save_sales(sales) | |
68 | + failed_sales = {} | |
69 | + sales.each do |sale| | |
70 | + sale.merge!({:contract_id => id}) | |
71 | + begin | |
72 | + BscPlugin::Sale.create!(sale) | |
73 | + rescue Exception => exception | |
74 | + name = Product.find(sale[:product_id]).name | |
75 | + failed_sales[exception.clean_message] ? failed_sales[exception.clean_message] << name : failed_sales[exception.clean_message] = [name] | |
76 | + end | |
77 | + end | |
78 | + failed_sales | |
79 | + end | |
80 | + | |
81 | + def total_price | |
82 | + sales.inject(0) {|result, sale| sale.price*sale.quantity + result} | |
83 | + end | |
84 | +end | ... | ... |
... | ... | @@ -0,0 +1,19 @@ |
1 | +class BscPlugin::Sale < Noosfero::Plugin::ActiveRecord | |
2 | + validates_presence_of :product, :contract | |
3 | + validates_uniqueness_of :product_id, :scope => :contract_id | |
4 | + validates_numericality_of :quantity, :only_integer => true, :greater_than_or_equal_to => 0 | |
5 | + validates_numericality_of :price, :allow_nil => true | |
6 | + | |
7 | + belongs_to :product | |
8 | + belongs_to :contract, :class_name => 'BscPlugin::Contract' | |
9 | + | |
10 | + before_create do |sale| | |
11 | + sale.price ||= sale.product.price || 0 | |
12 | + sale.created_at ||= Time.now.utc | |
13 | + sale.updated_at ||= Time.now.utc | |
14 | + end | |
15 | + | |
16 | + before_update do |contract| | |
17 | + contract.updated_at ||= Time.now.utc | |
18 | + end | |
19 | +end | ... | ... |
plugins/bsc/lib/ext/enterprise.rb
... | ... | @@ -2,6 +2,8 @@ require_dependency 'enterprise' |
2 | 2 | |
3 | 3 | class Enterprise |
4 | 4 | belongs_to :bsc, :class_name => 'BscPlugin::Bsc' |
5 | + has_and_belongs_to_many :contracts, :class_name => 'BscPlugin::Contract', :join_table => 'bsc_plugin_contracts_enterprises' | |
6 | + | |
5 | 7 | FIELDS << 'bsc_id' |
6 | 8 | FIELDS << 'enabled' |
7 | 9 | FIELDS << 'validated' | ... | ... |
plugins/bsc/lib/ext/product.rb
plugins/bsc/test/unit/bsc_plugin/bsc_test.rb
... | ... | @@ -5,6 +5,12 @@ require File.dirname(__FILE__) + '/../../../lib/ext/enterprise' |
5 | 5 | class BscPlugin::BscTest < Test::Unit::TestCase |
6 | 6 | VALID_CNPJ = '94.132.024/0001-48' |
7 | 7 | |
8 | + def setup | |
9 | + @bsc = BscPlugin::Bsc.create!(:business_name => 'Sample Bsc', :company_name => 'Sample Bsc', :identifier => 'sample-bsc', :cnpj => VALID_CNPJ) | |
10 | + end | |
11 | + | |
12 | + attr_accessor :bsc | |
13 | + | |
8 | 14 | should 'validate presence of cnpj' do |
9 | 15 | bsc = BscPlugin::Bsc.new() |
10 | 16 | bsc.valid? |
... | ... | @@ -13,7 +19,7 @@ class BscPlugin::BscTest < Test::Unit::TestCase |
13 | 19 | end |
14 | 20 | |
15 | 21 | should 'validate uniqueness of cnpj' do |
16 | - bsc1 = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
22 | + bsc1 = bsc | |
17 | 23 | bsc2 = BscPlugin::Bsc.new(:cnpj => VALID_CNPJ) |
18 | 24 | bsc2.valid? |
19 | 25 | assert bsc2.errors.invalid?(:cnpj) |
... | ... | @@ -22,7 +28,6 @@ class BscPlugin::BscTest < Test::Unit::TestCase |
22 | 28 | should 'have many enterprises' do |
23 | 29 | e1 = Enterprise.new(:name => 'Enterprise1', :identifier => 'enterprise1') |
24 | 30 | e2 = Enterprise.new(:name => 'Enterprise2', :identifier => 'enterprise2') |
25 | - bsc = BscPlugin::Bsc.new(:business_name => 'Sample Bsc', :company_name => 'Sample Bsc Ltda.', :identifier => 'sample-bsc', :cnpj => VALID_CNPJ) | |
26 | 31 | bsc.enterprises << e1 |
27 | 32 | bsc.enterprises << e2 |
28 | 33 | bsc.save! |
... | ... | @@ -34,7 +39,6 @@ class BscPlugin::BscTest < Test::Unit::TestCase |
34 | 39 | should 'verify already requested enterprises' do |
35 | 40 | e1 = fast_create(Enterprise) |
36 | 41 | e2 = fast_create(Enterprise) |
37 | - bsc = BscPlugin::Bsc.new() | |
38 | 42 | task = BscPlugin::AssociateEnterprise.new(:target => e1, :bsc => bsc) |
39 | 43 | bsc.enterprise_requests.stubs(:pending).returns([task]) |
40 | 44 | |
... | ... | @@ -46,7 +50,6 @@ class BscPlugin::BscTest < Test::Unit::TestCase |
46 | 50 | e1 = fast_create(Enterprise) |
47 | 51 | e2 = fast_create(Enterprise) |
48 | 52 | category = fast_create(ProductCategory) |
49 | - bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
50 | 53 | |
51 | 54 | p1 = fast_create(Product, :product_category_id => category.id) |
52 | 55 | p2 = fast_create(Product, :product_category_id => category.id) |
... | ... | @@ -67,8 +70,15 @@ class BscPlugin::BscTest < Test::Unit::TestCase |
67 | 70 | end |
68 | 71 | |
69 | 72 | should 'not be able to create product' do |
70 | - bsc = BscPlugin::Bsc.new | |
71 | 73 | assert !bsc.create_product? |
72 | 74 | end |
73 | 75 | |
76 | + should 'have many contracts' do | |
77 | + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') | |
78 | + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') | |
79 | + | |
80 | + assert_includes bsc.contracts, contract1 | |
81 | + assert_includes bsc.contracts, contract2 | |
82 | + end | |
83 | + | |
74 | 84 | end | ... | ... |
... | ... | @@ -0,0 +1,109 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | +#require File.dirname(__FILE__) + '/../../../../../app/models/uploaded_file' | |
3 | +#require File.dirname(__FILE__) + '/../../../lib/ext/enterprise' | |
4 | + | |
5 | +class BscPlugin::ContractTest < Test::Unit::TestCase | |
6 | + def setup | |
7 | + @contract = BscPlugin::Contract.new(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
8 | + end | |
9 | + | |
10 | + attr_accessor :contract | |
11 | + | |
12 | + should 'validates presence of bsc' do | |
13 | + contract.bsc = nil | |
14 | + contract.valid? | |
15 | + assert contract.errors.invalid?(:bsc) | |
16 | + | |
17 | + contract.bsc = BscPlugin::Bsc.new | |
18 | + contract.valid? | |
19 | + assert !contract.errors.invalid?(:bsc) | |
20 | + end | |
21 | + | |
22 | + should 'associate contract with products through sales' do | |
23 | + contract.save! | |
24 | + product1 = fast_create(Product) | |
25 | + product2 = fast_create(Product) | |
26 | + sale1 = BscPlugin::Sale.create!(:product => product1, :contract => contract, :quantity => 3) | |
27 | + sale2 = BscPlugin::Sale.create!(:product => product2, :contract => contract, :quantity => 5) | |
28 | + | |
29 | + assert_includes contract.products, product1 | |
30 | + assert_includes contract.products, product2 | |
31 | + end | |
32 | + | |
33 | + should 'have many enterprises' do | |
34 | + contract.save! | |
35 | + enterprise1 = fast_create(Enterprise) | |
36 | + contract.enterprises << enterprise1 | |
37 | + enterprise2 = fast_create(Enterprise) | |
38 | + contract.enterprises << enterprise2 | |
39 | + | |
40 | + assert_includes contract.enterprises, enterprise1 | |
41 | + assert_includes contract.enterprises, enterprise2 | |
42 | + end | |
43 | + | |
44 | + should 'filter contracts by status' do | |
45 | + bsc = BscPlugin::Bsc.new | |
46 | + opened = BscPlugin::Contract::Status::OPENED | |
47 | + negotiating = BscPlugin::Contract::Status::NEGOTIATING | |
48 | + executing = BscPlugin::Contract::Status::EXECUTING | |
49 | + closed = BscPlugin::Contract::Status::CLOSED | |
50 | + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :status => opened, :client_name => 'Marvin') | |
51 | + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :status => negotiating, :client_name => 'Marvin') | |
52 | + contract3 = BscPlugin::Contract.create!(:bsc => bsc, :status => executing, :client_name => 'Marvin') | |
53 | + contract4 = BscPlugin::Contract.create!(:bsc => bsc, :status => closed, :client_name => 'Marvin') | |
54 | + | |
55 | + opened_and_executing = BscPlugin::Contract.status([opened, executing]) | |
56 | + negotiating_and_closed = BscPlugin::Contract.status([negotiating, closed]) | |
57 | + all = BscPlugin::Contract.status([]) | |
58 | + | |
59 | + assert_includes opened_and_executing, contract1 | |
60 | + assert_not_includes opened_and_executing, contract2 | |
61 | + assert_includes opened_and_executing, contract3 | |
62 | + assert_not_includes opened_and_executing, contract4 | |
63 | + | |
64 | + assert_not_includes negotiating_and_closed, contract1 | |
65 | + assert_includes negotiating_and_closed, contract2 | |
66 | + assert_not_includes negotiating_and_closed, contract3 | |
67 | + assert_includes negotiating_and_closed, contract4 | |
68 | + | |
69 | + assert_includes all, contract1 | |
70 | + assert_includes all, contract2 | |
71 | + assert_includes all, contract3 | |
72 | + assert_includes all, contract4 | |
73 | + end | |
74 | + | |
75 | + should 'sort contracts by date' do | |
76 | + bsc = BscPlugin::Bsc.new | |
77 | + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 2.day.ago, :client_name => 'Marvin') | |
78 | + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 1.day.ago, :client_name => 'Marvin') | |
79 | + contract3 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 3.day.ago, :client_name => 'Marvin') | |
80 | + | |
81 | + assert_equal [contract3, contract1, contract2], BscPlugin::Contract.sorted_by('created_at', 'asc') | |
82 | + end | |
83 | + | |
84 | + should 'sort contracts by client name' do | |
85 | + bsc = BscPlugin::Bsc.new | |
86 | + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvim') | |
87 | + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Adam') | |
88 | + contract3 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Eva') | |
89 | + | |
90 | + assert_equal [contract2, contract3, contract1], BscPlugin::Contract.sorted_by('client_name', 'asc') | |
91 | + end | |
92 | + | |
93 | + should 'return contract total price' do | |
94 | + contract.save! | |
95 | + price1 = 1 | |
96 | + quantity1 = 3 | |
97 | + price2 = 2 | |
98 | + quantity2 = 5 | |
99 | + total = price1*quantity1 + price2*quantity2 | |
100 | + product1 = fast_create(Product, :price => price1) | |
101 | + product2 = fast_create(Product, :price => price2) | |
102 | + sale1 = BscPlugin::Sale.create!(:product => product1, :contract => contract, :quantity => quantity1) | |
103 | + sale2 = BscPlugin::Sale.create!(:product => product2, :contract => contract, :quantity => quantity2) | |
104 | + | |
105 | + contract.reload | |
106 | + | |
107 | + assert_equal total, contract.total_price | |
108 | + end | |
109 | +end | ... | ... |
plugins/bsc/test/unit/bsc_plugin/enterprise_test.rb
... | ... | @@ -1,26 +0,0 @@ |
1 | -require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | -require File.dirname(__FILE__) + '/../../../../../app/models/uploaded_file' | |
3 | -require File.dirname(__FILE__) + '/../../../../../app/models/enterprise' | |
4 | -require File.dirname(__FILE__) + '/../../../lib/ext/enterprise' | |
5 | - | |
6 | -class ProductTest < Test::Unit::TestCase | |
7 | - VALID_CNPJ = '94.132.024/0001-48' | |
8 | - | |
9 | - should 'belongs to a bsc' do | |
10 | - bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
11 | - enterprise = fast_create(Enterprise, :bsc_id => bsc.id) | |
12 | - | |
13 | - assert_equal bsc, enterprise.bsc | |
14 | - end | |
15 | - | |
16 | - should 'return correct enterprises on validated and not validated namedscopes' do | |
17 | - validated_enterprise = fast_create(Enterprise, :validated => true) | |
18 | - not_validated_enterprise = fast_create(Enterprise, :validated => false) | |
19 | - | |
20 | - assert_includes Enterprise.validated, validated_enterprise | |
21 | - assert_not_includes Enterprise.validated, not_validated_enterprise | |
22 | - assert_not_includes Enterprise.not_validated, validated_enterprise | |
23 | - assert_includes Enterprise.not_validated, not_validated_enterprise | |
24 | - end | |
25 | -end | |
26 | - |
plugins/bsc/test/unit/bsc_plugin/product_test.rb
... | ... | @@ -1,14 +0,0 @@ |
1 | -require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | -require File.dirname(__FILE__) + '/../../../../../app/models/uploaded_file' | |
3 | - | |
4 | -class ProductTest < Test::Unit::TestCase | |
5 | - VALID_CNPJ = '94.132.024/0001-48' | |
6 | - | |
7 | - should 'return have bsc' do | |
8 | - bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
9 | - enterprise = fast_create(Enterprise, :bsc_id => bsc.id) | |
10 | - product = fast_create(Product, :enterprise_id => enterprise.id) | |
11 | - | |
12 | - assert_equal bsc, product.bsc | |
13 | - end | |
14 | -end |
... | ... | @@ -0,0 +1,86 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | + | |
3 | +class BscPlugin::SaleTest < Test::Unit::TestCase | |
4 | + def setup | |
5 | + @sale = BscPlugin::Sale.new | |
6 | + end | |
7 | + | |
8 | + attr_accessor :sale | |
9 | + | |
10 | + should 'validate presence of product and contract' do | |
11 | + sale.valid? | |
12 | + | |
13 | + assert sale.errors.invalid?(:product) | |
14 | + assert sale.errors.invalid?(:contract) | |
15 | + | |
16 | + product = Product.new | |
17 | + contract = BscPlugin::Contract.new | |
18 | + sale.product = product | |
19 | + sale.contract = contract | |
20 | + | |
21 | + assert !sale.errors.invalid?(product) | |
22 | + assert !sale.errors.invalid?(contract) | |
23 | + end | |
24 | + | |
25 | + should 'validate uniqueness of product and contract composed' do | |
26 | + product = fast_create(Product) | |
27 | + contract = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
28 | + sale1 = BscPlugin::Sale.create!(:product => product, :contract => contract, :quantity => 1) | |
29 | + sale2 = BscPlugin::Sale.new(:product => product, :contract => contract, :quantity => 1) | |
30 | + sale2.valid? | |
31 | + | |
32 | + assert sale2.errors.invalid?(:product_id) | |
33 | + end | |
34 | + | |
35 | + should 'validate quantity as a positive integer' do | |
36 | + sale.quantity = -1 | |
37 | + sale.valid? | |
38 | + assert sale.errors.invalid?(:quantity) | |
39 | + | |
40 | + sale.quantity = 1.5 | |
41 | + sale.valid? | |
42 | + assert sale.errors.invalid?(:quantity) | |
43 | + | |
44 | + sale.quantity = 3 | |
45 | + sale.valid? | |
46 | + assert !sale.errors.invalid?(:quantity) | |
47 | + end | |
48 | + | |
49 | + should 'set default price as product price if no price indicated' do | |
50 | + product = fast_create(Product, :price => 3.50) | |
51 | + contract = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
52 | + sale.product = product | |
53 | + sale.contract = contract | |
54 | + sale.quantity = 1 | |
55 | + sale.save! | |
56 | + | |
57 | + assert_equal product.price, sale.price | |
58 | + end | |
59 | + | |
60 | + should 'not overwrite with the product price if price informed' do | |
61 | + product = fast_create(Product, :price => 3.50) | |
62 | + contract = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
63 | + sale.product = product | |
64 | + sale.contract = contract | |
65 | + sale.quantity = 1 | |
66 | + sale.price = 2.50 | |
67 | + sale.save! | |
68 | + | |
69 | + assert_equal 2.50, sale.price | |
70 | + end | |
71 | + | |
72 | + should 'have default value for price' do | |
73 | + product1 = fast_create(Product, :price => 1) | |
74 | + product2 = fast_create(Product, :price => 1) | |
75 | + product3 = fast_create(Product) | |
76 | + contract = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
77 | + sale1 = BscPlugin::Sale.create!(:price => 2, :product => product1, :contract => contract, :quantity => 1) | |
78 | + sale2 = BscPlugin::Sale.create!(:product => product2, :contract => contract, :quantity => 1) | |
79 | + sale3 = BscPlugin::Sale.create!(:product => product3, :contract => contract, :quantity => 1) | |
80 | + | |
81 | + assert_equal 2, sale1.price | |
82 | + assert_equal 1, sale2.price | |
83 | + assert_equal 0, sale3.price | |
84 | + end | |
85 | +end | |
86 | + | ... | ... |
... | ... | @@ -0,0 +1,41 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | +require File.dirname(__FILE__) + '/../../../../../app/models/uploaded_file' | |
3 | +require File.dirname(__FILE__) + '/../../../../../app/models/enterprise' | |
4 | +require File.dirname(__FILE__) + '/../../../lib/ext/enterprise' | |
5 | + | |
6 | +class EnterpriseTest < Test::Unit::TestCase | |
7 | + VALID_CNPJ = '94.132.024/0001-48' | |
8 | + | |
9 | + def setup | |
10 | + @bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
11 | + end | |
12 | + | |
13 | + attr_accessor :bsc | |
14 | + | |
15 | + should 'belongs to a bsc' do | |
16 | + enterprise = fast_create(Enterprise, :bsc_id => bsc.id) | |
17 | + assert_equal bsc, enterprise.bsc | |
18 | + end | |
19 | + | |
20 | + should 'return correct enterprises on validated and not validated namedscopes' do | |
21 | + validated_enterprise = fast_create(Enterprise, :validated => true) | |
22 | + not_validated_enterprise = fast_create(Enterprise, :validated => false) | |
23 | + | |
24 | + assert_includes Enterprise.validated, validated_enterprise | |
25 | + assert_not_includes Enterprise.validated, not_validated_enterprise | |
26 | + assert_not_includes Enterprise.not_validated, validated_enterprise | |
27 | + assert_includes Enterprise.not_validated, not_validated_enterprise | |
28 | + end | |
29 | + | |
30 | + should 'be involved with many contracts' do | |
31 | + enterprise = fast_create(Enterprise) | |
32 | + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') | |
33 | + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') | |
34 | + enterprise.contracts << contract1 | |
35 | + enterprise.contracts << contract2 | |
36 | + | |
37 | + assert_includes enterprise.contracts, contract1 | |
38 | + assert_includes enterprise.contracts, contract2 | |
39 | + end | |
40 | +end | |
41 | + | ... | ... |
... | ... | @@ -0,0 +1,25 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../../test/test_helper' | |
2 | +require File.dirname(__FILE__) + '/../../../../../app/models/uploaded_file' | |
3 | + | |
4 | +class ProductTest < Test::Unit::TestCase | |
5 | + VALID_CNPJ = '94.132.024/0001-48' | |
6 | + | |
7 | + should 'return have bsc' do | |
8 | + bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) | |
9 | + enterprise = fast_create(Enterprise, :bsc_id => bsc.id) | |
10 | + product = fast_create(Product, :enterprise_id => enterprise.id) | |
11 | + | |
12 | + assert_equal bsc, product.bsc | |
13 | + end | |
14 | + | |
15 | + should 'have contracts through sales' do | |
16 | + product = fast_create(Product) | |
17 | + contract1 = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
18 | + contract2 = BscPlugin::Contract.create!(:bsc => BscPlugin::Bsc.new, :client_name => 'Marvin') | |
19 | + sale1 = BscPlugin::Sale.create!(:product => product, :contract => contract1, :quantity => 3) | |
20 | + sale2 = BscPlugin::Sale.create!(:product => product, :contract => contract2, :quantity => 5) | |
21 | + | |
22 | + assert_includes product.contracts, contract1 | |
23 | + assert_includes product.contracts, contract2 | |
24 | + end | |
25 | +end | ... | ... |