def test_retrieving_limited_number_of_valid_donations(self): test_identifier = 'limited_donation_listing_test' updated_values = { 'identifier': test_identifier, 'title': 'Donation Limited Listing Test Event' } test_config = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=test_config) # Add a few donations donation_count = 10 values = range(1, donation_count + 1) for i in values: donation = Donation( amount=i, event_identifier=test_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # Retrieve the stored donations and confirm they are correct test_limit = 3 donations = private_api_calls.get_event_donations(event_identifier=test_identifier, limit=test_limit) assert test_limit == len(donations) for i in range(0, test_limit): donation = donations[i] assert isinstance(donation, Donation) assert values[-1] - i == donation.amount assert values[-1] - i == donation.timestamp assert test_identifier == donation.event_identifier
def test_updating_new_event(self): updated_values = { 'identifier': 'update_test', 'title': 'Update Test Event' } registration_test_configuration = get_test_event_configuration(updated_values=updated_values) response = private_api_calls.register_event(event_configuration=registration_test_configuration) assert True is response updated_values = { 'identifier': 'update_test', 'start_time': 500, 'end_time': 1000 } update_test_configuration = get_test_event_configuration(updated_values=updated_values) response = private_api_calls.update_event(event_configuration=update_test_configuration) assert True is response
def test_retrieving_largest_donation(self): largest_donation_identifier = 'largest_donation' updated_values = { 'identifier': largest_donation_identifier, 'title': 'Largest Donation Test Event' } largest_donation_test_configuration = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=largest_donation_test_configuration) # Add a number of donations donation_count = 5 for i in range(1, donation_count): donation = Donation( amount=i, event_identifier=largest_donation_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # add a large one then a small one donation = Donation( amount=500, event_identifier=largest_donation_identifier, timestamp=500) private_api_calls.register_donation(donation=donation) donation = Donation( amount=200, event_identifier=largest_donation_identifier, timestamp=200) private_api_calls.register_donation(donation=donation) # retrieve the largest donation largest = private_api_calls.get_latest_event_donation(event_identifier=largest_donation_identifier) assert isinstance(largest, Donation) assert 500 == largest.amount assert 500 == largest.timestamp
def test_retrieving_valid_time_filtered_donations(self): filtered_donations_identifier = 'filtered_donation_listing_test' updated_values = { 'identifier': filtered_donations_identifier, 'title': 'Filtered Donation Listing Test Event' } donation_listing_test_configuration = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=donation_listing_test_configuration) # Add a few donations donation_count = 7 for i in range(1, donation_count): donation = Donation( amount=i, event_identifier=filtered_donations_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # Retrieve the stored donations and confirm they are correct lower_bound = 3 upper_bound = 5 expected_donation_count = upper_bound - lower_bound + 1 donations = private_api_calls.get_event_donations( event_identifier=filtered_donations_identifier, time_bounds=(3, 5)) assert expected_donation_count == 3 for donation in donations: assert isinstance(donation, Donation) assert donation.amount == donation.timestamp
def test_registering_new_event(self): updated_values = { 'identifier': 'registration_test', 'title': 'Registration Test Event' } registration_test_configuration = get_test_event_configuration(updated_values=updated_values) response = private_api_calls.register_event(event_configuration=registration_test_configuration) assert True is response
def test_creating_unregistered_event(self): registration_test_configuration = get_test_event_configuration( {'identifier': 'registration_test'}) event_creator = EventRegister( event_configuration=registration_test_configuration, event_repository=self.test_event_repository) assert event_creator.event_is_registered() is False new_event = event_creator.get_event() assert isinstance(new_event, Event) assert event_creator.event_is_registered() is True assert new_event.configuration.identifier == registration_test_configuration.identifier
def test_updating_registered_event(self): update_test_configuration = get_test_event_configuration( {'identifier': 'update_event_test'}) event_creator = EventRegister( event_configuration=update_test_configuration, event_repository=self.test_event_repository) assert event_creator.event_is_registered() is False test_event = event_creator.get_event() assert event_creator.event_is_registered() is True assert isinstance(test_event, Event) update_test_configuration = get_test_event_configuration({ 'identifier': 'update_event_test', 'end_time': 999 }) event_creator = EventRegister( event_configuration=update_test_configuration, event_repository=self.test_event_repository) assert event_creator.event_is_registered() is True test_event = event_creator.get_event() assert event_creator.event_is_registered() is True assert test_event.configuration.end_time == update_test_configuration.end_time
def setup_test_database(event_values=None, donation_count=10, donation_amount=None, db_path=None): if db_path is None: db_path = test_repository_db_path print('--- SETTING UP TEST DATABASE ---') wipe_database(db_path) if event_values is None: event_values = updated_values event_configuration = get_test_event_configuration( updated_values=event_values) register_event(db_path, event_configuration) register_donations(db_path=db_path, event_configuration=event_configuration, donation_count=donation_count, donation_amount=donation_amount) print('--- TEST DATABASE SETUP COMPLETE ---')
def test_retrieving_donation_count(self): donation_count_identifier = 'donation_count' updated_values = { 'identifier': donation_count_identifier, 'title': 'Donation Count Test Event' } donation_count_test_configuration = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=donation_count_test_configuration) # Add a number of donations donation_count = 5 for i in range(1, donation_count + 1): donation = Donation( amount=i, event_identifier=donation_count_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # retrieve the donation count count = private_api_calls.get_donation_count(event_identifier=donation_count_identifier) assert donation_count == count
def test_retrieving_donation_distribution(self): distribution_identifier = 'donation_distribution' updated_values = { 'identifier': distribution_identifier, 'title': 'Donation Distribution Test Event' } config = get_test_event_configuration(updated_values=updated_values) private_api_calls.register_event(event_configuration=config) donation_values = (0.1, 2, 5, 9.9, 10, 11, 15, 19.9, 20, 45, 55.5, 205.34) expected_counts = (4, 4, 2, 1, 0, 1) assert len(donation_values) == sum(expected_counts) for i in range(0, len(donation_values)): donation = Donation( amount=donation_values[i], event_identifier=distribution_identifier, timestamp=i ) private_api_calls.register_donation(donation=donation) distribution = private_api_calls.get_donation_distribution(event_identifier=distribution_identifier) assert 6 == len(expected_counts) for i in range(0, len(expected_counts)): assert expected_counts[i] == distribution[i]
def test_retrieving_last_donation_only(self): last_donation_identifier = 'last_donation_only' updated_values = { 'identifier': last_donation_identifier, 'title': 'Last Donation Only Test Event' } last_donation_test_configuration = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=last_donation_test_configuration) # Add a number of donations donation_count = 5 for i in range(1, donation_count): donation = Donation( amount=i, event_identifier=last_donation_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # retrieve the last donation only last_donation = private_api_calls.get_last_event_donation(event_identifier=last_donation_identifier) assert isinstance(last_donation, Donation) assert (donation_count - 1) == last_donation.amount assert (donation_count - 1) == last_donation.timestamp
def test_retrieving_valid_donations(self): donation_listing_test_identifier = 'donation_listing_test' updated_values = { 'identifier': donation_listing_test_identifier, 'title': 'Donation Listing Test Event' } donation_listing_test_configuration = get_test_event_configuration(updated_values=updated_values) # Register the event private_api_calls.register_event(event_configuration=donation_listing_test_configuration) # Add a few donations donation_count = 3 for i in range(1, donation_count + 1): donation = Donation( amount=i, event_identifier=donation_listing_test_identifier, timestamp=i) private_api_calls.register_donation(donation=donation) # Retrieve the stored donations and confirm they are correct donations = private_api_calls.get_event_donations(event_identifier=donation_listing_test_identifier) assert donation_count == len(donations) for donation in donations: assert isinstance(donation, Donation) assert donation.amount == donation.timestamp
def test_retrieving_donation_average(self): average_donation_identifier = 'donation_average' updated_values = { 'identifier': average_donation_identifier, 'title': 'Average Donation Test Event' } average_donation_test_configuration = get_test_event_configuration(updated_values=updated_values) # Registration private_api_calls.register_event(event_configuration=average_donation_test_configuration) # Add donations donation_count = 10 for i in range(1, donation_count + 1): donation = Donation( amount=i, event_identifier=average_donation_identifier, timestamp=i ) private_api_calls.register_donation(donation=donation) # retrieve average donation count average_donation_amount = private_api_calls.get_average_donation_amount( event_identifier=average_donation_identifier ) expected_average = 5.5 assert expected_average == average_donation_amount
def test_getting_event_from_creator(self): event_creator = EventCreator( event_configuration=get_test_event_configuration()) event = event_creator.get_event() assert isinstance(event, Event)
def test_getting_event_info(self): info = private_api_calls.get_event_info(identifier=test_event_identifier) test_config_values = get_test_event_configuration().configuration_values assert test_config_values.get('title') == info.get('title')
import pytest from charitybot2.api_calls.private_api_calls import PrivateApiCalls from charitybot2.exceptions import IllegalArgumentException from charitybot2.models.donation import Donation from charitybot2.models.event import NonExistentEventException from charitybot2.paths import private_api_script_path from charitybot2.api.api import private_api_identity, private_api_service, app from charitybot2.start_service import Service, ServiceRunner from helpers.event_config import get_test_event_configuration from helpers.setup_test_database import setup_test_database private_api_calls = PrivateApiCalls(base_api_url=private_api_service.full_url) test_event_identifier = get_test_event_configuration().identifier service = Service( name='Test Private API', app=app, address='127.0.0.1', port=8001, debug=True) service_runner = ServiceRunner(service=service, file_path=private_api_script_path) def setup_module(): setup_test_database(donation_count=10) service_runner.run() def teardown_module(): service_runner.stop_running()