|
| 1 | +# Copyright (c) 2024 Microsoft Corporation. |
| 2 | +# Licensed under the MIT License |
| 3 | +"""StorageFactory Tests. |
| 4 | +
|
| 5 | +These tests will test the StorageFactory class and the creation of each storage type that is natively supported. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from graphrag.config.enums import StorageType |
| 13 | +from graphrag.storage.blob_pipeline_storage import BlobPipelineStorage |
| 14 | +from graphrag.storage.cosmosdb_pipeline_storage import CosmosDBPipelineStorage |
| 15 | +from graphrag.storage.factory import StorageFactory |
| 16 | +from graphrag.storage.file_pipeline_storage import FilePipelineStorage |
| 17 | +from graphrag.storage.memory_pipeline_storage import MemoryPipelineStorage |
| 18 | + |
| 19 | +# cspell:disable-next-line well-known-key |
| 20 | +WELL_KNOWN_BLOB_STORAGE_KEY = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;" |
| 21 | +# cspell:disable-next-line well-known-key |
| 22 | +WELL_KNOWN_COSMOS_CONNECTION_STRING = "AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" |
| 23 | + |
| 24 | + |
| 25 | +def test_create_blob_storage(): |
| 26 | + kwargs = { |
| 27 | + "type": "blob", |
| 28 | + "connection_string": WELL_KNOWN_BLOB_STORAGE_KEY, |
| 29 | + "base_dir": "testbasedir", |
| 30 | + "container_name": "testcontainer", |
| 31 | + } |
| 32 | + storage = StorageFactory.create_storage(StorageType.blob, kwargs) |
| 33 | + assert isinstance(storage, BlobPipelineStorage) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.skipif( |
| 37 | + not sys.platform.startswith("win"), |
| 38 | + reason="cosmosdb emulator is only available on windows runners at this time", |
| 39 | +) |
| 40 | +def test_create_cosmosdb_storage(): |
| 41 | + kwargs = { |
| 42 | + "type": "cosmosdb", |
| 43 | + "connection_string": WELL_KNOWN_COSMOS_CONNECTION_STRING, |
| 44 | + "base_dir": "testdatabase", |
| 45 | + "container_name": "testcontainer", |
| 46 | + } |
| 47 | + storage = StorageFactory.create_storage(StorageType.cosmosdb, kwargs) |
| 48 | + assert isinstance(storage, CosmosDBPipelineStorage) |
| 49 | + |
| 50 | + |
| 51 | +def test_create_file_storage(): |
| 52 | + kwargs = {"type": "file", "base_dir": "/tmp/teststorage"} |
| 53 | + storage = StorageFactory.create_storage(StorageType.file, kwargs) |
| 54 | + assert isinstance(storage, FilePipelineStorage) |
| 55 | + |
| 56 | + |
| 57 | +def test_create_memory_storage(): |
| 58 | + kwargs = {"type": "memory"} |
| 59 | + storage = StorageFactory.create_storage(StorageType.memory, kwargs) |
| 60 | + assert isinstance(storage, MemoryPipelineStorage) |
| 61 | + |
| 62 | + |
| 63 | +def test_register_and_create_custom_storage(): |
| 64 | + class CustomStorage: |
| 65 | + def __init__(self, **kwargs): |
| 66 | + pass |
| 67 | + |
| 68 | + StorageFactory.register("custom", CustomStorage) |
| 69 | + storage = StorageFactory.create_storage("custom", {}) |
| 70 | + assert isinstance(storage, CustomStorage) |
| 71 | + |
| 72 | + |
| 73 | +def test_create_unknown_storage(): |
| 74 | + with pytest.raises(ValueError, match="Unknown storage type: unknown"): |
| 75 | + StorageFactory.create_storage("unknown", {}) |
0 commit comments