Skip to content

Commit

Permalink
Remove redundant serialization context method
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jul 8, 2022
1 parent de222da commit f7f6f72
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 69 deletions.
87 changes: 30 additions & 57 deletions src/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Dynamite\Exception\SchemaException;
use Dynamite\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

final class Table
{
Expand Down Expand Up @@ -121,14 +120,36 @@ public function addGlobalSecondaryIndex(
$this->globalSecondaryIndexes = [];
}

$this->globalSecondaryIndexes[] = $this->buildGlobalSecondaryIndex(
$name,
$projectionType,
$hashAttribute,
$rangeAttribute,
$writeCapacity,
$readCapacity
);
$keySchema = [
[
'AttributeName' => $hashAttribute,
'KeyType' => KeyType::HASH,
],
];

if ($rangeAttribute !== null) {
$keySchema[] = [
'AttributeName' => $rangeAttribute,
'KeyType' => KeyType::RANGE,
];
}

$provisionedThroughput = null;
if ($readCapacity !== null && $writeCapacity !== null) {
$provisionedThroughput = [
'ReadCapacityUnits' => $readCapacity,
'WriteCapacityUnits' => $writeCapacity,
];
}

$this->globalSecondaryIndexes[] = [
'IndexName' => $name,
'KeySchema' => $keySchema,
'Projection' => [
'ProjectionType' => $projectionType,
],
'ProvisionedThroughput' => $provisionedThroughput,
];
}

public function getGlobalSecondaryIndexes(): ?array
Expand Down Expand Up @@ -161,46 +182,6 @@ function (array $index): array {
);
}

private function buildGlobalSecondaryIndex(
string $name,
string $projectionType,
string $hashAttribute,
?string $rangeAttribute,
?int $writeCapacity,
?int $readCapacity
): array {
$keySchema = [
[
'AttributeName' => $hashAttribute,
'KeyType' => KeyType::HASH,
],
];

if ($rangeAttribute !== null) {
$keySchema[] = [
'AttributeName' => $rangeAttribute,
'KeyType' => KeyType::RANGE,
];
}

$provisionedThroughput = null;
if ($readCapacity !== null && $writeCapacity !== null) {
$provisionedThroughput = [
'ReadCapacityUnits' => $readCapacity,
'WriteCapacityUnits' => $writeCapacity,
];
}

return [
'IndexName' => $name,
'KeySchema' => $keySchema,
'Projection' => [
'ProjectionType' => $projectionType,
],
'ProvisionedThroughput' => $provisionedThroughput,
];
}

public function addLocalSecondaryIndex(string $name, string $hashAttribute, ?string $rangeAttribute): void
{
if ($this->localSecondaryIndexes === null) {
Expand Down Expand Up @@ -267,12 +248,4 @@ public function assertHashKeySet(): void
throw SchemaException::hashKeyNotSet();
}
}

public function getSerializationContext(string $group): array
{
return [
'groups' => $group,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
];
}
}
12 changes: 0 additions & 12 deletions tests/Unit/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,4 @@ public function testAddAttribute(): void

self::assertSame($expected, $schema->getAttributeDefinitions());
}

public function testGetSerializationContext(): void
{
$schema = new Table();

$expected = [
'groups' => 'create',
'skip_null_values' => true,
];

self::assertSame($expected, $schema->getSerializationContext('create'));
}
}
37 changes: 37 additions & 0 deletions tests/Unit/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,48 @@
use AsyncAws\DynamoDb\Result\CreateTableOutput;
use Dynamite\AbstractTable;
use Dynamite\Exception\SchemaException;
use Dynamite\Exception\ValidationException;
use Dynamite\TableInterface;
use Psr\Log\LogLevel;

class TableTest extends UnitTestCase
{
public function testLoadWithoutTableNameSet(): void
{
$validator = $this->createValidator();
$serializer = $this->createSerializer();
$dynamoDbClientMock = $this->createMock(DynamoDbClient::class);
$logger = $this->createTestLogger();

$table = new class() extends AbstractTable implements TableInterface {
public function configure(): void
{
$this->addAttributes([
['Id', ScalarAttributeType::S],
['Email', ScalarAttributeType::S],
]);
}
};

try {
$table->setValidator($validator);
$table->setNormalizer($serializer);
$table->create($dynamoDbClientMock, $logger);

self::fail('Exception is not thrown');
} catch (\Throwable $e) {
$expectedErrors = [
'tableName' => [
'Table name is not defined',
],
];

self::assertInstanceOf(ValidationException::class, $e);
self::assertSame('Validation failed', $e->getMessage());
self::assertSame($expectedErrors, $e->getErrors());
}
}

public function testAddAttributeWithUnexpectedType(): void
{
$this->expectException(SchemaException::class);
Expand Down

0 comments on commit f7f6f72

Please sign in to comment.