def test_put_tags_view_will_raise_404(self): """testing if the PUT /api/entities/{id}/tags view will return 404 """ from stalker import db, User, Tag test_user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(test_user1) tag1 = Tag(name='Tag1') tag2 = Tag(name='Tag2') tag3 = Tag(name='Tag3') tag4 = Tag(name='Tag4') tag5 = Tag(name='Tag5') db.DBSession.add_all([tag1, tag2, tag3, tag4, tag5]) test_user1.tags = [tag1, tag2, tag3] import transaction transaction.commit() test_user1 = User.query.filter(User.login == test_user1.login).first() self.test_app.put('/api/entities/%s/tags?tag=%s' % (test_user1.id, tag4.name), status=404)
def test_delete_tags_view_is_working_properly(self): """testing if the DELETE /api/entities/{id}/tags view is working properly """ from stalker import db, User, Tag test_user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(test_user1) tag1 = Tag(name='Tag1') tag2 = Tag(name='Tag2') tag3 = Tag(name='Tag3') tag4 = Tag(name='Tag4') tag5 = Tag(name='Tag5') db.DBSession.add_all([tag1, tag2, tag3, tag4, tag5]) test_user1.tags = [tag1, tag2, tag3] import transaction transaction.commit() test_user1 = User.query.filter(User.login == test_user1.login).first() self.test_app.delete('/api/entities/%s/tags?tag=%s' % (test_user1.id, tag3.name), status=200) test_user1 = User.query.filter(User.login == test_user1.login).first() self.assertEqual(sorted([tag.name for tag in test_user1.tags]), ['Tag1', 'Tag2'])
def test_update_tags_is_working_properly_with_post(self): """testing if the update_tags() view is working properly when the request.method is POST """ # create some tags from stalker import db, Tag t1 = Tag(name='tag1') t2 = Tag(name='tag2') t3 = Tag(name='tag3') db.DBSession.add_all([t1, t2, t3]) from stalker import Entity test_entity = Entity(name='Test Entity') db.DBSession.add(test_entity) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = test_entity.id request.method = 'POST' request.params = DummyMultiDict() request.params['tag'] = ['tag1', 'tag2'] request.POST = request.params entity_view = entity.EntityViews(request) entity_view.update_tags() # now query entity tags test_entity = Entity.query.filter(Entity.id == test_entity.id).first() self.assertEqual(sorted([t.name for t in test_entity.tags]), sorted(['tag1', 'tag2']))
def setUp(self): """setting up some proper values """ # create a user self.test_user = User(name="Test User", login="******", email="*****@*****.**", password="******") # create some test Tag objects, not necessarily needed but create them self.test_tag1 = Tag(name="Test Tag 1") self.test_tag2 = Tag(name="Test Tag 1") # make it equal to tag1 self.test_tag3 = Tag(name="Test Tag 3") self.tags = [self.test_tag1, self.test_tag2] # create a couple of test Note objects self.test_note1 = Note(name="test note1", content="test note1") self.test_note2 = Note(name="test note2", content="test note2") self.test_note3 = Note(name="test note3", content="test note3") self.notes = [self.test_note1, self.test_note2] self.kwargs = { "name": "Test Entity", "description": "This is a test entity, and this is a proper \ description for it", "created_by": self.test_user, "updated_by": self.test_user, "tags": self.tags, "notes": self.notes, } # create a proper SimpleEntity to use it later in the tests self.test_entity = Entity(**self.kwargs)
def test_remove_tags_is_working_properly_with_non_existing_tags(self): """testing if the remove_tags() method is working properly """ # create some tags from stalker import db, Tag t1 = Tag(name='tag1') t2 = Tag(name='tag2') t3 = Tag(name='tag3') from stalker import Entity test_entity = Entity(name='Test Entity', tags=[t1, t2]) db.DBSession.add_all([t1, t2, t3, test_entity]) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.method = 'DELETE' request.matchdict['id'] = test_entity.id request.params = DummyMultiDict() request.params['tag'] = ['tag3'] request.POST = request.params entity_view = entity.EntityViews(request) entity_view.remove_tags() # now query entity tags test_entity = Entity.query.filter(Entity.id == test_entity.id).first() response = [t.name for t in test_entity.tags] expected = ['tag1', 'tag2'] self.assertEqual(sorted(response), sorted(expected))
def setUp(self): """setup the test """ super(RepositoryTester, self).setUp() self.patcher = PlatformPatcher() # create a couple of test tags from stalker import Tag self.test_tag1 = Tag(name="test tag 1") self.test_tag2 = Tag(name="test tag 2") self.kwargs = { "name": "a repository", "description": "this is for testing purposes", "tags": [self.test_tag1, self.test_tag2], "linux_path": "/mnt/M/Projects", "osx_path": "/Volumes/M/Projects", "windows_path": "M:/Projects" } from stalker import Repository from stalker.db.session import DBSession self.test_repo = Repository(**self.kwargs) DBSession.add(self.test_repo) DBSession.commit()
def test_inequality(self): """testing the inequality of two Tags """ a_tag_object1 = Tag(**self.kwargs) a_tag_object2 = Tag(**self.kwargs) self.kwargs["name"] = "a new test Tag" self.kwargs["description"] = "this is a new test Tag" a_tag_object3 = Tag(**self.kwargs) self.assertFalse(a_tag_object1 != a_tag_object2) self.assertTrue(a_tag_object1 != a_tag_object3) self.assertTrue(a_tag_object1 != self.simple_entity)
def test_delete_entity_is_working_properly(self): """testing if delete_entity() method is working properly """ from stalker import db, Tag test_tag1 = Tag(name='Test Tag 1') test_tag2 = Tag(name='Test Tag 2') test_tag3 = Tag(name='Test Tag 3') db.DBSession.add_all([test_tag1, test_tag2, test_tag3]) db.DBSession.commit() self.test_app.delete('/api/tags/%s' % test_tag1.id, status=200) tags = Tag.query.all() self.assertEqual(sorted(tags), sorted([test_tag2, test_tag3]))
def test_update_entity_is_working_properly_with_patch(self): """testing if update_entity() method is working properly with PATCH """ from stalker import db, Tag new_tag = Tag(name='Test Tag 1') db.DBSession.add(new_tag) db.DBSession.flush() db.DBSession.commit() from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = new_tag.id self.patch_logged_in_user(request) request.params = DummyMultiDict() request.params['name'] = 'New Tag Name' request.params['description'] = 'This also should be updated' request.method = 'PATCH' tag_view = tag.TagViews(request) tag_view.update_entity() new_tag_db = Tag.query.get(new_tag.id) self.assertEqual(new_tag_db.name, 'New Tag Name') self.assertEqual(new_tag_db.description, 'This also should be updated') self.assertEqual(new_tag_db.updated_by, self.admin)
def test_get_entities_view_is_working_properly(self): """testing if GET: /api/tags view is working properly """ from stalker import db, Tag test_tag1 = Tag(name='Test Tag 1') test_tag2 = Tag(name='Test Tag 2') test_tag3 = Tag(name='Test Tag 3') db.DBSession.add_all([test_tag1, test_tag2, test_tag3]) db.DBSession.commit() response = self.test_app.get('/api/tags', status=200) self.assertEqual(response.json_body, [{ 'id': t.id, '$ref': '/api/tags/%s' % t.id, 'name': t.name, 'entity_type': t.entity_type } for t in [test_tag1, test_tag2, test_tag3]])
def test_inequality(): """testing the inequality of two Tags """ kwargs = dict(name="a test tag", description="this is a test tag") simple_entity = SimpleEntity(**kwargs) a_tag_object1 = Tag(**kwargs) a_tag_object2 = Tag(**kwargs) kwargs["name"] = "a new test Tag" kwargs["description"] = "this is a new test Tag" a_tag_object3 = Tag(**kwargs) assert not a_tag_object1 != a_tag_object2 assert a_tag_object1 != a_tag_object3 assert a_tag_object1 != simple_entity
def test_get_tags_view_is_working_properly(self): """testing if the GET /api/entities/{id}/tags view is working properly """ from stalker import db, User, Tag test_user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(test_user1) tag1 = Tag(name='Tag1') tag2 = Tag(name='Tag2') tag3 = Tag(name='Tag3') tag4 = Tag(name='Tag4') tag5 = Tag(name='Tag5') db.DBSession.add_all([tag1, tag2, tag3, tag4, tag5]) test_user1.tags = [tag1, tag2, tag3] import transaction transaction.commit() test_user1 = User.query.filter(User.login == test_user1.login).first() response = self.test_app.get('/api/entities/%s/tags' % test_user1.id, status=200) self.assertEqual( sorted(response.json_body), sorted([{ 'id': tag1.id, '$ref': '/api/tags/%s' % tag1.id, 'name': 'Tag1', 'entity_type': 'Tag' }, { 'id': tag2.id, '$ref': '/api/tags/%s' % tag2.id, 'name': 'Tag2', 'entity_type': 'Tag' }, { 'id': tag3.id, '$ref': '/api/tags/%s' % tag3.id, 'name': 'Tag3', 'entity_type': 'Tag' }]))
def setUp(self): """setup the test """ self.patcher = PlatformPatcher() # create a couple of test tags self.test_tag1 = Tag(name="test tag 1") self.test_tag2 = Tag(name="test tag 2") self.kwargs = { "name": "a repository", "description": "this is for testing purposes", "tags": [self.test_tag1, self.test_tag2], "linux_path": "/mnt/M/Projects", "osx_path": "/Volumes/M/Projects", "windows_path": "M:/Projects" } self.test_repo = Repository(**self.kwargs)
def test_delete_entity_is_working_properly(self): """testing if delete_entity() method is working properly """ from stalker import db, Tag test_tag1 = Tag(name='Test Tag 1') test_tag2 = Tag(name='Test Tag 2') test_tag3 = Tag(name='Test Tag 3') db.DBSession.add_all([test_tag1, test_tag2, test_tag3]) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = test_tag1.id tag_view = tag.TagViews(request) tag_view.delete_entity() tags = Tag.query.all() self.assertEqual(sorted(tags), sorted([test_tag2, test_tag3]))
def test_get_entities_view_is_working_properly(self): """testing if get_entities() method is working properly """ from stalker import db, Tag test_tag1 = Tag(name='Test Tag 1') test_tag2 = Tag(name='Test Tag 2') test_tag3 = Tag(name='Test Tag 3') db.DBSession.add_all([test_tag1, test_tag2, test_tag3]) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() tag_view = tag.TagViews(request) response = tag_view.get_entities() self.assertEqual(response.json_body, [{ 'id': t.id, '$ref': '/api/tags/%s' % t.id, 'name': t.name, 'entity_type': t.entity_type } for t in [test_tag1, test_tag2, test_tag3]])
def test_get_entity(self): """testing if get_entity() method is working properly """ from stalker import db, Tag tag1 = Tag(name='Test Tag 1') db.DBSession.add(tag1) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = tag1.id tag_view = tag.TagViews(request) response = tag_view.get_entity() from stalker_pyramid.views import EntityViewBase import stalker self.assertEqual( response.json_body, { 'created_by': None, 'date_created': EntityViewBase.milliseconds_since_epoch(tag1.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch(tag1.date_updated), 'description': '', 'entity_type': 'Tag', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % tag1.id, 'length': 0 }, 'generic_text': '', 'id': tag1.id, 'name': 'Test Tag 1', 'stalker_version': stalker.__version__, 'thumbnail': None, 'type': None, 'updated_by': None })
def test_get_tags_is_working_properly(self): """testing if get tags is working properly """ # create some tags from stalker import db, Tag t1 = Tag(name='tag1') t2 = Tag(name='tag2') t3 = Tag(name='tag3') # create a test entity from stalker import Entity test_entity = Entity(name='Test Entity') db.DBSession.add(test_entity) test_entity.tags = [t1, t2] db.DBSession.add_all([t1, t2, t3, test_entity]) db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = test_entity.id # get the tags of the entity entity_view = entity.EntityViews(request) response = entity_view.get_tags() expected = [{ 'id': t1.id, '$ref': '/api/tags/%s' % t1.id, 'name': 'tag1', 'entity_type': 'Tag' }, { 'id': t2.id, '$ref': '/api/tags/%s' % t2.id, 'name': 'tag2', 'entity_type': 'Tag' }] self.assertEqual(sorted(response.json_body), sorted(expected))
def test_get_entity(self): """testing if GET: /api/tags/{id} view is working properly """ from stalker import db, Tag tag1 = Tag(name='Test Tag 1') db.DBSession.add(tag1) db.DBSession.commit() response = self.test_app.get('/api/tags/%s' % tag1.id, status=200) from stalker_pyramid.views import EntityViewBase import stalker self.assertEqual( response.json_body, { 'created_by': None, 'date_created': EntityViewBase.milliseconds_since_epoch(tag1.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch(tag1.date_updated), 'description': '', 'entity_type': 'Tag', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % tag1.id, 'length': 0 }, 'generic_text': '', 'id': tag1.id, 'name': 'Test Tag 1', 'stalker_version': stalker.__version__, 'thumbnail': None, 'type': None, 'updated_by': None })
def test_update_entity_is_working_properly_with_post(self): """testing if POST: /api/tags/{id} view is working properly """ from stalker import db, Tag new_tag = Tag(name='Test Tag 1') db.DBSession.add(new_tag) db.DBSession.commit() self.admin_login() self.test_app.post('/api/tags/%s' % new_tag.id, params={ 'name': 'New Tag Name', 'description': 'This also should be updated' }, status=200) new_tag_db = Tag.query.get(new_tag.id) self.assertEqual(new_tag_db.name, 'New Tag Name') self.assertEqual(new_tag_db.description, 'This also should be updated')
def get_tags_data(cls, request, parameter='tags[]'): """Extracts Tags from the given request :param request: Request object :param str parameter: :return: A list of stalker.models.tag.Tag instances """ # Tags tags = [] tag_names = request.POST.getall(parameter) for tag_name in tag_names: logger.debug('tag_name : %s' % tag_name) if tag_name == '': continue from stalker import Tag tag = Tag.query.filter(Tag.name == tag_name).first() if not tag: logger.debug('new tag is created %s' % tag_name) tag = Tag(name=tag_name) DBSession.add(tag) tags.append(tag) return tags
def test_plural_class_name(self): """testing the plural name of Tag class """ test_tag = Tag(**self.kwargs) self.assertTrue(test_tag.plural_class_name, "Tags")
def test_plural_class_name(): """testing the plural name of Tag class """ kwargs = dict(name="a test tag", description="this is a test tag") test_tag = Tag(**kwargs) assert test_tag.plural_class_name == "Tags"
def test_tag_init(self): """testing if tag inits properly """ # this should work without any error tag = Tag(**self.kwargs) self.assertTrue(isinstance(tag, Tag))
def test_tag_init(): """testing if tag inits properly """ # this should work without any error tag = Tag(name="a test tag", description="this is a test tag") assert isinstance(tag, Tag)