Example #1
0
    def test_create_job_code_reference(self):
        coderef = CodeReferenceConfig(
            commit='3783ab36703b14b91b15736fe4302bfb8d52af1c',
            head='3783ab36703b14b91b15736fe4302bfb8d52af1c',
            branch='feature1',
            git_url='https://bitbucket.org:foo/bar.git',
            is_dirty=True).to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'jobs', 1, 'coderef'),
                               body=json.dumps(coderef),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.create_code_reference('username',
                                                          'project_name',
                                                          1,
                                                          coderef=coderef)
        assert response.to_dict() == coderef

        # Raw response
        self.set_raw_response()
        response = self.api_handler.create_code_reference('username',
                                                          'project_name',
                                                          1,
                                                          coderef=coderef)
        assert response == coderef

        # Async
        self.assert_async_call(api_handler_call=lambda: self.api_handler.
                               create_code_reference('username',
                                                     'project_name',
                                                     1,
                                                     coderef=coderef,
                                                     background=True),
                               method='post')
Example #2
0
    def test_create_experiment_status(self):
        exp = ExperimentStatusConfig(id=1,
                                     uuid=uuid.uuid4().hex,
                                     experiment=1,
                                     created_at=datetime.datetime.now(),
                                     status='Running').to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'experiments', 1,
                                   'statuses'),
                               body=json.dumps(exp),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.create_status('username',
                                                  'project_name',
                                                  1,
                                                  status='running')
        assert response.to_dict() == exp

        # Raw response
        self.set_raw_response()
        response = self.api_handler.create_status('username',
                                                  'project_name',
                                                  1,
                                                  status='running')
        assert response == exp

        # Async
        self.assert_async_call(api_handler_call=lambda: self.api_handler.
                               create_status('username',
                                             'project_name',
                                             1,
                                             status='running',
                                             background=True),
                               method='post')
    def test_copy_experiment(self):
        exp = ExperimentConfig(config={}).to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'experiments', 1, 'copy'),
                               body=json.dumps(exp),
                               content_type='application/json',
                               status=200)

        # Schema response
        result = self.api_handler.copy('username', 'project_name', 1)
        assert result.to_dict() == exp

        # Raw response
        self.set_raw_response()
        result = self.api_handler.copy('username', 'project_name', 1)
        assert result == exp

        # Async
        self.assert_async_call(api_handler_call=lambda: self.api_handler.copy(
            'username', 'project_name', 1, background=True),
                               method='post')
Example #4
0
    def test_get_bookmarked_projects(self):
        projects = [{'content_object': ProjectConfig('proj').to_dict()} for _ in range(10)]
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(
                self.api_config.base_url,
                '/bookmarks',
                'user',
                'projects'),
            body=json.dumps({'results': projects, 'count': 10, 'next': None}),
            content_type='application/json',
            status=200)

        # Schema response
        result = self.api_handler.projects('user')
        assert len(result['results']) == 10
        assert isinstance(result['results'][0], ProjectConfig)

        # Raw response
        self.set_raw_response()
        result = self.api_handler.projects('user')
        assert len(result['results']) == 10
        assert isinstance(result['results'][0], Mapping)
Example #5
0
    def test_upload_repo(self):
        httpretty.register_uri(
            httpretty.PUT,
            BaseApiHandler.build_url(
                self.api_config.base_url,
                '/',
                'user',
                'project',
                'repo',
                'upload'),
            content_type='application/json',
            status=200)
        files = [('code', ('repo',
                           open('./tests/fixtures_static/repo.tar.gz', 'rb'),
                           'text/plain'))]
        result = self.api_handler.upload_repo('user', 'project', files=files, files_size=10)
        assert result.status_code == 200

        # Async
        self.assert_async_call(
            api_handler_call=lambda: self.api_handler.upload_repo(
                'user', 'project', files=files, files_size=10, background=True),
            method='upload')
