Skip to content

Commit

Permalink
delete InteractsWithDynamoDB trait
Browse files Browse the repository at this point in the history
  • Loading branch information
musonza committed Oct 19, 2023
1 parent 39363b6 commit d7150e1
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 106 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"nunomaduro/larastan": "^2.6",
"vimeo/psalm": "^5.4",
"rector/rector": "^0.15.2",
"laravel/pint": "^1.3",
"tuupola/ksuid": "^2.1"
"laravel/pint": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 11 additions & 8 deletions src/Commands/SetupDynamoDbTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ public function transformGlobalSecondaryIndexes(array $gsiConfig): array
$gsiAttributeDefinitions = [];

foreach ($gsiConfig as $gsi) {
$projectionType = $gsi['projection']['ProjectionType'] ?? 'ALL';
$readCapacityUnits = $gsi['provisioned_throughput']['ReadCapacityUnits'] ?? 5;
$writeCapacityUnits = $gsi['provisioned_throughput']['WriteCapacityUnits'] ?? 5;

$transformedGSIs[] = [
'IndexName' => $gsi['index_name'],
'KeySchema' => $this->getKeySchema(
$gsi['key_schema']['partition_key'],
$gsi['key_schema']['sort_key']
),
// Assuming all attributes are to be included in the GSI
'Projection' => [
'ProjectionType' => 'ALL',
'ProjectionType' => $projectionType,
],
// Assuming a provisioned throughput of 5 read and 5 write capacity units for each GSI
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5,
'ReadCapacityUnits' => $readCapacityUnits,
'WriteCapacityUnits' => $writeCapacityUnits,
],
];

