def test_update(odoodb): _install_one(odoodb, "v1") _check_expected(odoodb, "v1") # With --list-only option update shouldn't be performed: _update_list(odoodb, "v2") _check_expected(odoodb, "v1") # With --ignore-addons addon_app, update should not be performed _update_one(odoodb, "v1", ignore_addons="addon_app") _check_expected(odoodb, "v1") # Default update should: _update_one(odoodb, "v2") _check_expected(odoodb, "v2") _update_one(odoodb, "v3") _check_expected(odoodb, "v3") with OdooEnvironment(odoodb) as env: checksums = _load_installed_checksums(env.cr) print(checksums) assert "base" in checksums assert checksums.get("addon_app") == "bb1ff827fd6084e69180557c3183989100ddb62b" assert checksums.get("addon_d1") == "ff46eefbe846e1a46ff3de74e117fd285b72f298" assert checksums.get("addon_d2") == "edf58645e2e55a2d282320206f73df09a746a4ab" _update_one(odoodb, "v4") _check_expected(odoodb, "v4") _update_one(odoodb, "v5") _check_expected(odoodb, "v5") _update_one(odoodb, "v6", ignore_core_addons=True) _check_expected(odoodb, "v6") with OdooEnvironment(odoodb) as env: checksums = _load_installed_checksums(env.cr) assert "base" not in checksums # because ignore_Core_addons=True with pytest.raises(subprocess.CalledProcessError): _update_one(odoodb, "v7") if odoo.tools.parse_version(odoo.release.version) >= odoo.tools.parse_version("12"): # Odoo >= 12 does -u in a transaction _check_expected(odoodb, "v6")
def test_uninstall(odoodb): with OdooEnvironment(database=odoodb) as env: Imm = env["ir.module.module"] assert Imm.search([("name", "=", "base_import"), ("state", "=", "installed")]) result = CliRunner().invoke(main, ["-d", odoodb, "-m", "base_import"]) assert result.exit_code == 0 with OdooEnvironment(database=odoodb) as env: Imm = env["ir.module.module"] assert not Imm.search( [("name", "=", "base_import"), ("state", "=", "installed")] )
def test_env_cache(odoodb): """ test a new environment does not reuse cache """ _cleanup_testparam(odoodb) with OdooEnvironment(database=odoodb) as env: env["ir.config_parameter"].set_param("testparam", "testvalue") value = env["ir.config_parameter"].get_param("testparam") assert value == "testvalue" env.cr.commit() _assert_testparam_present(odoodb, "testvalue") _cleanup_testparam(odoodb) _assert_testparam_absent(odoodb) with OdooEnvironment(database=odoodb) as env: value = env["ir.config_parameter"].get_param("testparam") assert not value
def ir_config_param_test_values(odoodb): with OdooEnvironment(database=odoodb) as env: for key in _get_reset_config_params_key(): env.cr.execute( """ UPDATE ir_config_parameter SET value='test value' WHERE key=%s RETURNING id """, (key, ), ) if not env.cr.fetchall(): # Config parameter doesn't exist: create (ex enterprise params) env.cr.execute( """ INSERT INTO ir_config_parameter (key, value) VALUES (%s, 'test value') """, (key, ), )
def OdooEnvironmentWithUpdate(database, ctx, **kwargs): # Watch for database locks while Odoo updates watcher = None if ctx.params["watcher_max_seconds"] > 0: watcher = DbLockWatcher(database, ctx.params["watcher_max_seconds"]) watcher.start() ignore_addons = set() if ctx.params["ignore_addons"]: ignore_addons.update(ctx.params["ignore_addons"].strip().split(",")) if ctx.params["ignore_core_addons"]: ignore_addons.update(core_addons[odoo.release.series]) if ignore_addons and ctx.params["update_all"]: raise click.ClickException( "--update-all and --ignore(-core)-addons cannot be used together") # Update Odoo datatabase try: _update_db( database, ctx.params["update_all"], ctx.params["i18n_overwrite"], watcher, ctx.params["list_only"], ignore_addons, ) finally: if watcher: watcher.stop() # If we get here, the database has been updated with OdooEnvironment(database) as env: yield env
def _restore_from_folder(dbname, backup, copy=True, jobs=1): manifest_file_path = os.path.join(backup, MANIFEST_FILENAME) dbdump_file_path = os.path.join(backup, DBDUMP_FILENAME) filestore_dir_path = os.path.join(backup, FILESTORE_DIRNAME) if not os.path.exists(manifest_file_path) or not os.path.exists( dbdump_file_path): msg = ("{} is not folder backup created by the backupdb command. " "{} and {} files are missing.".format(backup, MANIFEST_FILENAME, DBDUMP_FILENAME)) raise click.ClickException(msg) odoo.service.db._create_empty_database(dbname) pg_args = [ "--jobs", str(jobs), "--dbname", dbname, "--no-owner", dbdump_file_path ] if odoo.tools.exec_pg_command("pg_restore", *pg_args): raise click.ClickException("Couldn't restore database") if copy: # if it's a copy of a database, force generation of a new dbuuid reset_config_parameters(dbname) with OdooEnvironment(dbname) as env: if os.path.exists(filestore_dir_path): filestore_dest = env["ir.attachment"]._filestore() shutil.move(filestore_dir_path, filestore_dest) if odoo.tools.config["unaccent"]: try: with env.cr.savepoint(): env.cr.execute("CREATE EXTENSION unaccent") except psycopg2.Error: pass odoo.sql_db.close_db(dbname)
def test_update(odoodb): _install_one(odoodb, "v1") _check_expected(odoodb, "v1") _update_one(odoodb, "v2") _check_expected(odoodb, "v2") _update_one(odoodb, "v3") _check_expected(odoodb, "v3") with OdooEnvironment(odoodb) as env: checksums = _load_installed_checksums(env.cr) print(checksums) assert checksums.get( "addon_app") == "bb1ff827fd6084e69180557c3183989100ddb62b" assert checksums.get( "addon_d1") == "ff46eefbe846e1a46ff3de74e117fd285b72f298" assert checksums.get( "addon_d2") == "edf58645e2e55a2d282320206f73df09a746a4ab" _update_one(odoodb, "v4") _check_expected(odoodb, "v4") _update_one(odoodb, "v5") _check_expected(odoodb, "v5") _update_one(odoodb, "v6") _check_expected(odoodb, "v6") with pytest.raises(subprocess.CalledProcessError): _update_one(odoodb, "v7") if odoo.tools.parse_version( odoo.release.version) >= odoo.tools.parse_version("12"): # Odoo >= 12 does -u in a transaction _check_expected(odoodb, "v6")
def OdooEnvironmentWithUpdate(database, ctx, **kwargs): conn = odoo.sql_db.db_connect(database) to_update = odoo.tools.config["update"] if ctx.params["update_all"]: to_update["base"] = 1 else: with conn.cursor() as cr: checksums = _load_installed_checksums(cr) cr.execute( "SELECT name FROM ir_module_module WHERE state = 'installed'") for (module_name, ) in cr.fetchall(): if _get_checksum_dir( cr, module_name) != checksums.get(module_name): to_update[module_name] = 1 if to_update: _logger.info( "Updating addons for their hash changed: %s.", ",".join(to_update.keys()), ) if ctx.params["i18n_overwrite"]: odoo.tools.config["overwrite_existing_translations"] = True if odoo.release.version_info[0] < 10: Registry = odoo.modules.registry.RegistryManager else: Registry = odoo.modules.registry.Registry Registry.new(database, update_module=True) with conn.cursor() as cr: _save_installed_checksums(cr) with OdooEnvironment(database) as env: yield env
def reset_config_parameters(dbname): """ Reset config parameters to default value. This is useful to avoid conflicts between databases on copy or restore (dbuuid, ...) """ with OdooEnvironment(dbname) as env: if odoo.release.version_info < (10, 0): env.registry("ir.config_parameter").init(env.cr, force=True) else: env["ir.config_parameter"].init(force=True) # reset enterprise keys if exists env.cr.execute(""" DELETE FROM ir_config_parameter WHERE key = 'database.enterprise_code'; UPDATE ir_config_parameter SET value = 'copy' WHERE key = 'database.expiration_reason' AND value != 'demo'; UPDATE ir_config_parameter SET value = CURRENT_DATE + INTERVAL '2 month' WHERE key = 'database.expiration_date'; """)
def test_uninstall(odoodb): with OdooEnvironment(database=odoodb) as env: Imm = env['ir.module.module'] assert Imm.search([ ('name', '=', 'base_import'), ('state', '=', 'installed'), ]) result = CliRunner().invoke(main, [ '-d', odoodb, '-m', 'base_import', ]) assert result.exit_code == 0 with OdooEnvironment(database=odoodb) as env: Imm = env['ir.module.module'] assert not Imm.search([ ('name', '=', 'base_import'), ('state', '=', 'installed'), ])
def _assert_ir_config_reset(db1, db2): params_by_db = defaultdict(dict) for db in (db1, db2): with OdooEnvironment(database=db) as env: IrConfigParameters = env["ir.config_parameter"] for key in _get_reset_config_params_key(): params_by_db[db][key] = IrConfigParameters.get_param(key) params1 = params_by_db[db1] params2 = params_by_db[db2] assert set(params1.keys()) == set(params2.keys()) for k, v in params1.items(): assert v != params2[k]
def _check_expected(odoodb, v): with OdooEnvironment(database=odoodb) as env: with open(os.path.join(_addons_dir(v), "expected.json")) as f: expected = json.load(f) for addon_name, expected_data in expected.items(): env.cr.execute( "SELECT state, latest_version FROM ir_module_module WHERE name=%s", (addon_name, ), ) state, version = env.cr.fetchone() expected_state = expected_data.get("state") if expected_state: assert state == expected_state expected_version = expected_data.get("version") if expected_version: assert version.split(".")[2:] == expected_version.split(".")
def OdooEnvironmentWithUpdate(database, ctx, **kwargs): # Watch for database locks while Odoo updates watcher = None if ctx.params["watcher_max_seconds"] > 0: watcher = DbLockWatcher(database, ctx.params["watcher_max_seconds"]) watcher.start() # Update Odoo datatabase try: _update_db( database, ctx.params["update_all"], ctx.params["i18n_overwrite"], watcher ) finally: if watcher: watcher.stop() # If we get here, the database has been updated with OdooEnvironment(database) as env: yield env
def test_odoo_env(odoodb): with OdooEnvironment(database=odoodb) as env: admin = env["res.users"].search([("login", "=", "admin")]) assert len(admin) == 1