Skip to content

Commit

Permalink
Merge pull request #1 from aplmicrons/chan_del_fix
Browse files Browse the repository at this point in the history
Fix deletion of channels that _had_ derived channels
  • Loading branch information
dkleissa authored Apr 20, 2017
2 parents 8520334 + 89c7f4b commit 337d347
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
10 changes: 9 additions & 1 deletion django/bosscore/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ def remove_source(self, source):
return

def get_derived(self):
derived = Source.objects.filter(source_channel=self)
"""
Get channels that list this channel as either a source or related channel.
Do not return any channels that are marked for deletion.
Returns:
(QuerySet)
"""
derived = Source.objects.filter(source_channel=self).exclude(derived_channel__to_be_deleted__isnull=False)
return derived

def __str__(self):
Expand Down
37 changes: 27 additions & 10 deletions django/bosscore/test/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from guardian.shortcuts import assign_perm

from ..models import Collection, Experiment, CoordinateFrame, Channel, BossLookup, BossRole, BossGroup
from ..views.views_resource import ChannelDetail

from spdb.spatialdb.test.setup import AWSSetupLayer

Expand Down Expand Up @@ -97,8 +98,8 @@ def insert_test_data(self):
self.add_experiment('col1', 'exp22', 'cf1', 10, 500, 1)
self.add_channel('col1', 'exp1', 'channel1', 0, 0, 'uint8', 'image')
self.add_channel('col1', 'exp1', 'channel2', 0, 0, 'uint8', 'image')
self.add_channel('col1', 'exp1', 'channel3', 0, 0, 'uint64', 'annotation')
self.add_channel('col1', 'exp1', 'layer1', 0, 0, 'uint64', 'annotation')
self.add_channel('col1', 'exp1', 'channel3', 0, 0, 'uint64', 'annotation', ['channel1'])
self.add_channel('col1', 'exp1', 'layer1', 0, 0, 'uint64', 'annotation', ['channel1'])

def insert_spatialdb_test_data(self):

Expand All @@ -107,9 +108,9 @@ def insert_spatialdb_test_data(self):
self.add_experiment('col1', 'exp1', 'cf1', 10, 500, 1)
self.add_channel('col1', 'exp1', 'channel1', 0, 0, 'uint8', 'image')
self.add_channel('col1', 'exp1', 'channel2', 0, 0, 'uint16', 'image')
self.add_channel('col1', 'exp1', 'layer1', 0, 0, 'uint64', 'annotation')
self.add_channel('col1', 'exp1', 'layer1', 0, 0, 'uint64', 'annotation', ['channel1'])
# bbchan1 is a channel for bounding box tests.
self.add_channel('col1', 'exp1', 'bbchan1', 0, 0, 'uint64', 'annotation')
self.add_channel('col1', 'exp1', 'bbchan1', 0, 0, 'uint64', 'annotation', ['channel1'])

def insert_ingest_test_data(self):

Expand Down Expand Up @@ -252,30 +253,46 @@ def add_experiment(self, collection_name, experiment_name, coordinate_name, num_
return exp

def add_channel(self, collection_name, experiment_name, channel_name,
default_time_sample, base_resolution, datatype, channel_type=None):
default_time_sample, base_resolution, datatype,
channel_type=None, source_channels=[]):
"""
Args:
collection_name: Name of the collection
experiment_name: Name of the experiment
channel_name: Name of the channel
collection_name (str): Name of the collection
experiment_name (str): Name of the experiment
channel_name (str): Name of the channel
default_time_sample: Default time sample
base_resolution: Base resolution of the channel
datatype: Data type
channel_type: Channel Type (image or annotation)
datatype (str): Data type
channel_type (str): Channel Type (image or annotation)
source_channels (list[str]): Source channel(s) for an annotation channel
Returns:
Channel
"""
if channel_type is None:
channel_type = 'image'
elif channel_type == 'annotation' and len(source_channels) == 0:
raise Exception('Annotation channel must have source channel.')

# Not setting up any related channels.
related_channels = []

col = Collection.objects.get(name=collection_name)
exp = Experiment.objects.get(name=experiment_name, collection=col)
channel = Channel.objects.create(name=channel_name, experiment=exp,
default_time_sample=default_time_sample, base_resolution=base_resolution,
type=channel_type, datatype=datatype, creator=self.user)

src_chan_objs, rel_chan_objs = ChannelDetail.validate_source_related_channels(
exp, source_channels, related_channels)

# Add source channels.
channel = ChannelDetail.add_source_related_channels(
channel, exp, src_chan_objs, rel_chan_objs)

# Set lookup key.
base_lkup_key = str(col.pk) + '&' + str(exp.pk) + '&' + str(channel.pk)
base_bs_key = col.name + '&' + exp.name + '&' + channel.name
BossLookup.objects.create(lookup_key=base_lkup_key, boss_key=base_bs_key,
Expand Down
2 changes: 1 addition & 1 deletion django/bosscore/test/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def setUp(self):
dbsetup.add_experiment('unittestcol', 'unittestexp', 'unittestcf', 10, 10, 1)

dbsetup.add_channel('unittestcol', 'unittestexp', 'unittestchannel', 0, 0, 'uint8')
dbsetup.add_channel('unittestcol', 'unittestexp', 'unittestlayer', 0, 0, 'uint16', 'annotation')
dbsetup.add_channel('unittestcol', 'unittestexp', 'unittestlayer', 0, 0, 'uint16', 'annotation', ['unittestchannel'])

def test_get_channel_no_permission(self):
"""
Expand Down
42 changes: 40 additions & 2 deletions django/bosscore/test/test_resource_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def test_put_channel_name(self):

def test_delete_channel(self):
"""
Delete a experiment
Delete a channel
"""
# Post a new channel
Expand Down Expand Up @@ -1107,11 +1107,49 @@ def test_delete_channel_invalid(self):
response = self.client.delete(url)
self.assertEqual(response.status_code, 400)

# Get an existing experiment
# Ensure channel still exists
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel1/'
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_delete_channel_ignore_derived_channels_marked_for_deletion(self):
"""
Delete a channel (allow when all derived channels are marked for deletion)
"""

# Post new channels
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel11/'
data = {'description': 'This is a new source channel', 'type': 'image', 'datatype': 'uint8'}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

url = '/' + version + '/collection/col1/experiment/exp1/channel/channel22/'
data = {'description': 'This is a new related channel', 'type': 'image', 'datatype': 'uint8'}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

url = '/' + version + '/collection/col1/experiment/exp1/channel/channel33/'
data = {'description': 'This is a new channel', 'type': 'annotation', 'datatype': 'uint64',
'sources': ['channel11'], 'related': ['channel22']}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

# Delete the new channel
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel33/'
response = self.client.delete(url, data=data)
self.assertEqual(response.status_code, 204)

# Delete the source channel
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel11'
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)

# Delete the related channel
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel22'
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)

def test_delete_channel_doesnotexist(self):
"""
Delete a channel (invalid - The channel does not exist )
Expand Down
2 changes: 1 addition & 1 deletion django/bossspatialdb/test/test_djangoresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_django_resource_channel_annotation(self):
assert channel.base_resolution == self.request_annotation.channel.base_resolution
assert channel.default_time_sample == self.request_annotation.channel.default_time_sample
assert channel.related == []
assert channel.sources == []
assert channel.sources == ['channel1']

def test_django_resource_channel_image_with_links(self):
"""Test basic get channel interface
Expand Down

0 comments on commit 337d347

Please sign in to comment.