test_parser.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "media",
  21. ]
  22. @pytest.mark.parametrize("tweet_type", all_types)
  23. def test_tweet(self, tweet_test_data_factory, tweet_type):
  24. t, tweet_info = tweet_test_data_factory(tweet_type)
  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 value == getattr(t, field)
  32. @pytest.mark.parametrize("tweet_type,expected_class", [
  33. ('plain', TweetTextOnly),
  34. ('reaction_tweet', TweetReaction),
  35. ('with_link', TweetLink),
  36. ('retweet', TweetRetweet)
  37. ])
  38. def test_tweet_type(self, tweet_test_data_factory, tweet_type, expected_class):
  39. t, tweet_info = tweet_test_data_factory(tweet_type)
  40. assert isinstance(t, expected_class)
  41. @pytest.mark.parametrize("media_type,expected_class", [
  42. ('media', MediaImage),
  43. ])
  44. def test_media_type(self, tweet_test_data_factory, media_type, expected_class):
  45. t, tweet_info = tweet_test_data_factory(media_type)
  46. assert isinstance(t.media, expected_class)
  47. class TestUser:
  48. all_handles = [
  49. "Marlene_beadles",
  50. "the_english_way"
  51. ]
  52. @pytest.mark.parametrize("user_handle", all_handles)
  53. def test_user(self, raw_html_user_initial_page_factory, user_collection, user_handle):
  54. user_info = user_collection[user_handle]
  55. raw_user = raw_html_user_initial_page_factory(user_handle)
  56. user, = ParserUser(raw_user)
  57. for field, value in user_info._asdict().items():
  58. # It would be rather complicated to keep some test fixtures values
  59. # accurate (e.g. number of likes, retweets, ...) so for most
  60. # of them, the expected values are not set on purpose and therefore
  61. # not tested
  62. if value is not None:
  63. assert value == getattr(user, field)