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_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]