conftest.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import yaml
  2. import pytest
  3. from twhatter.parser import TweetBase, User
  4. @pytest.fixture(scope="session")
  5. def fixtures_factory():
  6. """Factory for any kind of data that can be stored in YAML format"""
  7. def _fixtures_factory(yaml_file):
  8. with open(yaml_file, 'r') as f:
  9. fixtures = yaml.load(f)
  10. return fixtures
  11. return _fixtures_factory
  12. @pytest.fixture(scope="session")
  13. def tweets_factory(fixtures_factory):
  14. """Factory for tweets from YAML file"""
  15. def _tweets_factory(yaml_file):
  16. all_fixtures = fixtures_factory(yaml_file)
  17. return [t for t in all_fixtures if isinstance(t, TweetBase)]
  18. return _tweets_factory
  19. @pytest.fixture(scope="session")
  20. def users_factory(fixtures_factory):
  21. """Factory for tweets from YAML file"""
  22. def _users_factory(yaml_file):
  23. all_fixtures = fixtures_factory(yaml_file)
  24. return [u for u in all_fixtures if isinstance(u, User)]
  25. return _users_factory
  26. @pytest.fixture
  27. def timeline_mock_factory(tweets_factory, monkeypatch, timeline_attribute):
  28. def _timeline_mock_factory(tweets_file):
  29. tweets = tweets_factory(tweets_file)
  30. class MockClientTimeline:
  31. def __init__(self, user, limit):
  32. pass
  33. def __iter__(self):
  34. return iter(tweets)
  35. monkeypatch.setattr(timeline_attribute, MockClientTimeline)
  36. return _timeline_mock_factory
  37. @pytest.fixture
  38. def profile_mock_factory(users_factory, monkeypatch, profile_attribute):
  39. def _profile_mock_factory(user_file):
  40. users = users_factory(user_file)
  41. # Make a dictionary out of users, indexed by the twitter handle
  42. class MockClientProfile:
  43. def __init__(self, user):
  44. if user in users:
  45. self.user = users[user]
  46. monkeypatch.setattr(profile_attribute, MockClientProfile)
  47. return _profile_mock_factory