Exemple #1
0
    def run_calculation(self):
        components_api = ComponentsApi(self.api_client)
        components = components_api.get_pa_components(
            common_parameters.pa_deafult_document)
        component_id = list(components.keys())[0]

        pa_account_identifier = PAIdentifier(
            common_parameters.pa_benchmark_sp500)
        pa_accounts = [pa_account_identifier]
        pa_benchmark_identifier = PAIdentifier(
            common_parameters.pa_benchmark_r1000)
        pa_benchmarks = [pa_benchmark_identifier]
        pa_dates = PADateParameters(common_parameters.deafult_start_date,
                                    common_parameters.deafult_end_date,
                                    common_parameters.deafult_dates_frequency)

        pa_calculation_parameters = {
            "1":
            PACalculationParameters(component_id, pa_accounts, pa_benchmarks,
                                    pa_dates)
        }

        components = components_api.get_spar_components(
            common_parameters.spar_deafult_document)
        component_id = list(components.keys())[0]

        spar_account_identifier = SPARIdentifier(
            common_parameters.spar_benchmark_r1000,
            common_parameters.spar_benchmark_russell_return_type,
            common_parameters.spar_benchmark_russell_prefix)
        spar_accounts = [spar_account_identifier]
        spar_benchmark_identifier = SPARIdentifier(
            common_parameters.spar_benchmark_r2000,
            common_parameters.spar_benchmark_russell_return_type,
            common_parameters.spar_benchmark_russell_prefix)
        spar_dates = SPARDateParameters(
            common_parameters.deafult_start_date,
            common_parameters.deafult_end_date,
            common_parameters.deafult_dates_frequency)

        spar_calculation_parameters = {
            "2":
            SPARCalculationParameters(component_id, spar_accounts,
                                      spar_benchmark_identifier, spar_dates)
        }

        components = components_api.get_vault_components(
            common_parameters.vault_deafult_document)
        component_id = list(components.keys())[0]

        vault_account_identifier = VaultIdentifier(
            common_parameters.vault_deafult_account)
        vault_dates = VaultDateParameters(
            common_parameters.vault_start_date,
            common_parameters.vault_end_date,
            common_parameters.deafult_dates_frequency)

        configurations_api = ConfigurationsApi(self.api_client)
        configurations = configurations_api.get_vault_configurations(
            common_parameters.vault_deafult_account)
        configuration_id = list(configurations.keys())[0]

        vault_calculation_parameters = {
            "3":
            VaultCalculationParameters(component_id, vault_account_identifier,
                                       vault_dates, configuration_id)
        }

        calculation = Calculation(pa_calculation_parameters,
                                  spar_calculation_parameters,
                                  vault_calculation_parameters)
        return self.calculations_api.run_calculation_with_http_info(
            calculation=calculation)
class TestComponentsApi(unittest.TestCase):
    def setUp(self):
        self.components_api = ComponentsApi(CommonFunctions.build_api_client())

    ######################################################################################
    # PA Components Test Cases
    ######################################################################################

    def test_get_all_pa_components(self):
        response = self.components_api.get_pa_components_with_http_info(
            document=common_parameters.pa_deafult_document)
        component_id = list(response[0].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), dict,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0][component_id]), ComponentSummary,
                         "Response should be of ComponentSummary type")

    def test_get_pa_component_by_id(self):
        components = self.components_api.get_pa_components(
            document=common_parameters.pa_deafult_document)
        component_id = list(components.keys())[0]
        response = self.components_api.get_pa_component_by_id_with_http_info(
            component_id)
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), PAComponent,
                         "Response should be of PAComponent type.")

    ######################################################################################
    # Vault Components Test Cases
    ######################################################################################

    def test_get_all_vault_components(self):
        response = self.components_api.get_vault_components_with_http_info(
            document=common_parameters.vault_deafult_document)
        component_id = list(response[0].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), dict,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0][component_id]), ComponentSummary,
                         "Response should be of ComponentSummary type")

    def test_get_vault_component_by_id(self):
        components = self.components_api.get_vault_components(
            document=common_parameters.vault_deafult_document)
        component_id = list(components.keys())[0]
        response = self.components_api.get_vault_component_by_id_with_http_info(
            component_id)
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), VaultComponent,
                         "Response should be of VaultComponent type.")

    ######################################################################################
    # Spar Components Test Cases
    ######################################################################################

    def test_get_all_spar_components(self):
        response = self.components_api.get_spar_components_with_http_info(
            document=common_parameters.spar_deafult_document)
        component_id = list(response[0].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), dict,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0][component_id]), ComponentSummary,
                         "Response should be of ComponentSummary type")
