test_parser.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, tweet_test_data_factory, tweet_type):
  23. t, tweet_info = tweet_test_data_factory(tweet_type)
  24. for field, value in tweet_info._asdict().items():
  25. # It would be rather complicated to keep some test fixtures values
  26. # accurate (e.g. number of likes, retweets, ...) so for most
  27. # of them, the expected values are not set on purpose and therefore
  28. # not tested
  29. if value is not None:
  30. assert getattr(t, field) == value
  31. @pytest.mark.parametrize("tweet_type,expected_class", [
  32. ('plain', TweetTextOnly),
  33. ('reaction_tweet', TweetReaction),
  34. ('with_link', TweetLink),
  35. ('retweet', TweetRetweet)
  36. ])
  37. def test_tweet_type(self, tweet_test_data_factory, tweet_type, expected_class):
  38. t, tweet_info = tweet_test_data_factory(tweet_type)
  39. assert isinstance(t, expected_class)
  40. class TestUser:
  41. all_handles = [
  42. "Marlene_beadles"
  43. ]
  44. @pytest.mark.parametrize("user_handle", all_handles)
  45. def test_user(self, raw_html_user_initial_page_factory, user_collection, user_handle):
  46. user_info = user_collection[user_handle]
  47. raw_user = raw_html_user_initial_page_factory(user_handle)
  48. user = user_factory(raw_user)
  49. for field, value in user_info._asdict().items():
  50. assert getattr(user, field) == value