def test_calcjob_submission(db_test_app, data_regression): # type: (AiidaTestApp) -> None """Test submitting a calculation.""" from aiida.orm import SinglefileData # Prepare input parameters code = db_test_app.get_or_create_code("crystal17.basic") with open_resource_binary("crystal", "mgo_sto3g_scf", "INPUT") as handle: infile = SinglefileData(file=handle) infile.store() # set up calculation builder = code.get_builder() builder.metadata.options.withmpi = False builder.metadata.options.resources = { "num_machines": 1, "num_mpiprocs_per_machine": 1, } builder.input_file = infile with db_test_app.sandbox_folder() as folder: calc_info = db_test_app.generate_calcinfo("crystal17.basic", folder, builder) data_regression.check(sanitize_calc_info(calc_info))
def test_binary_file(self): """Test that the constructor accepts binary files.""" byte_array = [120, 3, 255, 0, 100] content_binary = bytearray(byte_array) with tempfile.NamedTemporaryFile(mode='wb+') as handle: basename = os.path.basename(handle.name) handle.write(bytearray(content_binary)) handle.flush() handle.seek(0) node = SinglefileData(handle.name) with node.open(mode='rb') as handle: content_stored = handle.read() self.assertEqual(content_stored, content_binary) self.assertEqual(node.list_object_names(), [basename]) node.store() with node.open(mode='rb') as handle: content_stored = handle.read() self.assertEqual(content_stored, content_binary) self.assertEqual(node.list_object_names(), [basename])
def get_singlefile_instance(description, path): """ Retrieve an instance of SinglefileData with the given description, loading it from ``path`` if it does not exist. """ query_builder = QueryBuilder() query_builder.append(SinglefileData, filters={'description': { '==': description }}) res = query_builder.all() if len(res) == 0: # create archive res = SinglefileData(file=os.path.abspath(path)) res.description = description res.store() elif len(res) > 1: raise ValueError( 'Query returned more than one matching SinglefileData instance.') else: res = res[0][0] return res
def test_construct_from_string(self): """Test constructing an instance from a string.""" content_original = 'some testing text\nwith a newline' with io.BytesIO(content_original.encode('utf-8')) as handle: node = SinglefileData(file=handle) with node.open() as handle: content_stored = handle.read() self.assertEqual(content_stored, content_original) self.assertEqual(node.list_object_names(), [SinglefileData.DEFAULT_FILENAME]) node.store() with node.open() as handle: content_stored = handle.read() self.assertEqual(content_stored, content_original) self.assertEqual(node.list_object_names(), [SinglefileData.DEFAULT_FILENAME])
def test_reload_singlefile_data(self): """Test writing and reloading a `SinglefileData` instance.""" content_original = 'some text ABCDE' with tempfile.NamedTemporaryFile(mode='w+') as handle: filepath = handle.name basename = os.path.basename(filepath) handle.write(content_original) handle.flush() node = SinglefileData(file=filepath) uuid = node.uuid with node.open() as handle: content_written = handle.read() self.assertEqual(node.list_object_names(), [basename]) self.assertEqual(content_written, content_original) node.store() with node.open() as handle: content_stored = handle.read() self.assertEqual(content_stored, content_original) self.assertEqual(node.list_object_names(), [basename]) node_loaded = load_node(uuid) self.assertTrue(isinstance(node_loaded, SinglefileData)) with node.open() as handle: content_loaded = handle.read() self.assertEqual(content_loaded, content_original) self.assertEqual(node_loaded.list_object_names(), [basename]) with node_loaded.open() as handle: self.assertEqual(handle.read(), content_original)
def test_construct_from_filelike(self): """Test constructing an instance from filelike instead of filepath.""" content_original = 'some testing text\nwith a newline' with tempfile.NamedTemporaryFile(mode='wb+') as handle: basename = os.path.basename(handle.name) handle.write(content_original.encode('utf-8')) handle.flush() handle.seek(0) node = SinglefileData(file=handle) with node.open() as handle: content_stored = handle.read() self.assertEqual(content_stored, content_original) self.assertEqual(node.list_object_names(), [basename]) node.store() with node.open() as handle: content_stored = handle.read() self.assertEqual(content_stored, content_original) self.assertEqual(node.list_object_names(), [basename])