Пример #1
0
    def test_get_transactions_get( self ):
        """Transaction endpoint with one ID retrieves the transaction ( methods = [ GET ] )."""

        with self.app.app_context():
            url = '/donation/transactions/{}'
            # Ensure a GET to with no database entries returns nothing.
            response = self.test_client.get( url.format( 2 ), headers=self.headers )
            data_returned = json.loads( response.data.decode( 'utf-8' ) )
            self.assertEqual( len( data_returned ), 0 )

            # Create a set of transactions.
            total_transactions = 5
            transaction_models = create_model_list(
                TransactionSchema(),
                get_transaction_dict( { 'gift_id': 1 } ),
                total_transactions
            )

            database.session.bulk_save_objects( transaction_models )
            database.session.commit()

            # Ensure a GET with one ID returns the correct transaction.
            response = self.test_client.get( url.format( 2 ), headers=self.headers )
            data_returned = json.loads( response.data.decode( 'utf-8' ) )
            self.assertEqual( data_returned[ 'id' ], 2 )
    def test_get_campaign_amounts( self ):
        """Retrieve the Campaign amounts given its ID ( methods = [ GET ] )."""
        with self.app.app_context():
            url = '/donation/campaigns/2/amounts'

            # Endpoint will check to see that the campaign exists before retrieving amounts and so create a few.
            total_campaigns = 4
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict(), total_campaigns
            )

            database.session.bulk_save_objects( campaign_models )

            campaign_amounts_dict = get_campaign_amount_jsons()
            campaign_amount_models = [ ]
            for amount in campaign_amounts_dict:
                campaign_amount_models.append( from_json( CampaignAmountsSchema(), amount ).data )

            database.session.bulk_save_objects( campaign_amount_models )
            database.session.commit()

            response = self.test_client.get( url, headers=self.headers )
            amounts_returned = json.loads( response.data.decode( 'utf-8' ) )

            self.assertEqual( len( amounts_returned ), 3 )
    def test_campaigns_default( self ):
        """Retrieve all default or not default Campaigns API ( methods = [ GET ] )."""
        with self.app.app_context():
            url = '/donation/campaigns/default/1'

            total_default_campaigns = 1
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict( { 'is_default': 1 } ), total_default_campaigns
            )
            database.session.bulk_save_objects( campaign_models )

            total_not_default_campaigns = 2
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict( { 'is_default': 0 } ), total_not_default_campaigns
            )

            database.session.bulk_save_objects( campaign_models )
            database.session.commit()

            response = self.test_client.get( url, headers=self.headers )
            self.assertEqual( json.loads( response.data.decode( 'utf-8' ) )[ 'is_default' ], 1 )
    def test_campaigns_active( self ):
        """Retrieve all active or inactive Campaigns API ( methods = [ GET ] )."""
        with self.app.app_context():
            url = '/donation/campaigns/active/1'

            total_active_campaigns = 3
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict( { 'is_active': 1 } ), total_active_campaigns
            )
            database.session.bulk_save_objects( campaign_models )

            total_inactive_campaigns = 2
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict( { 'is_active': 0 } ), total_inactive_campaigns
            )
            database.session.bulk_save_objects( campaign_models )

            database.session.commit()

            response = self.test_client.get( url, headers=self.headers )
            self.assertEqual( len( json.loads( response.data.decode( 'utf-8' ) ) ), total_active_campaigns )
    def test_get_gifts_with_id(self):
        """Gifts-transaction endpoint with one gift ID retrieves all transactions on gift ( methods = [ GET ] )."""

        with self.app.app_context():
            url = '/donation/gifts/{}/transactions'

            # Ensure that with no database entries endpoint returns nothing.
            response = self.test_client.get(url.format(str(uuid.uuid4())),
                                            headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 0)

            # Create some gifts to retrieve.
            total_gifts = 5
            gift_models = create_model_list(GiftSchema(), get_gift_dict(),
                                            total_gifts)

            # Create a set of transactions and attach to a specific gift.
            total_transactions = 5
            transaction_gift_id = 3

            transaction_models = create_model_list(
                TransactionSchema(),
                get_transaction_dict({'gift_id': transaction_gift_id}),
                total_transactions)

            database.session.bulk_save_objects(gift_models)
            database.session.bulk_save_objects(transaction_models)
            database.session.commit()

            # Build the URL using the searchable_id of the gift.
            gift_3 = GiftModel.query.filter_by(id=3).one_or_none()
            searchable_id = gift_3.searchable_id

            # Ensure GET retrieves the specified gift and all its transactions.
            response = self.test_client.get(url.format(str(searchable_id)),
                                            headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), total_transactions)
    def test_get_gifts_by_user_id_post(self):
        """Gifts endpoint which retrieves all gifts with list of given user_id's ( methods = [ POST ] )."""

        with self.app.app_context():
            url = '/donation/gift/user'

            # Ensure that with no database entries endpoint returns nothing.
            response = self.test_client.post(url,
                                             data=json.dumps(
                                                 {'user_ids': [1, 3]}),
                                             content_type='application/json',
                                             headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 0)

            # Create a gift with a user_id.
            max_number_of_users = 5
            gift_models = create_model_list(GiftSchema(), get_gift_dict(),
                                            max_number_of_users, 'user_id')

            database.session.bulk_save_objects(gift_models)
            database.session.commit()

            # Ensure that with no users endpoint returns no gifts.
            response = self.test_client.post(url,
                                             data=json.dumps({'user_ids': []}),
                                             content_type='application/json',
                                             headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 0)

            # Ensure that with one user endpoint returns one gift.
            response = self.test_client.post(url,
                                             data=json.dumps({'user_ids':
                                                              [2]}),
                                             content_type='application/json',
                                             headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 1)

            # Ensure that with multiple users endpoint returns multiple gifts.
            response = self.test_client.post(url,
                                             data=json.dumps(
                                                 {'user_ids': [2, 3, 4]}),
                                             content_type='application/json',
                                             headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 3)
    def test_get_campaign( self ):
        """Retrieve a Campaign given the ID ( methods = [ GET ] )."""
        with self.app.app_context():
            url = '/donation/campaigns/2'

            total_campaigns = 4
            campaign_models = create_model_list(
                CampaignSchema(), get_campaign_dict(), total_campaigns
            )

            database.session.bulk_save_objects( campaign_models )
            database.session.commit()

            response = self.test_client.get( url, headers=self.headers )
            campaign_returned = json.loads( response.data.decode( 'utf-8' ) )
            self.assertEqual( campaign_returned[ 'id' ], 2 )
    def test_get_gifts_by_date_future(self):
        """Gifts endpoint which retrieves all gifts newer than date, or between 2 dates ( methods = [ POST ] )."""

        with self.app.app_context():
            url = '/donation/gifts/date'

            # Ensure that with no database entries endpoint returns nothing.
            date_in_utc_now = datetime.utcnow()
            response = self.test_client.post(
                url,
                data=json.dumps({'date':
                                 date_in_utc_now.strftime('%Y-%m-%d')}),
                content_type='application/json',
                headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 0)

            # Create some gifts to retrieve.
            total_gifts = 2
            gift_models = create_model_list(GiftSchema(), get_gift_dict(),
                                            total_gifts)

            # Create a set of transactions and attach to a specific gift.
            # Here are the time deltas: { gift 1: [ 0, -2, -4, -6 ], gift 2: [ -8, -10, -12, -14 ] }
            total_transactions = 4
            transaction_models = create_gift_transactions_date(
                TransactionSchema(), get_transaction_dict(),
                total_transactions, total_gifts)

            database.session.bulk_save_objects(gift_models)
            database.session.bulk_save_objects(transaction_models)
            database.session.commit()

            date_in_utc_now = datetime.utcnow()

            # Date in the future should bring back no results.
            date_in_utc = date_in_utc_now + timedelta(days=2)
            response = self.test_client.post(
                url,
                data=json.dumps({'date': date_in_utc.strftime('%Y-%m-%d')}),
                content_type='application/json',
                headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 0)
