def test_newspost_eq(): newspost1 = NewsPost(_id="1", title="title", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") newspost2 = NewsPost(_id="1", title="title", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") newspost3 = NewsPost(_id="1", title="title2", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") assert newspost1 == newspost2 assert newspost1 != newspost3
def retrieve_newsposts( self, where: str, where_values: tuple = None, group_by: str = None, order_by: str = None, limit: int = None) -> Generator[NewsPost, None, None]: """Returns a generator of all newsposts from the database matching the given WHERE clause.""" fetched_rows = self.retrieve_table_data( table="newsposts", where=where, where_values=where_values, selection= "id, title, preview, author_id, author_name, slug, image_url", group_by=group_by, order_by=order_by, limit=limit) for row in (fetched_rows or []): _id = row[0] title = row[1] preview = row[2] author = self.retrieve_user("id=%s", (row[3], )) author_name = row[4] if not author: author = User(_id=None, name=author_name) slug = row[5] image_url = row[6] yield NewsPost(_id, title, preview, author, slug, image_url)
def test_newspost_hash(): newspost = NewsPost(_id="1", title="title", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") assert hash(newspost)
def test_newspost_eq_different_types(): newspost = NewsPost(_id="1", title="title", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") assert newspost != "string"
def test_newspost(): newspost = NewsPost(_id=1, title="title", preview="hello there", author=User(1, "someone"), slug="newspost-1", image_url="/image") assert newspost.id == 1 assert newspost.title == "title" assert newspost.preview == "hello there" assert newspost.author.id == 1 assert newspost.author.name == "someone" assert newspost.slug == "newspost-1" assert newspost.image_url == "/image"
def test_insert_retrieve_multiple_newsposts(test_database): author = User(_id=1, name="test") newspost1 = NewsPost(_id=1, title="title", preview="preview", author=author, slug="slug", image_url="image_url") newspost2 = NewsPost(_id=2, title="title2", preview="preview2", author=author, slug="slug2", image_url="image_url2") test_database.insert_newspost(newspost1) test_database.insert_newspost(newspost2) retrieved_newsposts = test_database.retrieve_newsposts( where="author_id=%s", where_values=(author.id, )) assert next(retrieved_newsposts, None) == newspost1 assert next(retrieved_newsposts, None) == newspost2
def parse_post_json(post_json: object) -> Event: """Returns an event representing the given news post json object (a single news post instance, for multiple see `parse_post_jsons`).""" author = User(name=post_json["author"].strip()) return Event(_type="news", time=from_string(post_json["published_at"]), newspost=NewsPost(_id=post_json["id"], title=post_json["title"], preview=post_json["preview"], author=author, slug=post_json["slug"], image_url=complete_image_url( post_json["first_image"])), user=author if author.id else None, content=post_json["preview"])
def test_insert_retrieve_newspost(test_database): author = User(1, name="test") newspost = NewsPost(_id=3, title="title", preview="preview", author=author, slug="slug", image_url="image_url") test_database.insert_newspost(newspost) retrieved_newspost = test_database.retrieve_newspost(where="id=%s", where_values=(3, )) assert retrieved_newspost.id == newspost.id assert retrieved_newspost.title == newspost.title assert retrieved_newspost.preview == newspost.preview assert retrieved_newspost.author.id == newspost.author.id assert retrieved_newspost.author.name == newspost.author.name assert retrieved_newspost.slug == newspost.slug assert retrieved_newspost.image_url == newspost.image_url assert retrieved_newspost == newspost
async def test_insert_retrieve_event_with_newspost(test_database): time = datetime.utcnow() author = User(1, name="test") newspost = NewsPost(_id=3, title="title", preview="preview", author=author, slug="slug", image_url="image_url") event = Event(_type="news", time=time, newspost=newspost, user=author) test_database.insert_event(event) retrieved_event = await test_database.retrieve_event("type=%s", ("news", )) assert retrieved_event.type == event.type assert retrieved_event.time == event.time assert retrieved_event.newspost == event.newspost assert retrieved_event.user == event.user assert retrieved_event.content == event.content assert retrieved_event == event