Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lehecht committed Jan 30, 2025
1 parent 0398b6c commit 9accc1c
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,60 @@ public function testGetProjectAnnotationLabelsOnlyVideos()
]);
}


public function testGetProjectAnnotationLabelsNoLabels()
{
$this->beEditor();
$this->getJson("/api/v1/projects/{$this->project()->id}/label-count")
->assertStatus(200)
->assertJsonCount(0);
}

public function testGetProjectAnnotationLabelsSorting()
{
$id = $this->project()->id;
$img = ImageTest::create(['volume_id' => $this->volume()->id, 'filename' => 'abc.jpg']);
$a = ImageAnnotationTest::create(['image_id' => $img]);
$l1 = LabelTest::create(['name' => '1']);
$l3 = LabelTest::create(['name' => '11']);
ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l1->id]);
ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l3->id]);


$videoVolume = VolumeTest::create(['media_type_id' => MediaType::video(), 'creator_id' => $this->volume()->creator_id]);
$this->project()->volumes()->attach($videoVolume->id);
$vid = VideoTest::create(['volume_id' => $videoVolume, 'filename' => 'abc.jpg']);
$a2 = VideoAnnotationTest::create(['video_id' => $vid]);
$l2 = LabelTest::create(['name' => '2']);
VideoAnnotationLabelTest::create(['annotation_id' => $a2->id, 'label_id' => $l2->id]);

$this->beEditor();
$this->getJson("/api/v1/projects/{$id}/label-count")
->assertStatus(200)
->assertJsonCount(3)
->assertExactJson(
[
[
"id" => $l1->id,
"color" => $l1->color,
"name" => $l1->name,
"label_tree_id" => $l1->label_tree_id,
"count" => 1
],
[
"id" => $l2->id,
"color" => $l2->color,
"name" => $l2->name,
"label_tree_id" => $l2->label_tree_id,
"count" => 1
],
[
"id" => $l3->id,
"color" => $l3->color,
"name" => $l3->name,
"label_tree_id" => $l3->label_tree_id,
"count" => 1
]
]
);
}
}
206 changes: 206 additions & 0 deletions tests/Http/Controllers/Api/Volumes/VolumeAnnotationLabelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Biigle\Tests\Modules\Largo\Http\Controllers\Api\Volumes;

use ApiTestCase;
use Carbon\Carbon;
use Biigle\MediaType;
use Biigle\Tests\ImageTest;
use Biigle\Tests\LabelTest;
use Biigle\Tests\VideoTest;
use Biigle\Tests\ImageAnnotationTest;
use Biigle\Tests\VideoAnnotationTest;
use Biigle\Tests\AnnotationSessionTest;
use Biigle\Tests\ImageAnnotationLabelTest;
use Biigle\Tests\VideoAnnotationLabelTest;

Expand Down Expand Up @@ -92,4 +94,208 @@ public function testGetImageVolumeAnnotationLabelsNoLabels()
->assertJsonCount(0);
}

public function testGetVolumeAnnotationLabelsSorting()
{
$id = $this->volume()->id;
$img = ImageTest::create(['volume_id' => $id, 'filename' => 'abc.jpg']);
$a = ImageAnnotationTest::create(['image_id' => $img]);
$l1 = LabelTest::create(['name' => '1']);
$l2 = LabelTest::create(['name' => '11']);
$l3 = LabelTest::create(['name' => '2']);
ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l1->id]);
ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l2->id]);
ImageAnnotationLabelTest::create(['annotation_id' => $a->id, 'label_id' => $l3->id]);

$this->beEditor();
$this->getJson("/api/v1/volume/{$id}/label-count")
->assertStatus(200)
->assertJsonCount(3)
->assertExactJson(
[
[
"id" => $l1->id,
"color" => $l1->color,
"name" => $l1->name,
"label_tree_id" => $l1->label_tree_id,
"count" => 1
],
[
"id" => $l3->id,
"color" => $l3->color,
"name" => $l3->name,
"label_tree_id" => $l3->label_tree_id,
"count" => 1
],
[
"id" => $l2->id,
"color" => $l2->color,
"name" => $l2->name,
"label_tree_id" => $l2->label_tree_id,
"count" => 1
]
]
);
}

