conftest.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import pytest
  2. from click.testing import CliRunner
  3. from bs4 import BeautifulSoup
  4. from twhatter.api import ApiUser
  5. from twhatter.parser import TweetList
  6. from typing import NamedTuple
  7. @pytest.fixture
  8. def cli_runner():
  9. """Runner for Click"""
  10. return CliRunner()
  11. @pytest.fixture(scope="session")
  12. def user():
  13. return "the_english_way"
  14. @pytest.fixture(scope="session")
  15. def tweet_limit():
  16. return 10
  17. # Fixtures for extraction of specific tweets of several kinds, whose author
  18. # and id are known in advance
  19. class TweetInfo(NamedTuple):
  20. """Class to hold information about a tweet that is already known"""
  21. id: int
  22. screen_name: str
  23. user_id: int
  24. comments_nb: int = None
  25. retweets_nb: int = None
  26. likes_nb: int = None
  27. retweeter: str = None
  28. @pytest.fixture(scope="session")
  29. def tweet_collection():
  30. return {
  31. 'plain': TweetInfo(
  32. id=1077838164813848576,
  33. screen_name="the_english_way",
  34. user_id=943804775942033408
  35. ),
  36. 'reaction_tweet': TweetInfo(
  37. id=1078281840945963008,
  38. screen_name="the_english_way",
  39. user_id=943804775942033408
  40. ),
  41. 'with_link': TweetInfo(
  42. id=1078281840945963008,
  43. screen_name="the_english_way",
  44. user_id=943804775942033408
  45. ),
  46. 'retweet': TweetInfo(
  47. id=1055037291108974592,
  48. screen_name="Senficon",
  49. user_id=14861745,
  50. retweeter="the_english_way"
  51. ),
  52. 'stats': TweetInfo(
  53. id=1039969574555471873,
  54. screen_name="BurgerQuizOff",
  55. user_id=949604705772228608,
  56. retweeter="the_english_way",
  57. comments_nb=12,
  58. retweets_nb=176,
  59. likes_nb=556
  60. ),
  61. }
  62. @pytest.fixture(scope="session")
  63. def raw_html_user_initial_page_factory():
  64. def _raw_html_user_initial_page(user):
  65. a = ApiUser(user)
  66. response = a.get_initial()
  67. return BeautifulSoup(response.text, "lxml")
  68. return _raw_html_user_initial_page
  69. @pytest.fixture(scope="session")
  70. def raw_html_user_initial_page(raw_html_user_initial_page_factory, user):
  71. return raw_html_user_initial_page_factory(user)
  72. @pytest.fixture(scope="session")
  73. def raw_tweet_factory(raw_html_user_initial_page_factory):
  74. def _raw_tweet_factory(tweet_info):
  75. user_page = tweet_info.retweeter or tweet_info.screen_name
  76. soup = raw_html_user_initial_page_factory(user_page)
  77. return soup.find(id="stream-item-tweet-{}".format(tweet_info.id))
  78. return _raw_tweet_factory