Repository: apify/crawlee-python
File: tests/unit/memory_storage_client/test_memory_storage_client.py (Python)

Code before:
            write_metadata=True,
        ),
    )
    ms_no_metadata = MemoryStorageClient(
        Configuration(
            crawlee_storage_dir=str(tmp_path),  # type: ignore
            write_metadata=False,
        )
    )
    datasets_client = ms.datasets()
    datasets_no_metadata_client = ms_no_metadata.datasets()
    await datasets_client.get_or_create(name=dataset_name)
    await datasets_no_metadata_client.get_or_create(name=dataset_no_metadata_name)
    assert os.path.exists(os.path.join(ms.datasets_directory, dataset_name, METADATA_FILENAME)) is True
    assert (
        os.path.exists(os.path.join(ms_no_metadata.datasets_directory, dataset_no_metadata_name, METADATA_FILENAME))
        is False
    )


@pytest.mark.parametrize(
    ('persist_storage'),
    [
        (True),
        (False),
    ],
)
async def test_persist_storage(persist_storage: bool, tmp_path: Path) -> None:  # noqa: FBT001
    ms = MemoryStorageClient(
        Configuration(
            crawlee_storage_dir=str(tmp_path),  # type: ignore
            persist_storage=persist_storage,
        )
    )

    # Key value stores
    kvs_client = ms.key_value_stores()
    kvs_info = await kvs_client.get_or_create(name='kvs')
    await ms.key_value_store(kvs_info.id).set_record('test', {'x': 1}, 'application/json')

    path = Path(ms.key_value_stores_directory) / (kvs_info.name or '') / 'test.json'
    assert os.path.exists(path) is persist_storage

    # Request queues
    rq_client = ms.request_queues()
    rq_info = await rq_client.get_or_create(name='rq')

    request = Request.from_url('http://lorem.com')
    await ms.request_queue(rq_info.id).add_request(request)

    path = Path(ms.request_queues_directory) / (rq_info.name or '') / f'{request.id}.json'

Review comment (suggestion):
Maybe a list of tuples?

```suggestion
    [
        (True,),
        (False,),
    ],
```

Code after:
            write_metadata=True,
        ),
    )
    ms_no_metadata = MemoryStorageClient(
        Configuration(
            crawlee_storage_dir=str(tmp_path),  # type: ignore
            write_metadata=False,
        )
    )
    datasets_client = ms.datasets()
    datasets_no_metadata_client = ms_no_metadata.datasets()
    await datasets_client.get_or_create(name=dataset_name)
    await datasets_no_metadata_client.get_or_create(name=dataset_no_metadata_name)
    assert os.path.exists(os.path.join(ms.datasets_directory, dataset_name, METADATA_FILENAME)) is True
    assert (
        os.path.exists(os.path.join(ms_no_metadata.datasets_directory, dataset_no_metadata_name, METADATA_FILENAME))
        is False
    )


@pytest.mark.parametrize(
    'persist_storage',
    [
        True,
        False,
    ],
)
async def test_persist_storage(persist_storage: bool, tmp_path: Path) -> None:  # noqa: FBT001
    ms = MemoryStorageClient(
        Configuration(
            crawlee_storage_dir=str(tmp_path),  # type: ignore
            persist_storage=persist_storage,
        )
    )

    # Key value stores
    kvs_client = ms.key_value_stores()
    kvs_info = await kvs_client.get_or_create(name='kvs')
    await ms.key_value_store(kvs_info.id).set_record('test', {'x': 1}, 'application/json')

    path = Path(ms.key_value_stores_directory) / (kvs_info.name or '') / 'test.json'
    assert os.path.exists(path) is persist_storage

    # Request queues
    rq_client = ms.request_queues()
    rq_info = await rq_client.get_or_create(name='rq')

    request = Request.from_url('http://lorem.com')
    await ms.request_queue(rq_info.id).add_request(request)

    path = Path(ms.request_queues_directory) / (rq_info.name or '') / f'{request.id}.json'