Exemple #3
0
def main():
    config = Configuration()
    config.host = host
    config.username = fds_username
    config.password = fds_api_key
    config.discard_unknown_keys = True
    # add proxy and/or disable ssl verification according to your development environment
    # config.proxy = "<proxyUrl>"
    config.verify_ssl = False

    # Setting configuration to retry api calls on http status codes of 429 and 503.
    config.retries = Retry(total=3,
                           status=3,
                           status_forcelist=frozenset([429, 503]),
                           backoff_factor=2,
                           raise_on_status=False)

    api_client = ApiClient(config)

    components_api = ComponentsApi(api_client)

    try:
        pa_document_name = "PA_DOCUMENTS:DEFAULT"
        pa_component_name = "Weights"
        pa_component_category = "Weights / Exposures"
        portfolio = "BENCH:SP50"
        benchmark = "BENCH:R.1000"
        startdate = "20180101"
        enddate = "20181231"
        frequency = "Monthly"
        # uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
        # Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
        # cache_control = "max-stale=0"
        get_components_response = components_api.get_pa_components(
            document=pa_document_name)
        component_id = [
            id for id in list(get_components_response[0].data.keys())
            if get_components_response[0].data[id].name == pa_component_name
            and get_components_response[0].data[id].category ==
            pa_component_category
        ][0]
        print("PA Component Id: " + component_id)
        pa_accounts = [PAIdentifier(id=portfolio)]
        pa_benchmarks = [PAIdentifier(id=benchmark)]
        pa_dates = PADateParameters(startdate=startdate,
                                    enddate=enddate,
                                    frequency=frequency)

        pa_calculation_parameters = {
            "1":
            PACalculationParameters(componentid=component_id,
                                    accounts=pa_accounts,
                                    benchmarks=pa_benchmarks,
                                    dates=pa_dates),
            "2":
            PACalculationParameters(componentid=component_id,
                                    accounts=pa_accounts,
                                    benchmarks=pa_benchmarks,
                                    dates=pa_dates)
        }

        pa_calculation_parameter_root = PACalculationParametersRoot(
            data=pa_calculation_parameters)

        pa_calculations_api = PACalculationsApi(api_client)

        post_and_calculate_response = pa_calculations_api.post_and_calculate(
            pa_calculation_parameters_root=pa_calculation_parameter_root)
        # comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
        # post_and_calculate_response = pa_calculations_api.post_and_calculate(pa_calculation_parameters_root=pa_calculation_parameter_root, cache_control=cache_control)
        if post_and_calculate_response[
                1] == 202 or post_and_calculate_response[1] == 200:
            calculation_id = post_and_calculate_response[0].data.calculationid
            print("Calculation Id: " + calculation_id)

            status_response = pa_calculations_api.get_calculation_status_by_id(
                id=calculation_id)

            while status_response[1] == 202 and (status_response[0].data.status
                                                 in ("Queued", "Executing")):
                max_age = '5'
                age_value = status_response[2].get("cache-control")
                if age_value is not None:
                    max_age = age_value.replace("max-age=", "")
                print('Sleeping: ' + max_age)
                time.sleep(int(max_age))
                status_response = pa_calculations_api.get_calculation_status_by_id(
                    calculation_id)

            for (calculation_unit_id,
                 calculation_unit) in status_response[0].data.units.items():
                if calculation_unit.status == "Success":
                    print("Calculation Unit Id: " + calculation_unit_id +
                          " Succeeded!!!")
                    result_response = pa_calculations_api.get_calculation_unit_result_by_id(
                        id=calculation_id, unit_id=calculation_unit_id)
                    output_calculation_result(result_response[0]['data'])
                else:
                    print("Calculation Unit Id:" + calculation_unit_id +
                          " Failed!!!")
                    print("Error message : " + str(calculation_unit.errors))
        else:
            print("Calculation creation failed")
            print("Error status : " + str(post_and_calculate_response[1]))
            print("Error message : " + str(post_and_calculate_response[0]))

    except ApiException as e:
        print("Api exception Encountered")
        print(e)
        exit()
