Skip to content

Commit

Permalink
Merge 6b83ac0 into a0fc745
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Jun 17, 2022
2 parents a0fc745 + 6b83ac0 commit 9c99c3d
Show file tree
Hide file tree
Showing 32 changed files with 579 additions and 595 deletions.
1 change: 1 addition & 0 deletions pytest.ini
Expand Up @@ -2,3 +2,4 @@
testpaths = tests
filterwarnings =
ignore::DeprecationWarning
asyncio_mode = auto
12 changes: 12 additions & 0 deletions tests/__init__.py
@@ -1 +1,13 @@
"""Async PRAW Test Suite."""


class HelperMethodMixin:
@staticmethod
async def async_list(async_generator):
"""Return a list from an async iterator."""
return [item async for item in async_generator]

@staticmethod
async def async_next(async_generator):
"""Return the next item from an async iterator."""
return await async_generator.__anext__()
13 changes: 2 additions & 11 deletions tests/integration/__init__.py
Expand Up @@ -7,10 +7,11 @@
import pytest

from asyncpraw import Reddit
from tests import HelperMethodMixin
from tests.conftest import vcr


class IntegrationTest(asynctest.TestCase):
class IntegrationTest(asynctest.TestCase, HelperMethodMixin):
"""Base class for Async PRAW integration tests."""

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,16 +73,6 @@ def set_up_record(self):
refresh_token=pytest.placeholders.refresh_token,
)

@staticmethod
async def async_list(async_generator):
"""Return a list from an async iterator."""
return [item async for item in async_generator]

@staticmethod
async def async_next(async_generator):
"""Return the next item from an async iterator."""
return await async_generator.__anext__()

