test_parser.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. "mentions",
  19. "stats",
  20. ])
  21. def test_plain_tweet(self, raw_tweet_factory, tweet_collection, tweet_type):
  22. tweet_info = tweet_collection[tweet_type]
  23. raw = raw_tweet_factory(tweet_info)
  24. t = Tweet.extract(raw)
  25. assert t
  26. for field, value in tweet_info._asdict().items():
  27. # It would be rather complicated to keep some test fixtures values
  28. # accurate (e.g. number of likes, retweets, ...) so for most
  29. # of them, the expected values are not set on purpose and therefore
  30. # not tested
  31. if value is not None:
  32. assert getattr(t, field) == value