function
stringlengths 11
56k
| repo_name
stringlengths 5
60
| features
sequence |
---|---|---|
def create_value(self, history):
return (history.values[-1] + 1) % 32 | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def __init__(self, type):
self.type = type | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def __init__(self):
pass | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def emit_dynasm(self, value):
raise NotImplementedError() | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def __init__(self, family):
self.family = family | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def emit_dynasm(self, value, allow_dynamic=True):
# randomly choose dynamic vs static notation
if random.choice((True, False)) or not allow_dynamic or ('SP' in self.family and value != 31):
return self.emit_gas(value)
else:
if self.family == "WX":
self.family = random.choice("WX")
return "{}({})".format(self.family, value) | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def emit_gas(self, value):
return "{}".format(value) | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def emit_gas(self, value):
return "{}".format(value) | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def emit_gas(self, value):
return value.lower() | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def make_wide_integer(bit64):
return random.randrange(0, 1<<16) << random.randrange(0, 64 if bit64 else 32, 16) | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def make_logical_imm(bit64):
element_size = random.choice([2, 4, 8, 16, 32, 64] if bit64 else [2, 4, 8, 16, 32])
ones = random.randrange(1, element_size)
rotation = random.randrange(0, element_size)
element = [0] * (element_size - ones) + [1] * ones
l = element * ((64 if bit64 else 32) // element_size)
rotated = l[rotation:] + l[:rotation]
return int("".join(map(str, rotated)), 2) | CensoredUsername/dynasm-rs | [
586,
43,
586,
11,
1467672562
] |
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.lv_load_area_group = kwargs.get('lv_load_area_group', None)
self.id_db = self.grid.cable_distributors_count() + 1 | openego/dingo | [
28,
4,
28,
53,
1450440330
] |
def pypsa_id(self):
""" :obj:`str`: Returns ...#TODO
"""
return '_'.join(['MV', str(self.grid.id_db),
'cld', str(self.id_db)]) | openego/dingo | [
28,
4,
28,
53,
1450440330
] |
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.string_id = kwargs.get('string_id', None)
self.branch_no = kwargs.get('branch_no', None)
self.load_no = kwargs.get('load_no', None)
self.id_db = self.grid.cable_distributors_count() + 1
self.in_building = kwargs.get('in_building', False) | openego/dingo | [
28,
4,
28,
53,
1450440330
] |
def test_course_key(self):
"""
Test CourseKey
"""
key = CourseKey.from_string('org.id/course_id/run')
self.assertEqual(key.org, 'org.id')
key = CourseKey.from_string('course-v1:org.id+course_id+run')
self.assertEqual(key.org, 'org.id') | edx/opaque-keys | [
7,
18,
7,
7,
1400865894
] |
def put(self, **kwargs):
ndb.Model.put(self, **kwargs)
self._setup()
try:
self.put_index()
except:
print 'error on saving document index' | ioGrow/iogrowCRM | [
28,
16,
28,
38,
1463922513
] |
def put_index(self, data=None):
""" index the element at each"""
empty_string = lambda x: x if x else ""
collaborators = " ".join(self.collaborators_ids)
organization = str(self.organization.id())
if data:
search_key = ['topics', 'tags']
for key in search_key:
if key not in data.keys():
data[key] = ""
my_document = search.Document(
doc_id=str(data['id']),
fields=[
search.TextField(name=u'type', value=u'Note'),
search.TextField(name='organization', value=empty_string(organization)),
search.TextField(name='access', value=empty_string(self.access)),
search.TextField(name='owner', value=empty_string(self.owner)),
search.TextField(name='collaborators', value=collaborators),
search.TextField(name='title', value=empty_string(self.title)),
search.TextField(name='content', value=empty_string(self.content)),
search.TextField(name='about_kind', value=empty_string(self.about_kind)),
search.TextField(name='about_item', value=empty_string(self.about_item)),
search.DateField(name='created_at', value=self.created_at),
search.DateField(name='updated_at', value=self.updated_at),
search.NumberField(name='comments', value=self.comments),
search.TextField(name='tags', value=data['tags']),
search.TextField(name='topics', value=data['topics']),
])
else:
my_document = search.Document(
doc_id=str(self.key.id()),
fields=[
search.TextField(name=u'type', value=u'Note'),
search.TextField(name='organization', value=empty_string(organization)),
search.TextField(name='access', value=empty_string(self.access)),
search.TextField(name='owner', value=empty_string(self.owner)),
search.TextField(name='collaborators', value=collaborators),
search.TextField(name='title', value=empty_string(self.title)),
search.TextField(name='content', value=empty_string(self.content)),
search.TextField(name='about_kind', value=empty_string(self.about_kind)),
search.TextField(name='about_item', value=empty_string(self.about_item)),
search.DateField(name='created_at', value=self.created_at),
search.DateField(name='updated_at', value=self.updated_at),
search.NumberField(name='comments', value=self.comments),
])
my_index = search.Index(name="GlobalIndex")
my_index.put(my_document) | ioGrow/iogrowCRM | [
28,
16,
28,
38,
1463922513
] |
def get_schema(cls, user_from_email, request):
note = cls.get_by_id(int(request.id))
if note is None:
raise endpoints.NotFoundException('Note not found.')
author = AuthorSchema(
google_user_id=note.author.google_user_id,
display_name=note.author.display_name,
google_public_profile_url=note.author.google_public_profile_url,
photo=note.author.photo
)
about = None
edge_list = Edge.list(start_node=note.key, kind='parents')
for edge in edge_list['items']:
about_kind = edge.end_node.kind()
parent = edge.end_node.get()
if parent:
if about_kind == 'Contact' or about_kind == 'Lead':
if parent.lastname and parent.firstname:
about_name = parent.firstname + ' ' + parent.lastname
else:
if parent.lastname:
about_name = parent.lastname
else:
if parent.firstname:
about_name = parent.firstname
else:
about_name = parent.name
about = DiscussionAboutSchema(
kind=about_kind,
id=str(parent.key.id()),
name=about_name
)
note_schema = NoteSchema(
id=str(note.key.id()),
entityKey=note.key.urlsafe(),
title=note.title,
content=note.content,
about=about,
created_by=author,
created_at=note.created_at.strftime("%Y-%m-%dT%H:%M:00.000"),
updated_at=note.updated_at.strftime("%Y-%m-%dT%H:%M:00.000")
)
return note_schema | ioGrow/iogrowCRM | [
28,
16,
28,
38,
1463922513
] |
def insert(cls, user_from_email, request):
parent_key = ndb.Key(urlsafe=request.about)
note_author = Userinfo()
note_author.display_name = user_from_email.google_display_name
note_author.photo = user_from_email.google_public_profile_photo_url
note = Note(
owner=user_from_email.google_user_id,
organization=user_from_email.organization,
author=note_author,
title=request.title,
content=request.content
)
entityKey_async = note.put_async()
entityKey = entityKey_async.get_result()
note.put_index()
Edge.insert(
start_node=parent_key,
end_node=entityKey,
kind='topics',
inverse_edge='parents'
)
author_shema = AuthorSchema(
google_user_id=note.owner,
display_name=note_author.display_name,
google_public_profile_url=note_author.google_public_profile_url,
photo=note_author.display_name,
edgeKey="",
email=note_author.email
)
note_schema = NoteSchema(
id=str(note.key.id()),
entityKey=note.key.urlsafe(),
title=note.title,
content=note.content,
created_by=author_shema
)
return note_schema | ioGrow/iogrowCRM | [
28,
16,
28,
38,
1463922513
] |
def list_by_parent(cls, parent_key, request):
topic_list = []
topic_edge_list = Edge.list(
start_node=parent_key,
kind='topics',
limit=request.topics.limit,
pageToken=request.topics.pageToken
)
for edge in topic_edge_list['items']:
end_node = edge.end_node.get()
try:
excerpt = end_node.content[0:100]
except:
excerpt = ''
last_updater = end_node.author
if edge.end_node.kind() == 'Note':
if end_node.comments == 0:
last_updater = end_node.author
excerpt = None
if end_node.content:
excerpt = end_node.content[0:100]
else:
# get the last comment
comments_edge_list = Edge.list(
start_node=end_node.key,
kind='comments',
limit=1
)
if len(comments_edge_list['items']) > 0:
last_comment = comments_edge_list['items'][0].end_node.get()
last_updater = last_comment.author
excerpt = None
if last_comment.content:
excerpt = end_node.content[0:100]
else:
# get the last comment
comments_edge_list = Edge.list(
start_node=end_node.key,
kind='comments',
limit=1
)
if len(comments_edge_list['items']) > 0:
last_comment = comments_edge_list['items'][0].end_node.get()
last_updater = last_comment.author
excerpt = None
if last_comment.content:
excerpt = end_node.content[0:100]
author = AuthorSchema(
google_user_id=last_updater.google_user_id,
display_name=last_updater.display_name,
google_public_profile_url=last_updater.google_public_profile_url,
photo=last_updater.photo
)
topic_list.append(
TopicSchema(
id=str(end_node.key.id()),
last_updater=author,
title=edge.end_node.get().title,
excerpt=excerpt,
topic_kind=end_node.key.kind(),
updated_at=end_node.updated_at.strftime(
"%Y-%m-%dT%H:%M:00.000"
)
)
)
if topic_edge_list['next_curs'] and topic_edge_list['more']:
topic_next_curs = topic_edge_list['next_curs'].urlsafe()
else:
topic_next_curs = None
return TopicListResponse(
items=topic_list,
nextPageToken=topic_next_curs
) | ioGrow/iogrowCRM | [
28,
16,
28,
38,
1463922513
] |
def __init__(self, id, name, root_id, shared_by_id, shared_to_id,
access, accepted):
"""Initialize an instance."""
self.id = id
self.name = name
self.root_id = root_id
self.shared_by_id = shared_by_id
self.shared_to_id = shared_to_id
self.access = access
self.accepted = accepted | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def __init__(self, owner_id, id, root_id, path):
"""Initialize an instance."""
self.owner_id = owner_id
self.id = id
self.root_id = root_id
self.path = path | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def __init__(self):
super(DummyNotifier, self).__init__()
self.notifications = [] | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def reset(self):
self.notifications = [] | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(GatewayBaseTestCase, self).setUp()
self.dummy_notifier = DummyNotifier()
self.gw = GatewayBase(notifier=self.dummy_notifier) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_user_username(self):
"""Test get_user with username"""
user = self.factory.make_user()
user2 = self.gw.get_user(username=user.username, session_id='QWERTY')
self.assertEqual(user.id, user2.id)
self.assertEqual(user.username, user2.username)
self.assertEqual(user2._gateway.session_id, 'QWERTY') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_user_locked_ignore_lock(self):
"""Test get_user with a locked user, but ignore the lock."""
user = self.factory.make_user(locked=True)
nuser = self.gw.get_user(
user.id, session_id='QWERTY', ignore_lock=True)
self.assertEqual(user.id, nuser.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_queue_new_generation(self):
"""When a new volume generation."""
vol_id = uuid.uuid4()
with fsync_commit():
self.gw.queue_new_generation(1, vol_id, 3)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(1, vol_id, 3, self.gw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_queue_share_created(self):
"""Test event generated when a share is created."""
self._test_queue_share_event(ShareCreated, 'queue_share_created') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_queue_share_accepted(self):
"""Test event generated when a share is accepted."""
self._test_queue_share_event(ShareAccepted, 'queue_share_accepted') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_queue_udf_create(self):
"""Test event generated when a UDF has been created."""
owner_id = 10
udf_id = uuid.uuid4()
root_id = uuid.uuid4()
suggested_path = 'foo/bar/baz'
udf = FakeUDF(owner_id, root_id, udf_id, suggested_path)
with fsync_commit():
self.gw.queue_udf_create(udf)
self.assertEqual(self.dummy_notifier.notifications, [
UDFCreate(owner_id, root_id, udf_id, suggested_path,
self.gw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(EventNotificationTest, self).setUp()
self.user = make_storage_user(username='user1')
self.dummy_notifier = DummyNotifier()
self.gw = GatewayBase(
session_id=uuid.uuid4(), notifier=self.dummy_notifier)
self.vgw = ReadWriteVolumeGateway(
self.user, notifier=self.dummy_notifier)
self.root = self.vgw.get_root() | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_queue_new_generation_generation_None(self):
"""When a new volume generation is None."""
vol_id = uuid.uuid4()
with fsync_commit():
self.gw.queue_new_generation(1, vol_id, None)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(1, vol_id, 0, self.gw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_handle_node_change_from_share(self):
"""Test the handle_node_change."""
self.setup_shares()
node = StorageObject.objects.get(id=self.d3.id)
user1 = make_storage_user(username=self.user1.username)
share = user1.get_share(self.share1.id)
vgw = ReadWriteVolumeGateway(
user1, share=share, notifier=self.dummy_notifier)
with fsync_commit():
vgw.handle_node_change(node)
self.assertIn(VolumeNewGeneration(self.user.id, None,
node.generation, vgw.session_id),
self.dummy_notifier.notifications)
self.assertIn(VolumeNewGeneration(self.user3.id, self.share3.id,
node.generation, vgw.session_id),
self.dummy_notifier.notifications)
self.assertIn(VolumeNewGeneration(self.user2.id, self.share2.id,
node.generation, vgw.session_id),
self.dummy_notifier.notifications)
self.assertIn(VolumeNewGeneration(self.user1.id, self.share1.id,
node.generation, vgw.session_id),
self.dummy_notifier.notifications) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_file_with_magic_content(self):
"""Make sure make_file with magic content sends a notification."""
cb = self.factory.make_content_blob(
content='FakeContent', magic_hash=b'magic')
with fsync_commit():
f = self.vgw.make_file(self.root.id, 'filename', hash=cb.hash,
magic_hash='magic')
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, f.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_delete_file_notifications(self):
"""Make sure delete_file sends notifications."""
name = 'filename'
hash = self.factory.get_fake_hash()
storage_key = uuid.uuid4()
crc = 12345
size = 100
deflated_size = 10000
with fsync_commit():
f = self.vgw.make_file_with_content(
self.root.id, name, hash, crc, size, deflated_size,
storage_key)
self.dummy_notifier.reset()
with fsync_commit():
f = self.vgw.delete_node(f.id)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, f.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_subdirectory_notification(self):
"""Make sure make_subdirectory sends notifications."""
# the root node gets a notification as a directory content change
with fsync_commit():
d = self.vgw.make_subdirectory(self.root.id, 'dirname')
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, d.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_delete_directory_notification(self):
"""Make sure make_subdirectory sends notifications."""
with fsync_commit():
d = self.vgw.make_subdirectory(self.root.id, 'dir')
self.dummy_notifier.reset()
with fsync_commit():
d = self.vgw.delete_node(d.id)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, d.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_move_notification_with_overwrite(self):
"""Make sure moves send the corrent notifications."""
root_id = self.vgw.get_root().id
name = 'filename'
hash = self.factory.get_fake_hash()
storage_key = uuid.uuid4()
crc = 12345
size = 111111111
deflated_size = 10000
with fsync_commit():
dira1 = self.vgw.make_subdirectory(root_id, 'dira1')
dira2 = self.vgw.make_subdirectory(dira1.id, 'dira2')
self.vgw.make_file_with_content(
dira1.id, name, hash, crc, size, deflated_size, storage_key)
self.dummy_notifier.reset()
with fsync_commit():
n = self.vgw.move_node(dira2.id, root_id, 'dira1')
# we get two generations, one for the delete and one for the move
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, n.generation - 1,
self.vgw.session_id),
VolumeNewGeneration(self.user.id, None, n.generation,
self.vgw.session_id),
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_file_with_content(self):
"""Ensure file creation with content sends corrent notifications."""
name = 'filename'
hash = self.factory.get_fake_hash()
storage_key = uuid.uuid4()
crc = 12345
size = 111111111
deflated_size = 10000
magic_hash = b'magic_hash'
with fsync_commit():
n = self.vgw.make_file_with_content(
self.root.id, name, hash, crc, size, deflated_size,
storage_key, magic_hash=magic_hash)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, n.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_content(self):
"""Make sure make_content sends correct notifications."""
with fsync_commit():
filenode = self.vgw.make_file(self.root.id, 'the file name')
self.dummy_notifier.reset()
new_hash = self.factory.get_fake_hash()
new_storage_key = uuid.uuid4()
crc = 12345
size = 11111
def_size = 10000
with fsync_commit():
newnode = self.vgw.make_content(filenode.id, filenode.content_hash,
new_hash, crc, size, def_size,
new_storage_key)
self.assertEqual(self.dummy_notifier.notifications, [
VolumeNewGeneration(self.user.id, None, newnode.generation,
self.vgw.session_id)
]) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(SystemGatewayTestCase, self).setUp()
self.gw = SystemGateway() | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_create_or_update_user_update(self):
"""Test the basic make user method"""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
# update the user info.
usr = StorageUser.objects.get(id=user.id)
usr.status = STATUS_DEAD
usr.save()
# creates for existing users
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name2', max_storage_bytes=2)
# their quota and other infor should get updated
self.assertEqual(user.username, 'username')
self.assertEqual(user.visible_name, 'Visible Name2')
self.assertEqual(user.max_storage_bytes, 2) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_claim_shareoffer(self):
"""Test that the claim_shareoffer function works properly.
A share_offer isn't a direct share to a user. Instead, its offered in
that an share is created but only sent to an email address. However,
any user can accept the share as long as it hasn't been accepted or the
offer hasn't been rescended.
"""
# setup the share_offer
user1 = self.factory.make_user(username='sharer')
root = StorageObject.objects.get_root(user1)
share = self.factory.make_share(
subtree=root, name='Share', access=Share.VIEW, accepted=False,
email='[email protected]')
# user can't accept their own share offer
self.assertRaises(
errors.DoesNotExist, self.gw.claim_shareoffer, user1.id,
user1.username, user1.get_full_name(), share.id)
# basic test success...user already exist
user2 = self.factory.make_user(username='sharee')
self.gw.claim_shareoffer(
user2.id, user2.username, user2.get_full_name(), share.id)
share = Share.objects.get(id=share.id)
self.assertEqual(share.shared_to, user2)
self.assertEqual(share.accepted, True)
user2 = self.gw.get_user(user2.id)
self.assertEqual(user2.is_active, True)
# Exception raised when accepted twice
self.assertRaises(
errors.ShareAlreadyAccepted, self.gw.claim_shareoffer, user2.id,
user2.username, user2.visible_name, share.id)
# test with new user
share = self.factory.make_share(
subtree=root, name='Share2', access=Share.VIEW, accepted=False,
email='[email protected]')
self.gw.claim_shareoffer(user2.id + 1, 'user3', 'user 3', share.id)
user3 = self.gw.get_user(user2.id + 1)
# the user is created, but is inactive
self.assertEqual(user3.is_active, False)
# subscribing will call something like this:
u = self.gw.create_or_update_user(
'user3', visible_name='user 3', max_storage_bytes=2 * (2 ** 30))
user3 = self.gw.get_user(u.id)
self.assertEqual(user3.is_active, True)
share = Share.objects.get(id=share.id)
self.assertEqual(share.shared_to, user3._user)
self.assertEqual(share.accepted, True) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_download(self):
"""The make_download() method creates a Download object."""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/path/name')
dl_url = 'http://download/url'
dl_key = uuid.uuid4()
download = self.gw.make_download(
user.id, udf.id, 'path', dl_url, dl_key)
self.assertTrue(isinstance(download, DAODownload))
self.assertEqual(download.owner_id, user.id)
self.assertEqual(download.volume_id, udf.id)
self.assertEqual(download.file_path, 'path')
self.assertEqual(download.download_url, dl_url)
self.assertEqual(download.download_key, unicode(repr(dl_key))) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_download(self):
"""The get_download() method can find downloads by user, volume,
path and URL or key.
"""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/path/name')
download = self.gw.make_download(
user.id, udf.id, 'path', 'http://download/url')
other_download = self.gw.get_download(
user.id, udf.id, 'path', 'http://download/url')
self.assertEqual(other_download.id, download.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_old_song_with_multiple_download_records(self):
"""Old songs may have multiple download records. Get the latest."""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/path/name')
file_path = 'path'
download_key = 'mydownloadkey'
download1_url = 'http://download/url/1'
download1 = self.gw.make_download(
user.id, udf.id, file_path, download1_url, download_key)
download2_url = 'http://download/url/2'
download2 = self.gw.make_download(
user.id, udf.id, file_path, download2_url, download_key)
assert download1.status_change_date < download2.status_change_date
download = self.gw.get_download(
user.id, udf.id, file_path, download1_url, download_key)
self.assertNotEqual(download.id, download1.id)
self.assertEqual(download.id, download2.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_download_with_same_key_and_different_url(self):
"""The get_download() method can find downloads by user, volume,
path and URL or key.
"""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/path/name')
key = ['some', 'key']
download = self.gw.make_download(
user.id, udf.id, 'path', 'http://download/url/1', key)
# request with same key and different URL
other_download = self.gw.get_download(
user.id, udf.id, 'path', 'http://download/url/2', key)
self.assertEqual(other_download.id, download.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_update_download_status(self):
"""The update_download() method can update a download's status."""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/spath/name')
download = self.gw.make_download(
user.id, udf.id, 'path', 'http://download/url')
new_download = self.gw.update_download(
download.owner_id, download.id,
status=Download.STATUS_DOWNLOADING)
self.assertEqual(
new_download.status, Download.STATUS_DOWNLOADING) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_update_download_error_message(self):
"""The update_download() method can update a download's error
message."""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
udf = self.factory.make_user_volume(
owner=user._user, path='~/path/name')
download = self.gw.make_download(
user.id, udf.id, 'path', 'http://download/url')
new_download = self.gw.update_download(
download.owner_id, download.id, error_message='error')
self.assertEqual(new_download.error_message, 'error') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_node(self):
"""Test the get_node method."""
user = self.gw.create_or_update_user(
'username', visible_name='Visible Name', max_storage_bytes=1)
sgw = SystemGateway()
root = StorageObject.objects.get_root(user._user)
node = root.make_file('TheName', mimetype='fakemime')
new_node = sgw.get_node(node.id)
self.assertEqual(node.id, new_node.id)
self.assertEqual(node.parent_id, new_node.parent_id)
self.assertEqual(node.name, new_node.name)
self.assertEqual(node.path, new_node.path)
self.assertEqual(node.content_hash, new_node.content_hash)
# check that DoesNotExist is raised
self.assertRaises(errors.DoesNotExist, sgw.get_node, uuid.uuid4()) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_cleanup_abandoned_uploadjobs(self):
"""Test the cleanup_uploadjobs method."""
right_now = now()
crc = 12345
size = 100
def_size = 100
for uid in range(0, 10):
suser = self.factory.make_user(
username='testuser_%d' % uid, max_storage_bytes=1024)
user = self.gw.get_user(suser.id)
vgw = user._gateway.get_root_gateway()
root = vgw.get_root()
new_hash = self.factory.get_fake_hash(os.urandom(100))
file1 = vgw.make_file(root.id, 'file1-%d' % uid)
up1 = vgw.make_uploadjob(file1.id, file1.content_hash, new_hash,
crc, size, def_size,
multipart_key=uuid.uuid4())
# change the when_started date for the test.
uploadjob = UploadJob.objects.get(id=up1.id)
uploadjob.when_last_active = right_now - timedelta(10)
uploadjob.save()
# check that filtering by date works as expected.
date = right_now - timedelta(9)
jobs = self.gw.get_abandoned_uploadjobs(date)
self.assertEqual(len(jobs), 10)
self.gw.cleanup_uploadjobs(jobs)
jobs = self.gw.get_abandoned_uploadjobs(date)
self.assertEqual(len(jobs), 0) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(SystemGatewayPublicFileTestCase, self).setUp()
self.gw = SystemGateway()
self.user1 = make_storage_user(username='sharer')
self.vgw = ReadWriteVolumeGateway(self.user1)
name = 'filename'
hash = self.factory.get_fake_hash()
storage_key = uuid.uuid4()
crc = 12345
size = deflated_size = 10
self.root = self.vgw.get_root()
file1 = self.vgw.make_file_with_content(
self.root.id, name, hash, crc, size, deflated_size, storage_key,
mimetype='fakemime')
self.file = self.vgw._get_node_simple(file1.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_public_file_no_content(self):
"""Test get_public_file when file has no content."""
file_dao = self.vgw.change_public_access(self.file.id, True)
self.file.content_blob = None
self.file.save()
self.assertRaises(errors.DoesNotExist,
self.gw.get_public_file, file_dao.public_key) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_public_file(self):
"""Tests for get_public_file."""
file_dao = self.vgw.change_public_access(self.file.id, True)
self.assertIsNotNone(file_dao.public_uuid)
public_key = file_dao.public_key
# Once a file has been made public, it can be looked up by its ID.
file2 = self.gw.get_public_file(public_key)
self.assertEqual(file2.id, self.file.id)
self.assertEqual(file2.mimetype, 'fakemime')
# this file was created with content, the content must be returned
self.assertNotEquals(file2.content, None)
# DoesNotExist is raised if that file is made private.
file_dao = self.vgw.change_public_access(self.file.id, False)
self.assertRaises(errors.DoesNotExist,
self.gw.get_public_file, public_key)
# public stays the same when set back to public
file_dao = self.vgw.change_public_access(file_dao.id, True)
self.assertIsNotNone(file_dao.public_uuid)
public_key = file_dao.public_key
# DoesNotExist is raised if the underlying file is deleted.
self.vgw.delete_node(self.file.id)
self.assertRaises(
errors.DoesNotExist, self.gw.get_public_file, public_key) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(SystemGatewayPublicDirectoryTestCase, self).setUp()
self.gw = SystemGateway()
self.user1 = make_storage_user(username='sharer')
self.vgw = ReadWriteVolumeGateway(self.user1)
self.root = self.vgw.get_root()
dir1 = self.vgw.make_subdirectory(self.root.id, 'a-folder')
self.dir = self.vgw._get_node_simple(dir1.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_public_directory(self):
"""Tests for get_public_directory."""
dir_dao = self.vgw.change_public_access(
self.dir.id, True, allow_directory=True)
self.assertIsNotNone(dir_dao.public_uuid)
public_key = dir_dao.public_key
# If I don't do this, the test fails
# lock the user
StorageUser.objects.filter(id=self.user1.id).update(locked=True)
# Once a directory has been made public, it can be accessed
dir2 = self.gw.get_public_directory(public_key)
self.assertEqual(dir2.id, self.dir.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(StorageUserGatewayTestCase, self).setUp()
self.user = make_storage_user(username='testuser')
self.gw = StorageUserGateway(self.user) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_udf_gateway_None(self):
"""Make sure method returns None and no Error."""
vgw = self.gw.get_udf_gateway(uuid.uuid4())
self.assertEqual(vgw, None) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_accept_share(self):
"""Test accepting a direct share."""
user1 = self.factory.make_user(username='sharer')
root = StorageObject.objects.get_root(user1)
share = self.factory.make_share(
subtree=root, shared_to=self.user._user, name='Share',
accepted=False)
self.assertEqual(share.status, STATUS_LIVE)
self.assertFalse(share.accepted)
# this should succeed
self.gw.accept_share(share.id)
share.refresh_from_db()
self.assertTrue(share.accepted)
# cant be accepted twice
self.assertRaises(errors.DoesNotExist, self.gw.accept_share, share.id)
# if it's dead, it can't be accepted
share.status = STATUS_DEAD
share.save()
self.assertRaises(errors.DoesNotExist, self.gw.accept_share, share.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_delete_share(self):
"""Test delete shares from share-er and share-ee"""
user1 = self.factory.make_user(username='sharer')
root = StorageObject.objects.get_root(user1)
share = self.factory.make_share(
subtree=root, shared_to=self.user._user, name='Share')
share = self.gw.get_share(share.id, accepted_only=False)
self.assertEqual(share.status, STATUS_LIVE)
self.gw.delete_share(share.id)
# share is now inaccessible
self.assertRaises(
errors.DoesNotExist, self.gw.get_share, share.id,
accepted_only=False)
# make it live again so we can test it with the share-ee
Share.objects.filter(id=share.id).update(status=STATUS_LIVE)
# get user1's gateway and delete it
user1 = make_storage_user(username=user1.username)
user1._gateway.delete_share(share.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_udf(self):
"""Test make_udf method."""
udf = self.gw.make_udf('~/path/name')
self.assertEqual(udf.owner_id, self.user.id)
self.assertEqual(udf.path, '~/path/name')
self.assertNotEqual(udf.when_created, None)
# make sure the same UDF is created
udf2 = self.gw.make_udf('~/path/name')
self.assertEqual(udf.id, udf2.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_udf(self):
"""Test make and get udf methods."""
udf = self.gw.make_udf('~/path/name')
udf2 = self.gw.get_udf(udf.id)
self.assertEqual(udf.id, udf2.id)
self.assertEqual(udf.path, udf2.path) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_udfs(self):
"""Test get_udfs method."""
for i in range(10):
self.gw.make_udf('~/path/to/file/name %s' % i)
udfs = self.gw.get_udfs()
udfs = list(udfs)
self.assertEqual(len(udfs), 10)
for udf in udfs:
self.gw.delete_udf(udf.id)
udfs2 = self.gw.get_udfs()
self.assertEqual(len(list(udfs2)), 0) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_delete_udf_with_children(self):
"""Test delete_udf method."""
udf = self.gw.make_udf('~/path/name')
vgw = ReadWriteVolumeGateway(self.user, udf=udf)
a_dir = vgw.make_subdirectory(vgw.root_id, 'subdir')
vgw.make_file(a_dir.id, 'file')
self.gw.delete_udf(udf.id)
self.assertRaises(errors.DoesNotExist, self.gw.get_udf, udf.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_noaccess_inactive_user(self):
"""Make sure NoAccess is rasied when the user is inactive."""
self.user.is_active = False
self.assertRaises(errors.NoPermission, self.gw.make_udf, '~/p/n')
self.assertRaises(errors.NoPermission, self.gw.accept_share, 1)
self.assertRaises(errors.NoPermission, self.gw.get_share, 1)
self.assertRaises(errors.NoPermission, self.gw.delete_share, 1)
self.assertRaises(errors.NoPermission, self.gw.get_udf, 1)
self.assertRaises(errors.NoPermission, self.gw.get_volume_gateway)
# the generators behave a little differently
self.assertRaises(errors.NoPermission, list, self.gw.get_shared_by())
self.assertRaises(errors.NoPermission, list, self.gw.get_udfs()) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_delete_related_shares_lotsofchildren(self):
"""Make sure the query splitting does it's job."""
vgw = self.gw.get_root_gateway()
dir1 = vgw.make_subdirectory(vgw.get_root().id, 'shared1')
# make lots of children
for i in xrange(300):
vgw.make_subdirectory(dir1.id, 'd%s' % i)
usera = make_storage_user(username='sharee1')
sharea = vgw.make_share(dir1.id, 'sharea', user_id=usera.id)
usera._gateway.accept_share(sharea.id)
dir1 = StorageObject.objects.get(id=dir1.id)
self.user._gateway.delete_related_shares(dir1)
self.assertRaises(
errors.DoesNotExist,
self.user._gateway.get_share, sharea.id, accepted_only=False) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_public_files(self):
"""Test get_public_files method."""
vgw = self.gw.get_root_gateway()
root = StorageObject.objects.get_root(self.user._user)
node = root.make_file('TheName', mimetype='fakemime')
vgw.change_public_access(node.id, True)
nodes = list(self.gw.get_public_files())
self.assertEqual(1, len(nodes))
self.assertEqual(node.id, nodes[0].id)
self.assertEqual(node.volume_id, nodes[0].volume_id)
self.assertIsNotNone(nodes[0].public_uuid)
# now test it with more than one file (and some dirs too)
def create_files(root):
"""Create a 5 dirs with 5 files each in the specified root."""
for dname in ['dir_%s' % i for i in xrange(5)]:
d = root.make_subdirectory(dname)
for fname, j in [('file_%s' % j, j) for j in xrange(10)]:
f = d.make_file(fname, mimetype='fakemime')
if j % 2:
continue
vgw.change_public_access(f.id, True)
vgw.make_file(d.id, fname)
create_files(root)
file_cnt = 5 * 5 + 1
nodes = list(self.gw.get_public_files())
self.assertEqual(file_cnt, len(nodes))
# and test it with public files inside a UDF
udf = self.gw.make_udf('~/path/name')
udf_root = StorageObject.objects.get(id=udf.root_id)
create_files(udf_root)
file_cnt = file_cnt + 5 * 5
nodes = list(self.gw.get_public_files())
self.assertEqual(file_cnt, len(nodes)) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_share_generation(self):
"""Test the get_share_generation method."""
user1 = self.factory.make_user(username='sharer')
root = StorageObject.objects.get_root(user1)
share = self.factory.make_share(
subtree=root, shared_to=self.user._user, name='Share')
share = self.gw.get_share(share.id, accepted_only=False)
self.assertEqual(0, self.gw.get_share_generation(share))
# increase the generation
user1.root_node.make_subdirectory('a dir')
# check we get the correct generation
self.assertEqual(1, self.gw.get_share_generation(share)) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_reusable_content_no_blob(self):
"""No blob at all, can not reuse."""
blobexists, storage_key = self.gw._get_reusable_content(
b'hash_value', b'magic')
self.assertFalse(blobexists)
self.assertEqual(storage_key, None) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_reusable_content_same_owner_no_magic(self):
"""Test update_content will reuse owned content, even with no magic."""
hash_value = self.factory.get_fake_hash()
node = self._make_file_with_content(hash_value)
blobexists, storage_key = self.gw.is_reusable_content(hash_value, None)
self.assertTrue(blobexists)
self.assertEqual(storage_key, node.content.storage_key) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_reusable_content_different_owner_no_magic(self):
"""Test update_content will not reuse someone elses content."""
user2 = make_storage_user(
username='testuserX', max_storage_bytes=2 ** 32)
assert user2.id != self.user.id
hash_value = self.factory.get_fake_hash()
self._make_file_with_content(hash_value, gw=user2._gateway)
blobexists, storage_key = self.gw.is_reusable_content(hash_value, None)
self.assertTrue(blobexists)
self.assertEqual(storage_key, None) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__get_reusable_content_no_blob(self):
"""No blob at all, can not reuse."""
blobexists, blob = self.gw._get_reusable_content(
b'hash_value', b'magic')
self.assertFalse(blobexists)
self.assertEqual(blob, None) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__get_reusable_content_same_owner_with_magic(self):
"""Test update_content will reuse owned content."""
hash_value = self.factory.get_fake_hash()
node = self._make_file_with_content(hash_value, magic_hash=b'magic')
blobexists, blob = self.gw._get_reusable_content(hash_value, 'magic')
self.assertTrue(blobexists)
self.assertEqual(bytes(blob.hash), node.content_hash) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__get_reusable_content_different_owner_with_magic(self):
"""Test update_content will reuse someone elses content with magic."""
user2 = make_storage_user(
username='testuserX', max_storage_bytes=2 ** 32)
assert user2.id != self.user.id
hash_value = self.factory.get_fake_hash()
node = self._make_file_with_content(
hash_value, gw=user2._gateway, magic_hash=b'magic')
self.assertTrue(self.gw._get_reusable_content(hash_value, 'magic'))
blobexists, blob = self.gw._get_reusable_content(hash_value, 'magic')
self.assertTrue(blobexists)
self.assertEqual(bytes(blob.hash), node.content_hash) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(ReadWriteVolumeGatewayUtilityTests, self).setUp()
self.gw = SystemGateway()
self.user = make_storage_user(
username='testuser', max_storage_bytes=2000)
self.vgw = self.user._gateway.get_root_gateway()
self.root = self.vgw.get_root() | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_nodes_only_from_volume(self):
"""Test get_all_nodes."""
# make some files on a UDF...
udf = self.factory.make_user_volume(
owner=self.user._user, path='~/thepath/thename')
udf_dao = DAOUserVolume(udf, self.user)
udf_vgw = ReadWriteVolumeGateway(self.user, udf=udf_dao)
udf_root_id = udf_vgw.get_root().id
for i in range(10):
d = udf_vgw.make_subdirectory(udf_root_id, '%sd' % i)
f = udf_vgw.make_file(d.id, '%sfile.txt' % i)
# make files on the root and make sure only they are returned
root = self.vgw.get_root()
root_id = root.id
# make dirs and put files in them
dirs = []
files = []
for i in range(10):
d = self.vgw.make_subdirectory(root_id, '%sd' % i)
f = self.vgw.make_file(d.id, '%sfile.txt' % i)
dirs.append(d)
files.append(f)
nodes = dirs + files
nodes.append(root)
all_nodes = self.vgw.get_all_nodes()
self.assertEqual(len(all_nodes), 21)
# the lists are not sorted so not equal
self.assertNotEquals(all_nodes, nodes)
# sort them in the right order
nodes.sort(key=attrgetter('path', 'name'))
self.assertEqual(all_nodes, nodes)
# with a max_generation
nodes_gen_10 = self.vgw.get_all_nodes(max_generation=10)
self.assertEqual(
nodes_gen_10, [n for n in nodes if n.generation <= 10])
nodes_gen_20 = self.vgw.get_all_nodes(max_generation=20)
self.assertEqual(nodes_gen_20, nodes)
# with max_generation and limit
nodes_limit_5 = self.vgw.get_all_nodes(max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10[:5])
last_node = nodes_limit_5[-1]
nodes_limit_5 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10[:10])
last_node = nodes_limit_5[-1]
nodes_limit_5 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10)
# same but with the last gen.
nodes_limit_10 = self.vgw.get_all_nodes(max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes[:10])
last_node = nodes_limit_10[-1]
nodes_limit_10 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes[:20])
last_node = nodes_limit_10[-1]
nodes_limit_10 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_with_mimetype(self):
"""Test get_all_nodes with mimetype filter."""
hash = self.factory.get_fake_hash()
key = uuid.uuid4()
root_id = self.vgw.get_root().id
def mkfile(i, m):
return self.vgw.make_file_with_content(
root_id, 'file%s' % i, hash, 1, 1, 1, key, mimetype=m)
# make a bunch of files with content
nodes = [mkfile(i, 'image/tif') for i in range(10)]
# make a bunch of files with content and the wrong mimetype
other_nodes = [mkfile(i, 'fake') for i in range(100, 110)]
# an unknown mimetype will return nothing
all_nodes = self.vgw.get_all_nodes(mimetypes=['mmm'])
self.assertEqual(len(all_nodes), 0)
# this will only return the ones with the matching mimetype
all_nodes = self.vgw.get_all_nodes(mimetypes=['image/tif'])
self.assertEqual(len(all_nodes), 10)
# sort them in the right order
nodes.sort(key=attrgetter('path', 'name'))
self.assertEqual(all_nodes, nodes)
# get both mimetypes
nodes.extend(other_nodes)
nodes.sort(key=attrgetter('path', 'name'))
all_nodes = self.vgw.get_all_nodes(mimetypes=['image/tif', 'fake'])
self.assertEqual(len(all_nodes), 20)
self.assertEqual(all_nodes, nodes) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def mkfile(i, m):
return self.vgw.make_file_with_content(
root_id, 'file%s.tif' % i, hash, 1, 1, 1, key, mimetype=m) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_with_content_and_mimetype(self):
"""Test get_all_nodes with mimetype filter."""
hash = self.factory.get_fake_hash()
key = uuid.uuid4()
root_id = self.vgw.get_root().id
def mkfile(i, m):
return self.vgw.make_file_with_content(
root_id, 'file%s.tif' % i, hash, 1, 1, 1, key, mimetype=m)
# make a bunch of files with content
[mkfile(i, 'image/tif') for i in range(10)]
all_nodes = self.vgw.get_all_nodes(with_content=True,
mimetypes=['image/tif'])
self.assertEqual(len(all_nodes), 10)
for n in all_nodes:
self.assertTrue(n.content.hash, hash) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_nodes_with_limit(self):
"""Test get_all_nodes with a limit."""
root = self.vgw.get_root()
root_id = root.id
# make dirs and put files in them
dirs = []
files = []
for i in range(10):
d = self.vgw.make_subdirectory(root_id, '%sd' % i)
f = self.vgw.make_file(d.id, '%sfile.txt' % i)
dirs.append(d)
files.append(f)
nodes = dirs + files
nodes.append(root)
# sort them in the right order
nodes.sort(key=attrgetter('path', 'name'))
# get the nodes by chunks of 10 top
nodes_limit_10 = self.vgw.get_all_nodes(limit=10)
self.assertEqual(nodes_limit_10, nodes[:10])
last_node = nodes_limit_10[-1]
nodes_limit_10 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
limit=10)
self.assertEqual(nodes_limit_10, nodes[:20])
last_node = nodes_limit_10[-1]
nodes_limit_10 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
limit=10)
self.assertEqual(nodes_limit_10, nodes) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_nodes_with_max_generation_and_limit_only_from_volume(
self):
"""Test get_all_nodes wiht max_generation and limit for a UDF."""
# make some files on a UDF...
udf = self.factory.make_user_volume(
owner=self.user._user, path='~/thepath/thename')
udf_dao = DAOUserVolume(udf, self.user)
udf_vgw = ReadWriteVolumeGateway(self.user, udf=udf_dao)
udf_root_id = udf_vgw.get_root().id
# make files on the root and make sure only they are returned
dirs = []
files = []
for i in range(10):
d = udf_vgw.make_subdirectory(udf_root_id, '%sd' % i)
f = udf_vgw.make_file(d.id, '%sfile.txt' % i)
dirs.append(d)
files.append(f)
nodes = dirs + files
nodes.append(udf_vgw.get_root())
all_nodes = udf_vgw.get_all_nodes()
self.assertEqual(len(all_nodes), 21)
# the lists are not sorted so not equal
self.assertNotEquals(all_nodes, nodes)
# sort them in the right order
nodes.sort(key=attrgetter('path', 'name'))
self.assertEqual(all_nodes, nodes)
# with a max_generation
nodes_gen_10 = udf_vgw.get_all_nodes(max_generation=10)
self.assertEqual(
nodes_gen_10, [n for n in nodes if n.generation <= 10])
nodes_gen_20 = udf_vgw.get_all_nodes(max_generation=20)
self.assertEqual(nodes_gen_20, nodes)
# with max_generation and limit
nodes_limit_5 = udf_vgw.get_all_nodes(max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10[:5])
last_node = nodes_limit_5[-1]
nodes_limit_5 += udf_vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10[:10])
last_node = nodes_limit_5[-1]
nodes_limit_5 += udf_vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, nodes_gen_10)
# same but with the last gen.
nodes_limit_10 = udf_vgw.get_all_nodes(max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes[:10])
last_node = nodes_limit_10[-1]
nodes_limit_10 += udf_vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes[:20])
last_node = nodes_limit_10[-1]
nodes_limit_10 += udf_vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=20, limit=10)
self.assertEqual(nodes_limit_10, nodes) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_nodes_chunked_with_move_middle(self):
"""Test chunked get_all_nodes with a move in the middle."""
root = self.vgw.get_root()
root_id = root.id
# make dirs and put files in them
dirs = []
files = []
for i in range(10):
d = self.vgw.make_subdirectory(root_id, '%sd' % i)
f = self.vgw.make_file(d.id, '%sfile.txt' % i)
dirs.append(d)
files.append(f)
nodes = dirs + files
nodes.append(root)
# sort them in the right order
nodes.sort(key=attrgetter('path', 'name'))
nodes_gen_19 = self.vgw.get_all_nodes(max_generation=19)
self.assertEqual(nodes_gen_19, nodes[:20])
# same but with the last generation
nodes_limit_5 = self.vgw.get_all_nodes(max_generation=19, limit=5)
self.assertEqual(nodes_limit_5, nodes[:5])
last_node = nodes_limit_5[-1]
nodes_limit_5 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=19, limit=5)
self.assertEqual(nodes_limit_5, nodes[:10])
# move a node that's already in the result
to_move = nodes[10:15][2]
# now move a node that it's in the result,
self.vgw.move_node(to_move.id, nodes[0].id, to_move.name)
last_node = nodes_limit_5[-1]
# get the rest of the result.
nodes_limit_5 += self.vgw.get_all_nodes(
start_from_path=(last_node.path, last_node.name),
max_generation=19)
# remove the moved node from the nodes_gen_19 list
del nodes_gen_19[12]
self.assertEqual(nodes_limit_5, nodes_gen_19) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_all_nodes_chunked_only_root_in_volume(self):
"""Test chunked get_all_nodes with only one node, the root."""
udf = self.factory.make_user_volume(
owner=self.user._user, path='~/thepath/thename')
udf_dao = DAOUserVolume(udf, self.user)
udf_vgw = ReadWriteVolumeGateway(self.user, udf=udf_dao)
# sort them in the right order
nodes = udf_vgw.get_all_nodes()
self.assertEqual(len(nodes), 1)
# same but with the last generation
nodes_limit_5 = udf_vgw.get_all_nodes(
start_from_path=(nodes[-1].path, nodes[-1].name),
max_generation=10, limit=5)
self.assertEqual(nodes_limit_5, []) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def mkfile(dir_id, i):
return self.vgw.make_file(dir_id, 'file%s.txt' % i) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setup_shares(self):
"""Setup some shares for moves from shares."""
d = self.vgw.make_tree(self.root.id, '/a/b/c/d')
y = self.vgw.make_tree(self.root.id, '/x/y')
usera = make_storage_user(username='sharee1')
userb = make_storage_user(username='sharee2')
userc = make_storage_user(username='sharee3')
# make 4 shares
sharea = self.vgw.make_share(d.id, 'sharea', user_id=usera.id)
shareb = self.vgw.make_share(d.id, 'shareb', user_id=userb.id)
sharec = self.vgw.make_share(d.id, 'sharec', user_id=userc.id)
# userc will not accept the share in this test
usera._gateway.accept_share(sharea.id)
userb._gateway.accept_share(shareb.id)
return d, y, sharea, shareb, sharec | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_move_from_share_2(self):
"""Test _make_move_from_share method.
Move into a child of the node's folder.
"""
d, y, sa, sb, sc = self.setup_shares()
f1 = self.vgw.make_file(d.id, 'file.txt')
np = self.vgw.make_tree(d.id, '/q/w/e/r/t/y')
self.assertTrue(np.full_path.startswith(f1.path))
self.vgw.move_node(f1.id, np.id, f1.name)
mfs = MoveFromShare.objects.all()
# should have no MoveFromShare
self.assertEqual(mfs.count(), 0) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_change_public_access_file(self):
"""Test the basics of changing public access to a file."""
a_file = self.vgw.make_file(self.root.id, 'a-file.txt')
# It has no public ID
self.assertIsNone(a_file.public_uuid)
# It now has a public ID
self.assertIsNotNone(
self.vgw.change_public_access(a_file.id, True).public_uuid)
# Disabled it, back to None
self.assertIsNone(
self.vgw.change_public_access(a_file.id, False).public_uuid) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_change_public_access_directory(self):
"""Test that directories can be made public if explicitly requested."""
a_dir = self.vgw.make_tree(self.root.id, '/x')
# It has no public ID
self.assertIsNone(a_dir.public_uuid)
# It now has a public ID
self.assertIsNotNone(
self.vgw.change_public_access(a_dir.id, True, True).public_uuid) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def setUp(self):
super(CommonReadWriteVolumeGatewayApiTest, self).setUp()
self.gw = SystemGateway()
user = make_storage_user(username='testuser', max_storage_bytes=2000)
self.user = self.gw.get_user(user.id)
self.setup_volume() | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def tweak_users_quota(self, dao_user, max_bytes, used_bytes=0):
"""Utility to toy with the user's quota."""
StorageUser.objects.filter(id=dao_user.id).update(
max_storage_bytes=max_bytes, used_storage_bytes=used_bytes)
dao_user._load() | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__get_root_node(self):
"""Test _get_root_node method."""
node = self.vgw._get_root_node()
self.assertEqual(node, self.root) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_user_volume(self):
"""Test get_user_volume method."""
vol = self.vgw.get_user_volume()
self.assertTrue(isinstance(vol, DAOUserVolume))
self.assertEqual(vol.id, self.root.volume_id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__node_finder(self):
"""Test the _node_finder method."""
nodes = StorageObject.objects.filter(id=self.file.id)
result = self.vgw._node_finder(nodes)
node = self.vgw._get_node_from_result(result)
self.assertEqual(node.id, self.file.id)
self.assertEqual(node.content_hash, self.file.content_hash) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test__get_node_with_parent(self):
nodes = StorageObject.objects.filter(id=self.file.id)
result = self.vgw._node_finder(nodes, with_parent=True)
node = self.vgw._get_node_from_result(result)
self.assertEqual(node.id, self.file.id)
self.assertEqual(node.content_hash, self.file.content_hash)
self.assertEqual(node.parent_id, self.file.parent_id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_check_has_children(self):
"""Test the check_has_children function."""
a_dir = self.vgw.make_subdirectory(
self.root.id, StorageObject.DIRECTORY)
self.assertTrue(
self.vgw.check_has_children(self.root.id, StorageObject.FILE))
self.assertTrue(
self.vgw.check_has_children(self.root.id, StorageObject.DIRECTORY))
self.vgw.delete_node(self.file.id)
self.assertFalse(
self.vgw.check_has_children(self.root.id, StorageObject.FILE))
self.assertTrue(
self.vgw.check_has_children(self.root.id, StorageObject.DIRECTORY))
self.vgw.delete_node(a_dir.id)
self.assertFalse(
self.vgw.check_has_children(self.root.id, StorageObject.FILE))
self.assertFalse(
self.vgw.check_has_children(self.root.id, StorageObject.DIRECTORY)) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_get_node(self):
"""Test get_node method."""
# make sure it returns the root node when special 'root' is used:
root_node = self.vgw.get_node('root')
self.assertEqual(root_node.id, self.vgw.get_root().id)
node = self.vgw.get_node(self.file.id, with_content=True)
self.assertEqual(node.id, self.file.id)
self.assertEqual(node.content_hash, self.file.content_hash)
self.assertEqual(node.mimetype, self.file.mimetype)
self.assertRaises(errors.DoesNotExist, self.vgw.get_node, uuid.uuid4())
self.assertRaises(errors.HashMismatch, self.vgw.get_node, self.file.id,
verify_hash='fake hash') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_tree(self):
"""Test make_tree method."""
node = self.vgw.make_tree(self.root.id, '/a/b/c')
self.assertEqual(node.full_path, '/a/b/c')
node = self.vgw.make_tree(self.root.id, '/')
self.assertEqual(node.id, self.root.id) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_file_with_magic(self):
"""Test make_file method."""
cb = self.factory.make_content_blob(
content='FakeContent', magic_hash=b'magic')
# make enough room
self.tweak_users_quota(self.owner, cb.deflated_size)
node = self.vgw.make_file(self.root.id, 'the file name',
hash=cb.hash, magic_hash=cb.magic_hash)
self.assertEqual(node.content_hash, cb.hash) | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
def test_make_subdirectory(self):
"""Test make_subdirectory method."""
node = self.vgw.make_subdirectory(self.root.id, 'the file name')
self.assertTrue(isinstance(node, DirectoryNode))
self.assertEqual(node.name, 'the file name')
self.assertEqual(node.parent_id, self.root.id)
self.assertEqual(node.volume_id, self.root.volume_id)
self.assertNotEqual(node.when_created, None)
self.assertNotEqual(node.when_last_modified, None)
self.vgw.make_file(self.root.id, 'duplicatename')
self.assertRaises(
errors.AlreadyExists,
self.vgw.make_subdirectory, self.root.id, 'duplicatename') | magicicada-bot/magicicada-server | [
6,
1,
6,
4,
1441315733
] |
Subsets and Splits