def test_require(self): s = Scalar(3) assert s.require(Q > 1) is s with pytest.raises(NullValueError): s.require(Q > 4)
def test_dictzip(self): c = Collection([Scalar(1), Scalar(2)]) result = c.dictzip(['a', 'b']) expected = {'a': 1, 'b': 2} assert result.val() == expected lbls = Collection([Scalar('a'), Scalar('b')]) assert c.dictzip(lbls).val() == expected
def test_repr_unicode(self): s = Scalar('∂ƒ') print(s) print(repr(s)) print(text_type(s)) assert repr(s)[0] != "'" s = Scalar('∂ƒ'.encode('utf-8')) print(s) print(repr(s)) print(text_type(s)) s = Scalar(b'\xc2') print(s) print(repr(s)) print(text_type(s))
def test_arithmetic_with_null(self): l, r = Scalar(1), Null() assert isinstance(l + r, Null) assert isinstance(l - r, Null) assert isinstance(l * r, Null) assert isinstance(l / r, Null) assert isinstance(l // r, Null) assert isinstance(l**r, Null) assert isinstance(l % r, Null) if not PY3: assert isinstance(operator.div(l, r), Null)
def test_arithmetic(self): c = Scalar(1) assert (c + 1).val() == 2 assert (c - 1).val() == 0 assert (c * 2).val() == 2 assert (c / 1).val() == 1 assert operator.truediv(c, 2).val() == 0.5 if not PY3: assert operator.div(c, 2).val() == 0.5 assert (c // 1).val() == 1 assert (c**2).val() == 1 assert (c % 2).val() == 1 assert (c + c).val() == 2
def test_bool(self): assert Collection([Scalar(1)]) assert not Collection([])
def test_get_single_on_iterator(self): c = Collection((Scalar(i) for i in range(5))) assert c[2].val() == 2
def test_slice_on_iterator(self): c = Collection((Scalar(i) for i in range(5))) assert c[::2].val() == [0, 2, 4]
def test_apply(self): assert Scalar(3).apply(lambda x: x.val() * 2).val() == 6 assert isinstance(Null().apply(lambda x: x), Null)
def test_wrap(self): v = Scalar(3) assert isinstance(Wrapper.wrap(3), Scalar) assert Wrapper.wrap(v) is v assert isinstance(Wrapper.wrap(BeautifulSoup('a')), Node)
def test_hash(self): """ wrappers are hashed by their content """ assert hash(Scalar(2)) == hash(Scalar(2)) assert hash(Some(2)) == hash(Some(2))
def test_len(self): assert len(Scalar([1, 2, 3])) == 3
def test_lt(self): assert (Scalar(3) < 4).val()
def test_ne(self): assert (Scalar('test') != 'foo').val()
def test_gt(self): assert (Scalar(3) > 2).val()
def test_isnull(self): assert not Scalar(1).isnull()
def test_getkey(self): assert Scalar({'a': 1})['a'].val() == 1
def test_slice(self): assert Scalar('test')[1:-1].val() == 'es'
def test_count(self): assert Collection([]).count().val() == 0 assert Collection([Scalar(1)]).count().val() == 1 assert NullCollection().count().val() == 0
def test_setitem(self): s = Scalar({'a': 1}) s['b'] = 2 assert s.val() == {'a': 1, 'b': 2}
def test_ge(self): assert (Scalar(3) >= 2).val()
def test_le(self): assert (Scalar(3) <= 3).val()
def test_eq(self): assert (Scalar('test') == 'test').val()