test_parser.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import pytest
  2. from twhatter.parser import *
  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, TweetBase)
  11. class TestTweet:
  12. all_types = [
  13. "plain",
  14. "reaction_tweet",
  15. "with_link",
  16. "retweet",
  17. "hashtags",
  18. "mentions",
  19. "stats",
  20. ]
  21. @pytest.mark.parametrize("tweet_type", all_types)
  22. def test_tweet(self, raw_tweet_factory, tweet_collection, tweet_type):
  23. tweet_info = tweet_collection[tweet_type]
  24. raw = raw_tweet_factory(tweet_info)
  25. t = TweetBase.extract(raw)
  26. assert t
  27. for field, value in tweet_info._asdict().items():
  28. # It would be rather complicated to keep some test fixtures values
  29. # accurate (e.g. number of likes, retweets, ...) so for most
  30. # of them, the expected values are not set on purpose and therefore
  31. # not tested
  32. if value is not None:
  33. assert getattr(t, field) == value
  34. @pytest.mark.parametrize("tweet_type,expected_class", [
  35. ('plain', TweetTextOnly),
  36. ('reaction_tweet', TweetReaction),
  37. ('with_link', TweetLink),
  38. ('retweet', TweetRetweet)
  39. ])
  40. def test_tweet_type(self, raw_tweet_factory, tweet_collection, tweet_type, expected_class):
  41. tweet_info = tweet_collection[tweet_type]
  42. raw = raw_tweet_factory(tweet_info)
  43. t = TweetBase.extract(raw)
  44. assert isinstance(t, expected_class)