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