Skip to content

Commit

Permalink
Merge pull request #919 from biigle/patch-1
Browse files Browse the repository at this point in the history
Fix error with label and user mapping during annotation import
  • Loading branch information
mzur authored Sep 5, 2024
2 parents 65792a4 + 9967b1b commit ba0c5c3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
9 changes: 5 additions & 4 deletions app/Http/Requests/UpdatePendingVolumeLabelMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function withValidator($validator)
}
}

$count = Label::whereIn('id', array_values($map))->count();
if (count($map) !== $count) {
$uniqueIds = array_values(array_unique($map));
$count = Label::whereIn('id', $uniqueIds)->count();
if (count($uniqueIds) !== $count) {
$validator->errors()->add('label_map', 'Some label IDs do not exist in the database.');
}

$count = Label::whereIn('id', array_values($map))
$count = Label::whereIn('id', $uniqueIds)
->whereIn('label_tree_id', function ($query) {
// All public and all accessible private label trees.
$query->select('id')
Expand All @@ -93,7 +94,7 @@ public function withValidator($validator)
})
->count();

if (count($map) !== $count) {
if (count($uniqueIds) !== $count) {
$validator->errors()->add('label_map', 'You do not have access to some label IDs in the database.');
}
});
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Requests/UpdatePendingVolumeUserMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public function withValidator($validator)
}
}

$count = User::whereIn('id', array_values($map))->count();
if (count($map) !== $count) {
$uniqueIds = array_values(array_unique($map));
$count = User::whereIn('id', $uniqueIds)->count();
if (count($uniqueIds) !== $count) {
$validator->errors()->add('user_map', 'Some user IDs do not exist in the database.');
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,47 @@ public function testUpdateLabelMapRedirectToUserMap()
])->assertRedirectToRoute('pending-volume-user-map', $id);
}

public function testUpdateLabelMapDuplicate()
{
$metadata = new VolumeMetadata;
$file = new ImageMetadata('1.jpg');
$metadata->addFile($file);
$label = new Label(123, 'my label');
$user = new User(321, 'joe user');
$lau = new LabelAndUser($label, $user);
$file->addFileLabel($lau);

$label = new Label(321, 'my label 2');
$lau = new LabelAndUser($label, $user);
$file->addFileLabel($lau);

Cache::store('array')->put('metadata-pending-metadata-mymeta.csv', $metadata);

$pv = PendingVolume::factory()->create([
'project_id' => $this->project()->id,
'media_type_id' => MediaType::imageId(),
'user_id' => $this->admin()->id,
'metadata_file_path' => 'mymeta.csv',
'volume_id' => $this->volume()->id,
]);
$id = $pv->id;

$this->beAdmin();

$this->putJson("/api/v1/pending-volumes/{$id}/label-map", [
'label_map' => [
123 => $this->labelRoot()->id,
321 => $this->labelRoot()->id,
],
])->assertSuccessful();

$pv->refresh();
$this->assertEquals([
123 => $this->labelRoot()->id,
321 => $this->labelRoot()->id,
], $pv->label_map);
}

public function testUpdateUserMap()
{
$metadata = new VolumeMetadata;
Expand Down Expand Up @@ -631,6 +672,46 @@ public function testUpdateUserMapRedirectToFinish()
])->assertRedirectToRoute('pending-volume-finish', $id);
}

public function testUpdateUserMapDuplicate()
{
$metadata = new VolumeMetadata;
$file = new ImageMetadata('1.jpg');
$metadata->addFile($file);
$label = new Label(123, 'my label');
$user = new User(321, 'joe user');
$lau = new LabelAndUser($label, $user);
$file->addFileLabel($lau);
$user = new User(123, 'jane user');
$lau = new LabelAndUser($label, $user);
$file->addFileLabel($lau);

Cache::store('array')->put('metadata-pending-metadata-mymeta.csv', $metadata);

$pv = PendingVolume::factory()->create([
'project_id' => $this->project()->id,
'media_type_id' => MediaType::imageId(),
'user_id' => $this->admin()->id,
'metadata_file_path' => 'mymeta.csv',
'volume_id' => $this->volume()->id,
]);
$id = $pv->id;

$this->beAdmin();

$this->putJson("/api/v1/pending-volumes/{$id}/user-map", [
'user_map' => [
321 => $this->user()->id,
123 => $this->user()->id,
],
])->assertSuccessful();

$pv->refresh();
$this->assertEquals([
321 => $this->user()->id,
123 => $this->user()->id,
], $pv->user_map);
}

public function testStoreImport()
{
$dbUser = DbUser::factory()->create();
Expand Down

0 comments on commit ba0c5c3

Please sign in to comment.