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