Пример #1
0
 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
Пример #2
0
 def get_event_donations(self,
                         event_identifier,
                         time_bounds=(),
                         limit=None):
     self.__validate_event_identifier(event_identifier=event_identifier)
     params_added = False
     # TODO: Rewrite to properly use query parameters built into requests instead of manually concatenating
     url = self._base_api_url + 'event/{}/donations/'.format(
         event_identifier)
     if len(time_bounds) == 2:
         lower_bound, upper_bound = time_bounds[0], time_bounds[1]
         if not isinstance(lower_bound, int) or not isinstance(
                 upper_bound, int):
             raise IllegalArgumentException(
                 'Time bounds must be a tuple of 2 integers')
         url += '?lower={}&upper={}'.format(lower_bound, upper_bound)
         params_added = True
     if limit is not None:
         if limit <= 0:
             raise IllegalArgumentException('Limit must be 1 or more')
         url += '&' if params_added else '?'
         url += 'limit={}'.format(limit)
         params_added = True
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)['donations']
     if isinstance(converted_content, list):
         donations = [
             Donation.from_json(donation) for donation in converted_content
         ]
     else:
         donations = Donation.from_json(converted_content)
     return donations
Пример #3
0
 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
Пример #4
0
 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
Пример #5
0
def submit_api_donation(amount, donor, notes):
    donation = Donation(amount=amount,
                        event_identifier=test_event_identifier,
                        donor_name=donor,
                        notes=notes)
    private_api_calls.register_donation(donation=donation)
    sleep(1)
Пример #6
0
def register_donations(db_path, event_configuration, donation_count,
                       donation_amount):
    # TODO: Refactor this!
    randomise_donations = False
    if donation_amount is None:
        randomise_donations = True
    print('Recording donations')
    donations_repository = DonationSQLiteRepository(db_path=db_path)
    shifting_time = start_time + 5
    fake = Faker()
    print('Adding {} donations'.format(donation_count))
    total = 0
    for i in tqdm(range(0, donation_count)):
        shifting_time += random.randint(5, 60)
        donor_name = fake.name()
        if randomise_donations:
            donation_amount = round(random.uniform(1.0, 100.0), 2)
            total += donation_amount
        donation = Donation(amount=donation_amount,
                            timestamp=shifting_time,
                            event_identifier=event_configuration.identifier,
                            external_reference='N/A',
                            notes='N/A',
                            donor_name=donor_name)
        donations_repository.record_donation(donation=donation)
    # set the total
    print('Setting total to: {}'.format(total))
    events_repository = EventSQLiteRepository(db_path=db_path)
    events_repository.update_event_current_amount(identifier='test',
                                                  current_amount=round(
                                                      total, 2))
Пример #7
0
 def test_incorrect_values_throws_exception(self, amount, timestamp,
                                            event_identifier,
                                            internal_reference):
     with pytest.raises(InvalidDonationException):
         Donation(amount=amount,
                  event_identifier=event_identifier,
                  timestamp=timestamp,
                  internal_reference=internal_reference)
Пример #8
0
 def get_latest_event_donation(self, event_identifier):
     self.__validate_event_identifier(event_identifier=event_identifier)
     url = self._base_api_url + 'event/{}/donations/largest'.format(
         event_identifier)
     response = UrlCall(url=url, timeout=self._timeout).get()
     decoded_content = response.content.decode('utf-8')
     converted_content = json.loads(decoded_content)
     return Donation.from_dict(converted_content)
Пример #9
0
 def test_conversion_from_valid_json(self):
     donation_json = json.dumps(self.test_donation_dict)
     donation = Donation.from_json(donation_json=donation_json)
     assert test_donation.amount == donation.amount
     assert test_donation.event_identifier == donation.event_identifier
     assert test_donation.timestamp == donation.timestamp
     assert test_donation.internal_reference == donation.internal_reference
     assert test_donation.notes == donation.notes
     assert test_donation.validity == donation.validity
Пример #10
0
 def test_conversion_from_valid_dict(self):
     donation = Donation.from_dict(self.test_donation_dict)
     assert test_donation.amount == donation.amount
     assert test_donation.event_identifier == donation.event_identifier
     assert test_donation.timestamp == donation.timestamp
     assert test_donation.internal_reference == donation.internal_reference
     assert test_donation.external_reference == donation.external_reference
     assert test_donation.donor_name == donation.donor_name
     assert test_donation.notes == donation.notes
     assert test_donation.validity == donation.validity
Пример #11
0
 def _convert_to_donation(self, donation):
     # /Date(1505495742000+0000)/
     return Donation(
         amount=donation['amount'],
         event_identifier=self._event_identifier,
         timestamp=int(
             donation['donationDate'].split('(')[1].split('+')[0]) // 1000,
         external_reference=str(donation['id']),
         donor_name=donation['donorDisplayName'],
         notes=donation['message'])
