Follow the tryond configuration refactoring

issue3627
review11381002
This commit is contained in:
Cédric Krier committed 2014-09-03 23:32:22 +02:00
1 parent 3844823465
commit e69dfa3a4a
8 files changed
+58 -65

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* New configuration syntax
* Add Report
* Explicitly set value of parent field
* Add duplicate method
+1 -5
View File
@@ -18,11 +18,7 @@ Creating a database
Configuration to connect to a sqlite memory database using trytond as module.
>>> config = config.set_trytond(':memory:', database_type='sqlite')
When connecting to a database that doesn't exist, Proteus will create it.
If no database name was given, then Proteus will generate one. It will choose
':memory': for 'sqlite' type otherwise `'test_%' % int(time.time())`.
>>> config = config.set_trytond('sqlite:///:memory:')
Installing a module
~~~~~~~~~~~~~~~~~~~
+27 -35
View File
@@ -9,7 +9,8 @@ import xmlrpclib
import threading
from decimal import Decimal
import datetime
import time
import os
import urlparse
def dump_decimal(self, value, write):
@@ -169,7 +170,7 @@ class _TrytondMethod(object):
for i in inst]
if not rpc.readonly:
transaction.cursor.commit()
Cache.resets(self._config.database_name)
Cache.resets(self._config.database_name)
return result
@@ -190,40 +191,32 @@ class TrytondProxy(object):
class TrytondConfig(Config):
'Configuration for trytond'
def __init__(self, database_name=None, user='admin', database_type=None,
language='en_US', password='', config_file=None):
def __init__(self, database=None, user='admin', language='en_US',
password='', config_file=None):
super(TrytondConfig, self).__init__()
from trytond.config import CONFIG
CONFIG.update_etc(config_file)
if database_type is not None:
CONFIG['db_type'] = database_type
if not database:
database = os.environ.get('TRYTOND_DATABASE_URI')
else:
os.environ['TRYTOND_DATABASE_URI'] = database
from trytond.config import config
config.update_etc(config_file)
from trytond.pool import Pool
from trytond import backend
from trytond.protocols.dispatcher import create
from trytond.cache import Cache
from trytond.transaction import Transaction
self.database_type = CONFIG['db_type']
if database_name is None:
if self.database_type == 'sqlite':
database_name = ':memory:'
else:
database_name = 'test_%s' % int(time.time())
self.database = database
database_name = None
if database:
uri = urlparse.urlparse(database)
database_name = uri.path.strip('/')
if not database_name:
database_name = os.environ['DB_NAME']
self.database_name = database_name
self._user = user
self.config_file = config_file
Pool.start()
with Transaction().start(None, 0) as transaction:
cursor = transaction.cursor
databases = backend.get('Database').list(cursor)
if database_name not in databases:
create(database_name, CONFIG['admin_passwd'], language, password)
database_list = Pool.database_list()
self.pool = Pool(database_name)
if database_name not in database_list:
self.pool.init()
self.pool.init()
with Transaction().start(self.database_name, 0) as transaction:
Cache.clean(database_name)
@@ -234,14 +227,13 @@ class TrytondConfig(Config):
], limit=1)[0].id
with transaction.set_user(self.user):
self._context = User.get_preferences(context_only=True)
Cache.resets(database_name)
Cache.resets(database_name)
__init__.__doc__ = object.__init__.__doc__
def __repr__(self):
return "proteus.config.TrytondConfig"\
"('%s', '%s', '%s', config_file=%s)"\
% (self.database_name, self._user, self.database_type,
self.config_file)
% (self.database, self._user, self.config_file)
__repr__.__doc__ = object.__repr__.__doc__
def __eq__(self, other):
@@ -249,12 +241,12 @@ class TrytondConfig(Config):
raise NotImplementedError
return (self.database_name == other.database_name
and self._user == other._user
and self.database_type == other.database_type
and self.database == other.database
and self.config_file == other.config_file)
def __hash__(self):
return hash((self.database_name, self._user,
self.database_type, self.config_file))
self.database, self.config_file))
def get_proxy(self, name, type='model'):
'Return Proxy class'
@@ -269,11 +261,11 @@ class TrytondConfig(Config):
return methods
def set_trytond(database_name=None, user='admin', database_type=None,
language='en_US', password='', config_file=None):
def set_trytond(database=None, user='admin', language='en_US', password='',
config_file=None):
'Set trytond package as backend'
_CONFIG.current = TrytondConfig(database_name, user, database_type,
language=language, password=password, config_file=config_file)
_CONFIG.current = TrytondConfig(database, user, language=language,
password=password, config_file=config_file)
return _CONFIG.current
+18
View File
@@ -0,0 +1,18 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import os
from unittest import TestCase
os.environ.setdefault('TRYTOND_DATABASE_URI', 'sqlite://')
os.environ.setdefault('DB_NAME', ':memory:')
from trytond.tests.test_tryton import db_exist, create_db
from proteus import config
class ProteusTestCase(TestCase):
def setUp(self):
if not db_exist():
create_db()
self.config = config.set_trytond()
+3 -6
View File
@@ -1,13 +1,10 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from unittest import TestCase
import proteus.config
from .common import ProteusTestCase
class TestConfig(TestCase):
def setUp(self):
proteus.config.set_trytond(database_type='sqlite')
class TestConfig(ProteusTestCase):
def test_proxy(self):
config = proteus.config.get_config()
@@ -22,7 +19,7 @@ class TestConfig(TestCase):
def test_trytond_config_eq(self):
config1 = proteus.config.get_config()
proteus.config.set_trytond(database_type='sqlite')
proteus.config.set_trytond()
config2 = proteus.config.get_config()
self.assertEqual(config1, config2)
+2 -7
View File
@@ -1,14 +1,9 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of this
# repository contains the full copyright notices and license terms.
from unittest import TestCase
from proteus import config
from .common import ProteusTestCase
class TestContext(TestCase):
def setUp(self):
self.config = config.set_trytond(database_type='sqlite')
class TestContext(ProteusTestCase):
def test_config(self):
prev_ctx = self.config._context
+3 -6
View File
@@ -1,13 +1,10 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from unittest import TestCase
from proteus import config, Model
from proteus import Model
from .common import ProteusTestCase
class TestModel(TestCase):
def setUp(self):
config.set_trytond(database_type='sqlite')
class TestModel(ProteusTestCase):
def test_class_cache(self):
User1 = Model.get('res.user')
+3 -6
View File
@@ -1,13 +1,10 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from unittest import TestCase
from proteus import config, Wizard, Model
from proteus import Wizard, Model
from .common import ProteusTestCase
class TestWizard(TestCase):
def setUp(self):
config.set_trytond(database_type='sqlite')
class TestWizard(ProteusTestCase):
def test_translation_clean(self):
translation_clean = Wizard('ir.translation.clean')