public function testGetVolumeAnnotationLabelsAnnotationSession(){
$id = $this->volume()->id;
$image = ImageTest::create(['volume_id' => $id, 'filename' => 'abc.jpg']);

$l1 = LabelTest::create(['name' => '1']);

$l2 = LabelTest::create(['name' => '2']);

$l3 = LabelTest::create(['name' => '3']);

$a1 = ImageAnnotationTest::create([
'image_id' => $image->id,
'created_at' => Carbon::yesterday(),
]);

$a2 = ImageAnnotationTest::create([
'image_id' => $image->id,
'created_at' => Carbon::today(),
]);

$a3 = ImageAnnotationTest::create([
'image_id' => $image->id,
'created_at' => Carbon::yesterday(),
]);

ImageAnnotationLabelTest::create([
'annotation_id' => $a1->id,
'label_id' => $l1->id,
'user_id' => $this->editor()->id,
]);

ImageAnnotationLabelTest::create([
'annotation_id' => $a2->id,
'label_id' => $l2->id,
'user_id' => $this->editor()->id,
]);

ImageAnnotationLabelTest::create([
'annotation_id' => $a3->id,
'label_id' => $l3->id,
'user_id' => $this->admin()->id,
]);

$this->beEditor();

// test hide own
$session = AnnotationSessionTest::create([
'volume_id' => $id,
'starts_at' => Carbon::today(),
'ends_at' => Carbon::tomorrow(),
'hide_own_annotations' => true,
'hide_other_users_annotations' => false,
]);

$session->users()->attach($this->editor());

$this->getJson("/api/v1/volume/{$id}/label-count")
->assertStatus(200)
->assertExactJson([
[
"id" => $l2->id,
"color" => $l2->color,
"name" => $l2->name,
"label_tree_id" => $l2->label_tree_id,
"count" => 1
],
[
"id" => $l3->id,
"color" => $l3->color,
"name" => $l3->name,
"label_tree_id" => $l3->label_tree_id,
"count" => 1
],
]);

// test hide other
$session->hide_own_annotations = false;
$session->hide_other_users_annotations = true;
$session->save();

$this->getJson("/api/v1/volume/{$id}/label-count")
->assertStatus(200)
->assertExactJson([
[
"id" => $l1->id,
"color" => $l1->color,
"name" => $l1->name,
"label_tree_id" => $l1->label_tree_id,
"count" => 1
],
[
"id" => $l2->id,
"color" => $l2->color,
"name" => $l2->name,
"label_tree_id" => $l2->label_tree_id,
"count" => 1
],
]);

// test hide both
$session->hide_own_annotations = true;
$session->save();

$this->getJson("/api/v1/volume/{$id}/label-count")
->assertStatus(200)
->assertExactJson([
[
"id" => $l2->id,
"color" => $l2->color,
"name" => $l2->name,
"label_tree_id" => $l2->label_tree_id,
"count" => 1
],
]);
}

public function testGetVolumeAnnotationLabelsAnnotationSessionEdgeCaseHideOther()
{
$id = $this->volume()->id;
$image = ImageTest::create(['volume_id' => $id]);

$l1 = LabelTest::create();

$l2 = LabelTest::create();

$a1 = ImageAnnotationTest::create([
'image_id' => $image->id,
]);
ImageAnnotationLabelTest::create([
'annotation_id' => $a1->id,
'user_id' => $this->editor()->id,
'label_id' => $l1->id,
]);
ImageAnnotationLabelTest::create([
'annotation_id' => $a1->id,
'user_id' => $this->admin()->id,
'label_id' => $l2->id,
]);
$session = AnnotationSessionTest::create([
'volume_id' => $id,
'starts_at' => Carbon::yesterday(),
'ends_at' => Carbon::tomorrow(),
'hide_own_annotations' => false,
'hide_other_users_annotations' => true,
]);
$session->users()->attach($this->editor());

$this->beEditor();
$this->getJson("/api/v1/volume/{$id}/label-count")
->assertStatus(200)
->assertExactJson([
[
"id" => $l1->id,
"color" => $l1->color,
"name" => $l1->name,
"label_tree_id" => $l1->label_tree_id,
"count" => 1
],
]);
}

}

0 comments on commit 9accc1c

Please sign in to comment.