Example #6
0
    def test_login_job_impersonate_token(self):
        token = uuid.uuid4().hex
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'jobs', '1', 'impersonatetoken'),
                               body=json.dumps({'token': token}),
                               content_type='application/json',
                               status=200)

        # Login without updating the token and without persistence
        if os.path.exists(settings.CONTEXT_AUTH_TOKEN_PATH):
            os.remove(settings.CONTEXT_AUTH_TOKEN_PATH)
        assert self.api_config.token == 'token'
        assert token == self.api_handler.login_job_impersonate_token(
            username='******',
            project_name='project',
            job_id=1,
            internal_token='foo',
            set_token=False,
            persist_token=False)
        assert self.api_config.token == 'token'
        assert os.path.exists(settings.CONTEXT_AUTH_TOKEN_PATH) is False

        # Login and update the token and persistence
        if os.path.exists(settings.CONTEXT_AUTH_TOKEN_PATH):
            os.remove(settings.CONTEXT_AUTH_TOKEN_PATH)
        assert self.api_config.token == 'token'
        assert token == self.api_handler.login_job_impersonate_token(
            username='******',
            project_name='project',
            job_id=1,
            internal_token='foo',
            set_token=True,
            persist_token=True)
        assert self.api_config.token == token
        assert os.path.exists(settings.CONTEXT_AUTH_TOKEN_PATH) is True
Example #7
0
    def test_list_experiments(self):
        experiments = [ExperimentConfig().to_dict() for _ in range(10)]
        httpretty.register_uri(httpretty.GET,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/',
                                   'experiments'),
                               body=json.dumps({
                                   'results': experiments,
                                   'count': 10,
                                   'next': None
                               }),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.list_experiments()
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments()
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)
Example #8
0
    def test_resume_job(self):
        job = JobConfig().to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'jobs', 1, 'resume'),
                               body=json.dumps(job),
                               content_type='application/json',
                               status=200)

        # Schema response
        result = self.api_handler.resume('username', 'project_name', 1)
        assert result.to_dict() == job

        # Raw response
        self.set_raw_response()
        result = self.api_handler.resume('username', 'project_name', 1)
        assert result == job

        # Async
        self.assert_async_call(
            api_handler_call=lambda: self.api_handler.restart(
                'username', 'project_name', 1, background=True),
            method='post')
Example #9
0
    def test_copy_job_with_config(self):
        job = JobConfig(config={}).to_dict()
        config = {'config': {'declarations': {'lr': 0.1}}}
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'jobs', 1, 'copy'),
                               body=json.dumps(job),
                               content_type='application/json',
                               status=200)

        # Schema response
        result = self.api_handler.copy('username', 'project_name', 1, config)
        assert result.to_dict() == job

        # Raw response
        self.set_raw_response()
        result = self.api_handler.copy('username', 'project_name', 1, config)
        assert result == job

        # Async
        self.assert_async_call(api_handler_call=lambda: self.api_handler.copy(
            'username', 'project_name', 1, config, background=True),
                               method='post')
