def test_delete_entity(self): """testing if delete_entity() method is working properly """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) import datetime start = datetime.datetime(2016, 4, 22, 10) end = datetime.datetime(2016, 4, 22, 16) vac1 = Vacation(user=user1, start=start, end=end) db.DBSession.commit() vac1 = Vacation.query.filter(Vacation.name == vac1.name).first() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = vac1.id vacation_views = vacation.VacationViews(request) vacation_views.delete_entity() vac = Vacation.query.filter(Vacation.name == vac1.name).all() self.assertEqual(vac, [])
def test_create_entity_with_missing_end(self): """testing if create_entity() method is working properly with missing end parameter """ from stalker import db, User user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) db.DBSession.commit() import datetime start = datetime.datetime(2016, 4, 22, 10) user1 = User.query.filter(User.login == user1.login).first() from stalker_pyramid.testing import DummyRequest, DummyMultiDict from stalker_pyramid.views import EntityViewBase request = DummyRequest() request.params = DummyMultiDict() request.params['user_id'] = user1.id request.params['start'] = \ EntityViewBase.milliseconds_since_epoch(start) vacation_views = vacation.VacationViews(request) from pyramid.httpexceptions import HTTPServerError with self.assertRaises(HTTPServerError) as cm: vacation_views.create_entity() self.assertEqual(str(cm.exception), 'Missing "end" parameter')
def test_update_entity_change_user(self): """testing if update_entity() method is working properly for changing user attribute """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) user2 = User(name='Test User 2', login='******', email='*****@*****.**', password='******') db.DBSession.add(user2) import datetime start = datetime.datetime(2016, 4, 22, 10) end = datetime.datetime(2016, 4, 22, 16) vac1 = Vacation(user=user1, start=start, end=end) db.DBSession.commit() user2 = User.query.filter(User.login == user2.login).first() vac1 = Vacation.query.filter(Vacation.name == vac1.name).first() # also update updated_by_id attribute from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = vac1.id request.params = DummyMultiDict() # change user request.params['user_id'] = user2.id request.params['updated_by_id'] = user2.id self.patch_logged_in_user(request) vacation_views = vacation.VacationViews(request) vacation_views.update_entity() vac = Vacation.query.filter(Vacation.name == vac1.name).first() self.assertEqual(vac.start, start) self.assertEqual(vac.end, end) self.assertEqual(vac.user, user2) self.assertEqual(vac.updated_by, user2)
def test_update_entity_change_end(self): """testing if update_entity() method is working properly for changing end attribute """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) import datetime start = datetime.datetime(2016, 4, 22, 10) end = datetime.datetime(2016, 4, 22, 16) new_end = datetime.datetime(2016, 4, 22, 17) vac1 = Vacation(user=user1, start=start, end=end) db.DBSession.commit() user1 = User.query.filter(User.login == user1.login).first() vac1 = Vacation.query.filter(Vacation.name == vac1.name).first() from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = vac1.id request.params = DummyMultiDict() # change start from stalker_pyramid.views import EntityViewBase request.params['end'] = \ EntityViewBase.milliseconds_since_epoch(new_end) self.patch_logged_in_user(request) vacation_views = vacation.VacationViews(request) vacation_views.update_entity() vac = Vacation.query.filter(Vacation.name == vac1.name).first() self.assertEqual(vac.start, start) self.assertEqual(vac.end, new_end) self.assertEqual(vac.user, user1)
def test_create_entity_with_invalid_user_id(self): """testing if create_entity() method is working properly with invalid user_id parameter """ import datetime start = datetime.datetime(2016, 4, 22, 10) end = datetime.datetime(2016, 4, 22, 16) from stalker_pyramid.testing import DummyRequest, DummyMultiDict from stalker_pyramid.views import EntityViewBase request = DummyRequest() request.params = DummyMultiDict() request.params['user_id'] = -1 request.params['start'] = \ EntityViewBase.milliseconds_since_epoch(start) request.params['end'] = EntityViewBase.milliseconds_since_epoch(end) vacation_views = vacation.VacationViews(request) from pyramid.httpexceptions import HTTPServerError with self.assertRaises(HTTPServerError) as cm: vacation_views.create_entity() self.assertEqual(str(cm.exception), 'Missing "user_id" parameter')
def test_get_entity_is_working_properly(self): """testing if get_entity() method is working properly """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) import datetime start = datetime.datetime(2016, 4, 22) end = datetime.datetime(2016, 4, 23) # get admin admin = User.query.filter(User.login == 'admin').first() vac1 = Vacation( user=user1, start=start, end=end, created_by=admin, ) db.DBSession.add(vac1) db.DBSession.commit() # get the id vac1 = Vacation.query.filter(Vacation.name == vac1.name).first() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = vac1.id vacation_views = vacation.VacationViews(request) response = vacation_views.get_entity() from stalker_pyramid.views import EntityViewBase import stalker self.maxDiff = None self.assertEqual( response.json_body, { 'created_by': { 'id': vac1.created_by.id, '$ref': '/api/users/%s' % vac1.created_by.id, 'name': vac1.created_by.name, 'entity_type': 'User' }, 'date_created': EntityViewBase.milliseconds_since_epoch(vac1.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch(vac1.date_updated), 'description': '', 'end': EntityViewBase.milliseconds_since_epoch(vac1.end), 'entity_type': 'Vacation', 'generic_text': '', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % vac1.id, 'length': 0 }, 'id': vac1.id, 'name': vac1.name, 'stalker_version': stalker.__version__, 'start': EntityViewBase.milliseconds_since_epoch(vac1.start), 'thumbnail': None, 'type': None, 'user': { 'id': vac1.user.id, '$ref': '/api/users/%s' % vac1.user.id, 'name': vac1.user.name, 'entity_type': 'User' }, 'updated_by': { 'id': vac1.updated_by.id, '$ref': '/api/users/%s' % vac1.updated_by.id, 'name': vac1.updated_by.name, 'entity_type': 'User' }, })
def test_create_entity_is_working_properly(self): """testing if create_entity() method is working properly """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) db.DBSession.flush() import transaction transaction.commit() import datetime start = datetime.datetime(2016, 4, 22, 10) end = datetime.datetime(2016, 4, 22, 16) user1 = User.query.filter(User.login == user1.login).first() from stalker_pyramid.testing import DummyRequest, DummyMultiDict from stalker_pyramid.views import EntityViewBase request = DummyRequest() request.params = DummyMultiDict() request.params['user_id'] = user1.id request.params['start'] = \ EntityViewBase.milliseconds_since_epoch(start) request.params['end'] = EntityViewBase.milliseconds_since_epoch(end) self.patch_logged_in_user(request) vacation_views = vacation.VacationViews(request) response = vacation_views.create_entity() vac = Vacation.query.filter(Vacation.user == user1).first() self.assertEqual(vac.start, start) self.assertEqual(vac.end, end) self.assertEqual(vac.user, user1) import stalker self.maxDiff = None self.assertEqual( response.json_body, { 'created_by': { 'id': self.admin.id, '$ref': '/api/users/%s' % self.admin.id, 'name': self.admin.name, 'entity_type': 'User' }, 'date_created': EntityViewBase.milliseconds_since_epoch(vac.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch(vac.date_created), 'description': '', 'end': EntityViewBase.milliseconds_since_epoch(vac.end), 'entity_type': 'Vacation', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % vac.id, 'length': 0 }, 'generic_text': '', 'id': vac.id, 'name': vac.name, 'stalker_version': stalker.__version__, 'start': EntityViewBase.milliseconds_since_epoch(vac.start), 'thumbnail': None, 'type': None, 'updated_by': { 'id': self.admin.id, '$ref': '/api/users/%s' % self.admin.id, 'name': self.admin.name, 'entity_type': 'User' }, 'user': { 'id': vac.user_id, '$ref': '/api/users/%s' % vac.user_id, 'name': vac.user.name, 'entity_type': 'User' } })
def test_get_entities_is_working_properly(self): """testing if get_entities() method is working properly """ from stalker import db, User, Vacation user1 = User(name='Test User 1', login='******', email='*****@*****.**', password='******') db.DBSession.add(user1) user2 = User(name='Test User 2', login='******', email='*****@*****.**', password='******') db.DBSession.add(user2) admin = User.query.filter(User.login == 'admin').first() import datetime vac1 = Vacation(user=user1, start=datetime.datetime(2016, 4, 22), end=datetime.datetime(2016, 4, 23), created_by=admin) db.DBSession.add(vac1) vac2 = Vacation(user=user1, start=datetime.datetime(2016, 4, 24), end=datetime.datetime(2016, 4, 25), created_by=admin) db.DBSession.add(vac2) vac3 = Vacation(user=user2, start=datetime.datetime(2016, 4, 10), end=datetime.datetime(2016, 4, 12), created_by=admin) db.DBSession.add(vac3) vac4 = Vacation(user=user2, start=datetime.datetime(2016, 4, 13), end=datetime.datetime(2016, 4, 20), created_by=admin) db.DBSession.add(vac4) db.DBSession.commit() # get the id vac1 = Vacation.query.filter(Vacation.name == vac1.name).first() vac2 = Vacation.query.filter(Vacation.name == vac2.name).first() vac3 = Vacation.query.filter(Vacation.name == vac3.name).first() vac4 = Vacation.query.filter(Vacation.name == vac4.name).first() from stalker_pyramid.testing import DummyRequest request = DummyRequest() vacation_views = vacation.VacationViews(request) response = vacation_views.get_entities() self.assertEqual(response.json_body, [{ 'id': v.id, '$ref': '/api/vacations/%s' % v.id, 'name': v.name, 'entity_type': 'Vacation' } for v in [vac1, vac2, vac3, vac4]])