Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added api tests #59

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion knewkarma/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

author: str = "Richard Mwewa"
about_author: str = "https://about.me/rly0nheart"
version: str = "3.3.0.0"
version: str = "3.3.1.0"

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #

Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "knewkarma"
version = "3.3.0.0"
version = "3.3.1.0"
description = "A Reddit Data Analysis Toolkit."
authors = ["Richard Mwewa <rly0nheart@duck.com>"]
readme = "README.md"
Expand All @@ -30,5 +30,11 @@ aiohttp = "*"
rich = "*"
rich-argparse = "*"

[tool.poetry.dev-dependencies]
pytest = "*"
pytest-asyncio = "*"
pytest-html = "*"


[tool.poetry.scripts]
knewkarma = "knewkarma._cli:execute"
11 changes: 11 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[pytest]
minversion =
6.0.2
testpaths =
tests/
python_files =
*.py
addopts =
-vvv
--html='reports/tests.html'
--self-contained-html
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# +++++++++++++++++++++++++++++++++++++++++++++++++++++ #

TEST_USERNAME: str = "AutoModerator"
TEST_USER_ID: str = "6l4z3"
TEST_USER_CREATED_TIMESTAMP: float = 1325741068.0

TEST_SUBREDDIT: str = "AskReddit"
TEST_SUBREDDIT_ID: str = "2qh1i"
TEST_SUBREDDIT_CREATED_TIMESTAMP: float = 1201233135.0


# +++++++++++++++++++++++++++++++++++++++++++++++++++++ #
129 changes: 129 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #

import aiohttp
import pytest

from conftest import (
TEST_USERNAME,
TEST_SUBREDDIT,
TEST_USER_ID,
TEST_USER_CREATED_TIMESTAMP,
TEST_SUBREDDIT_CREATED_TIMESTAMP,
TEST_SUBREDDIT_ID,
)
from knewkarma.api import get_profile, get_posts


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #


@pytest.mark.asyncio
async def test_get_profile():
async with aiohttp.ClientSession() as session:
# ------------------------------------------------------------- #

user_profile: dict = await get_profile(
profile_type="user_profile",
profile_source=TEST_USERNAME,
session=session,
)

assert user_profile.get("id") == TEST_USER_ID
assert user_profile.get("created") == TEST_USER_CREATED_TIMESTAMP

# ------------------------------------------------------------- #

subreddit_profile: dict = await get_profile(
profile_type="subreddit_profile",
profile_source=TEST_SUBREDDIT,
session=session,
)

assert subreddit_profile.get("id") == TEST_SUBREDDIT_ID
assert subreddit_profile.get("created") == TEST_SUBREDDIT_CREATED_TIMESTAMP


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #


@pytest.mark.asyncio
async def test_get_posts():
async with aiohttp.ClientSession() as session:
# ------------------------------------------------------------- #

user_posts: list = await get_posts(
posts_type="user_posts",
posts_source=TEST_USERNAME,
sort="all",
timeframe="all",
limit=100,
session=session,
)

assert isinstance(user_posts, list)
assert len(user_posts) == 100
assert user_posts[0].get("data").get("author") == TEST_USERNAME

# ------------------------------------------------------------- #

subreddit_posts: list = await get_posts(
posts_type="subreddit_posts",
posts_source=TEST_SUBREDDIT,
sort="top",
timeframe="week",
limit=200,
session=session,
)

assert isinstance(subreddit_posts, list)
assert len(subreddit_posts) == 200
assert subreddit_posts[0].get("data").get("subreddit") == TEST_SUBREDDIT

# ------------------------------------------------------------- #

listing_posts: list = await get_posts(
posts_type="listing_posts",
posts_source="best",
sort="hot",
timeframe="month",
limit=10,
session=session,
)

assert isinstance(listing_posts, list)
assert len(listing_posts) == 10
assert listing_posts[0].get("data").get("subreddit") == "best"

# ------------------------------------------------------------- #

search_posts: list = await get_posts(
posts_type="search_posts",
posts_source="covid-19",
sort="controversial",
timeframe="all",
limit=5,
session=session,
)

assert isinstance(search_posts, list)
assert len(search_posts) == 5
assert (
"covid-19" in search_posts[0].get("data").get("selftext").lower()
or search_posts[0].get("data").get("title").lower()
)

# ------------------------------------------------------------- #

front_page_posts: list = await get_posts(
posts_type="front_page_posts",
sort="top",
timeframe="hour",
limit=3,
session=session,
)

assert isinstance(front_page_posts, list)
assert len(front_page_posts) == 3


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #