def testWrite(self, cassette: Cassette):
     self.reset()
     response = requests.get("http://example.com")
     self.assertIn("This domain is for use", response.text)
     self.assertFalse(os.path.exists(cassette.storage_file))
     cassette.dump()
     self.assertTrue(os.path.exists(cassette.storage_file))
     os.remove(cassette.storage_file)
    def test_regex_write(self, cassette: Cassette):
        self.assertIsNotNone(cassette)

        self.use_requests()

        self.assertIn("requests.sessions", cassette.storage_object)

        self.assertFalse(Path(cassette.storage_file).exists())
        cassette.dump()
        self.assertTrue(Path(cassette.storage_file).exists())
        Path(cassette.storage_file).unlink()
    def test_write(self, cassette: Cassette):
        self.assertIsNotNone(cassette)
        self.assertEqual(self.count_cassette_setup_triggered, 1)
        self.assertEqual(self.count_cassette_teardown_triggered, 0)

        self.use_requests()

        self.assertIn("requests.sessions", cassette.storage_object)

        self.assertFalse(Path(cassette.storage_file).exists())
        cassette.dump()
        self.assertTrue(Path(cassette.storage_file).exists())
        Path(cassette.storage_file).unlink()
Exemple #4
0
def recording(
    what: str,
    decorate: Optional[Union[List[Callable], Callable]] = None,
    replace: Optional[Callable] = None,
    storage_file: Optional[str] = None,
    storage_keys_strategy=StorageKeysInspectSimple,
):
    """
    Context manager which can be used to store calls of the function and
    and replay responses on the next run.

    :param what: str - full path of function inside module
    :param decorate: function decorator what will be applied to what, could be also list of
                     decorators, to be able to apply more decorators on one function
                     eg. store files and store output
    :param replace: replace original function by given one
    :param storage_file: path for storage file if you don't want to use default location
    :param storage_keys_strategy: you can change key strategy for storing data
                                  default simple one avoid to store stack information
    """
    cassette = Cassette()
    cassette.storage_file = storage_file
    cassette.data_miner.key_stategy_cls = storage_keys_strategy
    # ensure that directory structure exists already
    os.makedirs(os.path.dirname(cassette.storage_file), exist_ok=True)
    # use default decorator for context manager if not given.
    if decorate is None and replace is None:
        logger.info(f"Using default decorator for {what}")
        decorate = Guess.decorator_plain(cassette=cassette)
    elif decorate is not None and replace is not None:
        raise ValueError(
            "right one from [decorate, replace] parameter has to be set.")
    # Store values and their replacements for modules to be able to _revert_modules changes back
    module_list = _parse_and_replace_sys_modules(what=what,
                                                 cassette=cassette,
                                                 decorate=decorate,
                                                 replace=replace)
    try:
        yield cassette
    finally:
        # dump data to storage file
        cassette.dump()
        _revert_modules(module_list)
Exemple #5
0
from requre.helpers.files import StoreFiles
from requre.cassette import Cassette
from tempfile import mkdtemp
import os

cassette = Cassette()
cassette.storage_file = "/tmp/files.yaml"
temp_dir = mkdtemp()


@StoreFiles.where_arg_references(key_position_params_dict={"dir_name": 0},
                                 cassette=cassette)
def return_result(dir_name):
    file_name = input("enter file name: ")
    with open(os.path.join(dir_name, file_name), "w") as fd:
        fd.write("empty")
    return file_name


print("returned file name:", return_result(temp_dir))
print("dir name (current):", temp_dir)
print("files:", os.listdir(temp_dir))
cassette.dump()