Hello I am a little bit confused about unit tests in Odoo
I have this two models :
###### Model 1
class VatTaxBreak(models.Model):
_name = "numidoo_invoicing.vattaxbreak"
order_id = fields.Many2one('sale.order', string="order_id")
date = fields.Date(string="Date", required=True)
currency_id = fields.Many2one(related='order_id.currency_id', depends=['order_id.currency_id'], store=True, string='Currency', readonly=True)
amount = fields.Monetary(string='Amount', store=True, required=True)
ref = fields.Char(string="Reference", required=True)
scanned_doc = fields.Binary(string="Scanned document", required=True)
scanned_doc_name=fields.Char()
@api.constrains('date')
def _date_valid(self):
for record in self:
if record.date > datetime.today():
raise ValidationError('La date de la franchise ne peut pas être dans le future')
@api.constrains('amount')
def _amount_valid(self):
for record in self:
if record.amount == 0 or record.amount < 0:
raise ValidationError('Le montant ne peut pas être nul ou négatif')
@api.constrains('scanned_doc_name')
def _scanned_doc_name_valid(self):
for record in self:
if record.scanned_doc:
if not record.scanned_doc_name:
raise ValidationError('pas de fichier')
else:
# check the file's extension
tmp = record.scanned_doc_name.split('.')
ext = tmp[len(tmp)-1]
if ext != 'pdf' and ext != 'png' and ext != 'jpg':
raise exceptions.ValidationError("Le fichier doit être une image (extension png ou jpg) ou bien un fichier pdf")
######### Model 2
class SaleOrderNumidoo(models.Model):
_inherit = 'sale.order'
vattaxbreak_ids = fields.One2many('numidoo_invoicing.vattaxbreak', 'order_id', string="Child franchise")
amount_vattaxbreak_total = fields.Monetary(string="Vat tax break", store=True, readonly=True, compute="_amount_vattaxbreak")
payment_mode = fields.Selection([('especes', 'Espèces'),
('par_cheque', 'Par chèque'),
('virement_bancaire', 'Virement Bancaire'),
('versement', 'Versement')
('a_terme', 'A terme')])
@api.depends('vattaxbreak_ids.amount')
def _amount_vattaxbreak(self):
"""
Compute the total amounts of the SO.
"""
for order in self:
amount_total = 0
for line in order.vattaxbreak_ids:
amount_total += line.amount
order.update({
'amount_vattaxbreak_total': - amount_total,
})
@api.depends('order_line.price_total','vattaxbreak_ids.amount')
def _amount_all(self):
super(SaleOrderNumidoo, self)._amount_all()
for order in self:
order.update({
'amount_total': order.amount_total + order.amount_vattaxbreak_total,
})
And I have three questions :
- When I create an instance in the class test, should I give the binary file in the Model 1 (vattaxbreak) because it is required?? if yes how would I do that
- My second question is how to create an instance should I use self.env or unittest.mock?? and why.
- In Model2 (SaleOrderNumidoo) I have a one2many field and in the Model 1 (vattaxbreak) I have many2one field, how would I populate this fields in my instance of tests?