def creates_cycle(connections, test):
    """Checks to see if adding the test connection to the network would create
    a cycle.

    Args:
        connections (list): A list of (node in key, node out key) pairs for each
            connection.
        test (tuple): A tuple of the form (node in key, node out key) that
            represents the connection trying to be added that needs to be
            tested.

    Returns:
        bool: True if 'test' creates a cycle, False otherwise.
    """
    node_in, node_out = test

    if node_in == node_out:
        # Self-loop
        return True

    visited = {node_out}
    while True:
        num_added = 0
        for a, b in connections:
            if a in visited and b not in visited:
                if b == node_in:
                    return True

                visited.add(b)
                num_added += 1

        if num_added == 0:
            return False