Example #10
0
    def test_login_experiment_ephemeral_token(self):
        token = uuid.uuid4().hex
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'experiments', '1',
                                   'ephemeraltoken'),
                               body=json.dumps({'token': token}),
                               content_type='application/json',
                               status=200)

        # Login without updating the token and without persistence
        if os.path.exists(settings.TMP_AUTH_TOKEN_PATH):
            os.remove(settings.TMP_AUTH_TOKEN_PATH)
        assert self.api_config.token == 'token'
        assert token == self.api_handler.login_experiment_ephemeral_token(
            username='******',
            project_name='project',
            experiment_id=1,
            ephemeral_token='foo',
            set_token=False,
            persist_token=False)
        assert self.api_config.token == 'token'
        assert os.path.exists(settings.TMP_AUTH_TOKEN_PATH) is False

        # Login and update the token and persistence
        if os.path.exists(settings.TMP_AUTH_TOKEN_PATH):
            os.remove(settings.TMP_AUTH_TOKEN_PATH)
        assert self.api_config.token == 'token'
        assert token == self.api_handler.login_experiment_ephemeral_token(
            username='******',
            project_name='project',
            experiment_id=1,
            ephemeral_token='foo',
            set_token=True,
            persist_token=True)
        assert self.api_config.token == token
        assert os.path.exists(settings.TMP_AUTH_TOKEN_PATH) is True

        # Login remove ephemeral token from env var and settings
        os.environ[settings.SECRET_EPHEMERAL_TOKEN_KEY] = 'value'
        settings.SECRET_EPHEMERAL_TOKEN = 'eph_token'  # noqa
        if os.path.exists(settings.TMP_AUTH_TOKEN_PATH):
            os.remove(settings.TMP_AUTH_TOKEN_PATH)
        assert self.api_config.token == token
        assert os.environ.get(settings.SECRET_EPHEMERAL_TOKEN_KEY) == 'value'
        assert settings.SECRET_EPHEMERAL_TOKEN == 'eph_token'
        assert token == self.api_handler.login_experiment_ephemeral_token(
            username='******',
            project_name='project',
            experiment_id=1,
            ephemeral_token='foo',
            set_token=True,
            persist_token=True)
        assert self.api_config.token == token
        assert os.path.exists(settings.TMP_AUTH_TOKEN_PATH) is True
        assert os.environ.get(settings.SECRET_EPHEMERAL_TOKEN_KEY) is None
        assert not hasattr(settings, 'SECRET_EPHEMERAL_TOKEN')

        assert token == self.api_handler.login_experiment_ephemeral_token(
            username='******',
            project_name='project',
            experiment_id=1,
            ephemeral_token='foo',
            set_token=True,
            persist_token=True)
    def test_list_experiments(self):
        group_uuid = uuid.uuid4().hex
        project_uuid = uuid.uuid4().hex
        xp_uuid = uuid.uuid4().hex
        xps = [
            ExperimentConfig(uuid=xp_uuid,
                             project=project_uuid,
                             experiment_group=group_uuid).to_dict()
            for _ in range(10)
        ]
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                     'project_name', 'experiments') +
            '?group=1',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        response = self.api_handler.list_experiments('username',
                                                     'project_name', 1)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)
        assert response['count'] == 10
        assert response['next'] is None
        assert response['previous'] is None

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments('username',
                                                     'project_name', 1)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)
        assert response['count'] == 10
        assert response['next'] is None
        assert response['previous'] is None

        # Pagination
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                     'project_name', 'experiments') +
            '?group=1&offset=2',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_experiments('username',
                                                     'project_name',
                                                     1,
                                                     page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments('username',
                                                     'project_name',
                                                     1,
                                                     page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Query, Sort
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                     'project_name', 'experiments') +
            '?group=1&query=started_at:>=2010-10-10,sort=created_at',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_experiments(
            'username',
            'project_name',
            1,
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments(
            'username',
            'project_name',
            1,
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)
class TestBaseApiHandler(TestBaseApi):
    # pylint:disable=protected-access
    def setUp(self):
        super(TestBaseApiHandler, self).setUp()
        self.api_handler = BaseApiHandler(transport=self.transport,
                                          config=self.api_config)

    def test_get_page(self):
        assert self.api_handler.get_page() == {}
        assert self.api_handler.get_page(page=1) == {}
        assert self.api_handler.get_page(page=2) == {
            'offset': self.api_config.PAGE_SIZE
        }
        assert self.api_handler.get_page(page=3) == {
            'offset': self.api_config.PAGE_SIZE * 2
        }

    def test_build_url(self):
        assert self.api_handler.build_url('a') == 'a/'
        assert self.api_handler.build_url('a', 'b') == 'a/b/'

    def test_get_url(self):
        with self.assertRaises(ERRORS_MAPPING['base']):
            self.api_handler._get_url('a')

        assert self.api_handler._get_url('base',
                                         'endpoint') == 'base/endpoint/'
        assert self.api_handler._get_http_url(
            'endpoint') == '{}/endpoint/'.format(self.api_config.base_url)
        assert self.api_handler._get_ws_url(
            'endpoint') == '{}/endpoint/'.format(self.api_config.base_ws_url)

    def test_prepare_results(self):
        # Schema response
        results = self.api_handler.prepare_results(
            response_json={'foo': 'bar'}, config=DummyConfig)
        assert results == ('from_dict', {'foo': 'bar'})

        # Raw response
        self.api_config.schema_response = False
        results = self.api_handler.prepare_results(
            response_json={'foo': 'bar'}, config=DummyConfig)
        assert results == {'foo': 'bar'}

    def test_prepare_list_results(self):
        # Schema response
        results = self.api_handler.prepare_list_results(
            response_json={'results': ['bar']},
            current_page=1,
            config=DummyConfig)
        assert results == {
            'count': 0,
            'next': None,
            'previous': None,
            'results': [('from_dict', 'bar')]
        }

        # Raw response
        self.api_config.schema_response = False
        results = self.api_handler.prepare_list_results(
            response_json={'results': ['bar']},
            current_page=1,
            config=DummyConfig)
        assert results == {
            'count': 0,
            'next': None,
            'previous': None,
            'results': ['bar']
        }
 def test_get_heartbeat_url(self):
     self.assertEqual(
         self.api_handler.get_heartbeat_url('username', 'project_name', 1),
         BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                  'project_name', 'experiments', 1,
                                  BaseApiHandler.HEARTBEAT))
Example #14
0
    def test_list_experiments(self):
        project_uuid = uuid.uuid4().hex
        xp_uuid = uuid.uuid4().hex
        xps = [
            ExperimentConfig(config={}, uuid=xp_uuid,
                             project=project_uuid).to_dict() for _ in range(10)
        ]
        httpretty.register_uri(httpretty.GET,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'experiments'),
                               body=json.dumps({
                                   'results': xps,
                                   'count': 10,
                                   'next': None
                               }),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.list_experiments('user', 'project')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments('user', 'project')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Pagination
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'user',
                                     'project', 'experiments') + '?offset=2',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_experiments('user', 'project', page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments('user', 'project', page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Metrics & Declarations
        for xp in xps:
            xp['last_metric'] = {'loss': 0.1}
            xp['declarations'] = {'foo': 'bar'}

        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'user',
                                     'project', 'experiments') +
            '?metrics=true',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        response = self.api_handler.list_experiments('user',
                                                     'project',
                                                     metrics=True,
                                                     page=2)
        assert len(response['results']) == 10

        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'user',
                                     'project', 'experiments') +
            '?declarations=true',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        response = self.api_handler.list_experiments('user',
                                                     'project',
                                                     declarations=True,
                                                     page=2)
        assert len(response['results']) == 10

        # Query, Sort
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                     'project_name', 'experiments') +
            '?independent=true&query=started_at:>=2010-10-10,sort=created_at',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_experiments(
            'user',
            'project',
            True,
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_experiments(
            'user',
            'project',
            True,
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)
    def test_list_experiment_jobs(self):
        job_uuid = uuid.uuid4().hex
        xps = [
            ExperimentJobConfig(uuid=job_uuid,
                                experiment=1,
                                created_at=datetime.datetime.now(),
                                updated_at=datetime.datetime.now(),
                                definition={}).to_dict() for _ in range(10)
        ]
        httpretty.register_uri(httpretty.GET,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url,
                                   '/',
                                   'username',
                                   'project_name',
                                   'experiments',
                                   1,
                                   'jobs',
                               ),
                               body=json.dumps({
                                   'results': xps,
                                   'count': 10,
                                   'next': None
                               }),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.list_jobs('username', 'project_name', 1)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentJobConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_jobs('username', 'project_name', 1)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Pagination
        httpretty.register_uri(httpretty.GET,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'experiments', 1, 'jobs') +
                               '?offset=2',
                               body=json.dumps({
                                   'results': xps,
                                   'count': 10,
                                   'next': None
                               }),
                               content_type='application/json',
                               status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_jobs('username',
                                              'project_name',
                                              1,
                                              page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], ExperimentJobConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_jobs('username',
                                              'project_name',
                                              1,
                                              page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)
    def test_create_experiment_metric(self):
        exp = ExperimentMetricConfig(id=1,
                                     uuid=uuid.uuid4().hex,
                                     experiment=1,
                                     created_at=datetime.datetime.now(),
                                     values={
                                         'accuracy': 0.9,
                                         'loss': np.float64(0.34),
                                         'step': 1
                                     }).to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'username',
                                   'project_name', 'experiments', 1,
                                   'metrics'),
                               body=json.dumps(exp),
                               content_type='application/json',
                               status=200)

        # Raises
        with self.assertRaises(PolyaxonClientException):
            self.api_handler.create_metric('username',
                                           'project_name',
                                           1,
                                           values={
                                               'wrong_metric': 'foo',
                                               'loss': np.float64(0.34),
                                               'step': 1
                                           })

        # Schema response
        response = self.api_handler.create_metric('username',
                                                  'project_name',
                                                  1,
                                                  values={
                                                      'accuracy': 0.9,
                                                      'loss': np.float64(0.34),
                                                      'step': 1
                                                  })
        assert response.to_dict() == exp

        # Raw response
        self.set_raw_response()
        response = self.api_handler.create_metric('username',
                                                  'project_name',
                                                  1,
                                                  values={
                                                      'accuracy': 0.9,
                                                      'loss': np.float64(0.34),
                                                      'step': 1
                                                  })
        assert response == exp

        # Async
        self.assert_async_call(api_handler_call=lambda: self.api_handler.
                               create_metric('username',
                                             'project_name',
                                             1,
                                             values={
                                                 'accuracy': 0.9,
                                                 'loss': np.float64(0.34),
                                                 'step': 1
                                             },
                                             background=True),
                               method='post')

        # Periodic
        self.assert_async_call(api_handler_call=lambda: self.api_handler.
                               create_metric('username',
                                             'project_name',
                                             1,
                                             values={
                                                 'accuracy': 0.9,
                                                 'loss': np.float64(0.34),
                                                 'step': 1
                                             },
                                             periodic=True),
                               method='post')
