# AttributeError is raised when running custom module test suite

**URL:** https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877
**Category:** Developer
**Created:** [March 2, 2021, 8:13am UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877 "2021-03-02T08:13:31Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)
#### Post date: [March 2, 2021, 8:13am UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/1 "2021-03-02T08:13:31Z")

</div>

Hi,

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:

```auto
    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:

```auto
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

```

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [March 2, 2021, 11:00am UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/2 "2021-03-02T11:00:42Z")

</div>

Probably the problem is that you do not have the company value set on the context when running the test.

Then this line:

> [@bala4901](#):
>
> ```auto
> comp = Transaction().context.get("company")
> 
> ```

Sets the value of `comp` variable to `None`. This value is latter used to load the company record:

> [@bala4901](#):
>
> ```
> Company = pool.get("company.company")
> company = Company(comp)
> 
> ```
> 
> ```auto
> 
> ```

But as `comp` is None tryton will create a new record instead of reading it from the database. The new record makes this line fail:

> [@bala4901](#):
>
> ` if company.currency:`

because a newly created record does not have any currency set (your code does not set the any value) so when accessing them it crashes.

You should probably only load the company value when there is a value set on the context.

P.S: I updated your title to better describe the problem of your topic.

---

<div class="post-metadata">

### Author: ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)
#### Post date: [March 2, 2021, 6:01pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/3 "2021-03-02T18:01:26Z")

</div>

Hi @pokoli,

Thank you for your explaination.  
May I know how to set the context for testing?  
or set the user for the testing?

Because I would like to get the user’s company to load the default currency.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [March 3, 2021, 7:34am UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/4 "2021-03-03T07:34:57Z")

</div>

> [@bala4901](#):
>
> May I know how to set the context for testing?  
> or set the user for the testing?

The tests of the company include some functions to do so:

- The [create\_company tool](http://hg.tryton.org/modules/company/file/c4a453db686a/tests/tools.py) reloads the context which sets the company value. This should be used on scenarios.
- The [set company function](http://hg.tryton.org/modules/company/file/c4a453db686a/tests/test_company.py#l49) is a context manager that can be used on unittests.

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.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 3, 2021, 7:41am UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/5 "2021-03-03T07:41:20Z")

</div>

> [@pokoli](#):
>
> 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:

```auto
    @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

```

---

<div class="post-metadata">

### Author: ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)
#### Post date: [March 3, 2021, 1:41pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/6 "2021-03-03T13:41:28Z")

</div>

Hi @pokoli,

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.

Anyway, thank you

---

<div class="post-metadata">

### Author: ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)
#### Post date: [March 3, 2021, 1:42pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/7 "2021-03-03T13:42:25Z")

</div>

Thank @ced,  
check for None is working.

---

<div class="post-metadata">

### Author: ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)
#### Post date: [March 3, 2021, 1:50pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/8 "2021-03-03T13:50:59Z")

</div>

> [@ced](#):
>
> ```auto
> @classmethod
> def default_currency:
> pool = Pool()
> Company = pool.get("company.company")
> company_id = Transaction().context.get("company")
> if company_id is not None:
> 
> ```

Why explicit test for None? Isn’t it better to test for

```auto
          if company_id:

```

Otherwise if `company_id` is `False` or `0`, the following code would fail, too.

> [@ced](#):
>
> ```auto
> 
> company = Company(company_id)
> if company.currency:
> return company.currency.id
> 
> ```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 3, 2021, 1:57pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/9 "2021-03-03T13:57:53Z")

</div>

> [@udono](#):
>
> Why explicit test for None?

Because real ID could be 0.

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [April 2, 2021, 1:57pm UTC](https://discuss.tryton.org/t/attributeerror-is-raised-when-running-custom-module-test-suite/3877/10 "2021-04-02T13:57:56Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
