def test_frozendict_hash(self): """ Ensure that a frozendict is hashable. """ # dict with simple values hash(frozendict({'name': 'Joe', 'age': 42})) # dict with tuples, lists, and embedded dicts hash(frozendict({ 'user_id': (42, 'Joe'), 'line_ids': [(0, 0, {'values': [42]})], }))
def test_frozendict_immutable(self): """ Ensure that a frozendict is immutable. """ vals = {'name': 'Joe', 'age': 42} frozen_vals = frozendict(vals) # check __setitem__, __delitem__ with self.assertRaises(Exception): frozen_vals['surname'] = 'Jack' with self.assertRaises(Exception): frozen_vals['name'] = 'Jack' with self.assertRaises(Exception): del frozen_vals['name'] # check update, setdefault, pop, popitem, clear with self.assertRaises(Exception): frozen_vals.update({'surname': 'Jack'}) with self.assertRaises(Exception): frozen_vals.update({'name': 'Jack'}) with self.assertRaises(Exception): frozen_vals.setdefault('surname', 'Jack') with self.assertRaises(Exception): frozen_vals.pop('surname', 'Jack') with self.assertRaises(Exception): frozen_vals.pop('name', 'Jack') with self.assertRaises(Exception): frozen_vals.popitem() with self.assertRaises(Exception): frozen_vals.clear()
def _set_images(self): for fname in self._get_images_for_test(): fname_split = fname.split('.') if not fname_split[0] in _file_cache: with Image.open(os.path.join(dir_path, fname), 'r') as img: base64_img = image_to_base64(img, 'JPEG') primary, secondary = self.env[ 'base.document.layout'].create( {})._parse_logo_colors(base64_img) _img = frozendict({ 'img': base64_img, 'colors': { 'primary_color': primary, 'secondary_color': secondary, }, }) _file_cache[fname_split[0]] = _img self.company_imgs = frozendict(_file_cache)
def __new__(cls, cr, uid, context, su=False): if uid == SUPERUSER_ID: su = True assert context is not None args = (cr, uid, context, su) # if env already exists, return it env, envs = None, cls.envs for env in envs: if env.args == args: return env # otherwise create environment, and add it in the set self = object.__new__(cls) args = (cr, uid, frozendict(context), su) self.cr, self.uid, self.context, self.su = self.args = args self.registry = Registry(cr.dbname) self.cache = envs.cache self._protected = envs.protected # proxy to shared data structure self.all = envs envs.add(self) return self
def __new__(cls, cr, uid, context): assert context is not None args = (cr, uid, context) # if env already exists, return it env, envs = None, cls.envs for env in envs: if env.args == args: return env # otherwise create environment, and add it in the set self = object.__new__(cls) self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context)) self.registry = Registry(cr.dbname) self.cache = envs.cache self._cache_key = (cr, uid) self._protected = StackMap() # {field: ids, ...} self.dirty = defaultdict(set) # {record: set(field_name), ...} self.all = envs envs.add(self) return self