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