def test_set(self): with self.assertRaises(URI.Invalid): cio.set('page/title', 'fail') with self.assertRaises(URI.Invalid): cio.set('page/title.txt', 'fail') node = cio.set('i18n://sv-se@label/email.md', u'e-post') self.assertEqual(node.uri, 'i18n://sv-se@label/email.md#1') cache.clear() node = cio.get('label/email', u'fallback') self.assertEqual(node.content, u'<p>e-post</p>') self.assertEqual(node.uri, 'i18n://sv-se@label/email.md#1') self.assertEqual(node.initial, u'fallback') self.assertEqual(len(node.meta.keys()), 0) # No meta returned from non-versioned api get self.assertEqual(repr(node._node), '<Node: i18n://sv-se@label/email.md#1>') self.assertEqual(node.for_json(), { 'uri': node.uri, 'content': node.content, 'meta': node.meta }) node = cio.set('sv-se@label/email', u'e-post', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#draft') self.assertKeys(node.meta, 'modified_at', 'is_published') node = cio.publish(node.uri) self.assertKeys(node.meta, 'modified_at', 'published_at', 'is_published') self.assertTrue(node.meta['is_published']) node = cio.get('label/email') self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#2') self.assertEqual(node.content, u'e-post') self.assertEqual(node.uri.ext, 'txt') self.assertEqual(len(node.meta.keys()), 0)
def test_uri_redirect(self): cio.set('i18n://sv-se@page/title.txt', u'Title') node = cio.get('i18n://sv-se@page/title', u'Default') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') self.assertEqual(node.content, u'Title') node = cio.get('i18n://sv-se@page/title.up', u'Default Upper', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up') self.assertEqual( node.content, u'DEFAULT UPPER' ) # Cache still contains 'Title', but plugin diff and skipped cached_node = cache.get(node.uri) self.assertDictEqual(cached_node, { 'uri': node.uri, 'content': u'DEFAULT UPPER' }) cache.clear() node = cio.get('i18n://sv-se@page/title.up', u'Default-Upper', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up') self.assertEqual( node.content, u'DEFAULT-UPPER' ) # Cache cleared, storage plugin mismatch, default fallback
def test_cached_node(self): with self.assertRaises(NodeDoesNotExist): storage.get(self.uri) content = cache.get(self.uri) self.assertIsNone(content) node, _ = storage.set(self.uri + '#draft', u'e-post') storage.publish(node['uri']) with self.assertCache(calls=2, misses=1, sets=1): node = cio.get('i18n://label/email', lazy=False) cached_node = cache.get('i18n://sv-se@label/email') self.assertIsInstance(cached_node, dict) self.assertKeys(cached_node, 'uri', 'content') _uri, content = cached_node['uri'], cached_node['content'] self.assertEqual(_uri, 'i18n://sv-se@label/email.txt#1') self.assertTrue(content == node.content == u'e-post') with self.assertCache(calls=1, misses=0, hits=1): node = cio.get('i18n://label/email', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#1') cio.delete(self.uri) content = cache.get(self.uri) self.assertIsNone(content)
def test_environment_state(self): with cio.env(i18n='en-us'): node = cio.get('page/title') self.assertEqual(node.uri, 'i18n://en-us@page/title.txt') node = cio.get('page/title') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt')
def test_set(self): response = self.post('api', 'i18n://page/title', {'data': u'# Djedi'}) self.assertEqual(response.status_code, 400) response = self.post('api', 'i18n://sv-se@page/title.txt', {'data': u'# Djedi', 'data[extra]': u'foobar'}) self.assertEqual(response.status_code, 400) uri = 'i18n://sv-se@page/title.md' response = self.post('api', uri, {'data': u'# Djedi', 'meta[message]': u'lundberg'}) self.assertEqual(response.status_code, 200) node = json_node(response, simple=False) meta = node.pop('meta') self.assertDictEqual(node, {'uri': 'i18n://sv-se@page/title.md#draft', 'content': u'<h1>Djedi</h1>'}) self.assertEqual(meta['author'], u'master') self.assertEqual(meta['message'], u'lundberg') node = cio.get(uri, lazy=False) self.assertIsNone(node.content) cio.publish(uri) node = cio.get(uri, lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.md#1') self.assertEqual(node.content, u'<h1>Djedi</h1>') response = self.post('api', node.uri, {'data': u'# Djedi', 'meta[message]': u'Lundberg'}) node = json_node(response, simple=False) self.assertEqual(node['meta']['message'], u'Lundberg') with self.assertRaises(PersistenceError): storage.backend._create(URI(node['uri']), None)
def test_get_with_local_cache_pipe_settings(self): def assert_local_thread(): settings.configure(local=True, CACHE={'PIPE': { 'CACHE_ON_GET': False }}) self.assertIn('BACKEND', settings.CACHE, 'Cache settings should be merged') # Test twice so that not the first get() caches the reponse in pipeline with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) thread = threading.Thread(target=assert_local_thread) thread.start() thread.join() # Back on main thread, settings should not be affected # Test twice to make sure first get chaches the reponse in pipeline with self.assertCache(calls=2, misses=1, hits=0, sets=1): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=0, hits=1, sets=0): cio.get('local/settings', default=u'default', lazy=False)
def assert_local_thread(): settings.configure(local=True, CACHE={'PIPE': {'CACHE_ON_GET': False}}) self.assertIn('BACKEND', settings.CACHE, 'Cache settings should be merged') # Test twice so that not the first get() caches the reponse in pipeline with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False)
def test_get_with_empty_default(self): node = cio.get('page/title', default=u'', lazy=False) self.assertEqual(node.content, u'') node = cio.get('page/body', default=None, lazy=False) self.assertIsNone(node.content) # Testing same non existing uri's twice to assert cache handles None/"" default node = cio.get('page/title', default=u'', lazy=False) self.assertEqual(node.content, u'') node = cio.get('page/body', default=None, lazy=False) self.assertIsNone(node.content)
def test_non_distinct_uri(self): node1 = cio.get('page/title', u'Title1') node2 = cio.get('page/title', u'Title2') self.assertEqual(six.text_type(node1), u'Title1') self.assertEqual(six.text_type(node2), u'Title1') node1 = cio.get('page/title', u'Title1', lazy=False) cache.clear() node2 = cio.get('page/title', u'Title2', lazy=False) self.assertEqual(six.text_type(node1), u'Title1') self.assertEqual(six.text_type(node2), u'Title2') # Second node not buffered, therefore unique default content
def assert_local_thread(): settings.configure(local=True, CACHE={'PIPE': { 'CACHE_ON_GET': False }}) self.assertIn('BACKEND', settings.CACHE, 'Cache settings should be merged') # Test twice so that not the first get() caches the reponse in pipeline with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False)
def test_non_distinct_uri(self): node1 = cio.get('page/title', u'Title1') node2 = cio.get('page/title', u'Title2') self.assertEqual(six.text_type(node1), u'Title1') self.assertEqual(six.text_type(node2), u'Title1') node1 = cio.get('page/title', u'Title1', lazy=False) cache.clear() node2 = cio.get('page/title', u'Title2', lazy=False) self.assertEqual(six.text_type(node1), u'Title1') self.assertEqual( six.text_type(node2), u'Title2' ) # Second node not buffered, therefore unique default content
def tag(cls, parser, token): # Parse tag args and kwargs bits = token.split_contents()[1:] params = ('uri', 'edit') args, kwargs = parse_bits(parser, bits, params, None, True, (True,), None, 'blocknode') # Assert uri is the only tag arg if len(args) > 1: raise TemplateSyntaxError('Malformed arguments to blocknode tag') # Resolve uri variable uri = args[0].resolve({}) # Parse tag body (default content) tokens = parser.parse(('endblocknode',)) parser.delete_first_token() # Remove endblocknode tag # Render default content tokens and dedent common leading whitespace default = u''.join((token.render({}) for token in tokens)) default = default.strip('\n\r') default = textwrap.dedent(default) # Get node for uri, lacks context variable lookup due to lazy loading. node = cio.get(uri, default) return cls(tokens, node, kwargs)
def tag(cls, parser, token): # Parse tag args and kwargs bits = token.split_contents()[1:] params = ('uri', 'edit') args, kwargs = parse_bits(parser, bits, params, None, True, (True, ), None, 'blocknode') # Assert uri is the only tag arg if len(args) > 1: raise TemplateSyntaxError('Malformed arguments to blocknode tag') # Resolve uri variable uri = args[0].resolve({}) # Parse tag body (default content) tokens = parser.parse(('endblocknode', )) parser.delete_first_token() # Remove endblocknode tag # Render default content tokens and dedent common leading whitespace default = u''.join((token.render({}) for token in tokens)) default = default.strip('\n\r') default = textwrap.dedent(default) # Get node for uri, lacks context variable lookup due to lazy loading. node = cio.get(uri, default) return cls(tokens, node, kwargs)
def _render_node(self, node_or_uri, default, edit, params, tag, caller): if isinstance(node_or_uri, str): node = cio.get(node_or_uri, default=default, lazy=False) else: node = node_or_uri return Markup(render_node(node, edit=edit, context=params))
def node(key, default=None, edit=True): """ Simple node tag: {% node 'page/title' default='Lorem ipsum' edit=True %} """ node = cio.get(key, default=default or u'') return lambda _: render_node(node, edit=edit)
def test_plugin(self): self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md'])) settings.configure(PLUGINS=[ 'cio.plugins.txt.TextPlugin', 'cio.plugins.md.MarkdownPlugin', 'tests.UppercasePlugin' ]) self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md', 'up'])) node = cio.set('sv-se@page/title.up', {'name': u'lundberg'}, publish=False) self.assertListEqual(node._content, [{ 'name': u'lundberg' }, u'{"name": "lundberg"}', u'LUNDBERG']) cio.publish(node.uri) node = cio.get('page/title') raw_content = storage.get(node.uri) self.assertIsNotNone(raw_content) self.assertEqual(raw_content['uri'], 'i18n://sv-se@page/title.up#1') self.assertEqual(raw_content['content'], u'{"name": "lundberg"}') self.assertEqual(node.content, u'LUNDBERG') self.assertEqual(node.uri.ext, 'up') self.assertSetEqual(set(p for p in plugins), set(('txt', 'md', 'up')))
def test_forced_empty_content(self): with self.assertRaises(ValueError): cio.set('i18n://sv-se@none', None) node = cio.set('i18n://[email protected]', u'') node = cio.get(node.uri, default=u'fallback') self.assertEqual(six.text_type(node), u'')
def test_node_meta(self): node = cio.set('sv-se@page/title', u'', author=u'lundberg') self.assertEqual(node.meta.get('author'), u'lundberg') node = cio.get('page/title') self.assertEqual(len(node.meta.keys()), 0) # Cached node has no meta node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') cio.set('sv-se@page/title#1', u'', comment=u'This works!') node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'comment', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') self.assertEqual(meta.get('comment'), u'This works!') cio.set('sv-se@page/title#1', u'', comment=None) node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') self.assertNotIn('comment', meta)
def test_plugin(self): self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md'])) settings.configure(PLUGINS=[ 'cio.plugins.txt.TextPlugin', 'cio.plugins.md.MarkdownPlugin', 'tests.UppercasePlugin' ]) self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md', 'up'])) node = cio.set('sv-se@page/title.up', {'name': u'lundberg'}, publish=False) self.assertListEqual(node._content, [{'name': u'lundberg'}, u'{"name": "lundberg"}', u'LUNDBERG']) cio.publish(node.uri) node = cio.get('page/title') raw_content = storage.get(node.uri) self.assertIsNotNone(raw_content) self.assertEqual(raw_content['uri'], 'i18n://sv-se@page/title.up#1') self.assertEqual(raw_content['content'], u'{"name": "lundberg"}') self.assertEqual(node.content, u'LUNDBERG') self.assertEqual(node.uri.ext, 'up') self.assertSetEqual(set(p for p in plugins), set(('txt', 'md', 'up')))
def test_uri_redirect(self): cio.set('i18n://sv-se@page/title.txt', u'Title') node = cio.get('i18n://sv-se@page/title', u'Default') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') self.assertEqual(node.content, u'Title') node = cio.get('i18n://sv-se@page/title.md', u'# Default Markdown', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.md') self.assertEqual(node.content, u'<h1>Default Markdown</h1>') # Cache still contains 'Title', but plugin diff and skipped cached_node = cache.get(node.uri) self.assertDictEqual(cached_node, {'uri': node.uri, 'content': u'<h1>Default Markdown</h1>'}) cache.clear() node = cio.get('i18n://sv-se@page/title.md', u'Default Markdown', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.md') self.assertEqual(node.content, u'<p>Default Markdown</p>') # Cache cleared, storage plugin mismatch, default fallback
def test_uri_redirect(self): cio.set('i18n://sv-se@page/title.txt', u'Title') node = cio.get('i18n://sv-se@page/title', u'Default') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') self.assertEqual(node.content, u'Title') node = cio.get('i18n://sv-se@page/title.up', u'Default Upper', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up') self.assertEqual(node.content, u'DEFAULT UPPER') # Cache still contains 'Title', but plugin diff and skipped cached_node = cache.get(node.uri) self.assertDictEqual(cached_node, {'uri': node.uri, 'content': u'DEFAULT UPPER'}) cache.clear() node = cio.get('i18n://sv-se@page/title.up', u'Default-Upper', lazy=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up') self.assertEqual(node.content, u'DEFAULT-UPPER') # Cache cleared, storage plugin mismatch, default fallback
def test_set(self): with self.assertRaises(URI.Invalid): cio.set('page/title', 'fail') with self.assertRaises(URI.Invalid): cio.set('page/title.txt', 'fail') node = cio.set('i18n://sv-se@label/email.up', u'e-post') self.assertEqual(node.uri, 'i18n://sv-se@label/email.up#1') cache.clear() node = cio.get('label/email', u'fallback') self.assertEqual(node.content, u'E-POST') self.assertEqual(node.uri, 'i18n://sv-se@label/email.up#1') self.assertEqual(node.initial, u'fallback') self.assertEqual(len(node.meta.keys()), 0) # No meta returned from non-versioned api get self.assertEqual(repr(node._node), '<Node: i18n://sv-se@label/email.up#1>') self.assertEqual(node.for_json(), { 'uri': node.uri, 'content': node.content, 'meta': node.meta }) node = cio.set('sv-se@label/email', u'e-post', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#draft') self.assertKeys(node.meta, 'modified_at', 'is_published') node = cio.publish(node.uri) self.assertKeys(node.meta, 'modified_at', 'published_at', 'is_published') self.assertTrue(node.meta['is_published']) node = cio.get('label/email') self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#2') self.assertEqual(node.content, u'e-post') self.assertEqual(node.uri.ext, 'txt') self.assertEqual(len(node.meta.keys()), 0) # Try publish non-existing node/uri node = cio.publish('i18n://sv-se@foo/bar.txt#draft') self.assertIsNone(node)
def test_get_with_local_cache_pipe_settings(self): def assert_local_thread(): settings.configure(local=True, CACHE={'PIPE': {'CACHE_ON_GET': False}}) self.assertIn('BACKEND', settings.CACHE, 'Cache settings should be merged') # Test twice so that not the first get() caches the reponse in pipeline with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=1, hits=0, sets=0): cio.get('local/settings', default=u'default', lazy=False) thread = threading.Thread(target=assert_local_thread) thread.start() thread.join() # Back on main thread, settings should not be affected # Test twice to make sure first get chaches the reponse in pipeline with self.assertCache(calls=2, misses=1, hits=0, sets=1): cio.get('local/settings', default=u'default', lazy=False) with self.assertCache(calls=1, misses=0, hits=1, sets=0): cio.get('local/settings', default=u'default', lazy=False)
def test_pipes_hits(self): with cio.env(i18n=('sv-se', 'en-us')): with self.assertDB(inserts=2): with self.assertCache(calls=2, sets=2): cio.set('i18n://sv-se@label/email.txt', u'epost') cio.set('i18n://en-us@label/surname.txt', u'surname') # Lazy gets with self.assertDB(calls=0): with self.assertCache(calls=0): node1 = cio.get('label/email') node2 = cio.get('i18n://label/surname') node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer') # with self.assertDB(calls=2), self.assertCache(calls=5, hits=1, misses=2, sets=2): with self.assertDB(calls=4, selects=4): with self.assertCache(calls=2, hits=1, misses=2, sets=2): self.assertEqual(six.text_type(node1), u'epost') self.assertEqual(node2.content, u'surname') self.assertEqual(six.text_type(node3), u'postnummer') with self.assertDB(calls=0): with self.assertCache(calls=1, hits=3): node1 = cio.get('label/email') node2 = cio.get('i18n://label/surname') node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer') self.assertEqual(six.text_type(node1), u'epost') self.assertEqual(node2.content, u'surname') self.assertEqual(six.text_type(node3), u'postnummer') self.assertIsNotNone(repr(node1)) self.assertIsNotNone(str(node1))
def test_context(self): self.assertTupleEqual(cio.env.state.i18n, ('sv-se',)) with settings(): settings.ENVIRONMENT['bogus'] = { 'g11n': 'global', 'i18n': ('sv', 'en'), 'l10n': ('foo', 'bar') } with cio.env('bogus'): self.assertTupleEqual(cio.env.state.i18n, ('sv', 'en')) self.assertTupleEqual(cio.env.state.l10n, ('foo', 'bar')) with cio.env(i18n='en-us'): node = cio.get('i18n://label/firstname', lazy=False) buffered_node = cio.get('i18n://label/email') self.assertTupleEqual(cio.env.i18n, ('en-us',)) self.assertEqual(len(pipeline._buffer), 1) self.assertEqual(len(pipeline.history), 1) def assert_new_thread_env(): self.assertTupleEqual(cio.env.i18n, ('sv-se',)) self.assertEqual(len(pipeline._buffer), 0) self.assertEqual(len(pipeline.history), 0) cio.get('i18n://label/surname', lazy=False) self.assertEqual(len(pipeline.history), 1) thread = threading.Thread(target=assert_new_thread_env) thread.start() thread.join() buffered_node.flush() self.assertEqual(len(pipeline._buffer), 0) self.assertEqual(len(pipeline.history), 2) self.assertListEqual(pipeline.history.list('get'), [node, buffered_node._node]) self.assertNotIn('bogus', settings.ENVIRONMENT.keys())
def get(self, request, uri): """ Return published node or specified version. JSON Response: {uri: x, content: y} """ uri = self.decode_uri(uri) node = cio.get(uri, lazy=False) if node.content is None: raise Http404 return self.render_to_json({'uri': node.uri, 'content': node.content})
def test_abandoned_buffered_node(self): cio.set('sv-se@foo/bar', u'foobar') node = cio.get('foo/bar') self.assertFalse(node._flushed) self.assertIn('get', pipeline._buffer._buffer) # Mess things up... pipeline.clear() self.assertFalse(node._flushed) self.assertNotIn('get', pipeline._buffer._buffer) self.assertEqual(node.content, u'foobar') self.assertTrue(node._flushed)
def test_delete(self): response = self.delete('api', 'i18n://sv-se@page/title') self.assertEqual(response.status_code, 404) node = cio.set('i18n://sv-se@page/title.md', u'# Djedi') response = self.delete('api', node.uri) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, u'') with self.assertRaises(NodeDoesNotExist): storage.get('i18n://sv-se@page/title') node = cio.get('i18n://page/title', lazy=False) self.assertIsNone(node.content)
def post(self, request): # Disable caching gets in CachePipe, defaults through this api is not trusted cio.conf.settings.configure(local=True, CACHE={'PIPE': { 'CACHE_ON_GET': False }}) nodes = [] for uri, default in six.iteritems(json.loads(request.body)): node = cio.get(uri, default=default) nodes.append(node) data = dict((node.uri, node.content) for node in nodes) return self.render_to_json(data)
def get(self, request, uri): """ Return published node or specified version. JSON Response: {uri: x, content: y} """ uri = self.decode_uri(uri) node = cio.get(uri, lazy=False) if node.content is None: raise Http404 return self.render_to_json({ 'uri': node.uri, 'content': node.content })
def test_fallback(self): with cio.env(i18n=('sv-se', 'en-us', 'en-uk')): node1 = cio.set('i18n://bogus@label/email.txt', u'epost') node2 = cio.set('i18n://en-uk@label/surname.txt', u'surname') with self.assertCache(misses=2, sets=2): with self.assertDB(calls=6, selects=6): node3 = cio.get('i18n://label/email') node4 = cio.get('i18n://label/surname', u'efternamn') self.assertIsNone(node3.content) self.assertEqual(node4.content, u'surname') cache.clear() with self.assertCache(misses=2, sets=2): with self.assertDB(calls=6): cio.get('i18n://label/email', lazy=False) cio.get('i18n://label/surname', u'lastname', lazy=False)
def test_fallback(self): with cio.env(i18n=('sv-se', 'en-us', 'en-uk')): cio.set('i18n://bogus@label/email.txt', u'epost') cio.set('i18n://en-uk@label/surname.txt', u'surname') with self.assertCache(misses=2, sets=2): with self.assertDB(calls=6, selects=6): node1 = cio.get('i18n://label/email') node2 = cio.get('i18n://label/surname', u'efternamn') self.assertEqual( node1.uri.namespace, 'sv-se' ) # No fallback, stuck on first namespace, sv-se self.assertEqual(node1.namespace_uri.namespace, 'sv-se') self.assertIsNone(node1.content) self.assertEqual(node2.uri.namespace, 'en-uk') self.assertEqual(node2.namespace_uri.namespace, 'sv-se') self.assertEqual(node2.content, u'surname') cache.clear() with self.assertCache(misses=2, sets=2): with self.assertDB(calls=6): cio.get('i18n://label/email', lazy=False) cio.get('i18n://label/surname', u'lastname', lazy=False)
def assert_new_thread_env(): self.assertTupleEqual(cio.env.i18n, ('sv-se',)) self.assertEqual(len(pipeline._buffer), 0) self.assertEqual(len(pipeline.history), 0) cio.get('i18n://label/surname', lazy=False) self.assertEqual(len(pipeline.history), 1)
def reload_node(self): self.node = cio.get(self.uri, self.default or u'')
def node(uri, default=None, edit=True, context=None): """Function that renders a Djedi node.""" node = cio.get(uri, default=default or u'', lazy=False) return Markup(render_node(node, edit=edit, context=context))
def _create_nodes(self, nodes, local_nodes): for node_id, uri, default in local_nodes: if node_id not in nodes: nodes[node_id] = cio.get(uri, default=default or '')
def test_revisions(self): def assertRevisions(*revs): revisions = set(cio.revisions('i18n://sv-se@page/title')) assert revisions == set(revs) self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0) node = cio.load('sv-se@page/title') self.assertDictEqual(node, { 'uri': 'i18n://sv-se@page/title.txt', 'data': None, 'content': None, 'meta': {} }) # First draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.txt', u'Content-IO', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft') assertRevisions(('i18n://sv-se@page/title.txt#draft', False)) self.assertIsNone(cio.get('page/title').content) # Publish first draft, version 1 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Second draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up', u'Content-IO - Fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#draft', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Publish second draft, version 2 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Alter published version 2 with self.assertDB(calls=2, selects=1, inserts=0, updates=1): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up#2', u'Content-IO - Lightening fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Not published, still in cache # Re-publish version 2, no change with self.assertDB(selects=1, inserts=0, updates=0): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!') # Rollback version 1 with self.assertDB(calls=3, selects=1, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish('i18n://sv-se@page/title#1') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#2', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Assert get specific version doesn't mess up the cache cache.clear() with self.assertCache(calls=0): self.assertEqual(cio.get('page/title#2').content, u'CONTENT-IO - LIGHTENING FAST!') with self.assertCache(calls=2, misses=1, sets=1): self.assertEqual(cio.get('page/title').content, u'Content-IO') # Load version 1 and 2 data = cio.load('sv-se@page/title#1') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO') data = cio.load('sv-se@page/title#2') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2') self.assertEqual(data['data'], {u'name': u'Content-IO - Lightening fast!'}) # Load without version and expect published version data = cio.load('sv-se@page/title') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO')
def test_revisions(self): def assertRevisions(*revs): revisions = set(cio.revisions('i18n://sv-se@page/title')) assert revisions == set(revs) self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0) node = cio.load('sv-se@page/title') self.assertDictEqual( node, { 'uri': 'i18n://sv-se@page/title.txt', 'data': None, 'content': None, 'meta': {} }) # First draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.txt', u'Content-IO', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft') assertRevisions(('i18n://sv-se@page/title.txt#draft', False)) self.assertIsNone(cio.get('page/title').content) # Publish first draft, version 1 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Second draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up', u'Content-IO - Fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#draft', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Publish second draft, version 2 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Alter published version 2 with self.assertDB(calls=2, selects=1, inserts=0, updates=1): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up#2', u'Content-IO - Lightening fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual( cio.get('page/title').content, u'CONTENT-IO - FAST!') # Not published, still in cache # Re-publish version 2, no change with self.assertDB(selects=1, inserts=0, updates=0): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual( cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!') # Rollback version 1 with self.assertDB(calls=3, selects=1, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish('i18n://sv-se@page/title#1') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#2', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Assert get specific version doesn't mess up the cache cache.clear() with self.assertCache(calls=0): self.assertEqual( cio.get('page/title#2').content, u'CONTENT-IO - LIGHTENING FAST!') with self.assertCache(calls=2, misses=1, sets=1): self.assertEqual(cio.get('page/title').content, u'Content-IO') # Load version 1 and 2 data = cio.load('sv-se@page/title#1') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO') data = cio.load('sv-se@page/title#2') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2') self.assertEqual(data['data'], {u'name': u'Content-IO - Lightening fast!'}) # Load without version and expect published version data = cio.load('sv-se@page/title') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO')
def test_get_with_context(self): node = cio.get('page/title', default=u'{Welcome} {firstname} {lastname}!') content = node.render(firstname=u'Jonas', lastname=u'Lundberg') self.assertEqual(content, u'{Welcome} Jonas Lundberg!')
def test_get(self): node = cio.get('label/email', default=u'fallback') self.assertEqual(node.content, u'fallback') self.assertEqual(node.initial_uri, 'label/email') self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt')