def test_process_g8_results_for_incomplete_declaration(mock_data_client):
    # Declaration not complete so application should fail
    declaration = VALID_COMPLETE_G8_DECLARATION.copy()
    declaration['status'] = 'started'
    mock_data_client.get_interested_suppliers.return_value = {"interestedSuppliers": [123456]}
    mock_data_client.get_supplier_declaration.return_value = {"declaration": declaration}
    mock_data_client.find_draft_services_iter.return_value = iter(({"status": "submitted"}, {"status": "submitted"},))
    process_g8_results(mock_data_client, 'user')
    mock_data_client.set_framework_result.assert_called_with(123456, 'g-cloud-8', False, 'user')
def test_process_g8_results_for_discretionary(mock_data_client):
    # Question 'bankrupt' = True is a discretionary fail, so result should be discretionary
    declaration = VALID_COMPLETE_G8_DECLARATION.copy()
    declaration['bankrupt'] = True
    mock_data_client.get_interested_suppliers.return_value = {"interestedSuppliers": [123456]}
    mock_data_client.get_supplier_declaration.return_value = {"declaration": declaration}
    mock_data_client.find_draft_services_iter.return_value = iter(({"status": "submitted"}, {"status": "submitted"},))
    process_g8_results(mock_data_client, 'user')
    # Discretionary result should not update `supplier_frameworks` at all
    mock_data_client.set_framework_result.assert_not_called()
def test_process_g8_results_for_successful(mock_data_client):
    mock_data_client.get_interested_suppliers.return_value = {"interestedSuppliers": [123456]}
    mock_data_client.get_supplier_declaration.return_value = {"declaration": VALID_COMPLETE_G8_DECLARATION}
    mock_data_client.find_draft_services_iter.return_value = iter(({"status": "submitted"}, {"status": "submitted"},))
    process_g8_results(mock_data_client, 'user')
    mock_data_client.set_framework_result.assert_called_with(123456, 'g-cloud-8', True, 'user')
 * checks whether it is a PASS, FAIL or DISCRETIONARY
 * updates the `supplier_frameworks` entry for the application accordingly, using the API:
   - on_framework=true if declaration OK and at least one completed service
   - on_framework=false if declaration FAILed or there are no completed services
   - on_framework=NULL (no call to API) if declaration DISCRETIONARY and at least one completed service

Usage:
    scripts/insert-g8-framework-results.py <stage> --api-token=<data_api_token>

Example:
    python scripts/insert-g8-framework-results.py dev --api-token=myToken
"""
import sys
sys.path.insert(0, '.')

import getpass
from dmscripts.env import get_api_endpoint_from_stage

from docopt import docopt
from dmapiclient import DataAPIClient
from dmscripts.insert_g8_framework_results import process_g8_results

if __name__ == '__main__':
    arguments = docopt(__doc__)

    data_api_url = get_api_endpoint_from_stage(arguments['<stage>'], 'api')
    client = DataAPIClient(data_api_url, arguments['--api-token'])
    user = getpass.getuser()

    process_g8_results(client, user)