test_parser.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pytest
  2. from twhatter.parser import TweetList, Tweet
  3. class TestTweetList:
  4. def test_len(self, raw_html_user_initial_page):
  5. t_list = TweetList(raw_html_user_initial_page)
  6. assert len(t_list) == 20
  7. def test_iter(self, raw_html_user_initial_page):
  8. t_list = TweetList(raw_html_user_initial_page)
  9. for t in t_list:
  10. assert isinstance(t, Tweet)
  11. class TestTweet:
  12. @pytest.mark.parametrize("tweet_type", [
  13. "plain",
  14. "reaction_tweet",
  15. "with_link",
  16. "retweet",
  17. "hashtags",
  18. "stats",
  19. ])
  20. def test_plain_tweet(self, raw_tweet_factory, tweet_collection, tweet_type):
  21. tweet_info = tweet_collection[tweet_type]
  22. raw = raw_tweet_factory(tweet_info)
  23. t = Tweet.extract(raw)
  24. assert t
  25. for field, value in tweet_info._asdict().items():
  26. # It would be rather complicated to keep some test fixtures values
  27. # accurate (e.g. number of likes, retweets, ...) so for most
  28. # of them, the expected values are not set on purpose and therefore
  29. # not tested
  30. if value is not None:
  31. assert getattr(t, field) == value