1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
# -*- coding: utf-8 -*-
import zipfile
from datetime import datetime
import odoorpc
from odoorpc.tests import BaseTestCase
class TestDB(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self.odoo.logout()
self.databases = [] # Keep databases created during tests
def test_db_dump(self):
dump = self.odoo.db.dump(self.env['super_pwd'], self.env['db'])
self.assertIn('dump.sql', zipfile.ZipFile(dump).namelist())
def test_db_dump_wrong_database(self):
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.dump,
self.env['super_pwd'],
'wrong_db',
)
def test_db_dump_wrong_password(self):
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.dump,
'wrong_password',
self.env['db'],
)
def test_db_create(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.create(self.env['super_pwd'], new_database)
def test_db_create_existing_database(self):
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.create,
self.env['super_pwd'],
self.env['db'],
)
def test_db_create_wrong_password(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.create,
'wrong_password',
new_database,
)
def test_db_drop(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.duplicate(
self.env['super_pwd'], self.env['db'], new_database
)
res = self.odoo.db.drop(self.env['super_pwd'], new_database)
self.assertTrue(res)
def test_db_drop_wrong_database(self):
res = self.odoo.db.drop(self.env['super_pwd'], 'wrong_database')
self.assertFalse(res)
def test_db_drop_wrong_password(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.duplicate(
self.env['super_pwd'], self.env['db'], new_database
)
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.drop,
'wrong_password',
new_database,
)
def test_db_duplicate(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.duplicate(
self.env['super_pwd'], self.env['db'], new_database
)
def test_db_duplicate_wrong_database(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.duplicate,
self.env['super_pwd'],
'wrong_database',
new_database,
)
def test_db_duplicate_wrong_password(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.duplicate,
'wrong_password',
self.env['db'],
new_database,
)
def test_db_list(self):
res = self.odoo.db.list()
self.assertIsInstance(res, list)
self.assertIn(self.env['db'], res)
def test_db_restore_new_database(self):
dump = self.odoo.db.dump(self.env['super_pwd'], self.env['db'])
date = datetime.strftime(datetime.today(), '%Y-%m-%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.restore(self.env['super_pwd'], new_database, dump)
def test_db_restore_existing_database(self):
dump = self.odoo.db.dump(self.env['super_pwd'], self.env['db'])
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.restore,
self.env['super_pwd'],
self.env['db'],
dump,
)
def test_db_restore_wrong_password(self):
dump = self.odoo.db.dump(self.env['super_pwd'], self.env['db'])
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.assertRaises(
odoorpc.error.RPCError,
self.odoo.db.restore,
'wrong_password',
new_database,
dump,
)
def test_db_drop_with_session_opened(self):
date = datetime.strftime(datetime.today(), '%Y%m%d_%Hh%Mm%S')
new_database = "{}_{}".format(self.env['db'], date)
self.databases.append(new_database)
self.odoo.db.duplicate(
self.env['super_pwd'], self.env['db'], new_database
)
self.odoo.login(new_database, self.env['user'], self.env['pwd'])
res = self.odoo.db.drop(self.env['super_pwd'], new_database)
self.assertTrue(res)
self.assertNotIn(new_database, self.odoo.db.list())
self.odoo.logout()
def tearDown(self):
"""Clean up databases created during tests."""
for db in self.databases:
try:
self.odoo.db.drop(self.env['super_pwd'], db)
except: # noqa: E722
pass
|