def use_cassette(self, cassette_name=None, **kwargs):
"""Use a cassette. The cassette name is dynamically generated.
Expand Down
19 changes: 4 additions & 15 deletions tests/unit/__init__.py
@@ -1,17 +1,6 @@
"""PRAW Unit test suite."""
import asynctest
"""Async PRAW Unit test suite."""
from tests import HelperMethodMixin

from asyncpraw import Reddit


class UnitTest(asynctest.TestCase):
"""Base class for PRAW unit tests."""

async def setUp(self):
"""Setup runs before all test cases."""
self.reddit = Reddit(
client_id="dummy", client_secret="dummy", user_agent="dummy"
)
# Unit tests should never issue requests
await self.reddit.close()
self.reddit._core._requestor._http = None
class UnitTest(HelperMethodMixin):
"""Base class for Async PRAW unit tests."""
12 changes: 12 additions & 0 deletions tests/unit/conftest.py
@@ -0,0 +1,12 @@
import pytest

from asyncpraw import Reddit


@pytest.fixture
async def reddit():
async with Reddit(
client_id="dummy", client_secret="dummy", user_agent="dummy"
) as reddit:
# Unit tests should never issue requests
yield reddit
4 changes: 2 additions & 2 deletions tests/unit/models/listing/test_listing.py
Expand Up @@ -4,8 +4,8 @@


class TestModmailConversationsListing(UnitTest):
def test_empty_conversations_list(self):
def test_empty_conversations_list(self, reddit):
assert (
ModmailConversationsListing(self.reddit, _data={"conversations": []}).after
ModmailConversationsListing(reddit, _data={"conversations": []}).after
is None
)
42 changes: 21 additions & 21 deletions tests/unit/models/reddit/test_collections.py
Expand Up @@ -8,30 +8,30 @@


class TestCollection(UnitTest):
def test_eq(self):
def test_eq(self, reddit):
uuid = "fake_uuid"
permalink = f"https://reddit.com/r/subreddit/collection/{uuid}"
collection1 = Collection(self.reddit, collection_id=uuid)
collection2 = Collection(self.reddit, permalink=permalink)
collection1 = Collection(reddit, collection_id=uuid)
collection2 = Collection(reddit, permalink=permalink)
assert collection1 == collection2
assert collection2 == collection2
assert collection1 == collection1
assert uuid == collection1
assert uuid == collection2

def test_init(self):
def test_init(self, reddit):
uuid = "fake_uuid"
assert uuid == Collection(self.reddit, collection_id=uuid).collection_id
assert uuid == Collection(reddit, collection_id=uuid).collection_id
permalink = f"https://reddit.com/r/subreddit/collection/{uuid}"
assert uuid == Collection(self.reddit, permalink=permalink).collection_id
assert uuid == Collection(reddit, permalink=permalink).collection_id

def test_init_bad(self):
def test_init_bad(self, reddit):
with pytest.raises(TypeError):
Collection(self.reddit)
Collection(reddit)
with pytest.raises(TypeError):
Collection(self.reddit, _data=dict(), collection_id="")
Collection(reddit, _data=dict(), collection_id="")
with pytest.raises(TypeError):
Collection(self.reddit, collection_id="fake_uuid", permalink="")
Collection(reddit, collection_id="fake_uuid", permalink="")
with pytest.raises(TypeError):
Collection(
None,
Expand All @@ -40,30 +40,30 @@ def test_init_bad(self):
permalink="https://reddit.com/r/sub/collection/fake_uuid",
)
with pytest.raises(ValueError):
Collection(self.reddit, collection_id="")
Collection(reddit, collection_id="")
with pytest.raises(ValueError):
Collection(self.reddit, permalink="")
Collection(reddit, permalink="")

def test_repr(self):
collection = Collection(self.reddit, collection_id="fake_uuid")
def test_repr(self, reddit):
collection = Collection(reddit, collection_id="fake_uuid")
assert "Collection(collection_id='fake_uuid')" == repr(collection)

def test_str(self):
collection = Collection(self.reddit, collection_id="fake_uuid")
def test_str(self, reddit):
collection = Collection(reddit, collection_id="fake_uuid")
assert "fake_uuid" == str(collection)

def test_neq(self):
collection1 = Collection(self.reddit, collection_id="1")
collection2 = Collection(self.reddit, collection_id="2")
def test_neq(self, reddit):
collection1 = Collection(reddit, collection_id="1")
collection2 = Collection(reddit, collection_id="2")
assert collection1 != collection2
assert "1" != collection2
assert "2" != collection1


class TestSubredditCollections(UnitTest):
async def test_call(self):
async def test_call(self, reddit):
collections = SubredditCollections(
self.reddit, await self.reddit.subreddit("placeholder")
reddit, await reddit.subreddit("placeholder")
)
with pytest.raises(TypeError):
await collections()
Expand Down
52 changes: 26 additions & 26 deletions tests/unit/models/reddit/test_comment.py
Expand Up @@ -7,14 +7,14 @@


class TestComment(UnitTest):
def test_attribute_error(self):
def test_attribute_error(self, reddit):
with pytest.raises(AttributeError):
Comment(self.reddit, _data={"id": "1"}).mark_as_read()
Comment(reddit, _data={"id": "1"}).mark_as_read()

def test_equality(self):
comment1 = Comment(self.reddit, _data={"id": "dummy1", "n": 1})
comment2 = Comment(self.reddit, _data={"id": "Dummy1", "n": 2})
comment3 = Comment(self.reddit, _data={"id": "dummy3", "n": 2})
def test_equality(self, reddit):
comment1 = Comment(reddit, _data={"id": "dummy1", "n": 1})
comment2 = Comment(reddit, _data={"id": "Dummy1", "n": 2})
comment3 = Comment(reddit, _data={"id": "dummy3", "n": 2})
assert comment1 == comment1
assert comment2 == comment2
assert comment3 == comment3
Expand All @@ -24,41 +24,41 @@ def test_equality(self):
assert "dummy1" == comment1
assert comment2 == "dummy1"

def test_construct_failure(self):
def test_construct_failure(self, reddit):
message = "Exactly one of `id`, `url`, or `_data` must be provided."
with pytest.raises(TypeError) as excinfo:
Comment(self.reddit)
Comment(reddit)
assert str(excinfo.value) == message

with pytest.raises(TypeError) as excinfo:
Comment(self.reddit, id="dummy", url="dummy")
Comment(reddit, id="dummy", url="dummy")
assert str(excinfo.value) == message

with pytest.raises(TypeError) as excinfo:
Comment(self.reddit, "dummy", _data={"id": "dummy"})
Comment(reddit, "dummy", _data={"id": "dummy"})
assert str(excinfo.value) == message

with pytest.raises(TypeError) as excinfo:
Comment(self.reddit, url="dummy", _data={"id": "dummy"})
Comment(reddit, url="dummy", _data={"id": "dummy"})
assert str(excinfo.value) == message

with pytest.raises(TypeError) as excinfo:
Comment(self.reddit, "dummy", "dummy", {"id": "dummy"})
Comment(reddit, "dummy", "dummy", {"id": "dummy"})
assert str(excinfo.value) == message

with pytest.raises(ValueError):
Comment(self.reddit, "")
Comment(reddit, "")
with pytest.raises(ValueError):
Comment(self.reddit, url="")
Comment(reddit, url="")

def test_construct_from_url(self):
def test_construct_from_url(self, reddit):
url = "https://reddit.com/comments/2gmzqe/_/cklhv0f/"
assert Comment(self.reddit, url=url) == "cklhv0f"
assert Comment(reddit, url=url) == "cklhv0f"

def test_hash(self):
comment1 = Comment(self.reddit, _data={"id": "dummy1", "n": 1})
comment2 = Comment(self.reddit, _data={"id": "Dummy1", "n": 2})
comment3 = Comment(self.reddit, _data={"id": "dummy3", "n": 2})
def test_hash(self, reddit):
comment1 = Comment(reddit, _data={"id": "dummy1", "n": 1})
comment2 = Comment(reddit, _data={"id": "Dummy1", "n": 2})
comment3 = Comment(reddit, _data={"id": "dummy3", "n": 2})
assert hash(comment1) == hash(comment1)
assert hash(comment2) == hash(comment2)
assert hash(comment3) == hash(comment3)
Expand Down Expand Up @@ -94,16 +94,16 @@ def test_id_from_url__invalid_urls(self):
with pytest.raises(ClientException):
Comment.id_from_url(url)

def test_repr(self):
comment = Comment(self.reddit, id="dummy")
def test_repr(self, reddit):
comment = Comment(reddit, id="dummy")
assert repr(comment) == "Comment(id='dummy')"

def test_str(self):
comment = Comment(self.reddit, _data={"id": "dummy"})
def test_str(self, reddit):
comment = Comment(reddit, _data={"id": "dummy"})
assert str(comment) == "dummy"

def test_unset_hidden_attribute_does_not_fetch(self):
comment = Comment(self.reddit, _data={"id": "dummy"})
def test_unset_hidden_attribute_does_not_fetch(self, reddit):
comment = Comment(reddit, _data={"id": "dummy"})
assert comment._fetched
with pytest.raises(AttributeError):
comment._ipython_canary_method_should_not_exist_

0 comments on commit 9c99c3d

Please sign in to comment.