def test_set_flat_pruned(): sub = String.named(u's') pairs = [(u's', u'val0'), (u's', ''), (u's', u'val1'), (u's', u'')] wanted = [u'val0', u'val1'] for schema in Array.of(sub), Array.of(sub).using(prune_empty=True): el = schema.from_flat(pairs) eq_(len(el), len(wanted)) eq_(el.value, wanted)
def test_el(): schema = Array.of(String.named(u's')) element = schema(u'abc') eq_(list(element.value), [u'a', u'b', u'c']) eq_(element.el(u'0').value, u'a') eq_(element.el(u'2').value, u'c') assert_raises(KeyError, element.el, u'a')
def test_el(): schema = Array.of(String.named(u's')) element = schema(u'abc') assert list(element.value) == [u'a', u'b', u'c'] assert element.el(u'0').value == u'a' assert element.el(u'2').value == u'c' with pytest.raises(KeyError): element.el(u'a')
def test_set_default(): schema = Array.of(String).using(default=[u'x', u'y']) el = schema() eq_(el.value, []) el.set_default() eq_(el.value, [u'x', u'y']) el.append(u'z') eq_(el.value, [u'x', u'y', u'z']) el.set_default() eq_(el.value, [u'x', u'y']) defaulted_child = String.using(default='not triggered') schema = Array.of(defaulted_child).using(default=[u'x']) el = schema.from_defaults() eq_(el.value, [u'x']) schema = Array.of(String.using(default=u'x')) el = schema.from_defaults() eq_(el.value, [])
def small_form(values=None): SmallForm = Dict.of( String.named(u'field1'), String.named(u'field2'), Boolean.named(u'toggle1'), Boolean.named(u'toggle2'), Array.named(u'multi').of(String), DateYYYYMMDD.named(u'date1')) if values is None: values = { u'field1': u'val', u'toggle2': True, u'multi': [u'a', u'b'], u'date1': datetime.date(1999, 12, 31), } el = SmallForm(values) return {'form': el}
def test_set(): schema = Array.of(Integer) el = schema() assert not el el = schema() assert not el.set(1) assert not el.value el = schema() assert el.set([]) assert not el.value el = schema() assert el.set([1]) assert el[0].u == u'1' assert el.value == [1] assert el[0].parent is el el = schema() assert el.set([1, 2, 3]) assert el[0].u == u'1' assert el[0].value == 1 assert el.value == [1, 2, 3]
def test_set_flat_anonymous_dict(): schema = Array.of(Dict.of(String.named('x'))) pairs = [(u'x', u'abc'), (u'x', u'def')] assert_raises(AssertionError, schema.from_flat, pairs)
def test_mutation(): schema = Array.of(String) el = schema() assert not el el.set([u'b']) assert el[0].u == u'b' assert el.value == [u'b'] el.append(u'x') assert el[1].u == u'x' assert el.value == [u'b', u'x'] assert el[1].parent is el el[1] = u'a' assert el[1].u == u'a' assert el.value == [u'b', u'a'] assert el[1].parent is el el.remove(u'b') assert el.value == [u'a'] el.extend(u'bcdefg') assert el.value[0:4] == [u'a', u'b', u'c', u'd'] assert el[2].parent is el del el[0] assert el.value[0:4] == [u'b', u'c', u'd', u'e'] del el[0:4] assert el.value == [u'f', u'g'] el.pop() assert el.value == [u'f'] assert el[0].u == u'f' assert el.u == u"[u'f']" del el[:] assert list(el) == [] assert el.value == [] assert el.u == u'[]' el[:] = u'abc' assert el.value == [u'a', u'b', u'c'] assert el[1].parent is el el.insert(1, u'z') assert el.value == [u'a', u'z', u'b', u'c'] assert el[1].parent is el def assign(): el.u = u'z' with pytest.raises(AttributeError): assign() assert el.value == [u'a', u'z', u'b', u'c'] def assign2(): el.value = u'abc' del el[:] with pytest.raises(AttributeError): assign2() assert el.value == []
#! -*- coding: utf-8 -*- from flatland import Array, Boolean, Integer from flatland.out import generic from flatland.out.generic import Context Unspecified = object() Unique = object() schema = Integer.named(u'number') boolean_schema = Boolean.named(u'bool') partial_anon_schema = Array.named(u'array').of(Integer) full_anon_schema = Array.of(Integer) def assert_bound_transform(fn, tagname, given, expected, **kw): return assert_transform(fn, tagname, given, expected, **kw) def assert_unbound_transform(fn, tagname, given, expected, **kw): kw['bind'] = None return assert_transform(fn, tagname, given, expected, **kw) def assert_transform(fn, tagname, given, expected, context=Unspecified, bind=Unspecified, contents=Unspecified, expected_contents=Unspecified): if context is Unspecified: context = Context() if bind is Unspecified:
def test_set_flat_anonymous_array(): schema = Array.of(String.named(u's')) pairs = [(u's', u'abc'), (u's', u'def')] bogus = [(u'', u'xxx')] _assert_array_set_flat(schema, pairs, bogus)
def test_set_flat_like_named(): pairs = [(u's_s', u'abc'), (u's_s', u'def')] bogus = [(u's', u'xxx')] schema = Array.named(u's').of(String.named(u's')) _assert_array_set_flat(schema, pairs, bogus)
def test_mutation(): schema = Array.of(String) el = schema() assert not el el.set([u'b']) assert el[0].u == u'b' assert el.value == [u'b'] el.append(u'x') assert el[1].u == u'x' assert el.value == [u'b', u'x'] assert el[1].parent is el el[1] = u'a' assert el[1].u == u'a' assert el.value == [u'b', u'a'] assert el[1].parent is el el.remove(u'b') assert el.value == [u'a'] el.extend(u'bcdefg') eq_(el.value[0:4], [u'a', u'b', u'c', u'd']) assert el[2].parent is el del el[0] eq_(el.value[0:4], [u'b', u'c', u'd', u'e']) del el[0:4] eq_(el.value, [u'f', u'g']) el.pop() eq_(el.value, [u'f']) eq_(el[0].u, u'f') eq_(el.u, "[u'f']") del el[:] eq_(list(el), []) eq_(el.value, []) eq_(el.u, u'[]') el[:] = u'abc' eq_(el.value, [u'a', u'b', u'c']) assert el[1].parent is el el.insert(1, u'z') eq_(el.value, [u'a', u'z', u'b', u'c']) assert el[1].parent is el def assign(): el.u = u'z' assert_raises(AttributeError, assign) eq_(el.value, [u'a', u'z', u'b', u'c']) def assign2(): el.value = u'abc' del el[:] assert_raises(AttributeError, assign2) eq_(el.value, [])
def test_mutation(): schema = Array.of(String) el = schema() assert not el el.set([u'b']) assert el[0].u == u'b' assert el.value == [u'b'] el.append(u'x') assert el[1].u == u'x' assert el.value == [u'b', u'x'] assert el[1].parent is el el[1] = u'a' assert el[1].u == u'a' assert el.value == [u'b', u'a'] assert el[1].parent is el el.remove(u'b') assert el.value == [u'a'] el.extend(u'bcdefg') eq_(el.value[0:4], [u'a', u'b', u'c', u'd']) assert el[2].parent is el del el[0] eq_(el.value[0:4], [u'b', u'c', u'd', u'e']) del el[0:4] eq_(el.value, [u'f', u'g']) el.pop() eq_(el.value, [u'f']) eq_(el[0].u, u'f') eq_(el.u.encode('ascii'), repr([u'f'])) del el[:] eq_(list(el), []) eq_(el.value, []) eq_(el.u, u'[]') el[:] = u'abc' eq_(el.value, [u'a', u'b', u'c']) assert el[1].parent is el el.insert(1, u'z') eq_(el.value, [u'a', u'z', u'b', u'c']) assert el[1].parent is el def assign(): el.u = u'z' assert_raises(AttributeError, assign) eq_(el.value, [u'a', u'z', u'b', u'c']) def assign2(): el.value = u'abc' del el[:] assert_raises(AttributeError, assign2) eq_(el.value, [])
#! -*- coding: utf-8 -*- from flatland import Array, Boolean, Integer from flatland.out import generic from flatland.out.generic import Context from tests._util import unicode_coercion_allowed, textstr Unspecified = object() Unique = object() schema = Integer.named(u'number') boolean_schema = Boolean.named(u'bool') partial_anon_schema = Array.named(u'array').of(Integer) full_anon_schema = Array.of(Integer) def assert_bound_transform(fn, tagname, given, expected, **kw): return assert_transform(fn, tagname, given, expected, **kw) def assert_unbound_transform(fn, tagname, given, expected, **kw): kw['bind'] = None return assert_transform(fn, tagname, given, expected, **kw) def assert_transform(fn, tagname, given, expected, context=Unspecified, bind=Unspecified, contents=Unspecified,
return cls @classmethod def _get_choice_specs(cls): revs = flaskg.storage.search(cls._query, **cls._query_args) choices = [(rev.meta[ITEMID], rev.meta[NAME][0]) for rev in revs] if cls.optional: choices.append((u'', cls.properties['empty_label'])) return choices def __init__(self, value=Unspecified, **kw): super(Reference, self).__init__(value, **kw) # NOTE There is a slight chance of two instances of the same Reference # subclass having different set of choices when the storage changes # between their initialization. choice_specs = self._get_choice_specs() self.properties['choice_specs'] = choice_specs self.valid_values = [id_ for id_, name in choice_specs] class BackReference(ReadonlyItemLinkList): """ Back references built from Whoosh query. """ def set(self, query, **query_args): revs = flaskg.storage.search(query, **query_args) super(BackReference, self).set([rev.meta[NAME] for rev in revs]) MultiSelect = Array.with_properties(widget=WIDGET_MULTI_SELECT)
def test_set_flat_unpruned(): pairs = [(u's', u'val0'), (u's', ''), (u's', u'val1'), (u's', u'')] schema = Array.of(String).named(u's').using(prune_empty=False) _assert_array_set_flat(schema, pairs)
@classmethod def _get_choice_specs(cls): revs = flaskg.storage.search(cls._query, **cls._query_args) label_getter = cls.properties['label_getter'] choices = [(rev.meta[ITEMID], label_getter(rev)) for rev in revs] if cls.optional: choices.append(('', cls.properties['empty_label'])) return choices def __init__(self, value=Unspecified, **kw): super(Reference, self).__init__(value, **kw) # NOTE There is a slight chance of two instances of the same Reference # subclass having different set of choices when the storage changes # between their initialization. choice_specs = self._get_choice_specs() self.properties['choice_specs'] = choice_specs self.valid_values = [id_ for id_, name in choice_specs] class BackReference(ReadonlyItemLinkList): """ Back references built from Whoosh query. """ def set(self, query, **query_args): revs = flaskg.storage.search(query, **query_args) super(BackReference, self).set([rev.meta[NAME] for rev in revs]) MultiSelect = Array.with_properties(widget=WIDGET_MULTI_SELECT)
def test_set_flat_unnamed_child(): pairs = [(u's', u'abc'), (u's', u'def')] bogus = [(u'', 'xxx')] schema = Array.named(u's').of(String) _assert_array_set_flat(schema, pairs, bogus)
def test_set_flat_unnamed_child(): pairs = [(u's', u'abc'), (u's', u'def')] bogus = [(u'', u'xxx')] schema = Array.named(u's').of(String) _assert_array_set_flat(schema, pairs, bogus)
def test_set_flat_anonymous_array(): schema = Array.of(String.named(u's')) pairs = [(u's', u'abc'), (u's', u'def')] bogus = [(u'', 'xxx')] _assert_array_set_flat(schema, pairs, bogus)
def test_set_flat_fully_anonymous_array(): schema = Array.of(String) pairs = [(u'', u'abc'), (u'', u'def')] _assert_array_set_flat(schema, pairs)
def multivalue_schema(): schema = Array.named(u'multi').of(String) return schema([u'abc', u'xyz'])