Skip to content

Commit

Permalink
Add class Post
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclav-2012 committed May 30, 2020
1 parent ad6be38 commit 54b0a37
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
We follow [Semantic Versions](https://semver.org/).


## Version 0.1.2 (unreleased)

- Add class `Post` - representing the Reddit post


## Version 0.1.1

- Configure logging with `structlog`
Expand Down
59 changes: 59 additions & 0 deletions slow_start_rewatch/post.py
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,
)
73 changes: 73 additions & 0 deletions tests/test_post.py
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)

0 comments on commit 54b0a37

Please sign in to comment.