| 1 |
package entities |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage" |
| 9 |
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/table/tables" |
| 10 |
"github.com/tombuildsstuff/giovanni/storage/internal/auth" |
| 11 |
"github.com/tombuildsstuff/giovanni/testhelpers" |
| 12 |
) |
| 13 |
|
| 14 |
var _ StorageTableEntity = Client{} |
| 15 |
|
| 16 |
func TestEntitiesLifecycle(t *testing.T) { |
| 17 |
client, err := testhelpers.Build(t) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
ctx := context.TODO() |
| 22 |
resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt()) |
| 23 |
accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString()) |
| 24 |
tableName := fmt.Sprintf("table%d", testhelpers.RandomInt()) |
| 25 |
|
| •26 |
testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.Storage) |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
defer client.DestroyTestResources(ctx, resourceGroup, accountName) |
| 31 |
|
| 32 |
storageAuth := auth.NewSharedKeyLiteTableAuthorizer(accountName, testData.StorageAccountKey) |
| 33 |
tablesClient := tables.NewWithEnvironment(client.Environment) |
| 34 |
tablesClient.Client = client.PrepareWithAuthorizer(tablesClient.Client, storageAuth) |
| 35 |
|
| 36 |
t.Logf("[DEBUG] Creating Table..") |
| 37 |
if _, err := tablesClient.Create(ctx, accountName, tableName); err != nil { |
| 38 |
t.Fatalf("Error creating Table %q: %s", tableName, err) |
| 39 |
} |
| 40 |
defer tablesClient.Delete(ctx, accountName, tableName) |
| 41 |
|
| 42 |
entitiesClient := NewWithEnvironment(client.Environment) |
| 43 |
entitiesClient.Client = client.PrepareWithAuthorizer(entitiesClient.Client, storageAuth) |
| 44 |
|
| 45 |
partitionKey := "hello" |
| 46 |
rowKey := "there" |
| 47 |
|
| 48 |
t.Logf("[DEBUG] Inserting..") |
| 49 |
insertInput := InsertEntityInput{ |
| 50 |
MetaDataLevel: NoMetaData, |
| 51 |
PartitionKey: partitionKey, |
| 52 |
RowKey: rowKey, |
| 53 |
Entity: map[string]interface{}{ |
| 54 |
"hello": "world", |
| 55 |
}, |
| 56 |
} |
| 57 |
if _, err := entitiesClient.Insert(ctx, accountName, tableName, insertInput); err != nil { |
| 58 |
t.Logf("Error retrieving: %s", err) |
| 59 |
} |
| 60 |
|
| 61 |
t.Logf("[DEBUG] Insert or Merging..") |
| 62 |
insertOrMergeInput := InsertOrMergeEntityInput{ |
| 63 |
PartitionKey: partitionKey, |
| 64 |
RowKey: rowKey, |
| 65 |
Entity: map[string]interface{}{ |
| 66 |
"hello": "ther88e", |
| 67 |
}, |
| 68 |
} |
| 69 |
if _, err := entitiesClient.InsertOrMerge(ctx, accountName, tableName, insertOrMergeInput); err != nil { |
| 70 |
t.Logf("Error insert/merging: %s", err) |
| 71 |
} |
| 72 |
|
| 73 |
t.Logf("[DEBUG] Insert or Replacing..") |
| 74 |
insertOrReplaceInput := InsertOrReplaceEntityInput{ |
| 75 |
PartitionKey: partitionKey, |
| 76 |
RowKey: rowKey, |
| 77 |
Entity: map[string]interface{}{ |
| 78 |
"hello": "pandas", |
| 79 |
}, |
| 80 |
} |
| 81 |
if _, err := entitiesClient.InsertOrReplace(ctx, accountName, tableName, insertOrReplaceInput); err != nil { |
| 82 |
t.Logf("Error inserting/replacing: %s", err) |
| 83 |
} |
| 84 |
|
| 85 |
t.Logf("[DEBUG] Querying..") |
| 86 |
queryInput := QueryEntitiesInput{ |
| 87 |
MetaDataLevel: NoMetaData, |
| 88 |
} |
| 89 |
results, err := entitiesClient.Query(ctx, accountName, tableName, queryInput) |
| 90 |
if err != nil { |
| 91 |
t.Logf("Error querying: %s", err) |
| 92 |
} |
| 93 |
|
| 94 |
if len(results.Entities) != 1 { |
| 95 |
t.Fatalf("Expected 1 item but got %d", len(results.Entities)) |
| 96 |
} |
| 97 |
|
| 98 |
for _, v := range results.Entities { |
| 99 |
thisPartitionKey := v["PartitionKey"].(string) |
| 100 |
thisRowKey := v["RowKey"].(string) |
| 101 |
if partitionKey != thisPartitionKey { |
| 102 |
t.Fatalf("Expected Partition Key to be %q but got %q", partitionKey, thisPartitionKey) |
| 103 |
} |
| 104 |
if rowKey != thisRowKey { |
| 105 |
t.Fatalf("Expected Partition Key to be %q but got %q", rowKey, thisRowKey) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
t.Logf("[DEBUG] Retrieving..") |
| 110 |
getInput := GetEntityInput{ |
| 111 |
MetaDataLevel: MinimalMetaData, |
| 112 |
PartitionKey: partitionKey, |
| 113 |
RowKey: rowKey, |
| 114 |
} |
| 115 |
getResults, err := entitiesClient.Get(ctx, accountName, tableName, getInput) |
| 116 |
if err != nil { |
| 117 |
t.Logf("Error querying: %s", err) |
| 118 |
} |
| 119 |
|
| 120 |
partitionKey2 := getResults.Entity["PartitionKey"].(string) |
| 121 |
rowKey2 := getResults.Entity["RowKey"].(string) |
| 122 |
if partitionKey2 != partitionKey { |
| 123 |
t.Fatalf("Expected Partition Key to be %q but got %q", partitionKey, partitionKey2) |
| 124 |
} |
| 125 |
if rowKey2 != rowKey { |
| 126 |
t.Fatalf("Expected Row Key to be %q but got %q", rowKey, rowKey2) |
| 127 |
} |
| 128 |
|
| 129 |
t.Logf("[DEBUG] Deleting..") |
| 130 |
deleteInput := DeleteEntityInput{ |
| 131 |
PartitionKey: partitionKey, |
| 132 |
RowKey: rowKey, |
| 133 |
} |
| 134 |
if _, err := entitiesClient.Delete(ctx, accountName, tableName, deleteInput); err != nil { |
| 135 |
t.Logf("Error deleting: %s", err) |
| 136 |
} |
| 137 |
} |