From a8d9b47f7b3e5eab315a6dda057ef865d3ca12b5 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Thu, 9 Jul 2015 10:20:27 +0200 Subject: [PATCH] Use new() to create onchange records. Reduce redundancy in tests. --- partner_firstname/tests/base.py | 10 +++++++ partner_firstname/tests/test_onchange.py | 36 ++++++++---------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/partner_firstname/tests/base.py b/partner_firstname/tests/base.py index 9a48dbf3b..77b0b7915 100644 --- a/partner_firstname/tests/base.py +++ b/partner_firstname/tests/base.py @@ -76,3 +76,13 @@ class BaseCase(TransactionCase): self.check_fields = False with self.assertRaises(ex.EmptyNamesError): self.original.firstname = self.original.lastname = False + + +class OnChangeCase(TransactionCase): + is_company = False + + def new_partner(self): + """Create an empty partner. Ensure it is (or not) a company.""" + new = self.env["res.partner"].new() + new.is_company = self.is_company + return new diff --git a/partner_firstname/tests/test_onchange.py b/partner_firstname/tests/test_onchange.py index cc30aae34..6d2ec278f 100644 --- a/partner_firstname/tests/test_onchange.py +++ b/partner_firstname/tests/test_onchange.py @@ -1,23 +1,21 @@ # -*- coding: utf-8 -*- """These tests try to mimic the behavior of the UI form. -The form operates in onchange mode, and has only some limitations that must be -met. +The form operates in onchange mode, with its limitations. """ -from openerp.tests.common import TransactionCase +from .base import OnChangeCase -class PartnerCompanyCase(TransactionCase): +class PartnerCompanyCase(OnChangeCase): + is_company = True + def test_create_from_form(self): """A user creates a company from the form.""" name = u"Sôme company" with self.env.do_in_onchange(): # User presses ``new`` - partner = self.env["res.partner"].create({}) - - # User ensures it is a company - partner.is_company = True + partner = self.new_partner() # User sets a name, which triggers onchanges partner.name = name @@ -34,10 +32,7 @@ class PartnerCompanyCase(TransactionCase): """ with self.env.do_in_onchange(): # User presses ``new`` - partner = self.env["res.partner"].create({}) - - # User ensures it is a company - partner.is_company = True + partner = self.new_partner() # User sets a name, which triggers onchanges partner.name = u"Foó" @@ -51,16 +46,13 @@ class PartnerCompanyCase(TransactionCase): self.assertEqual(partner.lastname, False) -class PartnerContactCase(TransactionCase): +class PartnerContactCase(OnChangeCase): def test_create_from_form_only_firstname(self): """A user creates a contact with only the firstname from the form.""" firstname = u"Fïrst" with self.env.do_in_onchange(): # User presses ``new`` - partner = self.env["res.partner"].create({}) - - # User ensures it is not a company - partner.is_company = False + partner = self.new_partner() # Changes firstname, which triggers onchanges partner.firstname = firstname @@ -76,10 +68,7 @@ class PartnerContactCase(TransactionCase): lastname = u"Läst" with self.env.do_in_onchange(): # User presses ``new`` - partner = self.env["res.partner"].create({}) - - # User ensures it is not a company - partner.is_company = False + partner = self.new_partner() # Changes lastname, which triggers onchanges partner.lastname = lastname @@ -96,10 +85,7 @@ class PartnerContactCase(TransactionCase): lastname = u"Läst" with self.env.do_in_onchange(): # User presses ``new`` - partner = self.env["res.partner"].create({}) - - # User ensures it is not a company - partner.is_company = False + partner = self.new_partner() # Changes firstname, which triggers onchanges partner.firstname = firstname