-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad6be38
commit 54b0a37
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from datetime import datetime | ||
|
||
from structlog import get_logger | ||
|
||
log = get_logger() | ||
|
||
|
||
class Post(object): | ||
"""Represents scheduled Reddit post.""" | ||
|
||
def __init__( | ||
self, | ||
submit_at: datetime, | ||
subreddit: str, | ||
title: str, | ||
body: str, | ||
) -> None: | ||
"""Initialize Post.""" | ||
log.info( | ||
"post_create", | ||
submit_at=str(submit_at), | ||
subreddit=subreddit, | ||
title=title, | ||
) | ||
|
||
if not all([submit_at, subreddit, title, body]): | ||
raise AttributeError("All Post fields must be set.") | ||
|
||
self.submit_at = submit_at | ||
self.subreddit = subreddit | ||
self.title = title | ||
self.body = body | ||
|
||
def __eq__(self, other: object) -> bool: | ||
"""Compare this instance to other object.""" | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
|
||
return ( | ||
self.submit_at, | ||
self.subreddit, | ||
self.title, | ||
self.body, | ||
) == ( | ||
other.submit_at, | ||
other.subreddit, | ||
other.title, | ||
other.body, | ||
) | ||
|
||
def __str__(self): | ||
"""Return string representation of this instance.""" | ||
return "/r/{subreddit} Post at {datetime}: {title}".format( | ||
subreddit=self.subreddit, | ||
datetime=self.submit_at, | ||
title=self.title, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from slow_start_rewatch.post import Post | ||
|
||
|
||
def test_create(): | ||
"""Test creating a Post instance.""" | ||
post = Post( | ||
submit_at=datetime(2018, 1, 6, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 1 Discussion", | ||
body="*Slow Start*, Episode 1: The First Butterflies", | ||
) | ||
|
||
assert post.submit_at == datetime(2018, 1, 6, 12, 0, 0) | ||
assert post.subreddit == "anime" | ||
assert post.title == "Slow Start - Episode 1 Discussion" | ||
assert post.body == "*Slow Start*, Episode 1: The First Butterflies" | ||
|
||
|
||
def test_create_with_empty_field(): | ||
"""Test that the Post cannot be instantiated with empty attributes.""" | ||
with pytest.raises(AttributeError): | ||
Post( | ||
submit_at=datetime(2018, 1, 6, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 1 Discussion", | ||
body=None, # type: ignore | ||
) | ||
|
||
|
||
def test_comparison(): | ||
"""Test the comparison of Post objects.""" | ||
post = Post( | ||
submit_at=datetime(2018, 1, 6, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 1 Discussion", | ||
body="*Slow Start*, Episode 1: The First Butterflies", | ||
) | ||
|
||
identical_post = Post( | ||
submit_at=datetime(2018, 1, 6, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 1 Discussion", | ||
body="*Slow Start*, Episode 1: The First Butterflies", | ||
) | ||
|
||
different_post = Post( | ||
submit_at=datetime(2018, 1, 13, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 2 Discussion", | ||
body="*Slow Start*, Episode 2: Exercise Wears Me Out ", | ||
) | ||
|
||
assert post == identical_post | ||
assert post != different_post | ||
assert post.__eq__("post") is NotImplemented # noqa: WPS609 | ||
|
||
|
||
def test_string_representation(): | ||
"""Test the string representation of a Post instance.""" | ||
post = Post( | ||
submit_at=datetime(2018, 1, 6, 12, 0, 0), | ||
subreddit="anime", | ||
title="Slow Start - Episode 1 Discussion", | ||
body="*Slow Start*, Episode 1: The First Butterflies", | ||
) | ||
|
||
assert "/r/anime Post at 2018-01-06 12:00:00: Slow Start" in str(post) |