Пример #12
0
 def __convert_row_to_donation(row):
     donation = Donation(amount=row[1],
                         event_identifier=row[2],
                         timestamp=row[3],
                         internal_reference=row[4],
                         external_reference=row[5],
                         donor_name=row[6],
                         notes=row[7],
                         valid=row[8])
     return donation
Пример #13
0
 def __convert_to_donation(self, donation):
     # 2014-09-17 16:06:21 -0400
     return Donation(
         amount=donation['amount'],
         event_identifier=self._event_identifier,
         timestamp=time.mktime(parser.parse(donation['created']).timetuple()),
         external_reference=str(donation['id']),
         donor_name=donation['name'],
         notes=donation['comment']
     )
Пример #14
0
 def test_registering_fully_defined_valid_donation(self):
     donation = Donation(
         amount=-30,
         event_identifier=test_event_identifier,
         timestamp=123,
         internal_reference='foobar',
         external_reference='wat',
         donor_name='some guy',
         notes='she sells sea shells on the sea shore',
         valid=False
     )
     response = private_api_calls.register_donation(donation=donation)
     assert True is response
Пример #15
0
 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
Пример #16
0
 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
Пример #17
0
 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]
Пример #18
0
def record_donation():
    success = False
    message = 'Donation successfully added'
    received_data = {}
    try:
        received_data = request.form.to_dict()
        new_donation = Donation.from_dict(received_data)
        get_donations_repository().record_donation(new_donation)
        success = True
    except InvalidRepositoryQueryException as e:
        message = 'Donation Repository Error: {}'.format(e)
    except InvalidDonationException as e:
        message = 'Donation was invalid: {}'.format(e)
        print('Error: {} Data: {}'.format(e, received_data))
    except Exception as e:
        message = 'Unknown exception: {}'.format(e)
    return jsonify(
        {
            'received': success,
            'message': message
        }
    )
Пример #19
0
 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
Пример #20
0
 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
Пример #21
0
import pytest
import time
from charitybot2.models.donation import Donation, InvalidDonationException

test_donation_amount = 50
test_donation_event_identifier = 'event_identifier'
test_donation_timestamp = 999999
test_donation_internal_reference = 'internal_reference'
test_donation_external_reference = 'external_reference'
test_donation_notes = 'Automated'
test_donation_donor_name = 'donor'

test_donation = Donation(amount=test_donation_amount,
                         event_identifier=test_donation_event_identifier,
                         timestamp=test_donation_timestamp,
                         internal_reference=test_donation_internal_reference,
                         external_reference=test_donation_external_reference,
                         donor_name=test_donation_donor_name,
                         notes=test_donation_notes)


class TestDonationInstantiation:
    @pytest.mark.parametrize(
        'expected,actual',
        [(test_donation_amount, test_donation.amount),
         (test_donation_event_identifier, test_donation.event_identifier),
         (test_donation_timestamp, test_donation.timestamp),
         (test_donation_internal_reference, test_donation.internal_reference),
         (test_donation_external_reference, test_donation.external_reference),
         (test_donation_donor_name, test_donation.donor_name),
         (test_donation_notes, test_donation.notes),
Пример #22
0
 def test_registering_valid_donation(self):
     donation = Donation(amount=50, event_identifier=test_event_identifier)
     response = private_api_calls.register_donation(donation=donation)
     assert True is response
Пример #23
0
 def test_passing_amount_string_with_commas_parses_properly(self, amount):
     donation = Donation(amount=amount, event_identifier='event')
     assert 12345.67 == donation.amount
Пример #24
0
 def test_conversion_from_invalid_json_throws_exception(
         self, donation_json):
     with pytest.raises(InvalidDonationException):
         donation = Donation.from_json(donation_json=donation_json)
Пример #25
0
def setup_test_donations(repository):
    donations = []
    for i in test_range:
        donations.append(Donation(amount=i, event_identifier='event', timestamp=i))
    for donation in donations:
        repository.record_donation(donation=donation)
Пример #26
0
 def test_two_donations_have_different_internal_references(self):
     donation_one = Donation(amount=1, event_identifier='event')
     donation_two = Donation(amount=2, event_identifier='event')
     assert not donation_one.internal_reference == donation_two.internal_reference
Пример #27
0
 def test_total_is_updated_when_registering_donation(self):
     amount_increment = 1.5
     donation = Donation(amount=amount_increment, timestamp=500, event_identifier=test_event_identifier)
     private_api_calls.register_donation(donation=donation)
     new_total = private_api_calls.get_event_total(event_identifier=test_event_identifier)
     assert (self.expected_total + amount_increment) == new_total
Пример #28
0
 def test_donation_optional_defaults(self):
     donation = Donation(amount=1, event_identifier='event')
     assert int(time.time()) + 2 >= donation.timestamp >= int(
         time.time()) - 2
     assert None is donation.notes
     assert True is donation.validity
Пример #29
0
 def test_donation_amount_rounding(self, amount):
     donation = Donation(amount=amount, event_identifier='event')
     assert 123.45 == donation.amount