|
| 1 | +import os |
| 2 | +from dotenv import load_dotenv |
| 3 | + |
| 4 | +import pytest_asyncio |
| 5 | +import pytest |
| 6 | + |
| 7 | +dotenv_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '.env') |
| 8 | +if os.path.exists(dotenv_path): |
| 9 | + load_dotenv(dotenv_path) |
| 10 | + |
| 11 | +from arangodb_client import ArangoDatabaseClient |
| 12 | + |
| 13 | +@pytest_asyncio.fixture(scope="function") |
| 14 | +async def db_client(): |
| 15 | + client = ArangoDatabaseClient() |
| 16 | + await client.connect() |
| 17 | + yield client |
| 18 | + await client.close() |
| 19 | + |
| 20 | +collection_name = "test_collection" |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_create_non_existing_collection(db_client): |
| 24 | + if await db_client.has_collection(collection_name): |
| 25 | + await db_client.delete_collection(collection_name) |
| 26 | + |
| 27 | + assert await db_client.has_collection(collection_name) is False |
| 28 | + |
| 29 | + creation_result = await db_client.create_collection(collection_name) |
| 30 | + assert creation_result is True |
| 31 | + |
| 32 | + assert await db_client.has_collection(collection_name) is True |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_create_existing_collection(db_client): |
| 36 | + if not await db_client.has_collection(collection_name): |
| 37 | + await db_client.create_collection(collection_name) |
| 38 | + |
| 39 | + assert await db_client.has_collection(collection_name) is True |
| 40 | + |
| 41 | + creation_result = await db_client.create_collection(collection_name) |
| 42 | + assert creation_result is False |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_insert_documents_to_existing_collection(db_client): |
| 46 | + if not await db_client.has_collection(collection_name): |
| 47 | + await db_client.create_collection(collection_name) |
| 48 | + |
| 49 | + assert await db_client.has_collection(collection_name) is True |
| 50 | + |
| 51 | + documents = [{"_key": "1", "name": "Andrew", "age": 30}, |
| 52 | + {"_key": "2", "name": "Anton", "age": 20}, |
| 53 | + {"_key": "3", "name": "Egor", "age": 21}] |
| 54 | + |
| 55 | + insertion_result = await db_client.insert_documents_to_collection(collection_name, documents) |
| 56 | + assert insertion_result is True |
| 57 | + |
| 58 | + result_documents = await db_client.get_all_documents_from_collection(collection_name) |
| 59 | + for i, result_document in enumerate(result_documents): |
| 60 | + assert (result_document["_key"] == documents[i]["_key"] |
| 61 | + and result_document["name"] == documents[i]["name"] |
| 62 | + and result_document["age"] == documents[i]["age"]) |
| 63 | + |
| 64 | +@pytest.mark.asyncio |
| 65 | +async def test_insert_documents_to_non_existing_collection(db_client): |
| 66 | + non_existing_collection_name = "non_existing_collection" |
| 67 | + |
| 68 | + if await db_client.has_collection(non_existing_collection_name): |
| 69 | + await db_client.delete_collection(non_existing_collection_name) |
| 70 | + |
| 71 | + assert await db_client.has_collection(non_existing_collection_name) is False |
| 72 | + |
| 73 | + documents = [{"_key": "7", "name": "Gleb", "age": 15}, |
| 74 | + {"_key": "8", "name": "Max", "age": 44}, |
| 75 | + {"_key": "9", "name": "Ilya", "age": 19}] |
| 76 | + |
| 77 | + insertion_result = await db_client.insert_documents_to_collection(non_existing_collection_name, documents) |
| 78 | + assert insertion_result is False |
| 79 | + |
| 80 | + result_documents = await db_client.get_all_documents_from_collection(non_existing_collection_name) |
| 81 | + assert result_documents is None |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_update_document_with_known_key(db_client): |
| 85 | + if not await db_client.has_collection(collection_name): |
| 86 | + await db_client.create_collection(collection_name) |
| 87 | + |
| 88 | + assert await db_client.has_collection(collection_name) is True |
| 89 | + |
| 90 | + document = {"_key": "123", "name": "Aleksandr", "age": 20} |
| 91 | + |
| 92 | + updated_data = {"name": "Kirill"} |
| 93 | + |
| 94 | + insertion_result = await db_client.insert_documents_to_collection(collection_name, [document]) |
| 95 | + assert insertion_result is True |
| 96 | + |
| 97 | + update_result = await db_client.update_document_by_key(collection_name, "123", updated_data) |
| 98 | + assert update_result is True |
| 99 | + |
| 100 | + result_document = await db_client.get_document_by_key(collection_name, "123") |
| 101 | + assert result_document["name"] == "Kirill" |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_delete_existing_document(db_client): |
| 105 | + if not await db_client.has_collection(collection_name): |
| 106 | + await db_client.create_collection(collection_name) |
| 107 | + |
| 108 | + assert await db_client.has_collection(collection_name) is True |
| 109 | + |
| 110 | + document = {"_key": "123", "name": "Aleksandr", "age": 20} |
| 111 | + |
| 112 | + insertion_result = await db_client.insert_documents_to_collection(collection_name, [document]) |
| 113 | + assert insertion_result is True |
| 114 | + |
| 115 | + assert await db_client.get_document_by_key(collection_name, "123") is not None |
| 116 | + |
| 117 | + deletion_result = await db_client.delete_document_by_key(collection_name, "123") |
| 118 | + assert deletion_result is True |
| 119 | + |
| 120 | + result_document = await db_client.get_document_by_key(collection_name, "123") |
| 121 | + assert result_document is None |
| 122 | + |
| 123 | +@pytest.mark.asyncio |
| 124 | +async def test_delete_non_existing_document(db_client): |
| 125 | + if not await db_client.has_collection(collection_name): |
| 126 | + await db_client.create_collection(collection_name) |
| 127 | + |
| 128 | + assert await db_client.has_collection(collection_name) is True |
| 129 | + |
| 130 | + if await db_client.get_document_by_key(collection_name, "doc_key") is not None: |
| 131 | + await db_client.delete_document_by_key(collection_name, "doc_key") |
| 132 | + |
| 133 | + assert await db_client.get_document_by_key(collection_name, "doc_key") is None |
| 134 | + |
| 135 | + deletion_result = await db_client.delete_document_by_key(collection_name, "doc_key") |
| 136 | + assert deletion_result is False |
| 137 | + |
| 138 | +@pytest.mark.asyncio |
| 139 | +async def test_find_existing_document(db_client): |
| 140 | + if not await db_client.has_collection(collection_name): |
| 141 | + await db_client.create_collection(collection_name) |
| 142 | + |
| 143 | + assert await db_client.has_collection(collection_name) is True |
| 144 | + |
| 145 | + document = {"_key": "111", "name": "Andrew", "age": 30} |
| 146 | + document_key = "111" |
| 147 | + |
| 148 | + if await db_client.get_document_by_key(collection_name, document_key) is None: |
| 149 | + await db_client.insert_documents_to_collection(collection_name, [document]) |
| 150 | + |
| 151 | + assert await db_client.get_document_by_key(collection_name, document_key) is not None |
| 152 | + |
| 153 | + search_result = await db_client.find_documents_by_fields(collection_name, {"name": "Andrew", "age": 30}) |
| 154 | + assert search_result is not None and len(search_result) > 0 |
| 155 | + |
| 156 | +@pytest.mark.asyncio |
| 157 | +async def test_find_non_existing_document(db_client): |
| 158 | + if not await db_client.has_collection(collection_name): |
| 159 | + await db_client.create_collection(collection_name) |
| 160 | + |
| 161 | + assert await db_client.has_collection(collection_name) is True |
| 162 | + |
| 163 | + document = { "_key": "cghgvjb", "name": "Aleksey", "age": 30 } |
| 164 | + document_key = "cghgvjb" |
| 165 | + |
| 166 | + if await db_client.get_document_by_key(collection_name, document_key) is not None: |
| 167 | + await db_client.delete_document_by_key(collection_name, document_key) |
| 168 | + |
| 169 | + assert await db_client.get_document_by_key(collection_name, document_key) is None |
| 170 | + |
| 171 | + search_result = await db_client.find_documents_by_fields(collection_name, {"name": "Aleksey", "age": 30}) |
| 172 | + assert search_result is not None and len(search_result) == 0 |
0 commit comments