def test_inserting_inserts(): # Gittip's fundamental context manager for testing is gittip.testing.load. # It's called that because its primary function is to load data into the # database. When the context manager exits, the database is wiped. with testing.load() as context: # The context object gives you access to the database. The db attribute # here is the usual PostgresManager that is used throughout Gittip. context.db.execute("INSERT INTO participants VALUES ('foo')") # There's a dump method on context that gives you all the data in the # database, as a mapping of table names to mappings of id to row dict. actual = context.dump() # The context.diff method gives you a diff of the state of the database # since you entered the context. With compact=True it returns a mapping # of the names of tables that have changed, to a list of ints showing # the number of rows that have been inserted, updated, and deleted, # respectively. actual = context.diff(compact=True) # If the expectation can be stated succinctly, it's acceptable to # inline it in the assertion, rather than defining it separately. assert actual == {"participants": [1, 0, 0]}, actual
def test_payday_doesnt_move_money_from_a_suspicious_account(charge_on_balanced): charge_on_balanced.return_value = (Decimal('10.00'), Decimal('0.68'), None) tips = testing.setup_tips(('buz', 'bar', '6.00', True, True)) # under $10! with testing.load(*tips) as context: Payday(context.db).run() actual = context.diff(compact=True) assert actual == {"paydays": [1,0,0]}, actual
def participants(foo_starts_suspicious=None): participants = ( {"id": "foo"} , {"id": "bar", "is_admin": True} ) if foo_starts_suspicious is not None: participants[0]["is_suspicious"] = foo_starts_suspicious return load('participants', *participants)
def test_github_proxy(requests): requests.get().status_code = 200 requests.get().text = GITHUB_USER_UNREGISTERED_LGTEST with load(): expected = "<b>lgtest</b> has not joined" actual = serve_request('/on/github/lgtest/').body assert expected in actual, actual
def test_inserting_inserts(): # Gittip's fundamental context manager for testing is gittip.testing.load. # It's called that because its primary function is to load data into the # database. When the context manager exits, the database is wiped. with testing.load() as context: # The context object gives you access to the database. The db attribute # here is the usual PostgresManager that is used throughout Gittip. context.db.execute("INSERT INTO participants VALUES ('foo')") # There's a dump method on context that gives you all the data in the # database, as a mapping of table names to mappings of id to row dict. actual = context.dump() # The context.diff method gives you a diff of the state of the database # since you entered the context. With compact=True it returns a mapping # of the names of tables that have changed, to a list of ints showing # the number of rows that have been inserted, updated, and deleted, # respectively. actual = context.diff(compact=True) # If the expectation can be stated succinctly, it's acceptable to # inline it in the assertion, rather than defining it separately. assert actual == {"participants": [1,0,0]}, actual
def test_payday_doesnt_move_money_from_a_suspended_payin_account(charge_on_balanced): charge_on_balanced.return_value = (Decimal('10.00'), Decimal('0.68'), None) tips = testing.setup_tips(('buz', 'bar', '6.00', True, True)) # under $10! with testing.load(*tips) as context: Payday(context.db).run() actual = context.diff(compact=True) assert actual == {"paydays": [1,0,0]}, actual
def test_payday_moves_money(charge_on_balanced): charge_on_balanced.return_value = (Decimal('10.00'), Decimal('0.68'), None) tips = testing.setup_tips(('buz', 'bar', '6.00', True)) # under $10! with testing.load(*tips) as context: Payday(context.db).run() expected = [ {"id": "buz", "balance": Decimal('3.32')} , {"id": "bar", "balance": Decimal('6.00')} ] actual = context.diff()['participants']['updates'] assert actual == expected, actual
def test_payday_does_stuff(charge_on_balanced): charge_on_balanced.return_value = (Decimal('10.00'), Decimal('0.68'), None) tips = testing.setup_tips(('buz', 'bar', '6.00', True)) # under $10! with testing.load(*tips) as context: Payday(context.db).run() expected = { 'exchanges': [1, 0, 0] , 'participants': [0, 2, 0] , 'paydays': [1, 0, 0] , 'transfers': [1, 0, 0] } actual = context.diff(compact=True) assert actual == expected, actual
def test_stats_description_accurate_outside_of_payday(mock_datetime): """Test stats page outside of the payday running""" with testing.load() as context: a_monday = datetime(2012, 8, 6, 12, 00, 01) mock_datetime.utcnow.return_value = a_monday pd = Payday(context.db) pd.start() body = get_stats_page() assert "is ready for <b>this Thursday</b>" in body, body pd.end()
def test_stats_description_accurate_during_payday_run(mock_datetime): """Test that stats page takes running payday into account. This test was originally written to expose the fix required for https://github.com/whit537/www.gittip.com/issues/92. """ with testing.load() as context: a_thursday = datetime(2012, 8, 9, 12, 00, 01) mock_datetime.utcnow.return_value = a_thursday wireup.billing() pd = Payday(context.db) pd.start() body = get_stats_page() assert "is changing hands <b>right now!</b>" in body, body pd.end()
def test_something_changes_something(): # The testing.load callable takes a data definition as positional # arguments. {str,unicode} is interpreted as a table name, and {dict,list, # tuple} is interpreted as a row of data to be inserted into the most # recently named table. Generally you'll end up defining "data" and then # calling testing.load(*data), as it won't fit on one line. with testing.load("participants", ("foo",)) as context: context.db.execute("UPDATE participants SET statement='BLAM!!!' " "WHERE id='foo'") # Calling context.diff without compact=True gives you a mapping of the # names of tables that have changed to a mapping with keys 'inserts', # 'updates', and 'deletes'. The values for inserts and deletes are # lists of row dicts containing the new and old data, respectively. The # value for updates is a list of dicts containing only the data that # has changed (and the primary key). expected = {"id": "foo", "statement": "BLAM!!!"} actual = context.diff()['participants']['updates'][0] assert actual == expected, actual
def test_something_changes_something(): # The testing.load callable takes a data definition as positional # arguments. {str,unicode} is interpreted as a table name, and {dict,list, # tuple} is interpreted as a row of data to be inserted into the most # recently named table. Generally you'll end up defining "data" and then # calling testing.load(*data), as it won't fit on one line. with testing.load("participants", ("foo", )) as context: context.db.execute("UPDATE participants SET statement='BLAM!!!' " "WHERE id='foo'") # Calling context.diff without compact=True gives you a mapping of the # names of tables that have changed to a mapping with keys 'inserts', # 'updates', and 'deletes'. The values for inserts and deletes are # lists of row dicts containing the new and old data, respectively. The # value for updates is a list of dicts containing only the data that # has changed (and the primary key). expected = {"id": "foo", "statement": "BLAM!!!"} actual = context.diff()['participants']['updates'][0] assert actual == expected, actual
def test_twitter_proxy(): with load(): expected = "<b>Twitter</b> has not joined" actual = serve_request('/on/twitter/twitter/').body assert expected in actual, actual
def participant(participant_id): # This is a context wrapper. """Wrap testing.load to install a participant. """ context = testing.load("participants", (participant_id, )) return context
def test_unsuspend_is_a_noop_when_not_payin_suspended(): with load('participants', ('foo',)) as context: Participant('foo').unsuspend_payin() actual = context.diff(compact=True) assert actual == {}, actual
def foo_user(): return testing.load("participants", ("foo",))
def participant(participant_id): # This is a context wrapper. """Wrap testing.load to install a participant. """ context = testing.load("participants", (participant_id,)) return context
def test_github_proxy(): with load(): expected = "<b>lgtest</b> has not joined" actual = serve_request('/on/github/lgtest/').body assert expected in actual, actual
def test_devnet_proxy(): with load(): expected = "<b>DevNet</b> has not joined" actual = serve_request('/on/devnet/devnet/').body assert expected in actual, actual
def test_unsuspend_changes_one_thing_only(): with load('participants', {'id': 'foo', 'payin_suspended': True}) as context: Participant('foo').unsuspend_payin() actual = context.diff(compact=True) assert actual == {'participants': [0,1,0]}, actual
def test_unsuspend_unsuspends(): with load('participants', {'id': 'foo', 'payin_suspended': True}) as context: Participant('foo').unsuspend_payin() actual = context.diff()['participants']['updates'][0]['payin_suspended'] assert actual is False, actual
def participants(foo_starts_suspicious=None): participants = ({"id": "foo"}, {"id": "bar", "is_admin": True}) if foo_starts_suspicious is not None: participants[0]["is_suspicious"] = foo_starts_suspicious return load('participants', *participants)
def test_participants_start_out_unpayin_suspended(): with load('participants', ('foo',)) as context: actual = context.dump()['participants']['foo']['payin_suspended'] assert actual is False, actual
def test_suspend_is_a_noop_when_payin_suspended(): with load('participants', {'id': 'foo', 'payin_suspended': True}) as context: Participant('foo').suspend_payin() actual = context.diff(compact=True) assert actual == {}, actual
def test_suspend_suspends(): with load('participants', ('foo',)) as context: Participant('foo').suspend_payin() actual = context.diff()['participants']['updates'][0]['payin_suspended'] assert actual is True, actual
def setup(*a): return testing.load(*testing.setup_tips(*a))