def test_send_updated_items_to_dest_valid_response(mock_server): with open("tests/resources/items/valid_dest_updated_items_response.json") as file: valid_dest_response = file.read() # Set up mock server with required behavior imposter = Imposter(Stub(Predicate(path="/items/updates", method="POST"), Response(body=valid_dest_response))) with mock_server(imposter) as server: config = { 'dest_updates_url': f"{imposter.url}/items/updates", 'storage_dir': '/tmp', 'last_success_lookup': 'tests/storage/items/items_last_success.txt', 'caiasoft_api_key': "SOME_SECRET_KEY" } job_config = ItemsJobConfig(config, 'test') # Override dest_updated_items_request_body_filepath job_config["dest_updated_items_request_body_filepath"] = \ "tests/resources/items/valid_dest_updated_items_request.json" with tempfile.TemporaryDirectory() as temp_storage_dir: job_config["dest_updated_items_response_body_filepath"] = \ temp_storage_dir + "/dest_updated_items_response.json" send_updated_items_to_dest = SendUpdatedItemsToDest(job_config) step_result = send_updated_items_to_dest.execute() assert step_result.was_successful() is True assert os.path.exists(job_config["dest_updated_items_response_body_filepath"]) assert_that(server, had_request().with_path("/items/updates").and_method("POST")) assert valid_dest_response == step_result.get_result()
def test_get_last_timestamp_default_timestamp(): config = { 'storage_dir': '/tmp', 'last_success_lookup': 'tests/storage/items/items_last_success.txt' } job_config = ItemsJobConfig(config, 'test') get_last_timestamp = GetLastTimestamp(job_config) step_result = get_last_timestamp.execute() assert step_result.was_successful() is True last_timestamp = step_result.get_result() assert "202007021300" == last_timestamp
def test_get_last_timestamp_bad_file(): config = { 'storage_dir': '/tmp', 'last_success_lookup': 'tests/storage/items/items_last_success.txt' } job_config = ItemsJobConfig(config, 'test') # Override "last_success_filepath" job_config[ 'last_success_filepath'] = 'tests/resources/items/non_existent_response.json' get_last_timestamp = GetLastTimestamp(job_config) with pytest.raises(FileNotFoundError): get_last_timestamp.execute()
def test_get_last_timestamp_no_timestamp_in_file(): config = { 'storage_dir': '/tmp', 'last_success_lookup': 'tests/storage/items/items_last_success.txt' } job_config = ItemsJobConfig(config, 'test') # Override "last_success_filepath" last_success_filepath = 'tests/resources/items/no_timestamp_src_response.json' job_config['last_success_filepath'] = last_success_filepath get_last_timestamp = GetLastTimestamp(job_config) step_result = get_last_timestamp.execute() assert step_result.was_successful() is False assert f"Could not find timestamp in {last_success_filepath}" in step_result.get_errors( )
def test_validate_preconditions_returns_true_if_all_preconditions_are_met(): config = { 'caiasoft_api_key': 'SECRET_CAIASOFT_API_KEY', 'source_url': 'http://example.com/source', 'dest_new_url': 'http://example.org/dest/incoming', 'dest_updates_url': 'http://example.org/dest/updates', 'log_dir': '/tmp/', 'storage_dir': '/tmp/', 'last_success_lookup': 'tests/storage/items/items_last_success.txt', 'last_success_filepath': 'etc/items_FIRST.json', } job_config = ItemsJobConfig(config) validate_job_preconditions = ValidateJobPreconditions(job_config) step_result = validate_job_preconditions.execute() assert step_result.was_successful() is True
def create_job_configuration(start_time: str) -> ItemsJobConfig: """ Creates the new job configuration """ # Create job configuration config = { 'source_url': os.getenv("ITEMS_SOURCE_URL", default=""), 'dest_new_url': os.getenv("ITEMS_DEST_NEW_URL", default=""), 'dest_updates_url': os.getenv("ITEMS_DEST_UPDATES_URL", default=""), 'caiasoft_api_key': os.getenv('CAIASOFT_API_KEY', default=""), 'storage_dir': os.getenv('ITEMS_STORAGE_DIR', default=""), 'last_success_lookup': os.getenv('ITEMS_LAST_SUCCESS_LOOKUP', default="") } job_id_prefix = "caia.items" job_config = ItemsJobConfig(config, job_id_prefix, start_time) logger.info(f"Job Id: {job_config['job_id']}") logger.debug(f"job_config={job_config}") return job_config