conftest.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # Name of the original author
  23. screen_name: str
  24. user_id: int
  25. # Name of the retweeter user
  26. retweeter: str = None
  27. @pytest.fixture(scope="session")
  28. def tweet_collection():
  29. return {
  30. 'plain': TweetInfo(
  31. id=1077838164813848576,
  32. screen_name="the_english_way",
  33. user_id=943804775942033408
  34. ),
  35. 'reaction_tweet': TweetInfo(
  36. id=1078281840945963008,
  37. screen_name="the_english_way",
  38. user_id=943804775942033408
  39. ),
  40. 'with_link': TweetInfo(
  41. id=1078281840945963008,
  42. screen_name="the_english_way",
  43. user_id=943804775942033408
  44. ),
  45. 'retweet': TweetInfo(
  46. id=1055037291108974592,
  47. screen_name="Senficon",
  48. user_id=14861745,
  49. retweeter="the_english_way"
  50. )
  51. }
  52. @pytest.fixture(scope="session")
  53. def raw_html_user_initial_page_factory():
  54. def _raw_html_user_initial_page(user):
  55. a = ApiUser(user)
  56. response = a.get_initial()
  57. return BeautifulSoup(response.text, "lxml")
  58. return _raw_html_user_initial_page
  59. @pytest.fixture(scope="session")
  60. def raw_html_user_initial_page(raw_html_user_initial_page_factory, user):
  61. return raw_html_user_initial_page_factory(user)
  62. @pytest.fixture(scope="session")
  63. def raw_tweet_factory(raw_html_user_initial_page_factory):
  64. def _raw_tweet_factory(tweet_info):
  65. user_page = tweet_info.retweeter or tweet_info.screen_name
  66. soup = raw_html_user_initial_page_factory(user_page)
  67. return soup.find(id="stream-item-tweet-{}".format(tweet_info.id))
  68. return _raw_tweet_factory