<commit_before>require "lib/ooor.rb"

#RSpec executable specification; see http://rspec.info/ for more information.
#Run the file with the rspec command  from the rspec gem
describe Ooor do
  before(:all) do
    @url = 'http://localhost:8069/xmlrpc'
    @db_password = '<PASSWORD>'
    @username = 'admin'
    @password = '<PASSWORD>'
    @database = 'ooor_test'
    @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>)
  end

  it "should keep quiet if no database is mentioned" do
    @ooor.loaded_models.should be_empty
  end

  it "should be able to list databases" do
    @ooor.list.should be_kind_of(Array) 
  end

  it "should be able to create a new database with demo data" do
    unless @ooor.list.index(@database)
      @ooor.create(@db_password, @database)
    end
    @ooor.list.index(@database).should_not be_nil
  end

  
  describe "Configure existing database" do
    before(:all) do
      @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>, :database => @database)
    end

    it "should be able to load a profile" do
      manufacturing_module_id = IrModuleModule.search([['name','=', 'profile_manufacturing']])[0]
      unless IrModuleModule.find(manufacturing_module_id).state == "installed"
        w = @ooor.old_wizard_step('base_setup.base_setup')
        w.company(:profile => manufacturing_module_id)
        w.update(:name => 'Akretion.com', :state_id => false)
        w.finish
        @ooor.load_models
        @ooor.loaded_models.should_not be_empty
      end
    end

    it "should be able to configure the database" do
      chart_module_id = IrModuleModule.search([['category_id', '=', 'Account Charts'], ['name','=', 'l10n_fr']])[0]
      unless IrModuleModule.find(chart_module_id).state == "installed"
        w2 = @ooor.const_get('account.config.wizard').create(:charts => chart_module_id)
        w2.action_create
        w3 = @ooor.const_get('wizard.multi.charts.accounts').create
        w3.action_create
      end
    end
  end


  describe "Do operations on configured database" do
    before(:all) do
      @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>, :database => @database,
        :models => ['res.user', 'res.partner', 'product.product',  'sale.order', 'account.invoice', 'product.category', 'stock.move', 'ir.ui.menu'])
    end

    describe "Finders operations" do

      it "should be able to find data by id" do
        p = ProductProduct.find(1)
        p.should_not be_nil
        l = ProductProduct.find([1,2])
        l.size.should == 2
        a = AccountInvoice.find(1)
        a.should_not be_nil
      end

      it "should load required models on the fly" do
        SaleOrder.find(1).shop_id.should be_kind_of(SaleShop)
      end

      it "should be able to specify the fields to read" do
        p = ProductProduct.find(1, :fields=>["state", "id"])
        p.should_not be_nil
      end

      it "should be able to find using ir.model.data absolute ids" do
        p = ProductProduct.find('product_product_pc1')
        p.should_not be_nil
        p = ProductProduct.find('product.product_product_pc1')#module scoping is optionnal
        p.should_not be_nil
      end

      it "should be able to use OpenERP domains" do
        partners = ResPartner.find(:all, :domain=>[['supplier', '=', 1],['active','=',1]], :fields=>["id", "name"])
        partners.should_not be_empty
        products = ProductProduct.find(:all, :domain=>[['categ_id','=',1],'|',['name', '=', 'PC1'],['name','=','PC2']])
        products.should be_kind_of(Array)
      end

      it "should mimic ActiveResource scoping" do
        partners = ResPartner.find(:all, :params => {:supplier => true})
        partners.should_not be_empty
	end
	
      it "should mimic ActiveResource scoping with first" do
        partner = ResPartner.find(:first, :params => {:supplier => true})
        partners.should be_kind_of ResPartner
      end

      it "should support OpenERP context in finders" do
        p = ProductProduct.find(1, :context => {:my_key => 'value'})
        p.should_not be_nil
      end

      it "should support OpenERP search method" do
        partners = ResPartner.search([['name', 'ilike', 'a']], 0, 2)
        partners.should_not be_empty
      end

      it "should cast dates properly from OpenERP to Ruby" do
        o = SaleOrder.find(1)
        o.date_order.should be_kind_of(Date)
        m = StockMove.find(1)
        m.date.should be_kind_of(Time)
      end

      it "should be able to call any Class method" do
        ResPartner.name_search('ax', [], 'ilike', {}).should_not be_nil
      end

    end

    describe "Relations reading" do
      it "should read many2one relations" do
        o = SaleOrder.find(1)
        o.partner_id.should be_kind_of(ResPartner)
        p = ProductProduct.find(1) #inherited via product template
        p.categ_id.should be_kind_of(ProductCategory)
      end

      it "should read one2many relations" do
        o = SaleOrder.find(1)
        o.order_line.each do |line|
          line.should be_kind_of(SaleOrderLine)
        end
      end

      it "should read many2many relations" do
        SaleOrder.find(1).order_line[1].invoice_lines.should be_kind_of(Array)
      end

      it "should read polymorphic references" do
        IrUiMenu.find(:first, :domain => [['name', '=', 'Partners'], ['parent_id', '!=', false]]).action.should be_kind_of(IrActionsAct_window)
      end
    end

    describe "Basic creations" do
      it "should be able to create a product" do
        p = ProductProduct.create(:name => "testProduct1", :categ_id => 1)
        ProductProduct.find(p.id).categ_id.id.should == 1
        p = ProductProduct.new(:name => "testProduct1")
        p.categ_id = 1
        p.save
        p.categ_id.id.should == 1
      end

      it "should support the context at object creation" do
		p = ProductProduct.new({:name => "testProduct1", :categ_id => 1}, false, {:lang => 'en_US', :user_id=>1, :password => '<PASSWORD>'})
		p.object_session[:context][:lang] .should == 'en_US'
		p.object_session[:user_id].should == 1
		p.object_session[:password].should == "<PASSWORD>"
		p.save
	  end

      it "should support context when instanciating collections" do
		products = ProductProduct.find([1, 2, 3], :context => {:lang => 'en_US', :user_id=>1, :password => '<PASSWORD>'})
		p = products[0]
		p.object_session[:context][:lang] .should == 'en_US'
		p.object_session[:user_id].should == 1
		p.object_session[:password].should == "<PASSWORD>"
		p.save
      end

      it "should be able to create an order" do
        o = SaleOrder.create(:partner_id => ResPartner.search([['name', 'ilike', 'Agrolait']])[0], 
          :partner_order_id => 1, :partner_invoice_id => 1, :partner_shipping_id => 1, :pricelist_id => 1)
        o.id.should be_kind_of(Integer)
      end

      it "should be able to to create an invoice" do
        i = AccountInvoice.new(:origin => 'ooor_test')
        partner_id = ResPartner.search([['name', 'ilike', 'Agrolait']])[0]
        i.on_change('onchange_partner_id', :partner_id, partner_id, 'out_invoice', partner_id, false, false)
        i.save
        i.id.should be_kind_of(Integer)
      end

      it "should be able to call on_change" do
        o = SaleOrder.new
        partner_id = ResPartner.search([['name', 'ilike', 'Agrolait']])[0]
        o.on_change('onchange_partner_id', :partner_id, partner_id, partner_id)
        o.save
        line = SaleOrderLine.new(:order_id => o.id)
        product_id = 1
        pricelist_id = 1
        product_uom_qty = 1
        line.on_change('product_id_change', :product_id, product_id, pricelist_id, product_id, product_uom_qty, false, 1, false, false, o.partner_id.id, 'en_US', true, false, false, false)
        line.save
        SaleOrder.find(o.id).order_line.size.should == 1
      end

      it "should use default fields on creation" do
        p = ProductProduct.new
        p.sale_delay.should be_kind_of(Integer)
      end

      it "should skipped inherited default fields properly, for instance at product variant creation" do
        ProductProduct.create(:product_tmpl_id => 25, :code => 'OOOR variant').should be_kind_of(ProductProduct)
      end
    end

    describe "Basic updates" do
      it "should cast properly from Ruby to OpenERP" do
        o = SaleOrder.find(1).copy()
        o.date_order = 2.days.ago
        o.save
      end

      it "should be able to reload resource" do
        s = SaleOrder.find(1)
        s.reload.should be_kind_of(SaleOrder)
      end
    end

    describe "Relations assignations" do
      it "should be able to do product.taxes_id = [1,2]" do
        p = ProductProduct.find(1)
        p.taxes_id = [1, 2]<commit_msg>fixed our test suite. Specially, we changed the way inherited resource such as product.product are created when specifying a template as OpenERP has some limitations here.<commit_after>require "lib/ooor.rb"