Пример #9
0
    def test_caged_donors(self):
        """Caged donor API ( methods = [ GET ] )."""
        with self.app.app_context():
            url = '/donation/donors/caged'
            # Ensure a GET with no saved caged_donors returns 0.
            response = self.test_client.get(url, headers=self.headers)
            self.assertEqual(len(json.loads(response.data.decode('utf-8'))), 0)

            # Create some caged_donors to retrieve.
            total_caged_donors = 5
            caged_donor_models = create_model_list(CagedDonorSchema(),
                                                   get_caged_donor_dict(),
                                                   total_caged_donors)
            database.session.bulk_save_objects(caged_donor_models)
            database.session.commit()

            # Ensure GET returns all caged_donors.
            response = self.test_client.get(url, headers=self.headers)
            self.assertEqual(len(json.loads(response.data.decode('utf-8'))),
                             total_caged_donors)
    def test_get_gifts_without_id(self):
        """Gifts-transaction endpoint with gift ID's retrieves all transactions on gifts ( methods = [ GET ] )."""

        with self.app.app_context():
            url = '/donation/gifts/transactions'

            # Create some gifts to retrieve.
            total_gifts = 5
            gift_models = create_model_list(GiftSchema(), get_gift_dict(),
                                            total_gifts)

            # Create 2 sets of transactions, each attached to a separate gift.
            total_transactions = 5
            transaction_gift_ids = [2, 4]

            transaction_models_1 = create_model_list(
                TransactionSchema(),
                get_transaction_dict({'gift_id': transaction_gift_ids[0]}),
                total_transactions)

            transaction_models_2 = create_model_list(
                TransactionSchema(),
                get_transaction_dict({'gift_id': transaction_gift_ids[1]}),
                total_transactions)

            database.session.bulk_save_objects(gift_models)
            database.session.bulk_save_objects(transaction_models_1)
            database.session.bulk_save_objects(transaction_models_2)
            database.session.commit()

            gift_2 = GiftModel.query.filter_by(id=2).one_or_none()
            searchable_id_2 = gift_2.searchable_id
            gift_4 = GiftModel.query.filter_by(id=4).one_or_none()
            searchable_id_4 = gift_4.searchable_id

            # Ensure a GET returns all transactions.
            response = self.test_client.get(url, headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 2 * total_transactions)

            # Ensure GET retrieves all transactions attached to the specified gift and the ID is correct.
            response = self.test_client.post(
                url,
                data=json.dumps({'searchable_ids': [str(searchable_id_2)]}),
                content_type='application/json',
                headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))

            self.assertEqual(len(data_returned), 5)
            self.assertEqual(data_returned[0]['gift_searchable_id'],
                             str(searchable_id_2))

            # Ensure GET retrieves all transactions attached to the 2 gifts.
            response = self.test_client.post(
                url,
                data=json.dumps({
                    'searchable_ids':
                    [str(searchable_id_2),
                     str(searchable_id_4)]
                }),
                content_type='application/json',
                headers=self.headers)
            data_returned = json.loads(response.data.decode('utf-8'))
            self.assertEqual(len(data_returned), 10)