Example #17
0
    def test_create_experiment(self):
        project_uuid = uuid.uuid4().hex
        obj = ExperimentConfig(project=project_uuid, config={})
        obj_dict = obj.to_dict()
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'experiments'),
                               body=json.dumps(obj_dict),
                               content_type='application/json',
                               status=200)

        # Schema response
        result = self.api_handler.create_experiment('user', 'project', obj)
        assert result.to_dict() == obj_dict

        # Raw response
        self.set_raw_response()
        result = self.api_handler.create_experiment('user', 'project', obj)
        assert result == obj_dict

        # Async
        self.assert_async_call(
            api_handler_call=lambda: self.api_handler.create_experiment(
                'user', 'project', obj, background=True),
            method='post')

        # Test create experiment with dict
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'experiments'),
                               body=json.dumps(obj_dict),
                               content_type='application/json',
                               status=200)

        # Schema response
        self.set_schema_response()
        result = self.api_handler.create_experiment('user', 'project',
                                                    obj_dict)
        assert result.to_dict() == obj_dict

        # Raw response
        self.set_raw_response()
        result = self.api_handler.create_experiment('user', 'project',
                                                    obj_dict)
        assert result == obj_dict

        # Async
        self.assert_async_call(
            api_handler_call=lambda: self.api_handler.create_experiment(
                'user', 'project', obj_dict, background=True),
            method='post')

        # Test create experiment with group
        group_id = 1
        httpretty.register_uri(httpretty.POST,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'experiments'),
                               body=json.dumps(obj_dict),
                               content_type='application/json',
                               status=200)

        # Schema response
        self.set_schema_response()
        result = self.api_handler.create_experiment('user',
                                                    'project',
                                                    obj_dict,
                                                    group=group_id)
        assert result.to_dict() == obj_dict

        # Raw response
        self.set_raw_response()
        result = self.api_handler.create_experiment('user', 'project',
                                                    obj_dict)
        assert result == obj.to_dict()

        # Async
        self.assert_async_call(
            api_handler_call=lambda: self.api_handler.create_experiment(
                'user', 'project', obj_dict, group=group_id, background=True),
            method='post')