Exemple #4
0
enddate = "20181231"
frequency = "Monthly"

config = Configuration()
config.host = host
config.username = username
config.password = password
# add proxy and/or disable ssl verification according to your development environment
# config.proxy = "<proxyUrl>"
config.verify_ssl = False

api_client = ApiClient(config)

components_api = ComponentsApi(api_client)

components = components_api.get_pa_components(pa_document_name)
component_id = list((dict(
    filter(
        lambda component: (component[1].name == pa_component_name and
                           component[1].category == pa_component_category),
        components.items()))).keys())[0]

pa_account_identifier = PAIdentifier(pa_benchmark_sp_50)
pa_accounts = [pa_account_identifier]
pa_benchmark_identifier = PAIdentifier(pa_benchmark_r_1000)
pa_benchmarks = [pa_benchmark_identifier]
pa_dates = PADateParameters(startdate, enddate, frequency)

pa_calculation_parameters = {
    "1":
    PACalculationParameters(component_id, pa_accounts, pa_benchmarks, pa_dates)
Exemple #5
0
class TestComponentsApi(unittest.TestCase):
    def setUp(self):
        self.components_api = ComponentsApi(CommonFunctions.build_api_client())

    ######################################################################################
    # PA Components Test Cases
    ######################################################################################

    def test_get_all_pa_components(self):
        response = self.components_api.get_pa_components(
            document=common_parameters.pa_default_document,
            _return_http_data_only=False)
        component_id = list(response[0]['data'].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), ComponentSummaryRoot,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0]['data'][component_id]),
                         ComponentSummary,
                         "Response should be of ComponentSummary type")

    # This test encounters ApiTypeException due to response having fields set to null instead of being hidden
    @unittest.skip(
        "Skip until fix API behavior where null property is returned instead of hidden"
    )
    def test_get_pa_component_by_id(self):
        components = self.components_api.get_pa_components(
            document=common_parameters.pa_default_document)
        component_id = list(components['data'].keys())[0]
        response = self.components_api.get_pa_component_by_id(
            id=component_id, _return_http_data_only=False)
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), PAComponentRoot,
                         "Response should be of PAComponentRoot type.")
        self.assertEqual(type(response[0]['data'][component_id]),
                         ComponentSummary,
                         "Response should be of ComponentSummary type")

    ######################################################################################
    # Vault Components Test Cases
    ######################################################################################

    def test_get_all_vault_components(self):
        response = self.components_api.get_vault_components(
            document=common_parameters.vault_default_document,
            _return_http_data_only=False)
        component_id = list(response[0]['data'].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), ComponentSummaryRoot,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0]['data'][component_id]),
                         ComponentSummary,
                         "Response should be of ComponentSummary type")

    # This test encounters ApiTypeException due to response having fields set to null instead of being hidden
    @unittest.skip(
        "Skip until fix API behavior where null property is returned instead of hidden"
    )
    def test_get_vault_component_by_id(self):
        components = self.components_api.get_vault_components(
            document=common_parameters.vault_default_document)
        component_id = list(components['data'].keys())[0]
        response = self.components_api.get_vault_component_by_id(
            component_id, _return_http_data_only=False)
        component_id = list(response[0]['data'].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]['data']), VaultComponent,
                         "Response should be of VaultComponent type.")

    ######################################################################################
    # Spar Components Test Cases
    ######################################################################################

    def test_get_all_spar_components(self):
        response = self.components_api.get_spar_components(
            document=common_parameters.spar_default_document,
            _return_http_data_only=False)
        component_id = list(response[0]['data'].keys())[0]
        self.assertEqual(response[1], 200, "Response should be 200 - Success")
        self.assertEqual(type(response[0]), ComponentSummaryRoot,
                         "Response should be of dictionary type")
        self.assertEqual(type(response[0]['data'][component_id]),
                         ComponentSummary,
                         "Response should be of ComponentSummary type")
