Skip to content

Commit

Permalink
Improve coverage and fix bug in tests
Browse files Browse the repository at this point in the history
Bugfixes:

Odoo tests split to prevent improperly reloading odoo.. The bug
is caused by Odoo:
odoo/odoo@d0ee861

The odoo code loaded by release is older while the version loaded with
git would be newer. For some reasons, odoo wouldn't properly reload and
tests would fail by running tests using addons of a newer version of
odoo with an older core of odoo without the proper imports of
dispatch_rpc
  • Loading branch information
llacroix committed Nov 16, 2022
1 parent 055d287 commit e6408e9
Show file tree
Hide file tree
Showing 20 changed files with 539 additions and 206 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ jobs:
PGPASSWORD: postgres
PGPORT: 5432
run: |
TEST_ODOO=${{ matrix.odoo-version }} pytest --cov=./ --cov-config=./setup.cfg -v
pytest --cov=./ --cov-config=./setup.cfg -v
TEST_ODOO=${{ matrix.odoo-version }} pytest --cov=./ --cov-config=./setup.cfg --cov-append -v tests/test_odoo_git.py
TEST_ODOO=${{ matrix.odoo-version }} pytest --cov=./ --cov-config=./setup.cfg --cov-append -v tests/test_odoo_release.py
codecov
- name: Install Odoo
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
odoo_env,
change_test_dir,
ignore_warnings,
runner
)
23 changes: 15 additions & 8 deletions odoo_tools/api/management.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import distro
import six
import logging
import psycopg2
Expand Down Expand Up @@ -318,10 +319,7 @@ def install_odoo(
.. _NIGHTLY: https://nightly.odoo.com/
"""
if hasattr(opts, 'cache'):
cache = opts.cache
else:
cache = None
cache = opts.cache

if release:
installer = OfficialRelease(
Expand Down Expand Up @@ -403,7 +401,7 @@ def packages(self):

return package_list

def native_packages(self):
def native_packages(self, distrib_override=None):
python_packages = [
"python3-pip",
"python3-psycopg2",
Expand All @@ -423,10 +421,19 @@ def native_packages(self):
"curl",
]

distrib = {
"default": base_packages + python_packages,
distrib_map = {
"default": base_packages,
"ubuntu": base_packages + python_packages,
"fedora": base_packages + python_packages,
"debian": base_packages + python_packages
}

return distrib['ubuntu']
if distrib_override is not None:
distrib_id = distro.id()
else:
distrib_id = distrib_override

if distrib_id in distrib_map:
return distrib_map[distrib_id]
else:
return distrib_map['default']
14 changes: 9 additions & 5 deletions odoo_tools/api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,16 @@ def files(self):
considered as "sources". It will also yield files located in
the static folder.
"""
for file in self.path.glob('**/*'):
if file.is_dir():
continue
if file.name.endswith('.pyc'):
continue
all_files = [
file
for file in self.path.glob('**/*')
if not file.is_dir()
if not file.name.endswith('.pyc')
]

all_files.sort()

for file in all_files:
yield file

def package(self):
Expand Down
5 changes: 3 additions & 2 deletions odoo_tools/api/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def package(
service,
output_path,
fetch_path=None,
decrypt_key=None
decrypt_key=None,
temp_dir_manager=TemporaryDirectory
):
with TemporaryDirectory() as tempdir:
with temp_dir_manager() as tempdir:
target = Path(tempdir)

self.checkout(
Expand Down
5 changes: 5 additions & 0 deletions odoo_tools/cli/click/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
@click.group()
def gen():
pass


@gen.command()
def info():
print("Install more modules like odoo-tools-rest")
17 changes: 4 additions & 13 deletions odoo_tools/cli/click/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,10 @@ def setup(

opts = DictObject()

if not languages:
opts.languages = "all"
else:
opts.languages = languages

if upgrade:
opts.upgrade = True

if target:
opts.target = Path.cwd() / target

if cache:
opts.cache = Path.cwd() / cache
opts.languages = languages if languages else "all"
opts.upgrade = True if upgrade else False
opts.target = Path.cwd() / target if target else None
opts.cache = Path.cwd() / cache if cache else None

env.manage.install_odoo(
version,
Expand Down
2 changes: 1 addition & 1 deletion odoo_tools/cli/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def register(self, name, command):

def register_commands(self):
for ep in iter_entry_points(group='odootools.command'):
registry.register(ep.name, ep.load())
self.register(ep.name, ep.load())

for ep in iter_entry_points(group='odootools.command.ext'):
command = self.groups[ep.name]
Expand Down
60 changes: 3 additions & 57 deletions odoo_tools/configuration/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,14 @@ def install(self):
self.installed_release
)

if hasattr(self.options, 'languages'):
languages = self.options.languages
else:
languages = 'all'
languages = self.options.languages

self.strip_languages(languages)

_logger.info("Installing odoo")

target = (
self.options.target
if hasattr(self.options, 'target') and self.options.target
else None
)

upgrade = (
self.options.upgrade
if hasattr(self.options, 'upgrade') and self.options.upgrade
else False
)
target = self.options.target
upgrade = self.options.upgrade

args = pip_command(target=target, upgrade=upgrade)

Expand Down Expand Up @@ -282,45 +270,3 @@ def installed_release(self):

def install(self):
super().install()
#
#
# def setup_odoo_from_git(repo, ref, version, options=None):
#
# if hasattr(options, 'cache'):
# cache = options.cache
# else:
# cache = None
#
# installer = GitRelease(
# version,
# repo,
# ref,
# options=options,
# cache=cache
# )
#
# with installer:
# if installer.need_update():
# installer.fetch()
# installer.checkout()
# installer.install()
#
#
# def setup_odoo_release(version, release, options=None):
# if hasattr(options, 'cache'):
# cache = options.cache
# else:
# cache = None
#
# installer = OfficialRelease(
# version,
# release,
# options=options,
# cache=cache
# )
#
# with installer:
# if installer.need_update():
# installer.fetch()
# installer.checkout()
# installer.install()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"packaging",
"lxml",
"docutils",
"distro",
"polib",
"six>=1.12.0",
"cryptography",
Expand Down
31 changes: 17 additions & 14 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile
import sys
import pytest
from click.testing import CliRunner
import subprocess
from pathlib import Path
from mock import MagicMock
Expand All @@ -10,6 +11,8 @@
ignore_odoo_warnings,
ignore_default_warnings
)
from odoo_tools.patch import unload_modules
from odoo_tools.exceptions import OdooNotInstalled

from tests.utils import generate_addons

Expand Down Expand Up @@ -44,22 +47,17 @@ def odoo_cleanup(env):
print("tearing down")
odoo_path = env.path()

# Remove odoo config
if Path(env.context.odoo_rc).exists():
Path(env.context.odoo_rc).unlink()

# Remove odoo
subprocess.run(['pip', 'uninstall', '-y', 'odoo'])

odoo_modules = [
key
for key in sys.modules.keys()
if key.startswith('odoo.')
]

for mod in odoo_modules:
del sys.modules[mod]

subprocess.run(['rm', '-rf', str(odoo_path)])

# Unload odoo
unload_modules("odoo")


@pytest.fixture(scope="module")
def odoo_env():
Expand All @@ -69,8 +67,8 @@ def odoo_env():

options.languages = "fr_CA"
options.upgrade = False
options.target = False
options.cache = False
options.target = None
options.cache = None

odoo_version = "{}.0".format(os.environ['TEST_ODOO'])

Expand All @@ -89,8 +87,8 @@ def odoo_release():

options.languages = "fr_CA"
options.upgrade = False
options.target = False
options.cache = False
options.target = None
options.cache = None

odoo_version = "{}.0".format(os.environ['TEST_ODOO'])

Expand All @@ -114,3 +112,8 @@ def change_test_dir(request, monkeypatch):
def ignore_warnings(request):
ignore_odoo_warnings()
ignore_default_warnings()


@pytest.fixture
def runner():
return CliRunner()
31 changes: 31 additions & 0 deletions tests/test_api_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import pytest
from pathlib import Path
from mock import patch
from odoo_tools.api.context import Context


def test_context_default_odoorc(tmp_path):
new_env = {
"HOME": str(tmp_path)
}

with patch.dict(os.environ, new_env, clear=True):
ctx = Context()
assert ctx.default_odoorc() == Path.cwd() / 'odoo.cfg'

new_env = {
"HOME": str(tmp_path),
}

with patch.dict(os.environ, new_env, clear=True):
odoo_rc = tmp_path / '.odoorc'
with odoo_rc.open('w') as fout:
fout.write('')

ctx = Context()
assert ctx.default_odoorc() == odoo_rc

ctx = Context()
assert ctx.default_odoorc() == Path.cwd() / 'odoo.cfg'
assert ctx.odoo_rc == Path.cwd() / 'odoo.cfg'
Loading

0 comments on commit e6408e9

Please sign in to comment.