test_db.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import pytest
  2. from twhatter.output import Database
  3. from twhatter.output.sqlalchemy import Tweet
  4. @pytest.fixture
  5. def db_url():
  6. return "sqlite://"
  7. @pytest.fixture(scope="function")
  8. def output(db_url):
  9. return Database(db_url)
  10. @pytest.fixture(scope="function")
  11. def session(output):
  12. session = output.session_maker()
  13. yield session
  14. session.close()
  15. @pytest.mark.parametrize("fixtures_file", [
  16. 'tests/fixtures/tweets/text_only_10.yaml',
  17. 'tests/fixtures/tweets/retweet_10.yaml',
  18. 'tests/fixtures/tweets/link_10.yaml',
  19. 'tests/fixtures/tweets/reaction_9.yaml',
  20. ], ids=[
  21. "TextOnly",
  22. "Retweet",
  23. "Link",
  24. "Reaction"
  25. ])
  26. def test_output_tweets_presence(capsys, tweets_factory, output, fixtures_file, session):
  27. testdata = [
  28. pytest.param('tests/fixtures/tweets/text_only_10.yaml', TweetTextOnly, id="text-only"),
  29. pytest.param('tests/fixtures/tweets/retweet_10.yaml', TweetRetweet, id="retweets"),
  30. pytest.param('tests/fixtures/tweets/link_10.yaml', TweetLink, id="link"),
  31. pytest.param('tests/fixtures/tweets/reaction_9.yaml', TweetReaction, id="reaction"),
  32. ]
  33. @pytest.mark.parametrize("fixtures_file, raw_class", testdata)
  34. def test_output_tweets_presence(tweets_factory, output, fixtures_file, session, raw_class):
  35. tweets = tweets_factory(fixtures_file)
  36. output.start()
  37. output.output_tweets(tweets)
  38. output.stop()
  39. for t in tweets:
  40. assert session.query(Tweet).filter(Tweet.id == t.id).one()
  41. @pytest.mark.parametrize("fixtures_file, raw_class", testdata)
  42. def test_output_tweets_twice(tweets_factory, output, fixtures_file, session, raw_class):
  43. tweets = tweets_factory(fixtures_file)
  44. output.start()
  45. output.output_tweets(tweets)
  46. output.stop()
  47. output.start()
  48. output.output_tweets(tweets)
  49. output.stop()
  50. for t in tweets:
  51. assert session.query(Tweet).filter(Tweet.id == t.id).one()