def test_exception__invalid_username():
    """
    Test if `InvalidUsernameError` gets raised if passing
    an invalid username
    """
    with pytest.raises(exceptions.InvalidUsernameError):
        malaffinity.MALAffinity("ALDDJFfnjegenfmekfmejfefep9re9444")
Example #2
0
def test_population__normal():
    """
    Population via normal methods (passing `base_user`
    to `malaffinity.MALAffinity.__init__`
    """
    ma = malaffinity.MALAffinity(const.TEST_USERNAME)

    # Check it returned the correct amount of items
    assert len(ma._base_scores) == const.TEST_LIST_LENGTH
Example #3
0
def test_population__init():
    """
    Population via the `init` method in `malaffinity.MALAffinity`
    """
    ma = malaffinity.MALAffinity()

    ma.init(const.TEST_USERNAME)

    assert len(ma._base_scores) == const.TEST_LIST_LENGTH
def test_exception__no_rated_anime():
    """
    Test if `NoAffinityError` gets raised on a user
    that hasn't rated any anime
    """
    with pytest.raises(exceptions.NoAffinityError) as excinfo:
        malaffinity.MALAffinity(const.TEST_NO_RATED_USERNAME)

    assert "hasn't rated any anime" in str(excinfo.value).lower()
def test_exception__calculate_affinity_without_init():
    """
    Test if `Exception` gets raised when calculating
    affinity without `init`ing
    """
    ma = malaffinity.MALAffinity()

    # TODO: Change if custom exception made for this
    with pytest.raises(Exception) as excinfo:
        ma.calculate_affinity("DUMMY_USER")

    assert "no base user has been specified" in str(excinfo.value).lower()
def test_affinity__rounded_with_dummy():
    """
    Test affinity with DUMMY_LIST, but rounded
    """
    ma = malaffinity.MALAffinity(const.TEST_USERNAME, round=2)

    with mock.patch("malaffinity.endpoints.myanimelist") as MockClass:
        MockClass.return_value = mocks.mock_myanimelist_endpoint()

        affinity, shared = ma.calculate_affinity("DUMMY_USER")

        assert affinity == round(const.AFFINITY_WITH_DUMMY, 2)
        assert shared == const.SHARED_WITH_DUMMY
def test_exception__zero_stdev():
    """
    Test if `NoAffinityError` gets raised when the
    stdev of one set of scores is zero
    """
    ma = malaffinity.MALAffinity()

    ma._base_user = "******"
    ma._base_scores = mocks.dummy_list_to_base_scores()

    with mock.patch("malaffinity.endpoints.myanimelist") as MockClass:
        MockClass.return_value = mocks.mock_stdev_zero_myanimelist_endpoint()

        with pytest.raises(exceptions.NoAffinityError) as excinfo:
            ma.calculate_affinity("DUMMY_USER_2")

        assert "standard deviation of" in str(excinfo.value).lower()
def test_exception__not_enough_shared():
    """
    Test if `NoAffinityError` gets raised if shared rated
    anime count is below threshold
    """
    ma = malaffinity.MALAffinity()

    ma._base_user = "******"
    ma._base_scores = mocks.dummy_list_to_base_scores()

    with mock.patch("malaffinity.endpoints.myanimelist") as MockClass:
        MockClass.return_value = mocks.mock_mini_myanimelist_endpoint()

        with pytest.raises(exceptions.NoAffinityError) as excinfo:
            ma.calculate_affinity("DUMMY_USER_2")

        assert "shared rated anime count" in str(excinfo.value).lower()
def test_affinity__unrounded_with_dummy():
    """
    Test affinity with DUMMY_LIST
    """
    ma = malaffinity.MALAffinity(const.TEST_USERNAME, round=False)

    with mock.patch("malaffinity.endpoints.myanimelist") as MockClass:
        MockClass.return_value = mocks.mock_myanimelist_endpoint()

        affinity, shared = ma.calculate_affinity("DUMMY_USER")

        # Technically, we're supposed to test the *unrounded* affinity,
        # but because floating point shit is a thing, we'll just have
        # to make do with 10dp. It's not like anyone'll need
        # anything more accurate.
        assert round(affinity, 10) == round(const.AFFINITY_WITH_DUMMY, 10)
        assert shared == const.SHARED_WITH_DUMMY
        "password": mal["password"],
        # Stay authenticated (not sure if this does anything)
        "cookie": "1",
        # Why, Xinil?
        "submit": "1",
        "csrf_token": csrf
    })

# Check the request went well, raise an exception if not
if r.ok:
    print("Successfully authenticated with MAL")
else:
    raise Exception("Something happened")

# Set up the malaffinity thing now.
pearson = malaffinity.MALAffinity(mal["username"], round=1)


def get_comment_stream():
    # HACK: avoiding dodgy "local variable referenced before assignment"
    # bullshit I can't be bothered to deal with.
    reddit = praw.Reddit(**globals()["reddit"])

    return reddit.subreddit("anime").stream.comments()


def get_affinity_from_mal(username):
    resp = session.request("GET", mal_profile_url.format(username))

    # TODO: Handle this better.
    if not resp.ok: