-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the
Comment
class to the new backend interface
- Loading branch information
Showing
26 changed files
with
619 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Unit tests for the Comment ORM class.""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
|
||
from aiida import orm | ||
from aiida.orm.comments import Comment | ||
from aiida.backends.testbase import AiidaTestCase | ||
from aiida.common import exceptions | ||
|
||
|
||
class TestComment(AiidaTestCase): | ||
"""Unit tests for the Comment ORM class.""" | ||
|
||
def setUp(self): | ||
super(TestComment, self).setUp() | ||
self.node = orm.Node().store() | ||
self.user = orm.User.objects.get_default() | ||
self.content = 'Sometimes when I am freestyling, I lose confidence' | ||
self.comment = Comment(self.node, self.user, self.content).store() | ||
|
||
def test_comment_content(self): | ||
"""Test getting and setting content of a Comment.""" | ||
content = 'Be more constructive with your feedback' | ||
self.comment.set_content(content) | ||
self.assertEqual(self.comment.content, content) | ||
|
||
def test_comment_mtime(self): | ||
"""Test getting and setting mtime of a Comment.""" | ||
mtime = self.comment.mtime | ||
self.comment.set_content('Changing an attribute should automatically change the mtime') | ||
self.assertEqual(self.comment.content, 'Changing an attribute should automatically change the mtime') | ||
self.assertNotEqual(self.comment.mtime, mtime) | ||
|
||
def test_comment_node(self): | ||
"""Test getting the node of a Comment.""" | ||
self.assertEqual(self.comment.node.uuid, self.node.uuid) | ||
|
||
def test_comment_user(self): | ||
"""Test getting the user of a Comment.""" | ||
self.assertEqual(self.comment.user.uuid, self.user.uuid) | ||
|
||
def test_comment_collection_get(self): | ||
"""Test retrieving a Comment through the collection.""" | ||
comment = Comment.objects.get(comment=self.comment.pk) | ||
self.assertEqual(self.comment.uuid, comment.uuid) | ||
|
||
def test_comment_collection_delete(self): | ||
"""Test deleting a Comment through the collection.""" | ||
comment = Comment(self.node, self.user, 'I will perish').store() | ||
comment_pk = comment.pk | ||
|
||
Comment.objects.delete(comment=comment.pk) | ||
|
||
with self.assertRaises(exceptions.NotExistent): | ||
Comment.objects.get(comment=comment_pk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Comment objects and functions""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
|
||
from . import backends | ||
from . import entities | ||
from . import users | ||
|
||
__all__ = ('Comment',) | ||
|
||
|
||
class Comment(entities.Entity): | ||
"""Base class to map a DbComment that represents a comment attached to a certain Node.""" | ||
|
||
class Collection(entities.Collection): | ||
"""The collection of Comment entries.""" | ||
|
||
def delete(self, comment): | ||
""" | ||
Remove a Comment from the collection with the given id | ||
:param comment: the id of the comment to delete | ||
""" | ||
self._backend.comments.delete(comment) | ||
|
||
def get(self, comment): | ||
""" | ||
Return a Comment given its id | ||
:param comment: the id of the comment to retrieve | ||
:return: the comment | ||
:raise NotExistent: if the comment with the given id does not exist | ||
:raise MultipleObjectsError: if the id cannot be uniquely resolved to a comment | ||
""" | ||
return self._backend.comments.get(comment) | ||
|
||
def __init__(self, node, user, content=None, backend=None): | ||
""" | ||
Create a Comment for a given node and user | ||
:param node: a Node instance | ||
:param user: a User instance | ||
:param content: the comment content | ||
:return: a Comment object associated to the given node and user | ||
""" | ||
backend = backend or backends.construct_backend() | ||
model = backend.comments.create(node=node, user=user.backend_entity, content=content) | ||
super(Comment, self).__init__(model) | ||
|
||
def __str__(self): | ||
arguments = [self.uuid, self.node.pk, self.user.email, self.content] | ||
return 'Comment<{}> for node<{}> and user<{}>: {}'.format(*arguments) | ||
|
||
@property | ||
def ctime(self): | ||
return self._backend_entity.ctime | ||
|
||
@property | ||
def mtime(self): | ||
return self._backend_entity.mtime | ||
|
||
def set_mtime(self, value): | ||
return self._backend_entity.set_mtime(value) | ||
|
||
@property | ||
def node(self): | ||
return self._backend_entity.node | ||
|
||
@property | ||
def user(self): | ||
return users.User.from_backend_entity(self._backend_entity.user) | ||
|
||
def set_user(self, value): | ||
self._backend_entity.set_user(value.backend_entity) | ||
|
||
@property | ||
def content(self): | ||
return self._backend_entity.content | ||
|
||
def set_content(self, value): | ||
return self._backend_entity.set_content(value) |
Oops, something went wrong.