Example #1
0
def test_insert_tweet():
    cursor = MagicMock()
    content = 'some interesting tweet'
    db.get_ops().insert_tweet(content, cursor)
    assert_query(cursor, db.TWEET_COLUMN_ORDER, (content, ), 'tweets',
                 'INSERT')
    assert_fetch_single(cursor)
Example #2
0
def test_create_retweet():
    cursor = MagicMock()
    server = 'some_server',
    ref = 44
    db.get_ops().create_retweet(server, ref, cursor)
    assert_query(cursor, db.TWEET_COLUMN_ORDER, ('retweet', f'{server}#{ref}'),
                 'tweets', 'INSERT')
Example #3
0
def test_modify_tweet():
    cursor = MagicMock()
    id_ = 42
    content = 'new tweet content'
    db.get_ops().modify_tweet(id_, content, cursor)
    assert_query(cursor, db.TWEET_COLUMN_ORDER, (content, id_), 'tweets',
                 'UPDATE', 'modified_at')
    assert_fetch_single(cursor)
Example #4
0
def test_update_node():
    cursor = MagicMock()
    name = 'node name'
    address = 'node address'
    db.get_ops().update_node(name, address, cursor)
    assert_query(cursor, db.NODE_COLUMN_ORDER, (name, address), 'nodes',
                 'UPDATE')
    assert_fetch_single(cursor)
Example #5
0
def test_insert_node():
    cursor = MagicMock()
    name = 'node_name'
    address = 'node address'
    db.get_ops().insert_node(name, address, cursor)
    assert_query(cursor, db.NODE_COLUMN_ORDER, (name, address), 'nodes',
                 'INSERT')
    assert_fetch_single(cursor)
Example #6
0
def get_all(db=None) -> List[Node]:
    if not db:
        db = get_db()
    """
    Returns list of all nodes.
    """
    return [Node(*args) for args in db.do(get_ops().get_all_nodes)]
Example #7
0
def test_delete_node():
    cursor = MagicMock()
    cursor.rowcount = 1
    name = 'node name'
    res = db.get_ops().delete_node(name, cursor)
    assert res is True
    assert_query(cursor, '', (name, ), 'nodes', 'DELETE')
Example #8
0
def search(content: str = None,
           from_created: datetime = None,
           to_created: datetime = None,
           from_modified: datetime = None,
           to_modified: datetime = None,
           retweet: bool = None,
           all: bool = False) -> List[Tweet]:
    """
    Performs search on tweets and returns list of results.
    If no parameters are provided, this will yield same results as
    listing tweets.

    :param content: Content to search in tweet.
    :param from_created: Start time for tweet creation.
    :param to_created: End time for tweet creation.
    :param from_modified: Start time for tweet modification.
    :param to_modified: End time for tweet modification.
    :param retweet:
        Flag indication if retweet or original tweets should be searched.
    :param all:
        Flag indicating if all nodes should be searched or only this one.
    :return: Result searching tweets.
    :rtype: [Tweet]
    """
    search_func = partial(get_ops().search_tweets, content, from_created,
                          to_created, from_modified, to_modified, retweet)
    res = [Tweet(*args) for args in get_db().do(search_func)]
    if all:
        others_res = search_others(content, from_created, to_created,
                                   from_modified, to_modified, retweet)
        res.extend(others_res)
    return res
Example #9
0
def test_delete_tweet():
    cursor = MagicMock()
    cursor.rowcount = 1
    id_ = 43
    res = db.get_ops().delete_tweet(id_, cursor)
    assert res is True
    assert_query(cursor, '', (id_, ), 'tweets', 'DELETE')
Example #10
0
def delete_all() -> bool:
    """
    Deletes all nodes from list of nodes.

    :return:
        Flag indicating if nodes were deleted. That might be false if the
        list was empty previously.
    """
    return get_db().do(get_ops().delete_all_nodes)
Example #11
0
def create(content):
    """
    Creates new tweet with provided content.

    :param str content: Tweet content.
    :return: Created tweet.
    :rtype: Tweet
    """
    check_length(content)
    return Tweet(*get_db().do(partial(get_ops().insert_tweet, content)))
Example #12
0
def test_search_tweets(params, expected_in_query, expected_in_params):
    all_args = {
        'content', 'from_created', 'to_created', 'from_modified',
        'to_modified', 'retweet'
    }
    for arg in all_args:
        if arg not in params:
            params[arg] = None

    cursor = MagicMock()
    params['cursor'] = cursor

    db.get_ops().search_tweets(**params)
    assert_query(cursor,
                 db.TWEET_COLUMN_ORDER,
                 expected_in_params,
                 'tweets',
                 more_query=expected_in_query)
    assert_fetch_all(cursor)
