<commit_before># -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
import tempfile

import mock

from openerp.modules import get_module_path
from openerp.tests.common import TransactionCase

from .. import post_init_hook

_logger = logging.getLogger(__name__)
try:
    from checksumdir import dirhash
except ImportError:
    _logger.debug('Cannot `import checksumdir`.')

model = 'openerp.addons.module_auto_update.models.module'


class TestModule(TransactionCase):

    def setUp(self):
        super(TestModule, self).setUp()
        module_name = 'module_auto_update'
        self.own_module = self.env['ir.module.module'].search([
            ('name', '=', module_name),
        ])
        self.own_dir_path = get_module_path(module_name)
        self.own_checksum = dirhash(
            self.own_dir_path,
            'sha1',
            excluded_extensions=['pyc', 'pyo'],
        )

    @mock.patch('%s.get_module_path' % model)
    def create_test_module(self, vals, get_module_path_mock):
        get_module_path_mock.return_value = self.own_dir_path
        test_module = self.env['ir.module.module'].create(vals)
        return test_module

    def test_compute_checksum_dir(self):
        """It should compute the directory's SHA-1 hash"""
        self.assertEqual(
            self.own_module.checksum_dir, self.own_checksum,
            'Module directory checksum not computed properly',
        )

    def test_compute_checksum_dir_ignore_excluded(self):
        """It should exclude .pyc/.pyo extensions from checksum
        calculations"""
        with tempfile.NamedTemporaryFile(
                suffix='.pyc', dir=self.own_dir_path):
            self.assertEqual(
                self.own_module.checksum_dir, self.own_checksum,
                'SHA1 checksum does not ignore excluded extensions',
            )

    def test_compute_checksum_dir_recomputes_when_file_added(self):
        """It should return a different value when a non-.pyc/.pyo file is
        added to the module directory"""
        with tempfile.NamedTemporaryFile(
                suffix='.py', dir=self.own_dir_path):
            self.assertNotEqual(
                self.own_module.checksum_dir, self.own_checksum,
                'SHA1 checksum not recomputed',
            )

    def test_store_checksum_installed_state_installed(self):
        """It should set the module's checksum_installed equal to
        checksum_dir when vals contain state 'installed'"""
        self.own_module.checksum_installed = 'test'
        self.own_module._store_checksum_installed({'state': 'installed'})
        self.assertEqual(
            self.own_module.checksum_installed, self.own_module.checksum_dir,
            'Setting state to installed does not store checksum_dir '<commit_msg>[FIX][module_auto_update] Pass tests if addon is in readonly directory<commit_after># -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
import os
import tempfile

import mock

from openerp.modules import get_module_path
from openerp.tests.common import TransactionCase

from .. import post_init_hook

_logger = logging.getLogger(__name__)
try:
    from checksumdir import dirhash
except ImportError:
    _logger.debug('Cannot `import checksumdir`.')

model = 'openerp.addons.module_auto_update.models.module'


class TestModule(TransactionCase):

    def setUp(self):
        super(TestModule, self).setUp()
        module_name = 'module_auto_update'
        self.own_module = self.env['ir.module.module'].search([
            ('name', '=', module_name),
        ])
        self.own_dir_path = get_module_path(module_name)
        self.own_checksum = dirhash(
            self.own_dir_path,
            'sha1',
            excluded_extensions=['pyc', 'pyo'],
        )
        self.own_writeable = os.access(self.own_dir_path, os.W_OK)

    @mock.patch('%s.get_module_path' % model)
    def create_test_module(self, vals, get_module_path_mock):
        get_module_path_mock.return_value = self.own_dir_path
        test_module = self.env['ir.module.module'].create(vals)
        return test_module

    def test_compute_checksum_dir(self):
        """It should compute the directory's SHA-1 hash"""
        self.assertEqual(
            self.own_module.checksum_dir, self.own_checksum,
            'Module directory checksum not computed properly',
        )

    def test_compute_checksum_dir_ignore_excluded(self):
        """It should exclude .pyc/.pyo extensions from checksum
        calculations"""
        if not self.own_writeable:
            self.skipTest("Own directory not writeable")
        with tempfile.NamedTemporaryFile(
                suffix='.pyc', dir=self.own_dir_path):
            self.assertEqual(
                self.own_module.checksum_dir, self.own_checksum,
                'SHA1 checksum does not ignore excluded extensions',
            )

    def test_compute_checksum_dir_recomputes_when_file_added(self):
        """It should return a different value when a non-.pyc/.pyo file is
        added to the module directory"""
        if not self.own_writeable:
            self.skipTest("Own directory not writeable")
        with tempfile.NamedTemporaryFile(
                suffix='.py', dir=self.own_dir_path):
            self.assertNotEqual(
                self.own_module.checksum_dir, self.own_checksum,
                'SHA1 checksum not recomputed',
            )

    def test_store_checksum_installed_state_installed(self):
        """It should set the module's checksum_installed equal to
        checksum_dir when vals contain state 'installed'"""
        self.own_module.checksum_installed = 'test'
        self.own_module._store_checksum_installed({'state': 'installed'})
        self.assertEqual(
            self.own_module.checksum_installed, self.own_module.checksum_dir,
            'Setting state to installed does not store checksum_dir '
{'fraction_non_alphanumeric': 0.09419568822553898, 'fraction_numerical': 0.003648424543946932, 'mean_word_length': 2.762320648783531, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549838', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1747, 'n_words': 381}