def begin_update(self): """Begins an update block.""" if self.in_update_block: raise RuntimeError("Artifact is already open for updates.") self.updated = False ctx = Context(self) ctx.push() self.in_update_block = True return ctx
def begin_update(self): """Begins an update block.""" if self.in_update_block: raise RuntimeError('Artifact is already open for updates.') self.updated = False ctx = Context(self) ctx.push() self.in_update_block = True self.clear_dirty_flag() return ctx
def test_datetime(env, pad): field = make_field(env, 'datetime') with Context(pad=pad): # default rv = field.deserialize_value('2016-04-30 01:02:03', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo is None # It is not datetime, it is None rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) # It is not datetime, it is empty string rv = field.deserialize_value('', pad=pad) assert isinstance(rv, BadValue) # It is not datetime, it is date rv = field.deserialize_value('2016-04-30', pad=pad) assert isinstance(rv, BadValue)
def test_get_config_returns_cached_value(self, scratch_plugin, scratch_env): with Context(pad=scratch_env.new_pad()): cfg = scratch_plugin.get_config() cfg["is_cached"] = "indeed" cfg = scratch_plugin.get_config() assert "is_cached" in cfg
def test_prev_next_virtual_resolver(pntest_pad): with Context(pad=pntest_pad): siblings = pntest_pad.get("post2@siblings") assert isinstance(siblings, Siblings) assert siblings.path == "/post2@siblings" assert siblings.record["_id"] == "post2" assert siblings.prev_page["_id"] == "post1" assert siblings.next_page["_id"] == "post4" with pytest.raises(NotImplementedError): siblings.url_path # pylint: disable=pointless-statement
def test_tags_expression(pad, builder, env): with Context(pad=pad): plugin = env.plugins['tags'] conf = plugin.get_config() parent = pad.get('/blog') conf['tags'] = 'parent.children.filter(F.published).distinct("tags")' assert plugin.get_all_tags(parent) == ['tag1', 'tag2'] conf['tags'] = '["foo", "bar", "bar"]' assert plugin.get_all_tags(parent) == ['bar', 'foo']
def test_tags_expression(pad, builder, env): with Context(pad=pad): plugin = env.plugins["tags"] conf = plugin.get_config() parent = pad.get("/blog") conf["tags"] = 'parent.children.filter(F.published).distinct("tags")' assert plugin.get_all_tags(parent) == ["tag1", "tag2"] conf["tags"] = '["foo", "bar", "bar"]' assert plugin.get_all_tags(parent) == ["bar", "foo"]
def test_datetime_timezone_utc(env, pad): field = make_field(env, "datetime") with Context(pad=pad): # Known timezone name, UTC rv = field.deserialize_value("2016-04-30 01:02:03 UTC", pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo is get_timezone("UTC")
def test_datetime_timezone_est(env, pad): field = make_field(env, 'datetime') with Context(pad=pad): # Known timezone name, EST rv = field.deserialize_value('2016-04-30 01:02:03 EST', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo is get_timezone('EST')
def test_datetime_timezone_mart(env, pad): field = make_field(env, 'datetime') with Context(pad=pad): # MART - http://www.timeanddate.com/time/zones/mart rv = field.deserialize_value('2016-04-30 01:02:03 -0930', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo._offset == datetime.timedelta(0, -(9 * 60 + 30) * 60)
def test_datetime_timezone_kst(env, pad): field = make_field(env, "datetime") with Context(pad=pad): # KST - http://www.timeanddate.com/time/zones/kst rv = field.deserialize_value("2016-04-30 01:02:03 +0900", pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo._offset == datetime.timedelta(0, 9 * 60 * 60)
def test_prev_next_virtual_resolver(pntest_pad): from lektor.context import Context from lektor.db import Siblings with Context(pad=pntest_pad): siblings = pntest_pad.get('post2@siblings') assert isinstance(siblings, Siblings) assert siblings.path == '/post2@siblings' assert siblings.record['_id'] == 'post2' assert siblings.prev_page['_id'] == 'post1' assert siblings.next_page['_id'] == 'post4' with pytest.raises(NotImplementedError): siblings.url_path
def test_datetime_timezone_location(env, pad): field = make_field(env, 'datetime') with Context(pad=pad): # Known location name, Asia/Seoul rv = field.deserialize_value('2016-04-30 01:02:03 Asia/Seoul', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 tzinfos = get_timezone('Asia/Seoul')._tzinfos # pylint: disable=no-member assert rv.tzinfo in itervalues(tzinfos)
def test_integer(env, pad): field = make_field(env, 'integer') with Context(pad=pad): rv = field.deserialize_value('', pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value('42', pad=pad) assert rv == 42 rv = field.deserialize_value(' 23 ', pad=pad) assert rv == 23
def test_integer(env, pad): field = make_field(env, "integer") with Context(pad=pad): rv = field.deserialize_value("", pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value("42", pad=pad) assert rv == 42 rv = field.deserialize_value(" 23 ", pad=pad) assert rv == 23
def test_text(env, pad): field = make_field(env, 'text') with Context(pad=pad): rv = field.deserialize_value('', pad=pad) assert rv == '' rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value('foo\nbar', pad=pad) assert rv == 'foo\nbar' rv = field.deserialize_value(' 123 ', pad=pad) assert rv == ' 123 '
def test_string(env, pad): field = make_field(env, "string") with Context(pad=pad): rv = field.deserialize_value("", pad=pad) assert rv == "" rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value("foo\nbar", pad=pad) assert rv == "foo" rv = field.deserialize_value(" 123 ", pad=pad) assert rv == "123"
def test_html_links(env, pad): field = make_field(env, "html") source = DummySource() def md(s): rv = field.deserialize_value(s, pad=pad) assert isinstance(rv, HTMLDescriptor) return str(rv.__get__(source)).strip() with Context(pad=pad): assert md('<p><a href="http://example.com/">foo</a></p>') == ("""<p> <a href="http://example.com/"> foo </a> </p>""")
def test_boolean(env, pad): field = make_field(env, 'boolean') with Context(pad=pad): rv = field.deserialize_value('', pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) for s in 'true', 'TRUE', 'True', '1', 'yes': rv = field.deserialize_value(s, pad=pad) assert rv is True for s in 'false', 'FALSE', 'False', '0', 'no': rv = field.deserialize_value(s, pad=pad) assert rv is False
def test_boolean(env, pad): field = make_field(env, "boolean") with Context(pad=pad): rv = field.deserialize_value("", pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) for s in "true", "TRUE", "True", "1", "yes": rv = field.deserialize_value(s, pad=pad) assert rv is True for s in "false", "FALSE", "False", "0", "no": rv = field.deserialize_value(s, pad=pad) assert rv is False
def test_float(env, pad): field = make_field(env, 'float') with Context(pad=pad): rv = field.deserialize_value('', pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value('42', pad=pad) assert rv == 42.0 rv = field.deserialize_value(' 23.0 ', pad=pad) assert rv == 23.0 rv = field.deserialize_value('-23.5', pad=pad) assert rv == -23.5
def test_markdown(env, pad): field = make_field(env, 'markdown') with Context(pad=pad): rv = field.deserialize_value('Hello **World**!', pad=pad) assert isinstance(rv, Markdown) assert rv assert rv.source == 'Hello **World**!' assert escape(rv) == Markup('<p>Hello <strong>World</strong>!</p>\n') assert rv.meta == {} for val in '', None: rv = field.deserialize_value(val, pad=pad) assert isinstance(rv, Markdown) assert not rv assert rv.source == '' assert escape(rv) == Markup('') assert rv.meta == {}
def test_float(env, pad): field = make_field(env, "float") with Context(pad=pad): rv = field.deserialize_value("", pad=pad) assert isinstance(rv, BadValue) rv = field.deserialize_value(None, pad=pad) assert isinstance(rv, Undefined) rv = field.deserialize_value("42", pad=pad) assert rv == 42.0 rv = field.deserialize_value(" 23.0 ", pad=pad) assert rv == 23.0 rv = field.deserialize_value("-23.5", pad=pad) assert rv == -23.5
def test_markdown_links(env, pad): field = make_field(env, 'markdown') source = DummySource() def md(s): rv = field.deserialize_value(s, pad=pad) assert isinstance(rv, MarkdownDescriptor) return text_type(rv.__get__(source)).strip() with Context(pad=pad): assert md('[foo](http://example.com/)') == ( '<p><a href="http://example.com/">foo</a></p>') assert md('[foo](javascript:foo)') == ( '<p><a href="javascript:foo">foo</a></p>') img = ('iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' '//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==') assert md('![test](data:image/png;base64,%s)' % img) == ( '<p><img src="data:image/png;base64,%s" alt="test"></p>') % img
def test_markdown(env, pad): field = make_field(env, "markdown") source = DummySource() with Context(pad=pad): rv = field.deserialize_value("Hello **World**!", pad=pad) assert isinstance(rv, MarkdownDescriptor) rv = rv.__get__(source) assert rv assert rv.source == "Hello **World**!" assert escape(rv) == Markup("<p>Hello <strong>World</strong>!</p>\n") assert rv.meta == {} for val in "", None: rv = field.deserialize_value(val, pad=pad) assert isinstance(rv, MarkdownDescriptor) rv = rv.__get__(source) assert not rv assert rv.source == "" assert escape(rv) == Markup("") assert rv.meta == {}
def test_datetime_timezone_name(env, pad): field = make_field(env, 'datetime') with Context(pad=pad): # with timezone name (case 1) rv = field.deserialize_value('2016-04-30 01:02:03 KST +0900', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo._offset == datetime.timedelta(0, 9 * 60 * 60) # with timezone name (case 2) rv = field.deserialize_value('2016-04-30 01:02:03 KST+0900', pad=pad) assert isinstance(rv, datetime.datetime) assert rv.year == 2016 assert rv.month == 4 assert rv.day == 30 assert rv.hour == 1 assert rv.minute == 2 assert rv.second == 3 assert rv.tzinfo._offset == datetime.timedelta(0, 9 * 60 * 60)
def test_basic_alts(pad): with Context(pad=pad): assert get_alts() == ["en", "de"]
def test_get_config_fresh(self, scratch_plugin, scratch_env): with Context(pad=scratch_env.new_pad()): cfg = scratch_plugin.get_config() cfg["is_cached"] = "indeed" cfg = scratch_plugin.get_config(fresh=True) assert "is_cached" not in cfg
def test_get_config_records_dependency(self, scratch_plugin, scratch_env): with Context(pad=scratch_env.new_pad()) as ctx: scratch_plugin.get_config() assert scratch_plugin.config_filename in ctx.referenced_dependencies
def test_basic_alts(pad): with Context(pad=pad): assert get_alts() == ['en', 'de']
def test_get_lektor_config_from_context(self, dummy_plugin, env): with Context(pad=env.new_pad()): cfg = dummy_plugin.get_lektor_config() assert cfg["PROJECT"]["name"] == "Demo Project"