[IMP] move storage of contexts to OpenERPSession.context_store, fix a number of tests it broke including one it revealed was completely broken
test_retrieve_second_domain passing relied on domains and contexts being stored in the same place, as it created a domain from a context key and tried to check if that worked. It did... bzr revid: xmo@openerp.com-20110329082002-8rnvnp9cog9gr7zd
This commit is contained in:
@@ -57,6 +57,7 @@ class Domain(object):
|
||||
"and a domain string")
|
||||
|
||||
self.session = session
|
||||
self.own = {}
|
||||
if domain_string:
|
||||
self.key = binascii.hexlify(
|
||||
hashlib.sha256(domain_string).digest()[:SHORT_HASH_BYTES_SIZE])
|
||||
@@ -79,8 +80,10 @@ class Domain(object):
|
||||
context (as well as the session's base context), and returns the
|
||||
evaluated result.
|
||||
"""
|
||||
return eval(self.get_domain_string(),
|
||||
self.session.evaluation_context(context))
|
||||
ctx = self.session.evaluation_context(context)
|
||||
if self.own:
|
||||
ctx.update(self.own)
|
||||
return eval(self.get_domain_string(), ctx)
|
||||
|
||||
class Context(object):
|
||||
def __init__(self, session, context_string=None, key=None):
|
||||
@@ -103,10 +106,11 @@ class Context(object):
|
||||
"and a domain string")
|
||||
|
||||
self.session = session
|
||||
self.own = {}
|
||||
if context_string:
|
||||
self.key = binascii.hexlify(
|
||||
hashlib.sha256(context_string).digest()[:SHORT_HASH_BYTES_SIZE])
|
||||
self.session.domains_store[self.key] = context_string
|
||||
self.session.contexts_store[self.key] = context_string
|
||||
elif key:
|
||||
self.key = key
|
||||
|
||||
@@ -118,12 +122,15 @@ class Context(object):
|
||||
the first place.
|
||||
:type session: openerpweb.openerpweb.OpenERPSession
|
||||
"""
|
||||
return self.session.domains_store[self.key]
|
||||
return self.session.contexts_store[self.key]
|
||||
|
||||
def evaluate(self, context=None):
|
||||
""" Forces the evaluation of the linked context, using the provided
|
||||
context (as well as the session's base context), and returns the
|
||||
evaluated result.
|
||||
"""
|
||||
ctx = self.session.evaluation_context(context)
|
||||
if self.own:
|
||||
ctx.update(self.own)
|
||||
return eval(self.get_context_string(),
|
||||
self.session.evaluation_context(context))
|
||||
ctx)
|
||||
|
||||
@@ -70,6 +70,7 @@ class OpenERPSession(object):
|
||||
self.model_factory = model_factory
|
||||
|
||||
self.context = {}
|
||||
self.contexts_store = {}
|
||||
self.domains_store = {}
|
||||
|
||||
def proxy(self, service):
|
||||
|
||||
@@ -44,15 +44,23 @@ class NonLiteralDomainTest(unittest2.TestCase):
|
||||
self.assertEqual(
|
||||
result, [('a', '=', 3)])
|
||||
|
||||
def test_own_values(self):
|
||||
self.session.evaluation_context.return_value = {}
|
||||
domain = Domain(self.session, "[('a', '=', self)]")
|
||||
domain.own = {'self': 3}
|
||||
result = domain.evaluate()
|
||||
self.assertEqual(
|
||||
result, [('a', '=', 3)])
|
||||
|
||||
class NonLiteralContextTest(unittest2.TestCase):
|
||||
def setUp(self):
|
||||
self.session = mock.Mock(spec=openerpweb.openerpweb.OpenERPSession)
|
||||
self.session.domains_store = {}
|
||||
self.session.contexts_store = {}
|
||||
def test_store_domain(self):
|
||||
c = Context(self.session, "some arbitrary string")
|
||||
|
||||
self.assertEqual(
|
||||
self.session.domains_store[c.key],
|
||||
self.session.contexts_store[c.key],
|
||||
"some arbitrary string")
|
||||
|
||||
def test_get_domain_back(self):
|
||||
@@ -68,7 +76,7 @@ class NonLiteralContextTest(unittest2.TestCase):
|
||||
key = Context(self.session, "some arbitrary string").key
|
||||
|
||||
self.assertEqual(
|
||||
Domain(self.session, key=key).get_domain_string(),
|
||||
Context(self.session, key=key).get_context_string(),
|
||||
"some arbitrary string")
|
||||
|
||||
def test_key_and_string(self):
|
||||
@@ -82,10 +90,19 @@ class NonLiteralContextTest(unittest2.TestCase):
|
||||
self.assertEqual(
|
||||
result, [('a', '=', 3)])
|
||||
|
||||
def test_own_values(self):
|
||||
self.session.evaluation_context.return_value = {}
|
||||
context = Context(self.session, "{'a': self}")
|
||||
context.own = {'self': 3}
|
||||
result = context.evaluate()
|
||||
self.assertEqual(
|
||||
result, {'a': 3})
|
||||
|
||||
class NonLiteralJSON(unittest2.TestCase):
|
||||
def setUp(self):
|
||||
self.session = mock.Mock(spec=openerpweb.openerpweb.OpenERPSession)
|
||||
self.session.domains_store = {}
|
||||
self.session.contexts_store = {}
|
||||
|
||||
def test_encode_domain(self):
|
||||
d = Domain(self.session, "some arbitrary string")
|
||||
|
||||
Reference in New Issue
Block a user