Exemple #1
0
def test_custom_data():
    item = Item(123, None, {'custom_attr': 'magicvalue'})
    assert item.custom_attr == 'magicvalue'
    try:
        item.nonexistant_attr
    except AttributeError:
        assert True
    else:
        assert False

    # we shouldn't be able to change the ID.
    item = Item(123, None, {'id': 456})
    assert item.id == 123

    # special "object iterators" should return objects
    item = Item(123, hs, {'kids': [123, 456, 789], 'parts': [987, 654, 321]})
    for kid in item.kids():
        assert isinstance(kid, Item)
    for part in item.parts():
        assert isinstance(part, Item)

    # special "object attributes" should return objects
    item = Item(123, hs, {'by': 'magicspiders', 'parent': 456, 'poll': 789})
    assert isinstance(item.parent, Item)
    assert isinstance(item.poll, Item)
    assert isinstance(item.by, User)
Exemple #2
0
def test_equality():
    story_int = Item(123, None)
    story_str = Item("123", None)

    assert story_int == story_int
    assert story_str == story_str

    assert story_int == 123
    assert story_int == "123"

    assert story_str == "123"
    assert story_str == 123

    assert story_str == story_int
Exemple #3
0
def test_content():
    no_content = Item(123, None, data={'dummy': ''})
    assert no_content.content == ''

    only_title = Item(123, None, data={'title': 'hello'})
    assert only_title.content == 'hello'

    title_and_url = Item(123,
                         None,
                         data={
                             'title': 'hello',
                             'url': 'https://github.com'
                         })
    assert title_and_url.content == 'https://github.com'

    all_three = Item(123,
                     None,
                     data={
                         'title': 'hello',
                         'url': 'https://github.com',
                         'text': 'main'
                     })
    assert all_three.content == 'main'
Exemple #4
0
def test_escape():
    item = Item(123, None, {'text': '<3'})
    assert item.text == '<3'
Exemple #5
0
def test_iterators():
    def len_iteration(thing):
        count = 0
        for _ in thing:
            count += 1
        return count

    item = Item(123, hs, {'kids': [1, 2, 3]})
    assert len_iteration(item.kids()) == 3
    assert len_iteration(item.kids(limit=1)) == 1
    assert len_iteration(item.kids(limit=0)) == 0
    assert len_iteration(item.kids(limit=-1)) == 0
    assert len_iteration(item.kids(limit=5)) == 3
    assert len_iteration(item.kids(3)) == 3

    item = Item(123, hs, {'parts': [1, 2, 3]})
    assert len_iteration(item.parts()) == 3
    assert len_iteration(item.parts(limit=1)) == 1
    assert len_iteration(item.parts(limit=0)) == 0
    assert len_iteration(item.parts(limit=-1)) == 0
    assert len_iteration(item.parts(limit=5)) == 3
    assert len_iteration(item.parts(3)) == 3
Exemple #6
0
def test_repr():
    item = Item(123, None)
    assert repr(item) == 'Item(123)'
Exemple #7
0
def test_link():
    story1 = Item(16071290, None)
    story2 = Item('16071290', None)

    assert story1.link == 'https://news.ycombinator.com/item?id=16071290' == story2.link