Expand All @@ -106,9 +108,10 @@ public function transformGlobalSecondaryIndexes(array $gsiConfig): array
protected function getAttributeDefinitions(array $attributes, array $globalSecondaryIndexes = []): array
{
$gsiAttributes = Collection::make($globalSecondaryIndexes)->reduce(function ($carry, $gsi) {
foreach ($gsi['key_schema'] as $attributeName) {
// TODO Assuming all GSI attributes are of type 'S' (string)
$carry[$attributeName] = 'S';
$gsiAttributes = $gsi['attributes'] ?? [];

foreach ($gsiAttributes as $attributeName => $type) {
$carry[$attributeName] = $type;
}

return $carry;
Expand Down
87 changes: 0 additions & 87 deletions src/InteractsWithDynamoDB.php

This file was deleted.

77 changes: 76 additions & 1 deletion tests/Feature/ExampleFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,82 @@ public function setUp(): void

public function testCreatesTablesFromConfiguration()
{
$this->assertTableExists(self::TABLE_NAME);
$data = [
'AttributeDefinitions' => [
[
'AttributeName' => 'PK',
'AttributeType' => 'S',
],
[
'AttributeName' => 'SK',
'AttributeType' => 'S',
],
[
'AttributeName' => 'GSI1PK',
'AttributeType' => 'S',
],
[
'AttributeName' => 'GSI1SK',
'AttributeType' => 'S',
],
[
'AttributeName' => 'GSI2PK',
'AttributeType' => 'S',
],
[
'AttributeName' => 'GSI2SK',
'AttributeType' => 'N',
],
],
'GlobalSecondaryIndexes' => [
[
'IndexName' => 'GSI1',
'KeySchema' => [
[
'AttributeName' => 'GSI1PK',
'KeyType' => 'HASH',
],
[
'AttributeName' => 'GSI1SK',
'KeyType' => 'RANGE',
],
],
'Projection' => [
'ProjectionType' => 'ALL',
],
'IndexStatus' => 'ACTIVE',
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 5,
],
'IndexSizeBytes' => 0,
],
[
'IndexName' => 'GSI2',
'KeySchema' => [
[
'AttributeName' => 'GSI2PK',
'KeyType' => 'HASH',
],
[
'AttributeName' => 'GSI2SK',
'KeyType' => 'RANGE',
],
],
'Projection' => [
'ProjectionType' => 'KEYS_ONLY',
],
'IndexStatus' => 'ACTIVE',
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 5,
],
'IndexSizeBytes' => 0,
],
],
];

$this->assertTableExists(self::TABLE_NAME, $data);
}

public function testFetchUserPosts(): void
Expand Down
7 changes: 0 additions & 7 deletions tests/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace Musonza\DynamoBreeze\Tests;

use Aws\DynamoDb\DynamoDbClient;
use Carbon\Carbon;
use Musonza\DynamoBreeze\Commands\SetupDynamoDbTables;
use Musonza\DynamoBreeze\Tests\Database\Seeders\DynamoDbTableSeeder;
use Tuupola\KsuidFactory;

class FeatureTestCase extends TestCase
{
Expand All @@ -30,9 +28,4 @@ public function seedDynamoDbTable(array $data, string $tableName): void
$seeder = app(DynamoDbTableSeeder::class);
$seeder->seed($data, $tableName);
}

public function generateKSUID(Carbon $date): string
{
return KsuidFactory::fromTimestamp($date->getTimestamp())->string();
}
}
50 changes: 49 additions & 1 deletion tests/Traits/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,64 @@

trait Helpers
{
private function assertTableExists(string $tableName): void
private function assertTableExists(string $tableName, array $data = []): void
{
try {
$result = $this->client->describeTable(['TableName' => $tableName]);

$this->assertEquals($tableName, $result['Table']['TableName']);

if (isset($data['AttributeDefinitions'])) {
$this->assertAttributeDefinitions($data['AttributeDefinitions'], $result['Table']['AttributeDefinitions']);
}

if (isset($data['GlobalSecondaryIndexes'])) {
$this->assertGlobalSecondaryIndexes($data['GlobalSecondaryIndexes'], $result['Table']['GlobalSecondaryIndexes'] ?? []);
}
} catch (DynamoDbException $e) {
$this->fail("Table [$tableName] was not created. ".$e->getMessage());
}
}

private function assertAttributeDefinitions(array $expectedAttributes, array $actualAttributes): void
{
$expectedAttributeDefinitions = array_map(function ($attr) {
return [
'AttributeName' => $attr['AttributeName'],
'AttributeType' => $attr['AttributeType'],
];
}, $expectedAttributes);

$this->assertEquals($expectedAttributeDefinitions, $actualAttributes);
}

private function assertGlobalSecondaryIndexes(array $expectedGSIs, array $actualGSIs): void
{
foreach ($expectedGSIs as $expectedGSI) {
$foundGSI = false;

foreach ($actualGSIs as $actualGSI) {
if ($expectedGSI['IndexName'] === $actualGSI['IndexName']) {
$foundGSI = true;

// Compare only the keys that are present in expected GSI
foreach ($expectedGSI as $key => $value) {
if (array_key_exists($key, $actualGSI)) {
$this->assertEquals($value, $actualGSI[$key], "Index: {$actualGSI['IndexName']}");
}
}

// Break out of the inner loop if we've found a match
break;
}
}

if (! $foundGSI) {
$this->fail("GSI with IndexName {$expectedGSI['IndexName']} not found.");
}
}
}

private function fetchUserPosts(int $userId): DynamoBreezeResult
{
return DynamoBreeze::table(self::TABLE_IDENTIFIER)
Expand Down
17 changes: 17 additions & 0 deletions tests/config/gsis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,22 @@
'GSI1SK' => 'S',
],
],
'GSI2' => [
'index_name' => 'GSI2',
'key_schema' => [
'partition_key' => 'GSI2PK',
'sort_key' => 'GSI2SK',
],
'attributes' => [
'GSI2PK' => 'S',
'GSI2SK' => 'N',
],
'provisioned_throughput' => [
'ReadCapacityUnits' => 10,
],
'projection' => [
'ProjectionType' => 'KEYS_ONLY',
],
],
// ... other GSIs ...
];

0 comments on commit d7150e1

Please sign in to comment.