#RSpec executable specification; see http://rspec.info/ for more information.
#Run the file with the rspec command  from the rspec gem
describe Ooor do
  before(:all) do
    @url = 'http://localhost:8069/xmlrpc'
    @db_password = '<PASSWORD>'
    @username = 'admin'
    @password = '<PASSWORD>'
    @database = 'ooor_test'
    @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>)
  end

  it "should keep quiet if no database is mentioned" do
    @ooor.loaded_models.should be_empty
  end

  it "should be able to list databases" do
    @ooor.list.should be_kind_of(Array) 
  end

  it "should be able to create a new database with demo data" do
    unless @ooor.list.index(@database)
      @ooor.create(@db_password, @database)
    end
    @ooor.list.index(@database).should_not be_nil
  end

  
  describe "Configure existing database" do
    before(:all) do
      @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>, :database => @database)
    end

    it "should be able to load a profile" do
      manufacturing_module_id = IrModuleModule.search([['name','=', 'profile_manufacturing']])[0]
      unless IrModuleModule.find(manufacturing_module_id).state == "installed"
        w = @ooor.old_wizard_step('base_setup.base_setup')
        w.company(:profile => manufacturing_module_id)
        w.update(:name => 'Akretion.com', :state_id => false)
        w.finish
        @ooor.load_models
        @ooor.loaded_models.should_not be_empty
      end
    end

    it "should be able to configure the database" do
      chart_module_id = IrModuleModule.search([['category_id', '=', 'Account Charts'], ['name','=', 'l10n_fr']])[0]
      unless IrModuleModule.find(chart_module_id).state == "installed"
        w2 = @ooor.const_get('account.config.wizard').create(:charts => chart_module_id)
        w2.action_create
        w3 = @ooor.const_get('wizard.multi.charts.accounts').create
        w3.action_create
      end
    end
  end


  describe "Do operations on configured database" do
    before(:all) do
      @ooor = Ooor.new(:url => @url, :username => @username, :password => <PASSWORD>, :database => @database,
        :models => ['res.user', 'res.partner', 'product.product',  'sale.order', 'account.invoice', 'product.category', 'stock.move', 'ir.ui.menu'])
    end

    describe "Finders operations" do

      it "should be able to find data by id" do
        p = ProductProduct.find(1)
        p.should_not be_nil
        l = ProductProduct.find([1,2])
        l.size.should == 2
        a = AccountInvoice.find(1)
        a.should_not be_nil
      end

      it "should load required models on the fly" do
        SaleOrder.find(1).shop_id.should be_kind_of(SaleShop)
      end

      it "should be able to specify the fields to read" do
        p = ProductProduct.find(1, :fields=>["state", "id"])
        p.should_not be_nil
      end

      it "should be able to find using ir.model.data absolute ids" do
        p = ProductProduct.find('product_product_pc1')
        p.should_not be_nil
        p = ProductProduct.find('product.product_product_pc1')#module scoping is optionnal
        p.should_not be_nil
      end

      it "should be able to use OpenERP domains" do
        partners = ResPartner.find(:all, :domain=>[['supplier', '=', 1],['active','=',1]], :fields=>["id", "name"])
        partners.should_not be_empty
        products = ProductProduct.find(:all, :domain=>[['categ_id','=',1],'|',['name', '=', 'PC1'],['name','=','PC2']])
        products.should be_kind_of(Array)
      end

      it "should mimic ActiveResource scoping" do
        partners = ResPartner.find(:all, :params => {:supplier => true})
        partners.should_not be_empty
      end
	
      it "should mimic ActiveResource scoping with first" do
        partner = ResPartner.find(:first, :params => {:customer => true})
        partner.should be_kind_of ResPartner
      end

      it "should support OpenERP context in finders" do
        p = ProductProduct.find(1, :context => {:my_key => 'value'})
        p.should_not be_nil
      end

      it "should support OpenERP search method" do
        partners = ResPartner.search([['name', 'ilike', 'a']], 0, 2)
        partners.should_not be_empty
      end

      it "should cast dates properly from OpenERP to Ruby" do
        o = SaleOrder.find(1)
        o.date_order.should be_kind_of(Date)
        m = StockMove.find(1)
        m.date.should be_kind_of(Time)
      end

      it "should be able to call any Class method" do
        ResPartner.name_search('ax', [], 'ilike', {}).should_not be_nil
      end

    end

    describe "Relations reading" do
      it "should read many2one relations" do
        o = SaleOrder.find(1)
        o.partner_id.should be_kind_of(ResPartner)
        p = ProductProduct.find(1) #inherited via product template
        p.categ_id.should be_kind_of(ProductCategory)
      end

      it "should read one2many relations" do
        o = SaleOrder.find(1)
        o.order_line.each do |line|
        line.should be_kind_of(SaleOrderLine)
        end
      end

      it "should read many2many relations" do
        s = SaleOrder.find(1)
        s.order_policy = 'manual'
        s.save
        s.wkf_action('order_confirm')
        s.wkf_action('manual_invoice')
        SaleOrder.find(1).order_line[1].invoice_lines.should be_kind_of(Array)
      end

      it "should read polymorphic references" do
        IrUiMenu.find(:first, :domain => [['name', '=', 'Partners'], ['parent_id', '!=', false]]).action.should be_kind_of(IrActionsAct_window)
      end
    end

    describe "Basic creations" do
      it "should be able to create a product" do
        p = ProductProduct.create(:name => "testProduct1", :categ_id => 1)
        ProductProduct.find(p.id).categ_id.id.should == 1
        p = ProductProduct.new(:name => "testProduct1")
        p.categ_id = 1
        p.save
        p.categ_id.id.should == 1
      end

      it "should support the context at object creation" do
        p = ProductProduct.new({:name => "testProduct1", :categ_id => 1}, false, {:lang => 'en_US', :user_id=>1, :password => '<PASSWORD>'})
        p.object_session[:context][:lang] .should == 'en_US'
        p.object_session[:user_id].should == 1
        p.object_session[:password].should == "<PASSWORD>"
        p.save
      end

      it "should support context when instanciating collections" do
        products = ProductProduct.find([1, 2, 3], :context => {:lang => 'en_US', :user_id=>1, :password => '<PASSWORD>'})
        p = products[0]
        p.object_session[:context][:lang] .should == 'en_US'
        p.object_session[:user_id].should == 1
        p.object_session[:password].should == "<PASSWORD>"
        p.save
      end

      it "should be able to create an order" do
        o = SaleOrder.create(:partner_id => ResPartner.search([['name', 'ilike', 'Agrolait']])[0], 
          :partner_order_id => 1, :partner_invoice_id => 1, :partner_shipping_id => 1, :pricelist_id => 1)
        o.id.should be_kind_of(Integer)
      end

      it "should be able to to create an invoice" do
        i = AccountInvoice.new(:origin => 'ooor_test')
        partner_id = ResPartner.search([['name', 'ilike', 'Agrolait']])[0]
        i.on_change('onchange_partner_id', :partner_id, partner_id, 'out_invoice', partner_id, false, false)
        i.save
        i.id.should be_kind_of(Integer)
      end

      it "should be able to call on_change" do
        o = SaleOrder.new
        partner_id = ResPartner.search([['name', 'ilike', 'Agrolait']])[0]
        o.on_change('onchange_partner_id', :partner_id, partner_id, partner_id)
        o.save
        line = SaleOrderLine.new(:order_id => o.id)
        product_id = 1
        pricelist_id = 1
        product_uom_qty = 1
        line.on_change('product_id_change', :product_id, product_id, pricelist_id, product_id, product_uom_qty, false, 1, false, false, o.partner_id.id, 'en_US', true, false, false, false)
        line.save
        SaleOrder.find(o.id).order_line.size.should == 1
      end

      it "should use default fields on creation" do
        p = ProductProduct.new
        p.sale_delay.should be_kind_of(Integer)
      end

      it "should skipped inherited default fields properly, for instance at product variant creation" do
        #note that we force [] here for the default_get_fields otherwise OpenERP will blows up while trying to write in the product template!
        create({:product_tmpl_id => 25, :code => 'OOOR variant'}, {}, []).should be_kind_of(ProductProduct)
      end
    end

    describe "Basic updates" do
      it "should cast properly from Ruby to OpenERP" do
        o = SaleOrder.find(1).copy()
        o.date_order = 2.days.ago
        o.save
      end

      it "should be able to reload resource" do
        s = SaleOrder.find(1)
        s.reload.should be_kind_of(SaleOrder)
      end
    end

    describe "Relations assignations" do
      it "should be able to do product.taxes_id = [1,2]" do
        p = ProductProduct.find(1)
        p.taxes_id = [1, 2]
{'fraction_non_alphanumeric': 0.12502771004211927, 'fraction_numerical': 0.008590113056971847, 'mean_word_length': 2.819856054191363, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64607', 'n_tokens_mistral': 5952, 'n_tokens_neox': 5751, 'n_words': 1511}