def setUp(self): # Mock resource model resource1 = MagicMock() resource1.name = 'Resource1' resource1.version = '1.0' resource1.description = 'Test resource 1' resource1.content_type = 'text/plain' resource1.state = 'created' resource1.open = False resource1.get_url.return_value = 'http://localhost/media/resources/resource1' resource2 = MagicMock() resource2.name = 'Resource2' resource2.version = '2.0' resource2.description = 'Test resource 2' resource2.content_type = 'text/plain' resource2.state = 'created' resource2.open = False resource2.get_url.return_value = 'http://localhost/media/resources/resource2' resource3 = MagicMock() resource3.name = 'Resource3' resource3.version = '2.0' resource3.description = 'Test resource 3' resource3.content_type = 'text/plain' resource3.state = 'created' resource3.open = True resource3.get_url.return_value = 'http://localhost/media/resources/resource3' resource4 = MagicMock() resource4.name = 'Resource4' resource4.version = '1.0' resource4.description = 'Test resource 4' resource4.content_type = 'text/plain' resource4.state = 'created' resource4.open = True resource4.get_url.return_value = 'http://localhost/media/resources/resource4' resource4.offerings = ['1111', '2222'] resources_management.Resource = MagicMock() resources_management.Resource.objects.filter.return_value = [ resource1, resource2, resource3, resource4 ] self.user = MagicMock() self.org = MagicMock() self.user.userprofile.current_organization = self.org
def test_update_project_another_error(self, find_mock): """Test update_project another error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project task_presenter = "test/template.html" results = "test/results.html" tutorial = "test/tutorial.html" long_description = "test/long_description.md" pbclient = MagicMock() pbclient.update_project.return_value = self.error self.config.pbclient = pbclient res = _update_project(self.config, task_presenter, results, long_description, tutorial) msg = ("Project not found! The project: short_name is missing." \ " Use the flag --all=1 to search in all the server ") assert res == msg, msg
def test_add_tasks_properties_from_filextension(self, auto_mock, find_mock): """Test add_tasks properties without specifying file extension works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.name = 'tasks.properties' tasks.read.return_value = "foo_id= foo\n" pbclient = MagicMock() pbclient.create_task.return_value = { 'id': 1, 'info': { 'var_id': 'foo_id', 'string': ' foo' } } self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, None, 0, 30) assert res == '1 tasks added to project: short_name', res
def test_init(self, release_mock): repo_mock = MagicMock() repo_mock.description = u'Takes a jinja2 template and some json and sends an email' repo_mock.forks_count = 1 repo_mock.html_url = u'https://github.com/sanger-pathogens/json2email' repo_mock.name = u'json2email' repo_mock.stargazers_count = 1 repo_mock.updated_at = datetime.datetime(2015, 3, 28, 16, 58, 12) repo_mock.url = u'https://api.github.com/repos/sanger-pathogens/json2email' repo = RepoParser(repo_mock) self.assertEqual(repo.name, u'json2email') self.assertEqual( repo.description, u'Takes a jinja2 template and some json and sends an email' ) self.assertEqual( repo.html_url, u'https://github.com/sanger-pathogens/json2email' ) self.assertEqual( repo.updated_at, datetime.datetime(2015, 3, 28, 16, 58, 12) ) self.assertEqual(repo.stargazers_count, 1) self.assertEqual(repo.forks_count, 1) release_mock.assert_called_once_with( u'https://api.github.com/repos/sanger-pathogens/json2email', None )
def test_add_helping_materials_unknow_type(self, auto_mock, find_mock): """Test add_helpingmaterials with unknown type works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_helping_material.return_value = { 'id': 1, 'info': { 'key': 'value' } } self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'doc') assert res == ( "Unknown format for the tasks file. Use json, csv, po or " "properties."), res
def test_add_helping_materials_excel_with_info(self, auto_mock, find_mock, workbook_mock): """Test add_helpingmaterials excel with info field works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() project.id = 1 wb = Workbook() ws = wb.active headers = ['Column Name', 'foo'] ws.append(headers) for row in range(2, 10): ws.append(['value', 'bar']) ws.append([None, None]) ws.append([None, None]) find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = wb workbook_mock.return_value = wb pbclient = MagicMock() self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'xlsx') self.config.pbclient.create_helpingmaterial.assert_called_with(project_id=find_mock().id, info={u'column_name': u'value', u'foo': u'bar'}) assert res == '8 helping materials added to project: short_name', res
def test_add_helping_materials_json_with_info(self, find_mock): """Test add_helpingmaterials json with info field works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = json.dumps([{ 'info': { 'key': 'value' } }]) pbclient = MagicMock() pbclient.create_helpingmaterial.return_value = { 'id': 1, 'info': { 'key': 'value' } } self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'json') assert res == '1 helping materials added to project: short_name', res
def test_add_helping_materials_json_from_filextension( self, auto_mock, find_mock): """Test add_helpingmaterials json without specifying file extension works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.name = 'helpingmaterials.json' helpingmaterials.read.return_value = json.dumps([{ 'info': { 'key': 'value' } }]) pbclient = MagicMock() pbclient.create_helping_material.return_value = { 'id': 1, 'info': { 'key': 'value' } } self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, None) assert res == '1 helping materials added to project: short_name', res
def test_add_helping_materials_csv_with_info(self, auto_mock, find_mock): """Test add_helpingmaterials csv with info field works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "info, value\n, %s, 2" % json.dumps( {'key': 'value'}) pbclient = MagicMock() pbclient.create_helping_material.return_value = { 'id': 1, 'info': { 'key': 'value' } } self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'csv') assert res == '1 helping materials added to project: short_name', res
def test_add_tasks_po_with_info(self, find_mock, po_mock): """Test add_tasks po with info field works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project entry = MagicMock() entry.msgid = 'English' entry.msgtr = '' po = MagicMock() po.untranslated_entries.return_value = [entry] po_mock.return_value = po tasks = MagicMock() tasks.read.return_value = json.dumps([{ 'info': { 'msgid': 'English', 'msgtr': '' } }]) pbclient = MagicMock() pbclient.create_task.return_value = { 'id': 1, 'info': { 'msgid': 'English', 'msgtr': '' } } self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'po', 0, 30) assert res == '1 tasks added to project: short_name', res
def an_experiment_leaderboard_entry_dto(): entry = MagicMock() entry.entryType = 'experiment' entry.id = a_uuid_string() entry.shortId = a_string() entry.projectId = a_uuid_string() entry.state = 'succeeded' entry.experimentStates = an_experiment_states(succeeded=1) entry.responding = True entry.name = a_string() entry.organizationName = a_string() entry.projectName = a_string() entry.description = a_string() entry.timeOfCreation = a_timestamp() entry.timeOfCompletion = a_timestamp() entry.runningTime = randint(1, 1000) entry.owner = a_string() entry.size = randint(1, 1000) entry.tags = [a_string(), a_string()] entry.environment = a_string() entry.workerType = a_string() entry.hostname = a_string() entry.sourceSize = randint(1, 1000) entry.sourceMd5 = a_string() entry.commitId = a_string() entry.properties = [a_property(), a_property()] entry.parameters = [a_parameter(), a_parameter()] entry.channelsLastValues = [a_channel_value(), a_channel_value()] entry.trashed = False entry.deleted = False entry.isBestExperiment = False return entry
def mock_projects(mocker): mock = mocker.patch('portal.apps.jupyter_mounts.api.views.ProjectsManager') project1 = MagicMock() project1.description = "test" project1.storage = MagicMock(id="cep.project-1") project1.absolute_path = "/projects/cep.project-1" project1.roles = MagicMock( roles=[MagicMock(role="ADMIN", username="******")]) project2 = MagicMock() project2.description = "test" project2.storage = MagicMock(id="cep.project-2") project2.absolute_path = "/projects/cep.project-2" project2.roles = MagicMock( roles=[MagicMock(role="GUEST", username="******")]) mock.return_value.list.return_value = [project1, project2] yield mock
def test_add_tasks_po_from_filextension(self, find_mock, po_mock): """Test add_tasks po without specifying file extension works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project entry = MagicMock() entry.msgid = 'English' entry.msgtr = '' po = MagicMock() po.untranslated_entries.return_value = [entry] po_mock.return_value = po tasks = MagicMock() tasks.name = 'tasks.po' tasks.read.return_value = json.dumps([{'info': {'msgid': 'English', 'msgtr':''}}]) pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'msgid': 'English', 'msgtr': ''}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, None, 0, 30) assert res == '1 tasks added to project: short_name', res
def test_execute_query(self, mock_db, mock_statsd, mock_statsd_scoring): mock_cursor = MagicMock(spec=Cursor) mock_cursor.fetchall.return_value = DUMMY_QUERY['rows'] mock_cursor.description = DUMMY_QUERY['description'] db = MagicMock() db.cursor = MagicMock(return_value=mock_cursor) results = self.grader.execute_query(db, DUMMY_QUERY['query']) self.assertEquals(DUMMY_QUERY['result'], results)
def _create_mock_snap(self, create_time, image_id=None, hostclass=None, env=None, description=None): mock = MagicMock() mock.start_time = create_time mock.tags = {} mock.id = 'snap-' + str(random.randrange(0, 9999999)) mock.description = description or '' if image_id: mock.description = 'Created by CreateImage for %s' % image_id if hostclass: mock.tags['hostclass'] = hostclass if env: mock.tags['env'] = env return mock
def test_write_temp_file(self): task_id = "some_test_id" sql = "some_sql" sql_params = {':p_data': "2018-01-01"} oracle_conn_id = "oracle_conn_id" filename = "some_filename" azure_data_lake_conn_id = 'azure_data_lake_conn_id' azure_data_lake_path = 'azure_data_lake_path' delimiter = '|' encoding = 'utf-8' cursor_description = [ ('id', "<class 'cx_Oracle.NUMBER'>", 39, None, 38, 0, 0), ('description', "<class 'cx_Oracle.STRING'>", 60, 240, None, None, 1), ] cursor_rows = [[1, 'description 1'], [2, 'description 2']] mock_cursor = MagicMock() mock_cursor.description = cursor_description mock_cursor.__iter__.return_value = cursor_rows op = OracleToAzureDataLakeOperator( task_id=task_id, filename=filename, oracle_conn_id=oracle_conn_id, sql=sql, sql_params=sql_params, azure_data_lake_conn_id=azure_data_lake_conn_id, azure_data_lake_path=azure_data_lake_path, delimiter=delimiter, encoding=encoding, ) with TemporaryDirectory(prefix='airflow_oracle_to_azure_op_') as temp: op._write_temp_file(mock_cursor, os.path.join(temp, filename)) assert os.path.exists(os.path.join(temp, filename)) == 1 with open(os.path.join(temp, filename), 'rb') as csvfile: temp_file = csv.reader(csvfile, delimiter=delimiter, encoding=encoding) rownum = 0 for row in temp_file: if rownum == 0: self.assertEqual(row[0], 'id') self.assertEqual(row[1], 'description') else: self.assertEqual(row[0], str(cursor_rows[rownum - 1][0])) self.assertEqual(row[1], cursor_rows[rownum - 1][1]) rownum = rownum + 1
def test_delete_another_error_one_tasks(self, find_mock): """Test delete tasks another error works for one task.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.delete_task.return_value = self.error_task self.config.pbclient = pbclient assert_raises(TaskNotFound, _delete_tasks, self.config, 1)
def test_delete_task(self, find_mock): """Test delete task works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.delete_task.return_value = [] self.config.pbclient = pbclient res = _delete_tasks(self.config, 1) assert res == "Task.id = 1 and its associated task_runs have been deleted", res
def test_update_task_redundancy_fails(self, find_mock): """Test update task redundancy fails works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.find_tasks.return_value = self.error_task self.config.pbclient = pbclient assert_raises(TaskNotFound, _update_tasks_redundancy, self.config, 9999, 5)
def test_update_task_redundancy_individually(self, find_mock): """Test update task redundancy individually works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() self.config.pbclient = pbclient res = _update_tasks_redundancy(self.config, 1, 5) msg = "Task.id = 1 redundancy has been updated to 5" assert res == msg, res
def test_delete_connection_error(self, find_mock): """Test delete tasks connection error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.delete_task.side_effect = exceptions.ConnectionError self.config.pbclient = pbclient res = _delete_tasks(self.config, 1) assert res == "Connection Error! The server http://server is not responding", res
def test_update_task_redundancy_all_tasks(self, find_mock): """Test update task redundancy all tasks works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.get_tasks = self.fake_return_tasks self.config.pbclient = pbclient res = _update_tasks_redundancy(self.config, None, 5) msg = "All tasks redundancy have been updated" assert res == msg, res
def _generate_basic_datarequest(id=DATAREQUEST_ID, user_id='example_uuidv4_user', title='This is a title', description='This is a basic description', organization_id='example_uuidv4_organization', closed=False): datarequest = MagicMock() datarequest.id = id datarequest.user_id = user_id datarequest.title = title datarequest.description = description datarequest.organization_id = organization_id datarequest.open_time = datetime.datetime.now() datarequest.closed = closed datarequest.close_time = None datarequest.accepted_dataset_id = None datarequest.accepted_dataset = {'test': 'test1', 'test2': 'test3'} return datarequest
def test_add_helping_materials_excel_with_file(self, auto_mock, find_mock, workbook_mock): """Test add_helpingmaterials excel with file_path field works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() project.id = 1 wb = Workbook() ws = wb.active headers = ['Column Name', 'foo', 'file Path'] ws.append(headers) for row in range(2, 10): ws.append(['value', 'bar', '/tmp/file.jpg']) ws.append([None, None, None]) ws.append([None, None, None]) find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = wb workbook_mock.return_value = wb pbclient = MagicMock() hm = MagicMock() hm.info = {'column_name': 'value', 'foo': 'bar'} hm.id = 1 pbclient.create_helpingmaterial.return_value = hm self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'xlsx') self.config.pbclient.create_helpingmaterial.assert_called_with( project_id=find_mock().id, file_path='/tmp/file.jpg', info={ u'column_name': u'value', u'foo': u'bar' }) self.config.pbclient.update_helping_material.assert_called_with(hm) assert res == '8 helping materials added to project: short_name', res
def test_update_task_redundancy_fails(self, auto_mock, find_mock): """Test update task redundancy fails works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.find_tasks.return_value = self.error_task self.config.pbclient = pbclient assert_raises(TaskNotFound, _update_tasks_redundancy, self.config, 9999, 5)
def create_mock_domain(properties=None): """Create and return a mocked Domain. :param properties: A dictionary of the domain's properties. :type properties: dict :return: A MagicMock object with the properties set. :rtype: MagicMock """ mock_object = MagicMock(name='Domain') mock_object.contact_address = '' mock_object.description = '' mock_object.mail_host = '' mock_object.lists = [] if properties is not None: for key in properties: setattr(mock_object, key, properties[key]) return mock_object
def test_get_sinks(self): request = MagicMock() sink = MagicMock() sink.channel_count = 2 sink.channel_list = ["front-left", "front-right"] sink.description = "test" sink.index = 0 sink.name = "test" sink.volume = MagicMock() sink.volume.values = [1.0, 1.0] self.pulse.sink_list.return_value = [sink] response = self.api.get_sinks(request) data = json.loads(response) assert len(data) == 1 assert data[0]['name'] == "test"
def test_delete_another_error_all_tasks(self, find_mock): """Test delete tasks another error works for all tasks.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.delete_task.return_value = self.error task = MagicMock() task.id = 1 pbclient.get_tasks.return_value = [task] self.config.pbclient = pbclient assert_raises(ProjectNotFound, _delete_tasks, self.config, None)
def test_update_task_redundancy_individually(self, auto_mock, find_mock): """Test update task redundancy individually works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() self.config.pbclient = pbclient res = _update_tasks_redundancy(self.config, 1, 5) msg = "Task.id = 1 redundancy has been updated to 5" assert res == msg, res
def test_add_tasks_another_error(self, find_mock): """Test add_tasks another error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = json.dumps([{'key': 'value'}]) pbclient = MagicMock() pbclient.create_task.return_value = self.error self.config.pbclient = pbclient assert_raises(ProjectNotFound, _add_tasks, self.config, tasks, 'json', 0, 30)
def test_add_helpingmaterial_another_error(self, find_mock): """Test add_tasks another error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = json.dumps([{'key': 'value'}]) pbclient = MagicMock() pbclient.create_helpingmaterial.return_value = self.error self.config.pbclient = pbclient assert_raises(ProjectNotFound, _add_helpingmaterials, self.config, tasks, 'json')
def test_add_helping_materials_csv_connection_error(self, find_mock): """Test add_helpingmaterials csv connection error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_helpingmaterial.side_effect = exceptions.ConnectionError self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'csv') assert res == "Connection Error! The server http://server is not responding", res
def test_add_helping_material_json_connection_error(self, find_mock): """Test add_helpingmaterials json connection error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = json.dumps([{'key': 'value'}]) pbclient = MagicMock() pbclient.create_helpingmaterial.side_effect = exceptions.ConnectionError self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, tasks, 'json') assert res == "Connection Error! The server http://server is not responding", res
def test_add_tasks_json_without_info(self, find_mock): """Test add_tasks json without info field works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = json.dumps([{'key': 'value'}]) pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'json', 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_tasks_properties_with_info(self, find_mock): """Test add_tasks properties with info field works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = "foo_id= foo\n" pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'var_id': 'foo_id', 'string': ' foo'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'properties', 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_tasks_json_connection_error(self, find_mock): """Test add_tasks json connection error works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = json.dumps([{'key': 'value'}]) pbclient = MagicMock() pbclient.create_task.side_effect = exceptions.ConnectionError self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'json', 0, 30) assert res == "Connection Error! The server http://server is not responding", res
def test_add_tasks_unknow_type(self, find_mock): """Test add_tasks with unknown type works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'doc', 0, 30) assert res == ("Unknown format for the tasks file. Use json, csv, po or " "properties."), res
def test_add_helping_materials_csv_with_info(self, auto_mock, find_mock): """Test add_helpingmaterials csv with info field works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "info, value\n, %s, 2" % json.dumps({'key':'value'}) pbclient = MagicMock() pbclient.create_helping_material.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'csv') assert res == '1 helping materials added to project: short_name', res
def test_update_task_redundancy_all_tasks(self, auto_mock, find_mock): """Test update task redundancy all tasks works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project pbclient = MagicMock() pbclient.get_tasks = self.fake_return_tasks self.config.pbclient = pbclient res = _update_tasks_redundancy(self.config, None, 5) msg = "All tasks redundancy have been updated" assert res == msg, res
def test_delete_all_tasks(self, find_mock): """Test delete all tasks works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project task = MagicMock() task.id = 1 pbclient = MagicMock() pbclient.get_tasks.side_effect = [[task], []] self.config.pbclient = pbclient res = _delete_tasks(self.config, None, limit=1, offset=0) assert res == "All tasks and task_runs have been deleted", res
def create_mock_domain(properties=None): """Create and return a mocked Domain. :param properties: A dictionary of the domain's properties. :type properties: dict :return: A MagicMock object with the properties set. :rtype: MagicMock """ mock_object = MagicMock(name='Domain') mock_object.base_url = '' mock_object.contact_address = '' mock_object.description = '' mock_object.mail_host = '' mock_object.url_host = '' mock_object.lists = [] if properties is not None: for key in properties: setattr(mock_object, key, properties[key]) return mock_object
def test_add_tasks_csv_from_filextension(self, find_mock): """Test add_tasks csv without specifying file extension works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.name = 'tasks.csv' tasks.read.return_value = "info, value\n, %s, 2" % json.dumps({'key':'value'}) pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, None, 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_tasks_json_from_filextension(self, find_mock): """Test add_tasks json without specifying file extension works.""" project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.name = 'tasks.json' tasks.read.return_value = json.dumps([{'info': {'key': 'value'}}]) pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, None, 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_helping_materials_json_from_filextension(self, auto_mock, find_mock): """Test add_helpingmaterials json without specifying file extension works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.name = 'helpingmaterials.json' helpingmaterials.read.return_value = json.dumps([{'info': {'key': 'value'}}]) pbclient = MagicMock() pbclient.create_helping_material.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, None) assert res == '1 helping materials added to project: short_name', res
def test_add_tasks_csv_without_info(self, auto_mock, find_mock): """Test add_tasks csv without info field works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, 'csv', 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_helping_materials_csv_connection_error(self, auto_mock, find_mock): """Test add_helpingmaterials csv connection error works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_helpingmaterial.side_effect = exceptions.ConnectionError self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'csv') assert res == "Connection Error! The server http://server is not responding", res
def test_add_tasks_properties_from_filextension(self, auto_mock, find_mock): """Test add_tasks properties without specifying file extension works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project tasks = MagicMock() tasks.name = 'tasks.properties' tasks.read.return_value = "foo_id= foo\n" pbclient = MagicMock() pbclient.create_task.return_value = {'id': 1, 'info': {'var_id': 'foo_id', 'string': ' foo'}} self.config.pbclient = pbclient res = _add_tasks(self.config, tasks, None, 0, 30) assert res == '1 tasks added to project: short_name', res
def test_add_helping_materials_unknow_type(self, auto_mock, find_mock): """Test add_helpingmaterials with unknown type works.""" auto_mock.return_value = (0, None) project = MagicMock() project.name = 'name' project.short_name = 'short_name' project.description = 'description' project.info = dict() find_mock.return_value = project helpingmaterials = MagicMock() helpingmaterials.read.return_value = "key, value\n, 1, 2" pbclient = MagicMock() pbclient.create_helping_material.return_value = {'id': 1, 'info': {'key': 'value'}} self.config.pbclient = pbclient res = _add_helpingmaterials(self.config, helpingmaterials, 'doc') assert res == ("Unknown format for the tasks file. Use json, csv, po or " "properties."), res
def setUp(self): cards_def = [ { 'name': 'XXX [cyclic {35 22 * * *}] asd', 'description': 'XXX desc' }, { 'name': 'YYY [cyclic {35 23 * * *}] asd', 'description': 'yyy desc' }, { 'name': 'ABC asd', 'description': 'abc desc' } ] self.cards = [] for cd in cards_def: m = MagicMock() m.name = cd['name'] m.description = cd['description'] self.cards.append(m)