Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Testing of computed field in Odoo 18 extension dev

Subscriure's

Get notified when there's activity on this post

This question has been flagged
developmentdebug
1 Respondre
484 Vistes
Avatar
Alex McColm

Hello,


I have been writing an extension which tracks when users have "marked as read" a given document in Odoo Knowledge. I am working on adding unit testing. I would like to ask what I might be doing wrong and what best practices are for testing in this context.

Here is an example of a unit test that fails, where as the actual behaviour seems to work in manual testing.

    def test_ack_outdated(self):

        """

        When a user acknowledges an article, 2s pass, then the article changes,

        the ack is marked outdated.

        """

        self.article_1.must_acknowledge = True

        self.article_1.with_user(self.user_1).verify_acknowledgement()

        self.article_1.sudo().write({"body": "<h1>Updated Article</h1>"})

        self.env.flush_all() # Flush changes to DB session after writing!

        sleep(2.0)


        ack_record = self.env['knowledge.article.ack'].search([

            ('user_id', '=', self.user_1.id),

            ('article_id', '=', self.article_1.id)

        ])

        self.assertEqual(ack_record.user_id.id, self.user_1.id)

        self.assertEqual(ack_record.article_id.id, self.article_1.id)

        self.assertIsNotNone(ack_record.ack_datetime)

        self.assertTrue(ack_record.is_outdated)


The final assertion fails - the acknowledgment by the user of the article has not been marked as outdated. The field is computed as follows - it is very simple.

    ack_datetime = fields.Datetime(

        string='Acknowledged Date & Time',

        default=fields.Datetime.now

    )


    is_outdated = fields.Boolean(

        compute='_compute_is_outdated',

        store=True,

    )


    @api.depends('ack_datetime', 'article_id.write_date')

    def _compute_is_outdated(self):

        for rec in self:

            rec.is_outdated = all([

                rec.ack_datetime,

                rec.article_id.write_date,

                rec.ack_datetime <= rec.article_id.write_date

            ])


Also, should i be using the Form class from the testing modules provided by Odoo? 

0
Avatar
Descartar
Avatar
Alex McColm
Autor Best Answer

OK, I might have found an answer to this, and wow it's weird.

So when we write to a model the write_date appears to be set to the time of the start of the database transaction. I present as evidence the following. There are a few other places in the file where cr.now() is used.

https://github.com/odoo/odoo/blob/18.0/odoo/models.py#L4732

My unit test is within one transaction. It's not that the write_date isn't updated when I modify the article. It is set to the same date as before, because we're in the same transaction. Rather than the actual time which includes a few seconds that have passed (I did fix the position of the sleep() call from OP)

Because my computed field checks write_date, it seems there are some barriers to testing it

0
Avatar
Descartar
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
How do I correctly create a calculated field to sum two other field values in Odoo Studio (Odoo Online)? Solved
development debug
Avatar
Avatar
2
de nov. 25
543
How can I make buttons in Odoo 18? Solved
development debug
Avatar
Avatar
Avatar
3
de set. 25
1575
Problem when creating my first module locally
development debug
Avatar
Avatar
Avatar
Avatar
Avatar
4
d’ag. 25
2147
Show amount of current users online
development debug
Avatar
Avatar
1
de juny 25
1814
How to override default Contacts form view without DB conflict? Solved
development debug
Avatar
Avatar
1
de maig 25
1879
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now