Best Python code snippet using localstack_python
capture_notimplemented_responses.py
Source:capture_notimplemented_responses.py  
...156        result["status_code"] = STATUS_PARSING_ERROR157    except Exception as e:158        logging.exception(e)159    return result160def map_to_notimplemented(row: RowEntry) -> bool:161    """162    Some simple heuristics to check the API responses and classify them into implemented/notimplemented163    Ideally they all should behave the same way when receiving requests for not yet implemented endpoints164    (501 with a "not yet implemented" message)165    :param row: the RowEntry166    :return: True if we assume it is not implemented, False otherwise167    """168    if row["status_code"] in [STATUS_TIMEOUT_ERROR, STATUS_PARSING_ERROR]:169        # parsing or timeout issue, interpreted as implemented until there's a better heuristic170        return False171    if row["status_code"] == STATUS_CONNECTION_ERROR:172        # affected services:173        # lakeformation, GetQueryStat174        # lakeformation, GetQueryStatistics175        # lakeformation, GetWorkUnitResults176        # lakeformation, GetWorkUnits177        # lakeformation, StartQueryPlanning178        # servicediscovery, DiscoverInstances179        # stepfunctions, StartSyncExecution180        return True181    if row["service"] == "dynamodb" and row.get("error_code") == "UnknownOperationException":182        return True183    if row["service"] == "lambda" and row["status_code"] == 404 and row.get("error_code") == "404":184        return True185    if (186        row["service"] == "apigateway"187        and row["status_code"] == 404188        and row.get("error_code") == "404"189        and row.get("error_message") is not None190        and "The requested URL was not found on the server" in row.get("error_message", "")191    ):192        return True193    if (194        row["service"] == "apigatewayv2"195        and row["status_code"] == 501196        and row.get("error_message") is not None197        and "not yet implemented" in row.get("error_message", "")198    ):199        return True200    if row.get("error_message") is not None and "not yet implemented" in row.get(201        "error_message", ""202    ):203        return True204    if row["status_code"] == 501:205        return True206    return False207def run_script(services: list[str]):208    """send requests against all APIs"""209    with (210        open("implementation_coverage_full.csv", "w") as csvfile,211        open("implementation_coverage_aggregated.csv", "w") as aggregatefile,212    ):213        full_w = csv.DictWriter(214            csvfile,215            fieldnames=[216                "service",217                "operation",218                "status_code",219                "error_code",220                "error_message",221                "is_implemented",222            ],223        )224        aggregated_w = csv.DictWriter(225            aggregatefile,226            fieldnames=["service", "operation", "implemented_count", "full_count", "percentage"],227        )228        full_w.writeheader()229        aggregated_w.writeheader()230        responses = {}231        for service_name in services:232            service = service_models.get(service_name)233            for op_name in service.operation_names:234                # here's the important part (the actual service call!)235                response = simulate_call(service_name, op_name)236                responses.setdefault(service_name, {})[op_name] = response237                is_implemented = str(not map_to_notimplemented(response))238                full_w.writerow(response | {"is_implemented": is_implemented})239            # calculate aggregate for service240            all_count = len(responses[service_name].values())241            implemented_count = len(242                [r for r in responses[service_name].values() if not map_to_notimplemented(r)]243            )244            implemented_percentage = implemented_count / all_count245            aggregated_w.writerow(246                {247                    "service": response["service"],248                    "operation": response["operation"],249                    "implemented_count": implemented_count,250                    "full_count": all_count,251                    "percentage": f"{implemented_percentage * 100:.1f}",252                }253            )254def calculate_percentages():255    aggregate = {}256    implemented_aggregate = {}...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
