No database name

Sir, can you plz Explain this error along with the solution.

database_name = Transaction().database.name
AttributeError: ‘NoneType’ object has no attribute ‘name’

It seems that no transaction is started so there is no conection to the database.

If you share the full trackeback and the code you are executing we can give more detailed help

import unittest
import doctest
import trytond.tests.test_tryton
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.pool import Pool
from trytond.exceptions import UserError
from trytond.transaction import Transaction
from trytond.tests.test_tryton import doctest_teardown
from trytond.tests.test_tryton import doctest_checker
from trytond.modules.company.tests import create_company, set_company

class HraTestCase(ModuleTestCase):

    'Test Hra module'
    module = 'hr_hra'
    
    @with_transaction()
    def test_employee_data(self):
        'Create employee'
        pool = Pool()
        party_cur = None
        company = create_company()
        Employee = pool.get('company.employee')
        party = pool.get('party.party')
        party1 = party.create([{
            'name': 'party 1',
            }])
        if party1 != []:
            party_cur = party1[0]
        employee = Employee.create([{
            'company': company,
            'party': party_cur,
            'employee_group': 'A',
            'employee_status': 'Regular',
            'primary_phone': '9935164850'
            }])
        if employee != []:
            employee_cur = employee[0]
        self.assertTrue(employee_cur.id)
    
    @with_transaction()
    def test_salary_code(self):
        'Test salary code constraint'
        pool = Pool()
        party_cur = None
        company = create_company()
        Employee = pool.get('company.employee')
        party = pool.get('party.party')
        party1 = party.create([{
            'name': 'party 1',
            }])
        if party1 != []:
            party_cur = party1[0]
        salary_code = None
        employee = Employee.create([{
            'company': company,
            'party': party_cur,
            # 'salary_code': '5555',
            'employee_group': 'A',
            'employee_status': 'Regular',
            'primary_phone': '9935164850'
            }])
        if employee != []:
            employee_cur = employee[0]
        salary_code = 5555

        # employee2, = employee.create([{
        #             'party': 'employee2[1]',
        #             }])

        self.assertRaises(Exception, Employee.write, [employee_cur], {
                'salary_code':  salary_code,
                })

def suite():
    suite = trytond.tests.test_tryton.suite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(HraTestCase))
    suite.addTests(doctest.DocFileSuite(
        'scenario_hra.rst',
        tearDown=doctest_teardown, encoding='utf-8',
        checker=doctest_checker,
        optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
    return suite
======================
Hr_Hra Replace Scenario
======================

Imports::

    >>> from proteus import Model, Wizard
    >>> from trytond.tests.tools import activate_modules
    >>> from trytond.modules.company.tests import create_company, set_company
    
Install hr_hra::

    >>> config = activate_modules('hr_hra')

Create a employee::
    >>> company = create_company()
    >>> employee = Model.get('company.employee')
    >>> party = Model.get('party.party')
    >>> if party1 != [ ]:
            party_cur = party1[0]
    >>> party1.save()
    >>> employee1 = employee(company=company,
                             party=party_cur,
                             employee_group: 'A',
                             employee_status:'Regular',
                             primary_phone: '9935164850')
    >>> employee1.save()

You can not use the unittest method in a scenario. You must use the proteus versions which are defined in modules.company.tests.tools.
This is because scenario are not run inside a Transaction but calls the RPC API but the unittest methods are designed to run under a transaction.

1 Like

A post was split to a new topic: Assert isinstance(value, (Model, type(None))