Example #18
0
class TestBaseApiHandler(TestBaseApi):
    # pylint:disable=protected-access
    def setUp(self):
        super(TestBaseApiHandler, self).setUp()
        self.api_handler = BaseApiHandler(transport=self.transport,
                                          config=self.api_config)

    def test_get_page(self):
        assert self.api_handler.get_page() == {}
        assert self.api_handler.get_page(page=1) == {}
        assert self.api_handler.get_page(page=2) == {
            'offset': self.api_config.PAGE_SIZE
        }
        assert self.api_handler.get_page(page=3) == {
            'offset': self.api_config.PAGE_SIZE * 2
        }

    def test_build_url(self):
        assert self.api_handler.build_url('a') == 'a/'
        assert self.api_handler.build_url('a', 'b') == 'a/b/'

    def test_get_url(self):
        with self.assertRaises(ERRORS_MAPPING['base']):
            self.api_handler._get_url('a')

        assert self.api_handler._get_url('base',
                                         'endpoint') == 'base/endpoint/'
        assert self.api_handler._get_http_url(
            'endpoint') == '{}/endpoint/'.format(self.api_config.base_url)
        assert self.api_handler._get_ws_url(
            'endpoint') == '{}/endpoint/'.format(self.api_config.base_ws_url)

    def test_prepare_results(self):
        # Schema response
        results = self.api_handler.prepare_results(
            response_json={'foo': 'bar'}, config=DummyConfig)
        assert results == ('from_dict', {'foo': 'bar'})

        # Raw response
        self.api_config.schema_response = False
        results = self.api_handler.prepare_results(
            response_json={'foo': 'bar'}, config=DummyConfig)
        assert results == {'foo': 'bar'}

    def test_prepare_list_results(self):
        # Schema response
        results = self.api_handler.prepare_list_results(
            response_json={'results': ['bar']},
            current_page=1,
            config=DummyConfig)
        assert results == {
            'count': 0,
            'next': None,
            'previous': None,
            'results': [('from_dict', 'bar')]
        }

        # Raw response
        self.api_config.schema_response = False
        results = self.api_handler.prepare_list_results(
            response_json={'results': ['bar']},
            current_page=1,
            config=DummyConfig)
        assert results == {
            'count': 0,
            'next': None,
            'previous': None,
            'results': ['bar']
        }

    def test_validate_config(self):
        assert isinstance(
            self.api_handler.validate_config(config={},
                                             config_schema=BuildJobConfig),
            BuildJobConfig)

        with self.assertRaises(PolyaxonClientException):
            self.api_handler.validate_config(config=ExperimentConfig(),
                                             config_schema=BuildJobConfig)

    def test_validate_content(self):
        assert self.api_handler.validate_content(None) is None
        assert self.api_handler.validate_content({'foo':
                                                  'bar'}) == "{'foo': 'bar'}"
        assert self.api_handler.validate_content(
            "{'foo': 'bar'}") == "{'foo': 'bar'}"
        assert self.api_handler.validate_content(
            BuildJobConfig()) == '{}'.format(BuildJobConfig().to_dict())