class TestPaCalculationsApi(unittest.TestCase):
    def setUp(self):
        api_client = CommonFunctions.build_api_client()
        self.pa_calculations_api = PACalculationsApi(api_client)
        self.components_api = ComponentsApi(api_client)

    def test_single_unit_scenario(self):
        create_step_name = "create_calculation"
        read_status_step_name = "read_status"
        read_result_step_name = "read_result"

        def create_calculation(test_context):
            print("Creating single unit calculation")
            components = self.components_api.get_pa_components(
                document="PA_DOCUMENTS:DEFAULT", _return_http_data_only=True)
            component_summary = ComponentSummary(
                name="Weights", category="Weights / Exposures")
            component_id = [
                id for id in list(components.data.keys())
                if components.data[id].name == component_summary.name
                and components.data[id].category == component_summary.category
            ][0]
            pa_accounts = [PAIdentifier(id="BENCH:SP50")]
            pa_benchmarks = [PAIdentifier(id="BENCH:R.1000")]
            pa_dates = PADateParameters(startdate="20180101",
                                        enddate="20181231",
                                        frequency="Monthly")

            pa_calculation_parameters = {
                "1":
                PACalculationParameters(componentid=component_id,
                                        accounts=pa_accounts,
                                        benchmarks=pa_benchmarks,
                                        dates=pa_dates)
            }

            pa_calculation_parameter_root = PACalculationParametersRoot(
                data=pa_calculation_parameters)

            post_and_calculate_response = self.pa_calculations_api.post_and_calculate(
                pa_calculation_parameters_root=pa_calculation_parameter_root)

            self.assertTrue(
                post_and_calculate_response[1] == 201
                or post_and_calculate_response[1] == 202,
                "Response for create_calculation should have been 201 or 202")

            if post_and_calculate_response[1] == 201:
                return {
                    "continue_workflow": False,
                    "next_request": None,
                    "test_context": None
                }
            elif post_and_calculate_response[1] == 202:
                test_context["calculation_id"] = post_and_calculate_response[
                    2]["X-Factset-Api-Calculation-Id"]
                return {
                    "continue_workflow": True,
                    "next_request": read_status_step_name,
                    "test_context": test_context
                }

        def read_calculation_status(test_context):
            print("Reading single unit calculation status")
            calculation_id = test_context["calculation_id"]
            print("Calculation Id: " + calculation_id)

            status_response = self.pa_calculations_api.get_calculation_status_by_id(
                id=calculation_id)

            self.assertTrue(
                status_response[1] == 202 and
                (status_response[0].data.status in ("Queued", "Executing")))

            while status_response[1] == 202 and (status_response[0].data.status
                                                 in ("Queued", "Executing")):
                max_age = '5'
                age_value = status_response[2].get("cache-control")
                if age_value is not None:
                    max_age = age_value.replace("max-age=", "")
                print('Sleeping: ' + max_age)
                time.sleep(int(max_age))
                status_response = self.pa_calculations_api.get_calculation_status_by_id(
                    id=calculation_id)

                test_context["calculation_units"] = status_response[
                    0].data.units.items()

            return {
                "continue_workflow": True,
                "next_request": read_result_step_name,
                "test_context": test_context
            }

        def read_calculation_unit_result(test_context):
            calculation_id = test_context["calculation_id"]
            for (calculation_unit_id,
                 calculation_unit) in test_context["calculation_units"]:
                result_response = self.pa_calculations_api.get_calculation_unit_result_by_id(
                    id=calculation_id, unit_id=calculation_unit_id)
                self.assertEqual(
                    result_response[1], 200,
                    "Get calculation result should have succeeded")

        workflow_specification = {
            create_step_name: create_calculation,
            read_status_step_name: read_calculation_status,
            read_result_step_name: read_calculation_unit_result
        }
        starting_request = workflow_specification['create_calculation']
        starting_test_context = {}
        run_api_workflow_with_assertions(workflow_specification,
                                         starting_request,
                                         starting_test_context)