Example #13
0
def delete(id_):
    """
    Removes tweet with provided ID from database.
    :param id_: ID of tweet to delete.
    :raises NotFound: If tweet with provided ID was not found.
    """
    deleted = get_db().do(partial(get_ops().delete_tweet, id_))
    if not deleted:
        raise NotFound(f'Tweet with ID: {id_} not found.')
    return deleted
Example #14
0
def retweet(server, id_):
    """
    Creates retweet of original tweet.

    :param server: Server name that holds original tweet.
    :param id_: ID of tweet on original server.
    :return: Newly created tweet.
    :rtype: Tweet
    """
    return Tweet(*get_db().do(partial(get_ops().create_retweet, server, id_)))
Example #15
0
def delete(name: str) -> bool:
    """
    Deletes node from list of nodes.

    :param name: Name of node to delete.
    :return:
        Flag indicating if node was deleted. That might be false if node was
        not found.
    """
    return get_db().do(partial(get_ops().delete_node, name))
Example #16
0
def by_id(id_):
    """
    Returns tweet with specified ID.
    :param int id_: ID of tweet to get.
    :return: Tweet with requested ID.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    res = get_db().do(partial(get_ops().get_tweet, id_))
    if res is None:
        raise NotFound(f'Tweet with id: {id_} not found.')
    return Tweet(*res)
Example #17
0
def add(name: str, address: str, update: bool = False) -> Node:
    """
    Adds new node to list of nodes.

    :param name: Name of the node.
    :param address: Address of the node.
    :param update:
        Flag indicating if update of existing node should be done if node is
        already registered.
    :return: Newly created node.
    :raises Conflict:
        If node with same name already exists and update is False.
    """
    found = get_db().do(partial(get_ops().get_node, name))
    if found:
        if not update:
            raise Conflict('Node with same name already registered.')
        else:
            return Node(
                *get_db().do(partial(get_ops().update_node, name, address)))
    return Node(*get_db().do(partial(get_ops().insert_node, name, address)))
Example #18
0
def count(type_: str = None):
    """
    Returns number of tweets in database. If `separate` is True, two values
    are returned. First is number of original tweets and second is number of
    retweets.

    If `separate` if False (default) only one number is returned.

    :param type_:
        Type of tweets to count. Valid values are 'original' and 'retweet'.
    :return:
    """
    return get_db().do(partial(get_ops().count_tweets, type_))
Example #19
0
def modify(id_, content):
    """
    Modifies existing tweet with provided ID. New content will be set to
    whatever is provided.

    :param int id_: ID of tweet to modify.
    :param str content: New tweet content.
    :return: Modified tweet.
    :rtype: Tweet
    :raises NotFound: If tweet with provided ID was not found.
    """
    check_length(content)
    updated = get_db().do(partial(get_ops().modify_tweet, id_, content))
    if not updated:
        raise NotFound(f'Tweet for ID: {id_} not found.')
    return Tweet(*updated)
Example #20
0
def get_node(name: str) -> Node:
    """
    Returns list of all nodes.
    """
    return Node(*get_db().do(partial(get_ops().get_node, name)))
Example #21
0
def get_all():
    """
    Returns list of all tweets.
    :rtype: [Tweet]
    """
    return [Tweet(*args) for args in get_db().do(get_ops().get_all_tweets)]
Example #22
0
def test_get_all_nodes():
    cursor = MagicMock()
    db.get_ops().get_all_nodes(cursor)
    assert_query(cursor, db.NODE_COLUMN_ORDER, None, 'nodes')
    assert_fetch_all(cursor)
Example #23
0
def test_get_node():
    cursor = MagicMock()
    db.get_ops().get_node('node', cursor)
    assert_query(cursor, db.NODE_COLUMN_ORDER, ('node', ), 'nodes')
    assert_fetch_single(cursor)
Example #24
0
def test_get_all_tweets():
    cursor = MagicMock()
    db.get_ops().get_all_tweets(cursor)
    assert_query(cursor, db.TWEET_COLUMN_ORDER, None, 'tweets')
    assert_fetch_all(cursor)
Example #25
0
def test_count_tweets(type):
    cursor = MagicMock()
    db.get_ops().count_tweets(type, cursor)
    assert_query(cursor, '', tuple(filter(None, [type])), 'tweets')
    assert_fetch_single(cursor)
Example #26
0
def test_get_tweet():
    cursor = MagicMock()
    db.get_ops().get_tweet(33, cursor)
    assert_query(cursor, db.TWEET_COLUMN_ORDER, (33, ), 'tweets')
    assert_fetch_single(cursor)