Skip to content

Commit

Permalink
Tests for 'aws_rekognition_detect_moderation_labels' functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew72ru committed Nov 5, 2023
1 parent b177f3c commit 4c9d1b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/File/AppData/AwsRecognitionModerationData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class AwsRecognitionModerationData implements AwsRecognitionDataInterface, Seria
/**
* @var AwsModerationLabel[]
*/
private array $labels = [];
private array $moderationLabels = [];

public static function rules(): array
{
return [
'labelModelVersion' => 'string',
'labels' => [AwsModerationLabel::class],
'moderationModelVersion' => 'string',
'moderationLabels' => [AwsModerationLabel::class],
];
}

Expand All @@ -27,7 +27,7 @@ public function getLabelModelVersion(): ?string
return $this->labelModelVersion;
}

public function setLabelModelVersion(?string $labelModelVersion): self
public function setModerationModelVersion(?string $labelModelVersion): self
{
$this->labelModelVersion = $labelModelVersion;

Expand All @@ -39,13 +39,13 @@ public function setLabelModelVersion(?string $labelModelVersion): self
*/
public function getLabels(): iterable
{
return $this->labels;
return $this->moderationLabels;
}

public function addLabel(AwsModerationLabel $label): self
public function addModerationLabel(AwsModerationLabel $label): self
{
if (!\in_array($label, $this->labels, true)) {
$this->labels[] = $label;
if (!\in_array($label, $this->moderationLabels, true)) {
$this->moderationLabels[] = $label;
}

return $this;
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/WordsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function conversions(): array
'files' => 'file',
'problems' => 'problem',
'labels' => 'label',
'moderationLabels' => 'moderationLabel',
'parents' => 'parent',
'instances' => 'instance',
];
Expand Down
31 changes: 29 additions & 2 deletions tests/AppData/AwsDeserializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use PHPUnit\Framework\TestCase;
use Uploadcare\File\AppData\AwsInstance;
use Uploadcare\File\AppData\AwsLabel;
use Uploadcare\File\AppData\AwsModerationLabel;
use Uploadcare\File\AppData\AwsRecognitionData;
use Uploadcare\File\AppData\AwsRecognitionLabels;
use Uploadcare\File\AppData\AwsRecognitionModerationData;
use Uploadcare\File\AppData\AwsRecognitionModerationLabels;
use Uploadcare\File\AppData\BoundingBox;
use Uploadcare\File\AppData\LabelParent;
use Uploadcare\Interfaces\Serializer\SerializerInterface;
Expand All @@ -16,6 +19,7 @@
class AwsDeserializationTest extends TestCase
{
private string $awsLabels;
private string $awsModerationLabels;

protected function setUp(): void
{
Expand All @@ -24,16 +28,19 @@ protected function setUp(): void
$fileInfoArray = \json_decode($fileInfo, true, 512, JSON_THROW_ON_ERROR);
$labels = $fileInfoArray['appdata']['aws_rekognition_detect_labels'] ?? null;
self::assertIsArray($labels);

$this->awsLabels = \json_encode($labels, JSON_THROW_ON_ERROR);

$moderationLabels = $fileInfoArray['appdata']['aws_rekognition_detect_moderation_labels'] ?? null;
self::assertIsArray($moderationLabels);
$this->awsModerationLabels = \json_encode($moderationLabels, JSON_THROW_ON_ERROR);
}

protected function getSerializer(): SerializerInterface
{
return new Serializer(new SnackCaseConverter());
}

public function testDeserialization(): void
public function testLabelsDeserialization(): void
{
$result = $this->getSerializer()->deserialize($this->awsLabels, AwsRecognitionLabels::class);
self::assertInstanceOf(AwsRecognitionLabels::class, $result);
Expand Down Expand Up @@ -69,4 +76,24 @@ public function testDeserialization(): void
self::assertNotEmpty($instance->getConfidence());
self::assertInstanceOf(BoundingBox::class, $instance->getBoundingBox());
}

public function testModerationLabelsDeserialization(): void
{
$result = $this->getSerializer()->deserialize($this->awsModerationLabels, AwsRecognitionModerationLabels::class);
self::assertInstanceOf(AwsRecognitionModerationLabels::class, $result);
self::assertInstanceOf(\DateTimeInterface::class, $result->getDatetimeCreated());
self::assertInstanceOf(\DateTimeInterface::class, $result->getDatetimeUpdated());

$data = $result->getData();
self::assertInstanceOf(AwsRecognitionModerationData::class, $data);
self::assertSame('6.0', $data->getLabelModelVersion());
$labels = $data->getLabels();
self::assertNotEmpty($labels);

$label = \reset($labels);
self::assertInstanceOf(AwsModerationLabel::class, $label);
self::assertIsFloat($label->getConfidence());
self::assertEquals('Weapons', $label->getName());
self::assertEquals('Violence', $label->getParentName());
}
}
15 changes: 15 additions & 0 deletions tests/_data/file-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
"foo": "2022-09-18T08:13:31+00:00"
},
"appdata": {
"aws_rekognition_detect_moderation_labels": {
"data": {
"ModerationModelVersion": "6.0",
"ModerationLabels": [
{
"Confidence": 93.41645812988281,
"Name": "Weapons",
"ParentName": "Violence"
}
]
},
"version": "2016-06-27",
"datetime_created": "2023-02-21T11:25:31.259763Z",
"datetime_updated": "2023-02-21T11:27:33.359763Z"
},
"aws_rekognition_detect_labels": {
"data": {
"Labels": [
Expand Down

0 comments on commit 4c9d1b1

Please sign in to comment.