def setUp(self): """Verifies that each of the model classes derived from declarative_base can be created""" Base.metadata.create_all(bind=Session.bind) # Should be bound to the session we've set up in __init__ self._service = DatasetService() user = User() user.name = "testing" another_user = User() another_user.name = "Someone else" session = Session() session.add(user) session.add(another_user) session.flush() self._user_id = user.id self._another_user_id = another_user.id session.commit() session.close() self._populate_session()
def test_app_user_can_create(self): """Can the application user perform inserts?""" with session_scope(Session) as session: user = User() user.name = "Test User" session.add(user) with session_scope(Session) as another_session: count = another_session.query(User).count() self.assertEqual(count, 1, "Expected a single user")
def test_create_user(self): # Important - don't instantiate the mock class, # as the session creation function in the service # will do that for us sample_user = User() sample_user.username = "******" sample_user.name = "Test User" sample_user.email = "*****@*****.**" sample_user.access_level = "External" self._mock_session.add = MagicMock() self._mock_session.commit = MagicMock() user_service = UserService(self._mock_session) user_service.create(sample_user.username, sample_user.name, sample_user.email, sample_user.access_level) self._mock_session.add.assert_called_once_with(ANY) self._mock_session.commit.assert_called_once_with()
def create(self, username, first_name, last_name, email, access_level): """Creates a user (if the user doesn't already exist) Params: username: The login name of the user first_name: User's first name last_name: User's last name email: User's email address access_level: Set to 'Admin' for administrative functions """ with self.transaction_scope() as session: user = User() user.username = username user.name = " ".join([first_name, last_name]) user.email = email user.access_level = access_level user.first_name = first_name user.last_name = last_name session.add(user)
def _populate_session(self): with self._service.transaction_scope() as session: user = User() user.username = "******" user.name = "Test User" user.email = "*****@*****.**" user.access_level = "CEH" session.add(user) pointDst = DatasetType() pointDst.type = "Point" coverDst = DatasetType() coverDst.type = "Coverage" resultDst = DatasetType() resultDst.type = "Result" session.add(pointDst) session.add(coverDst) session.add(resultDst) dataset_a = Dataset() dataset_a.dataset_type = pointDst dataset_a.viewable_by_user_id = self._user_id dataset_a.name = "Dataset1" session.add(dataset_a) dataset_b = Dataset() dataset_b.dataset_type = pointDst dataset_b.name = "Dataset2" session.add(dataset_b) dataset_c = Dataset() dataset_c.dataset_type = pointDst dataset_c.viewable_by_user_id = self._another_user_id dataset_c.name = "Dataset3" session.add(dataset_c) dataset_d = Dataset() dataset_d.dataset_type = resultDst dataset_d.name = "Results Dataset 1" dataset_d.viewable_by_user_id = 1 session.add(dataset_d) analysis_a = Analysis() analysis_a.point_dataset = dataset_a analysis_a.coverage_datasets.append(AnalysisCoverageDataset(dataset_b)) analysis_a.viewable_by = self._user_id analysis_a.result_dataset = dataset_d analysis_a.deleted = False analysis_b = Analysis() analysis_b.point_dataset = dataset_a analysis_b.coverage_datasets.append(AnalysisCoverageDataset(dataset_b)) analysis_b.run_by = self._user_id analysis_b.result_dataset = dataset_d analysis_b.deleted = False analysis_c = Analysis() analysis_c.point_dataset = dataset_a analysis_c.coverage_datasets.append(AnalysisCoverageDataset(dataset_b)) analysis_c.viewable_by = self._another_user_id analysis_c.result_dataset = dataset_d analysis_c.deleted = False session.add(analysis_a) session.add(analysis_b) session.add(analysis_c)
def setup_app(command, conf, vars): """Place any commands to setup ecomaps here - currently creating db tables""" # Don't reload the app if it was loaded under the testing environment if not pylons.test.pylonsapp: load_environment(conf.global_conf, conf.local_conf) # Create the tables if they don't already exist Base.metadata.drop_all(bind=Session.bind) Base.metadata.create_all(bind=Session.bind) with session_scope(Session) as session: user = User() user.name = "Phil Jenkins" user.first_name = "Phil" user.last_name = "Jenkins" user.username = "******" user.email = "*****@*****.**" user.access_level = "Admin" session.add(user) user2 = User() user2.name = "Mike Wilson" user2.first_name = "Mike" user2.last_name = "Wilson" user2.username = "******" user2.email = "*****@*****.**" user2.access_level = "Admin" session.add(user2) # Model that provides the interface to the R code model = Model() model.name = "LCM Thredds Model" model.id = 1 model.description = "LCM Thredds model written in R" model.code_path = "code_root" session.add(model) pointDst = DatasetType() pointDst.type = "Point" coverDst = DatasetType() coverDst.type = "Coverage" resultDst = DatasetType() resultDst.type = "Result" session.add(pointDst) session.add(coverDst) session.add(resultDst) # Define a datasetType lookup. This will conver the possible thredds # datasets into their EcoMaps equivalents. datasetTypes = {"GRID": coverDst, "POINT": pointDst} # Populate from thredds registerThreddsDatasets("http://thredds.ceh.ac.uk/thredds/ecomaps.xml", datasetTypes, session)