def test_push_git_success(request_mocker, mocker, upload_url, query_project, upsert_run): query_project(request_mocker) upload_url(request_mocker) update_mock = upsert_run(request_mocker) with CliRunner().isolated_filesystem(): res = os.mkdir("wandb") with open("wandb/latest.yaml", "w") as f: f.write( yaml.dump({ 'wandb_version': 1, 'test': { 'value': 'success', 'desc': 'My life' } })) with open("weights.h5", "w") as f: f.write("weight") with open("model.json", "w") as f: f.write("model") r = git.Repo.init(".") r.index.add(["model.json"]) r.index.commit("initial commit") api = wandb_api.Api(load_settings=False, default_settings={'git_tag': True}) mock = mocker.patch.object(api.git, "push") res = api.push("test/test", ["weights.h5", "model.json"]) assert res[0].status_code == 200 mock.assert_called_once_with("test")
def runner(monkeypatch): monkeypatch.setattr(cli, 'api', wandb_api.Api( default_settings={'project': 'test', 'git_tag': True}, load_settings=False)) monkeypatch.setattr(click, 'launch', lambda x: 1) monkeypatch.setattr(whaaaaat, 'prompt', lambda x: { 'project_name': 'test_model', 'files': ['weights.h5'], 'team_name': 'Manual Entry'}) monkeypatch.setattr(webbrowser, 'open_new_tab', lambda x: True) return CliRunner()
def test_default_settings(): assert wandb_api.Api({ 'base_url': 'http://localhost' }, load_settings=False).settings() == { 'base_url': 'http://localhost', 'entity': 'models', 'section': 'default', 'run': 'latest', 'git_remote': 'origin', 'git_tag': False, 'project': None, }
def test_restore(runner, request_mocker, query_run, git_repo, monkeypatch): # git_repo creates it's own isolated filesystem mock = query_run(request_mocker) with open("patch.txt", "w") as f: f.write("test") git_repo.repo.index.add(["patch.txt"]) git_repo.repo.commit() monkeypatch.setattr(cli, 'api', wandb_api.Api({'project': 'test'})) result = runner.invoke(cli.restore, ["test/abcdef"]) print(result.output) print(traceback.print_tb(result.exc_info[2])) assert result.exit_code == 0 assert "Created branch wandb/abcdef" in result.output assert "Applied patch" in result.output assert "Restored config variables" in result.output
def test_push_success(request_mocker, upload_url, query_project, upsert_run): query_project(request_mocker) upload_url(request_mocker) update_mock = upsert_run(request_mocker) with CliRunner().isolated_filesystem(): res = os.mkdir("wandb") # TODO: need this for my mock to work api = wandb_api.Api(load_settings=False) with open("wandb/latest.yaml", "w") as f: f.write( yaml.dump({ 'wandb_version': 1, 'test': { 'value': 'success', 'desc': 'My life' } })) with open("weights.h5", "w") as f: f.write("weight") with open("model.json", "w") as f: f.write("model") res = api.push("test/test", ["weights.h5", "model.json"]) assert res[0].status_code == 200
def test_dynamic_settings(): assert wandb_api.Api({}).dynamic_settings == { 'heartbeat_seconds': 30, 'system_sample_seconds': 2, 'system_samples': 15 }
Tests for the `wandb.Api` module. """ import datetime import pytest import os import yaml from .api_mocks import * from click.testing import CliRunner import git from .utils import git_repo import wandb from wandb import api as wandb_api from six import StringIO api = wandb_api.Api(load_settings=False, retry_timedelta=datetime.timedelta(0, 0, 50)) def test_projects_success(request_mocker, query_projects): query_projects(request_mocker) res = api.list_projects() assert len(res) == 3 def test_projects_failure(request_mocker, query_projects): query_projects(request_mocker, status_code=400, error=[{ 'message': "Bummer" }]) with pytest.raises(wandb.Error):
def init(job_type='train', config=None): global run global __stage_dir__ # If a thread calls wandb.init() it will get the same Run object as # the parent. If a child process with distinct memory space calls # wandb.init(), it won't get an error, but it will get a result of # None. # This check ensures that a child process can safely call wandb.init() # after a parent has (only the parent will create the Run object). # This doesn't protect against the case where the parent doesn't call # wandb.init but two children do. if run or os.getenv('WANDB_INITED'): return run if __stage_dir__ is None: __stage_dir__ = "wandb" util.mkdir_exists_ok(wandb_dir()) try: signal.signal(signal.SIGQUIT, _debugger) except AttributeError: pass run = wandb_run.Run.from_environment_or_defaults() run.job_type = job_type run.set_environment() api = wandb_api.Api() api.set_current_run_id(run.id) if run.mode == 'clirun' or run.mode == 'run': api.ensure_configured() if run.mode == 'run': _init_headless(api, run, job_type) def config_persist_callback(): api.upsert_run(id=run.storage_id, name=run.id, project=api.settings('project'), entity=api.settings('entity'), config=run.config.as_dict()) # set the run directory in the config so it actually gets persisted run.config.set_run_dir(run.dir) run.config.set_persist_callback(config_persist_callback) if bool(os.environ.get('WANDB_SHOW_RUN')): webbrowser.open_new_tab(run.get_url(api)) elif run.mode == 'dryrun': termlog( 'wandb dry run mode. Run `wandb board` from this directory to see results' ) termlog() run.config.set_run_dir(run.dir) _init_headless(api, run, job_type, False) else: termlog( 'Invalid run mode "%s". Please unset WANDB_MODE to do a dry run or' % run.mode) termlog('run with "wandb run" to do a real run.') sys.exit(1) if config: run.config.update(config) atexit.register(run.close_files) os.environ['WANDB_INITED'] = '1' return run