Example #19
0
    def test_list_builds(self):
        project_uuid = uuid.uuid4().hex
        xp_uuid = uuid.uuid4().hex
        xps = [
            JobConfig(config={}, uuid=xp_uuid, project=project_uuid).to_dict()
            for _ in range(10)
        ]
        httpretty.register_uri(httpretty.GET,
                               BaseApiHandler.build_url(
                                   self.api_config.base_url, '/', 'user',
                                   'project', 'builds'),
                               body=json.dumps({
                                   'results': xps,
                                   'count': 10,
                                   'next': None
                               }),
                               content_type='application/json',
                               status=200)

        # Schema response
        response = self.api_handler.list_builds('user', 'project')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], JobConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_builds('user', 'project')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Pagination
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'user',
                                     'project', 'builds') + '?offset=2',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_builds('user', 'project', page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], JobConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_builds('user', 'project', page=2)
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)

        # Query, Sort
        httpretty.register_uri(
            httpretty.GET,
            BaseApiHandler.build_url(self.api_config.base_url, '/', 'username',
                                     'project_name', 'builds') +
            '?query=started_at:>=2010-10-10,sort=created_at',
            body=json.dumps({
                'results': xps,
                'count': 10,
                'next': None
            }),
            content_type='application/json',
            status=200)

        # Schema response
        self.set_schema_response()
        response = self.api_handler.list_builds(
            'user',
            'project',
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], JobConfig)

        # Raw response
        self.set_raw_response()
        response = self.api_handler.list_builds(
            'user',
            'project',
            query='started_at:>=2010-10-10',
            sort='created_at')
        assert len(response['results']) == 10
        assert isinstance(response['results'][0], Mapping)