def test_unretweet(self): fake_user_id = uuid4() tweet = Tweet(uuid4(), "hello", uuid4(), set(), {fake_user_id}) tweet.unretweet(fake_user_id) assert fake_user_id not in tweet.retweeted_user_ids assert "retweeted_user_ids" in tweet.pending_changes
def test_dislike(self): fake_user_id = uuid4() tweet = Tweet(uuid4(), "hello", uuid4(), {fake_user_id}, set()) tweet.dislike(fake_user_id) assert fake_user_id not in tweet.liked_user_ids assert "liked_user_ids" in tweet.pending_changes
async def test_save(self, postgres_session): repo = PostgresTweetAggregateRepository(postgres_session) aggregate = Tweet.new("Hello", uuid4()) await repo.save(aggregate) print(await repo.find_by_id(aggregate.id))
def test_new(self): fake_user_id = uuid4() new_tweet = Tweet.new("hello world", fake_user_id) new_tweet = cast(Tweet, new_tweet) assert new_tweet.text == "hello world" assert new_tweet.tweeted_user_id == fake_user_id
async def find_by_id(self, tweet_id: UUID) -> Optional[TweetAggregate]: async with self.session.begin(): query = TweetTable.select().where(TweetTable.c.id == tweet_id) result = await self.session.execute(query) row = result.fetchone() if not row: return None aggregate = Tweet(row["id"], row["text"], row["tweeted_user_id"], set(row["liked_user_ids"]), set(row["retweeted_user_ids"])) return aggregate
def tweet(self): return Tweet(uuid4(), "hello", uuid4(), set(), set())
async def handle(self, command: PostTweet) -> None: new_tweet = Tweet.new(command.text, command.tweeted_user_id) await self.tweet_aggregate_repo.save(new_tweet) await self.event_publisher.publish( TweetPosted(new_tweet.id, command.tweeted_user_id))