conftest.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import pytest
  2. from datetime import datetime
  3. from click.testing import CliRunner
  4. from bs4 import BeautifulSoup
  5. from twhatter.api import ApiUser
  6. from twhatter.parser import TweetList
  7. from typing import NamedTuple
  8. @pytest.fixture
  9. def cli_runner():
  10. """Runner for Click"""
  11. return CliRunner()
  12. @pytest.fixture(scope="session")
  13. def user():
  14. return "the_english_way"
  15. @pytest.fixture(scope="session")
  16. def tweet_limit():
  17. return 10
  18. # Fixtures for extraction of specific tweets of several kinds, whose author
  19. # and id are known in advance
  20. class TweetInfo(NamedTuple):
  21. """Class to hold information about a tweet that is already known"""
  22. id: int
  23. screen_name: str
  24. user_id: int
  25. permalink: str
  26. timestamp: datetime = None
  27. text: str = None
  28. comments_nb: int = None
  29. retweets_nb: int = None
  30. likes_nb: int = None
  31. retweeter: str = None
  32. retweet_id: int = None
  33. reacted_id: int = None
  34. reacted_user_id: int = None
  35. link_to: str = None
  36. @pytest.fixture(scope="session")
  37. def tweet_collection():
  38. return {
  39. 'plain': TweetInfo(
  40. id=1077838164813848576,
  41. screen_name="the_english_way",
  42. user_id=943804775942033408,
  43. timestamp=datetime.utcfromtimestamp(1545811618),
  44. permalink="/the_english_way/status/1077838164813848576",
  45. text="""Ca y est j'ai un pipeline Concourse avec un job qui builde une image @Docker qui affiche un "Hello World" dans un autre job \o/
  46. ........... je suis pas sûr de savoir ce que ça veut dire, mais en tout cas c'était mon objectif de la matinée """
  47. ),
  48. 'reaction_tweet': TweetInfo(
  49. id=1078281840945963008,
  50. screen_name="the_english_way",
  51. user_id=943804775942033408,
  52. timestamp=datetime.utcfromtimestamp(1545917399),
  53. permalink="/the_english_way/status/1078281840945963008",
  54. reacted_id=1078277316193726464,
  55. reacted_user_id=19976004
  56. ),
  57. 'with_link': TweetInfo(
  58. id=1077505613079429120,
  59. screen_name="the_english_way",
  60. user_id=943804775942033408,
  61. timestamp=datetime.utcfromtimestamp(1545732331),
  62. permalink="/the_english_way/status/1077505613079429120",
  63. link_to="https://t.co/el5VJucLRz"
  64. ),
  65. 'retweet': TweetInfo(
  66. id=1055037291108974592,
  67. screen_name="Senficon",
  68. user_id=14861745,
  69. retweeter="the_english_way",
  70. retweet_id=1055098556300828672,
  71. timestamp=datetime.utcfromtimestamp(1540375466),
  72. permalink="/Senficon/status/1055037291108974592"
  73. ),
  74. 'stats': TweetInfo(
  75. id=1039969574555471873,
  76. screen_name="BurgerQuizOff",
  77. user_id=949604705772228608,
  78. permalink="/BurgerQuizOff/status/1039969574555471873",
  79. retweeter="the_english_way",
  80. comments_nb=12,
  81. retweets_nb=176,
  82. likes_nb=556
  83. ),
  84. }
  85. @pytest.fixture(scope="session")
  86. def raw_html_user_initial_page_factory():
  87. def _raw_html_user_initial_page(user):
  88. a = ApiUser(user)
  89. response = a.get_initial()
  90. return BeautifulSoup(response.text, "lxml")
  91. return _raw_html_user_initial_page
  92. @pytest.fixture(scope="session")
  93. def raw_html_user_initial_page(raw_html_user_initial_page_factory, user):
  94. return raw_html_user_initial_page_factory(user)
  95. @pytest.fixture(scope="session")
  96. def raw_tweet_factory(raw_html_user_initial_page_factory):
  97. def _raw_tweet_factory(tweet_info):
  98. user_page = tweet_info.retweeter or tweet_info.screen_name
  99. soup = raw_html_user_initial_page_factory(user_page)
  100. return soup.find(id="stream-item-tweet-{}".format(tweet_info.id))
  101. return _raw_tweet_factory