def test_post_model_init(): id_ = 1 post = Post(id=id_, title="title", body="body") assert post.id == id_ assert post.title == "title" assert post.body == "body" assert post.comments == []
def domain_posts(): posts = [ { "id": 1, "title": "title one", "body": "body one" }, { "id": 2, "title": "title two", "body": "body two" }, { "id": 3, "title": "title three", "body": "body three" }, { "id": 4, "title": "title four", "body": "body four" }, { "id": 5, "title": "title five", "body": "body five" }, { "id": 6, "title": "title six", "body": "body six" }, ] return [Post.from_dict(post) for post in posts]
def create_post(self, adict): next_id = self._get_max_id("posts") + 1 adict["id"] = next_id self._data["posts"].append(adict) return Post.from_dict(adict)
def to_entity(self): return Post.from_dict({ "id": self.id, "title": self.title, "body": self.body, "comments": [c.to_dict() for c in self.comments], })
def get_post_item(self, id): filtered = list(filter(lambda d: d["id"] == id, self._data["posts"])) if not filtered: return None post_dict = filtered[0] post_dict["comments"] = list( filter(lambda d: d["post_id"] == id, self._data["comments"])) return Post.from_dict(post_dict)
def test_repository_post_item(data_dicts): repo = MemRepo(data_dicts) posts = [Post.from_dict(d) for d in data_dicts["posts"] if d["id"] == 1] posts_1 = posts[0] posts_1.comments = [ Comment.from_dict(d) for d in data_dicts["comments"] if d["post_id"] == 1 ] resp = repo.get_post_item(1) assert resp == posts_1
def test_serialize_domain_post(): post = Post(id=1, title="title", body="body text") expected_json = """ { "id": 1, "title": "title", "body": "body text" } """ json_post = PostSchema(exclude=("comments", )).dump(post).data assert json_post == json.loads(expected_json)
def test_post_model_init_with_comments(): id_ = 1 post = Post(id=id_, title="title", body="body", comments=[{ "id": 1, "body": "comment body" }]) assert post.id == id_ assert post.title == "title" assert post.body == "body" assert len(post.comments) == 1 assert isinstance(post.comments[0], Comment)
def test_serialize_domain_post_with_comments(): post = Post( id=1, title="title", body="body text", comments=[dict(id=1, body="body one"), dict(id=2, body="body two")], ) expected_json = """ { "id": 1, "title": "title", "body": "body text", "comments": [ { "id": 1, "body": "body one" }, { "id": 2, "body": "body two" } ] } """ json_post = PostSchema().dump(post).data assert json_post == json.loads(expected_json)
def test_repository_post_list_without_parameters(data_dicts): repo = MemRepo(data_dicts) posts = [Post.from_dict(d) for d in data_dicts["posts"]] assert repo.get_post_list() == posts
def test_post_model_comparison(): post_dict = {"id": 1, "title": "title", "body": "body"} post1 = Post.from_dict(post_dict) post2 = Post.from_dict(post_dict) assert post1 == post2
def test_post_model_to_dict(): post_dict = {"id": 1, "title": "title", "body": "body", "comments": []} post = Post.from_dict(post_dict) assert post.to_dict() == post_dict
def test_post_model_from_dict(): post = Post.from_dict({"id": 1, "title": "title", "body": "body"}) assert post.id == 1 assert post.title == "title" assert post.body == "body"
def get_post_list(self): result = [Post.from_dict(d) for d in self._data["posts"]] return result
def domain_posts(): posts = [{"id": 1, "title": "title one", "body": "body one"}] return [Post.from_dict(post) for post in posts]