|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include "../include/azurecosmos.h" |
| 8 | + |
| 9 | +#define SENTINEL_VALUE "test-sentinel-12345" |
| 10 | +#define ITEM_ID "test-item-id" |
| 11 | +#define PARTITION_KEY_VALUE "test-partition" |
| 12 | + |
| 13 | +int main() { |
| 14 | + cosmos_enable_tracing(); |
| 15 | + |
| 16 | + // Get environment variables |
| 17 | + const char *endpoint = getenv("AZURE_COSMOS_ENDPOINT"); |
| 18 | + const char *key = getenv("AZURE_COSMOS_KEY"); |
| 19 | + const char *database_name = getenv("AZURE_COSMOS_DATABASE"); |
| 20 | + const char *container_name = getenv("AZURE_COSMOS_CONTAINER"); |
| 21 | + |
| 22 | + if (!endpoint || !key || !database_name || !container_name) { |
| 23 | + printf("Error: Missing required environment variables.\n"); |
| 24 | + printf("Required: AZURE_COSMOS_ENDPOINT, AZURE_COSMOS_KEY, AZURE_COSMOS_DATABASE, AZURE_COSMOS_CONTAINER\n"); |
| 25 | + return 1; |
| 26 | + } |
| 27 | + |
| 28 | + printf("Running Cosmos DB item CRUD test...\n"); |
| 29 | + printf("Endpoint: %s\n", endpoint); |
| 30 | + printf("Database: %s\n", database_name); |
| 31 | + printf("Container: %s\n", container_name); |
| 32 | + |
| 33 | + struct CosmosError error = {0}; |
| 34 | + struct CosmosClient *client = NULL; |
| 35 | + struct DatabaseClient *database = NULL; |
| 36 | + struct ContainerClient *container = NULL; |
| 37 | + char *read_json = NULL; |
| 38 | + int result = 0; |
| 39 | + |
| 40 | + // Create Cosmos client |
| 41 | + CosmosErrorCode code = cosmos_client_create(endpoint, key, &client, &error); |
| 42 | + if (code != Success) { |
| 43 | + printf("Failed to create Cosmos client: %s (code: %d)\n", error.message, error.code); |
| 44 | + result = 1; |
| 45 | + goto cleanup; |
| 46 | + } |
| 47 | + printf("✓ Created Cosmos client\n"); |
| 48 | + |
| 49 | + // Get database client |
| 50 | + code = cosmos_client_database_client(client, database_name, &database, &error); |
| 51 | + if (code != Success) { |
| 52 | + printf("Failed to get database client: %s (code: %d)\n", error.message, error.code); |
| 53 | + result = 1; |
| 54 | + goto cleanup; |
| 55 | + } |
| 56 | + printf("✓ Got database client\n"); |
| 57 | + |
| 58 | + // Get container client |
| 59 | + code = cosmos_database_container_client(database, container_name, &container, &error); |
| 60 | + if (code != Success) { |
| 61 | + printf("Failed to get container client: %s (code: %d)\n", error.message, error.code); |
| 62 | + result = 1; |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + printf("✓ Got container client\n"); |
| 66 | + |
| 67 | + // Construct JSON document with sentinel value |
| 68 | + char json_data[512]; |
| 69 | + snprintf(json_data, sizeof(json_data), |
| 70 | + "{\"id\":\"%s\",\"partitionKey\":\"%s\",\"name\":\"Test Document\",\"sentinel\":\"%s\",\"description\":\"This is a test document for CRUD operations\"}", |
| 71 | + ITEM_ID, PARTITION_KEY_VALUE, SENTINEL_VALUE); |
| 72 | + |
| 73 | + printf("Upserting document: %s\n", json_data); |
| 74 | + |
| 75 | + // Upsert the item |
| 76 | + code = cosmos_container_upsert_item(container, PARTITION_KEY_VALUE, json_data, &error); |
| 77 | + if (code != Success) { |
| 78 | + printf("Failed to upsert item: %s (code: %d)\n", error.message, error.code); |
| 79 | + result = 1; |
| 80 | + goto cleanup; |
| 81 | + } |
| 82 | + printf("✓ Upserted item successfully\n"); |
| 83 | + |
| 84 | + // Read the item back |
| 85 | + code = cosmos_container_read_item(container, PARTITION_KEY_VALUE, ITEM_ID, &read_json, &error); |
| 86 | + if (code != Success) { |
| 87 | + printf("Failed to read item: %s (code: %d)\n", error.message, error.code); |
| 88 | + result = 1; |
| 89 | + goto cleanup; |
| 90 | + } |
| 91 | + printf("✓ Read item successfully\n"); |
| 92 | + |
| 93 | + printf("Read back JSON: %s\n", read_json); |
| 94 | + |
| 95 | + // Verify the sentinel value is present in the returned JSON |
| 96 | + if (strstr(read_json, SENTINEL_VALUE) == NULL) { |
| 97 | + printf("❌ FAIL: Sentinel value '%s' not found in returned JSON\n", SENTINEL_VALUE); |
| 98 | + result = 1; |
| 99 | + goto cleanup; |
| 100 | + } |
| 101 | + |
| 102 | + // Verify the item ID is present |
| 103 | + if (strstr(read_json, ITEM_ID) == NULL) { |
| 104 | + printf("❌ FAIL: Item ID '%s' not found in returned JSON\n", ITEM_ID); |
| 105 | + result = 1; |
| 106 | + goto cleanup; |
| 107 | + } |
| 108 | + |
| 109 | + printf("✓ All assertions passed!\n"); |
| 110 | + printf("SUCCESS: Item CRUD test completed successfully.\n"); |
| 111 | + |
| 112 | +cleanup: |
| 113 | +// // Free all allocated resources |
| 114 | +// if (read_json) { |
| 115 | +// cosmos_string_free(read_json); |
| 116 | +// } |
| 117 | +// if (container) { |
| 118 | +// cosmos_container_free(container); |
| 119 | +// } |
| 120 | +// if (database) { |
| 121 | +// cosmos_database_free(database); |
| 122 | +// } |
| 123 | +// if (client) { |
| 124 | +// cosmos_client_free(client); |
| 125 | +// } |
| 126 | +// if (error.message) { |
| 127 | +// cosmos_error_free(&error); |
| 128 | +// } |
| 129 | + |
| 130 | + return result; |
| 131 | +} |
0 commit comments