I am running the test but it will prompt error with the default data get from the context.
It will tell the result is ‘None’. May I know how to write the code so that the test will be passed?
Example of the code:
currency = fields.Many2One('currency.currency', 'Currency')
@classmethod
def default_currency:
pool = Pool()
comp = Transaction().context.get("company")
Company = pool.get("company.company")
company = Company(comp)
if company:
if company.currency:
return company.currency.id
Below is the error when running the test:
ERROR: test_field_methods (trytond.modules.wms.tests.test_wms.WmsTestCase)
Test field methods
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/trytond/model/model.py", line 282, in __getattr__
return self._values[name]
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/trytond/tests/test_tryton.py", line 203, in wrapper
result = func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/trytond/tests/test_tryton.py", line 444, in test_field_methods
getattr(model, attr)()
File "/config/workspace/wms/stock_owner.py", line 25, in default_currency
if company.currency:
File "/usr/local/lib/python3.6/dist-packages/trytond/model/modelstorage.py", line 1413, in __getattr__
return super(ModelStorage, self).__getattr__(name)
File "/usr/local/lib/python3.6/dist-packages/trytond/model/model.py", line 285, in __getattr__
% (self.__name__, name, self._values))
AttributeError: 'company.company' Model has no attribute 'currency': None
If you look at the tests suits of base modules (for example account) you will see examples it’s usage.
Having said that I must admit that is probably too much to add a test just for a default value, so maybe it’s better to create a scenario to test the entire module functionality and ensure that default values are correctly set there.
I think the main problem is that the default method fails if there are no company in the context. So the first thing to do is to make it works without it like this:
@classmethod
def default_currency:
pool = Pool()
Company = pool.get("company.company")
company_id = Transaction().context.get("company")
if company_id is not None:
company = Company(company_id)
if company.currency:
return company.currency.id
Thank you for your feedback. However, the test function you provide is when doing doctest.
However, the error I face is when doin then basic test from trytond itself.