def test_create_action_missing_arguments(instrument, capsys): assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_action', str(instrument.id)]) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] assert len(actions.get_actions()) == 0
def test_create_independent_action(): assert len(actions.get_actions()) == 0 action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA) assert action.name == "Example Action" assert action.description == "" assert action.schema == SCHEMA assert len(actions.get_actions()) == 1 assert action == actions.get_action(action_id=action.id)
def test_create_action_missing_instrument(capsys): action_type = 'sample' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'sampledb', 'schemas', 'minimal.json')) assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_action', '1', action_type, name, description, schema_file_name]) assert exc_info.value != 0 assert 'Error: no instrument with this id exists' in capsys.readouterr()[1] assert len(actions.get_actions()) == 0
def test_create_action_invalid_type(instrument, capsys): action_type = 'sample_creation' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'sampledb', 'schemas', 'minimal.json')) assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_action', str(instrument.id), action_type, name, description, schema_file_name]) assert exc_info.value != 0 assert 'Error: action type must be "sample" or "measurement"' in capsys.readouterr()[1] assert len(actions.get_actions()) == 0
def test_set_up_demo_twice(capsys, tmpdir): config.FILE_STORAGE_PATH = tmpdir scripts.main([scripts.__file__, 'set_up_demo']) assert 'Success' in capsys.readouterr()[0] assert actions.get_actions() num_actions = len(actions.get_actions()) with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'set_up_demo']) assert exc_info.value != 0 assert 'Error' in capsys.readouterr()[1] assert len(actions.get_actions()) == num_actions
def test_create_instrument_action(): instrument = instruments.create_instrument( "Example Instrument", "Example Instrument Description") assert len(actions.get_actions()) == 0 action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA, instrument_id=instrument.id) assert action.name == "Example Action" assert action.description == "" assert action.schema == SCHEMA assert len(actions.get_actions()) == 1 assert action == actions.get_action(action_id=action.id) assert action.instrument == instrument
def test_update_action(action, schema_file_name, capsys): name = 'Example Action' description = 'Example Action Description' assert len(actions.get_actions()) == 1 scripts.main([ scripts.__file__, 'update_action', str(action.id), name, '', schema_file_name ]) assert 'Success' in capsys.readouterr()[0] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == '' assert action.type == actions.ActionType.SAMPLE_CREATION
def test_set_up_demo_one_user_exists(capsys, tmpdir): config.FILE_STORAGE_PATH = tmpdir users.create_user("username", "*****@*****.**", users.UserType.PERSON) scripts.main([scripts.__file__, 'set_up_demo']) assert 'Success' in capsys.readouterr()[0] assert actions.get_actions()
def test_create_measurement_action(instrument, capsys): action_type = 'measurement' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'sampledb', 'schemas', 'minimal.json')) assert len(actions.get_actions()) == 0 scripts.main([scripts.__file__, 'create_action', str(instrument.id), action_type, name, description, schema_file_name]) assert 'Success' in capsys.readouterr()[0] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == description assert action.instrument_id == instrument.id assert action.type == actions.ActionType.MEASUREMENT
def test_update_action_missing_arguments(action, capsys): name = 'Example Action' description = 'Example Action Description' assert len(actions.get_actions()) == 1 with pytest.raises(SystemExit) as exc_info: scripts.main( [scripts.__file__, 'update_action', str(action.id), name, '']) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == description assert action.type == actions.ActionType.SAMPLE_CREATION
def test_update_action_invalid_action_id(action, schema_file_name, capsys): name = 'Example Action' description = 'Example Action Description' assert len(actions.get_actions()) == 1 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'update_action', name, name, '', schema_file_name ]) assert exc_info.value != 0 assert 'Error: action_id must be an integer' in capsys.readouterr()[1] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == description assert action.type == actions.ActionType.SAMPLE_CREATION
def test_create_action_invalid_instrument_id(instrument, capsys): action_type = 'sample' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'test_data', 'schemas', 'minimal.json')) assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'create_action', instrument.name, action_type, name, description, schema_file_name ]) assert exc_info.value != 0 assert 'Error: instrument_id must be an integer' in capsys.readouterr()[1] assert len(actions.get_actions()) == 0
def test_get_missing_action(): assert len(actions.get_actions()) == 0 action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA) with pytest.raises(errors.ActionDoesNotExistError): actions.get_action(action.id + 1)
def test_get_actions(): measurement_action = actions.create_action( sampledb.models.ActionType.MEASUREMENT, name="Example Action", description="", schema=SCHEMA) sample_action = actions.create_action( sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA) assert actions.get_actions() == [ measurement_action, sample_action ] or actions.get_actions() == [sample_action, measurement_action] assert actions.get_actions( sampledb.models.ActionType.SAMPLE_CREATION) == [sample_action] assert actions.get_actions( sampledb.models.ActionType.MEASUREMENT) == [measurement_action]
def test_create_action_invalid_schema(instrument, capsys): action_type = 'sample' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath( os.path.join(os.path.dirname(sampledb.__file__), 'schemas', 'action.json')) assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'create_action', str(instrument.id), action_type, name, description, schema_file_name ]) assert exc_info.value != 0 assert 'Error: invalid schema:' in capsys.readouterr()[1] assert len(actions.get_actions()) == 0
def test_update_action_missing_action(action, schema_file_name, capsys): name = 'Example Action' description = 'Example Action Description' assert len(actions.get_actions()) == 1 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'update_action', str(action.id + 1), name, '', schema_file_name ]) assert exc_info.value != 0 assert 'Error: no action with this id exists' in capsys.readouterr()[1] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == description assert action.type == actions.ActionType.SAMPLE_CREATION
def test_create_user_action(): user = users.User(name="Testuser", email="*****@*****.**", type=sampledb.models.UserType.PERSON) sampledb.db.session.add(user) sampledb.db.session.commit() assert len(actions.get_actions()) == 0 action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA, user_id=user.id) assert action.name == "Example Action" assert action.description == "" assert action.schema == SCHEMA assert action.user_id == user.id assert len(actions.get_actions()) == 1 assert action == actions.get_action(action_id=action.id)
def test_create_action_invalid_type(instrument, capsys): action_type = 'sample_creation' name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'test_data', 'schemas', 'minimal.json')) assert len(actions.get_actions()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'create_action', str(instrument.id), action_type, name, description, schema_file_name ]) assert exc_info.value != 0 assert 'Error: action type must be one of the following:\n- "sample" (for Sample Creation)\n- "measurement" (for Measurement)\n- "simulation" (for Simulation)\n- "-99" (for Sample Creation)\n- "-98" (for Measurement)\n- "-97" (for Simulation)\n' in capsys.readouterr( )[1] assert len(actions.get_actions()) == 0
def test_create_missing_instrument_action(): instrument = instruments.create_instrument( "Example Instrument", "Example Instrument Description") assert len(actions.get_actions()) == 0 with pytest.raises(errors.InstrumentDoesNotExistError): actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION, name="Example Action", description="", schema=SCHEMA, instrument_id=instrument.id + 1)
def test_set_up_demo_two_users_exist(capsys, tmpdir): config.FILE_STORAGE_PATH = tmpdir users.create_user("username", "*****@*****.**", users.UserType.PERSON) users.create_user("username", "*****@*****.**", users.UserType.PERSON) with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'set_up_demo']) assert exc_info.value != 0 assert 'Error' in capsys.readouterr()[1] assert not actions.get_actions()
def test_update_action_invalid_schema(action, capsys): name = 'Example Action' description = 'Example Action Description' schema_file_name = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', 'sampledb', 'schemas', 'action.json')) assert len(actions.get_actions()) == 1 with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'update_action', str(action.id), name, '', schema_file_name ]) assert exc_info.value != 0 assert 'Error: invalid schema:' in capsys.readouterr()[1] assert len(actions.get_actions()) == 1 action = actions.get_actions()[0] assert action.name == name assert action.description == description assert action.type == actions.ActionType.SAMPLE_CREATION
def test_set_up_demo(capsys, tmpdir): config.FILE_STORAGE_PATH = tmpdir scripts.main([scripts.__file__, 'set_up_demo']) assert 'Success' in capsys.readouterr()[0] assert actions.get_actions()