def test_returns_the_supposed_path_to_the_users_avatar_image_on_the_media_root(self): orm = OrmHolder().orm user = orm.query(User).get(3) path = user.avatar_path assert_equal(path, 'images/1/1.png')
def should_allow_admin_to_update_an_event_when_given_valid_input(self): from framework.orm_holder import OrmHolder from giveaminute.models import Event orm = OrmHolder().orm num_events = len(orm.query(Event).all()) # Login as an admin user self.login(1) response = self.app.post('/rest/v1/events/1/', params={ '_method': 'PUT', 'start_datetime': '2011-08-12 12:00:00', 'address': '85 2nd St., San Francisco CA 94105', 'project_id': 1, 'start_datetime': '2011-6-07 12:30', 'need_ids':'1,3' }, status=200) response_dict = json.loads(response.body) print response assert_equal(num_events, len(orm.query(Event).all())) assert_equal(response_dict['name'], 'Gallery Opening') assert_equal(response_dict['start_datetime'], '2011-06-07 12:30:00') assert_equal(set([int(need_dict['id']) for need_dict in response_dict['needs']]), set([1,3]))
def test_returns_None_if_user_has_no_avatar_set(self): orm = OrmHolder().orm user = orm.query(User).get(2) path = user.avatar_path assert_is_none(path)
def should_allow_admin_to_update_an_event_when_given_valid_input(self): from framework.orm_holder import OrmHolder from giveaminute.models import Event orm = OrmHolder().orm num_events = len(orm.query(Event).all()) # Login as an admin user self.login(1) response = self.app.post('/rest/v1/events/1/', params={ '_method': 'PUT', 'start_datetime': '2011-08-12 12:00:00', 'address': '85 2nd St., San Francisco CA 94105', 'project_id': 1, 'start_datetime': '2011-6-07 12:30', 'need_ids': '1,3' }, status=200) response_dict = json.loads(response.body) print response assert_equal(num_events, len(orm.query(Event).all())) assert_equal(response_dict['name'], 'Gallery Opening') assert_equal(response_dict['start_datetime'], '2011-06-07 12:30:00') assert_equal( set([int(need_dict['id']) for need_dict in response_dict['needs']]), set([1, 3]))
def test_does_not_contain_non_admin_users(self): orm = OrmHolder().orm project = orm.query(Project).get(1) user = orm.query(User).get(3) admins = project.admins assert_not_in(user, admins)
def should_return_true_if_user_is_member(self): from framework.orm_holder import OrmHolder from giveaminute.models import Project, User orm = OrmHolder().orm user = orm.query(User).get(1) project = orm.query(Project).get(1) rules = NonProjectMemberReadOnly() assert rules.is_member(user, project)
def test_makes_associated_need_accessible_when_it_is_created(self): orm = OrmHolder().orm vol = Volunteer() vol.need_id=2 #orm.query(Need).get(2) vol.member_id=1 orm.add(vol) orm.commit() assert_equal(vol.need_id, 2) assert vol.need is not None
def test_join_returns_false_when_user_is_already_a_member(self): orm = OrmHolder().orm user = orm.query(User).get(2) project = orm.query(Project).get(1) assert user in project.members joined = user.join(project) orm.commit() assert not joined
def test_leave_removes_a_member_from_the_project_and_returns_true(self): orm = OrmHolder().orm user = orm.query(User).get(2) project = orm.query(Project).get(1) assert user in project.members left = user.leave(project) orm.commit() assert user not in project.members assert left
def test_join_creates_a_member_in_the_project_and_returns_true(self): orm = OrmHolder().orm user = orm.query(User).get(3) project = orm.query(Project).get(1) assert user not in project.members joined = user.join(project) orm.commit() assert user in project.members assert joined
def should_be_able_to_delete_need_with_volunteers (self): orm = OrmHolder().orm need = orm.query(Need).get(1) assert len(need.volunteers) > 0 try: orm.delete(need) except AssertionError, e: # If the orm has a problem deleting the need, it will raise an # AssertionError ok_(False, 'AssertionError raised: %r' % (e,))
def __exit__(self, e_type=None, e_val=None, e_tb=None): from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Other exception with the ORM") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def test_removes_user_from_all_needs_on_a_project(self): orm = OrmHolder().orm user = orm.query(User).get(2) project = orm.query(Project).get(1) # Note: the following chain method is simply flattening the lists of # volunteers assert user in set(chain(*[need.volunteers for need in project.needs])) joined = user.unvolunteer_from_all_for(project) orm.commit() assert user not in set(chain(*[need.volunteers for need in project.needs]))
def test_will_remove_user_from_volunteering_on_needs_as_well(self): import giveaminute.project orm = OrmHolder().orm user = orm.query(User).get(2) need = orm.query(Need).get(1) assert user in need.volunteers giveaminute.project.removeUserFromProject(self.db, 1, 2) orm.commit() assert_not_in(user, need.volunteers)
def __exit__(self, e_type=None, e_val=None, e_tb=None): # Since load_sqla is a processor, it catches bubbled-up exceptions from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Unhandled exception - check console logs for details") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def should_not_change_the_event_if_not_logged_in(self): from framework.orm_holder import OrmHolder from giveaminute.models import Event orm = OrmHolder().orm response = self.app.post('/rest/v1/events/1/', params={ '_method': 'PUT', 'name': 'Changed name' }, status=403) print response assert_equal(orm.query(Event).get(1).name, 'Gallery Opening')
def should_not_delete_the_event_if_not_logged_in(self): from framework.orm_holder import OrmHolder from giveaminute.models import Event orm = OrmHolder().orm num_events = len(orm.query(Event).all()) response = self.app.post('/rest/v1/events/1/', params={ '_method': 'DELETE', }, status=403) print response assert_equal(num_events, len(orm.query(Event).all())) assert_in(1, [event.id for event in orm.query(Event).all()])
class OrmContextManager (object): def __enter__(self): log.debug("*** Loading the ORM") self.orm = OrmHolder().orm log.debug('***** loaded as %r' % self.orm) return self.orm def __exit__(self, e_type=None, e_val=None, e_tb=None): from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Other exception with the ORM") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def should_not_allow_anonymous_user_to_create_needs(self): # Check out http://webpy.org/cookbook/testing_with_paste_and_nose for # more about testing with Paste. from giveaminute.models import Need from framework.orm_holder import OrmHolder orm = OrmHolder().orm orig_needs = len(orm.query(Need).all()) response = self.app.post('/rest/v1/needs/', params={ 'type': 'volunteer', 'request': 'basketball players', 'quantity': '5', 'description': 'Play on my basketball team', 'project_id': 1, }, status=403) final_needs = len(orm.query(Need).all()) assert_equal(orig_needs, final_needs)
class OrmContextManager (object): def __enter__(self): log.debug("*** Loading the ORM") self.orm = OrmHolder().orm log.debug('***** loaded as %r' % self.orm) return self.orm def __exit__(self, e_type=None, e_val=None, e_tb=None): # Since load_sqla is a processor, it catches bubbled-up exceptions from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Unhandled exception - check console logs for details") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def raises_a_BadRequest_error_when_instance_refers_to_a_nonexistant_project( self): from framework.orm_holder import OrmHolder orm = OrmHolder().orm user = Mock() instance = Mock() instance.project = None instance.project_id = 55 access_rules = NonProjectAdminReadOnly() try: access_rules.can_create(user, instance, orm) ok_(False) except BadRequest: pass
def orm(self): """An active orm session for the controller.""" return OrmHolder().orm
responded_user_id = Column(Integer) # Should be foreign key is_active = Column(SmallInteger, nullable=False, default=1) created_datetime = Column(DateTime, nullable=False, default=datetime(1, 1, 1, 0, 0, 0)) updated_datetime = Column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) class Location (Base): __tablename__ = 'location' id = Column('location_id', Integer, primary_key=True) name = Column(String(50), nullable=False) lat = Column(Float) lon = Column(Float) borough = Column(String(50)) address = Column(String(100)) city = Column(String(50)) state = Column(String(2)) if __name__ == '__main__': import sys import os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from framework.orm_holder import OrmHolder oh = OrmHolder() config = oh.get_db_config() engine = oh.get_db_engine(config) Base.metadata.create_all(engine)
def __enter__(self): log.debug("*** Loading the ORM") self.orm = OrmHolder().orm log.debug('***** loaded as %r' % self.orm) return self.orm