def test_update_instrument_responsible_users(instrument, users, capsys): instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0 instruments.add_instrument_responsible_user(instrument.id, users[0].id) instruments.add_instrument_responsible_user(instrument.id, users[1].id) assert len(instrument.responsible_users) == 2 assert instrument.responsible_users == [ users[0], users[1] ] or instrument.responsible_users == [users[1], users[0]] scripts.main([ scripts.__file__, 'update_instrument_responsible_users', str(instrument.id), str(users[0].id), str(users[2].id) ]) assert "Success" in capsys.readouterr()[0] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 2 assert instrument.responsible_users == [ users[0], users[2] ] or instrument.responsible_users == [users[2], users[0]] scripts.main([ scripts.__file__, 'update_instrument_responsible_users', str(instrument.id) ]) assert "Success" in capsys.readouterr()[0] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0
def test_set_administrator_no_no_change(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id scripts.main([scripts.__file__, 'set_administrator', str(user_id), 'no']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_admin
def test_set_hidden_no_no_change(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id scripts.main([scripts.__file__, 'set_user_hidden', str(user_id), 'no']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_hidden
def test_export_action_schema_missing_arguments(action, capsys): with pytest.raises(SystemExit) as exc_info: scripts.main( [scripts.__file__, 'export_action_schema', str(action.id)]) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0]
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_set_hidden_yes(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id scripts.main([scripts.__file__, 'set_user_hidden', str(user_id), 'yes']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert user.is_hidden
def test_update_instrument_responsible_users_missing_instrument(users, capsys): with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'update_instrument_responsible_users', '1', str(users[0].id) ]) assert exc_info.value != 0 assert "Error: no instrument with this id exists" in capsys.readouterr()[1]
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_set_readonly_yes(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id scripts.main([scripts.__file__, 'set_user_readonly', str(user_id), 'yes']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert user.is_readonly
def test_create_other_user_empty_name(capsys): assert len(users.get_users()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_other_user', '', 'password']) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] assert len(users.get_users()) == 0
def test_set_hidden_invalid_argument(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'set_user_hidden', str(user_id), 'maybe']) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_hidden
def test_set_administrator_missing_arguments(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'set_administrator', str(user_id)]) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_admin
def test_export_action_schema_missing_action(capsys): with tempfile.NamedTemporaryFile('r') as schema_file: with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'export_action_schema', 1, schema_file.name ]) assert exc_info.value != 0 assert schema_file.read() == '' assert 'Error: no action with this id exists' in capsys.readouterr()[1]
def test_export_action_schema(action, capsys): with tempfile.NamedTemporaryFile('r') as schema_file: scripts.main([ scripts.__file__, 'export_action_schema', str(action.id), schema_file.name ]) schema = json.load(schema_file) assert action.schema == schema assert 'Success' in capsys.readouterr()[0]
def test_create_other_user_missing_arguments(capsys): assert len(users.get_users()) == 0 name = 'username' with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_other_user', name]) assert exc_info.value != 0 assert 'Usage' in capsys.readouterr()[0] assert len(users.get_users()) == 0
def test_export_action_schema_invalid_action_id(action, capsys): with tempfile.NamedTemporaryFile('r') as schema_file: with pytest.raises(SystemExit) as exc_info: scripts.main([ scripts.__file__, 'export_action_schema', action.name, schema_file.name ]) assert exc_info.value != 0 assert schema_file.read() == '' assert 'Error: action_id must be an integer' in capsys.readouterr()[1]
def test_update_instrument_responsible_users_invalid_user(instrument, capsys): instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'update_instrument_responsible_users', str(instrument.id), 'User 1']) assert exc_info.value != 0 assert "Error: instrument_reponsible_user_ids must be integer" in capsys.readouterr()[1] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0
def test_update_instrument_responsible_users_missing_arguments(instrument, users, capsys): instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'update_instrument_responsible_users']) assert exc_info.value != 0 assert "Usage" in capsys.readouterr()[0] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0
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_create_existing_instrument(capsys): name = 'Example Instrument' description = 'Example Instrument Description' instruments.create_instrument(name, description) assert len(instruments.get_instruments()) == 1 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'create_instrument', name, description]) assert exc_info.value != 0 assert 'Error: an instrument with this name already exists' in capsys.readouterr()[1] assert len(instruments.get_instruments()) == 1
def test_set_hidden_yes_no_change(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id user = users.get_user(user_id) user.is_hidden = True db.session.add(user) db.session.commit() scripts.main([scripts.__file__, 'set_user_hidden', str(user_id), 'yes']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert user.is_hidden
def test_update_instrument_responsible_users_invalid_instrument(instrument, users, capsys): instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'update_instrument_responsible_users', 'Example Instrument', str(users[0].id)]) assert exc_info.value != 0 assert "Error: instrument_id must be an integer" in capsys.readouterr()[1] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0
def test_update_instrument_responsible_users_missing_user(instrument, capsys): instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main([scripts.__file__, 'update_instrument_responsible_users', str(instrument.id), '42']) assert exc_info.value != 0 assert "Error: no user with the id #42 exists" in capsys.readouterr()[1] instrument = instruments.get_instruments()[0] assert len(instrument.responsible_users) == 0
def test_set_administrator_no(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id user = users.get_user(user_id) user.is_admin = True db.session.add(user) db.session.commit() scripts.main([scripts.__file__, 'set_administrator', str(user_id), 'no']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_admin
def test_set_readonly_no(capsys): user_id = users.create_user("username", "*****@*****.**", users.UserType.PERSON).id user = users.get_user(user_id) user.is_readonly = True db.session.add(user) db.session.commit() scripts.main([scripts.__file__, 'set_user_readonly', str(user_id), 'no']) assert 'Success' in capsys.readouterr()[0] user = users.get_user(user_id) assert not user.is_readonly
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_update_missing_instrument(capsys): name = 'Example Instrument' description = 'Example Instrument Description' assert len(instruments.get_instruments()) == 0 with pytest.raises(SystemExit) as exc_info: scripts.main( [scripts.__file__, 'update_instrument', str(1), name, description]) assert exc_info.value != 0 assert "Error: no instrument with this id exists" in capsys.readouterr()[1] assert len(instruments.get_instruments()) == 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_create_instrument(capsys): name = 'Example Instrument' description = 'Example Instrument Description' assert len(instruments.get_instruments()) == 0 scripts.main([scripts.__file__, 'create_instrument', name, description]) assert 'Success' in capsys.readouterr()[0] assert len(instruments.get_instruments()) == 1 instrument = instruments.get_instruments()[0] assert instrument.name == name assert instrument.description == description assert len(instrument.responsible_users) == 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