def test_study_integration(): # Test request real API study = Study(TEST_STUDY_KEY) trial_from_real = study.new_trial() # Create mock with patch('hyperkite.hyperkite.requests.get') as mock_get: mock_get.return_value.ok = True mock_json = {'trial_key': '1ab23456cdef7gh8i9jk012l', 'values': {'test_param': 1.234}} mock_get.return_value = Mock() mock_get.return_value.json.return_value = mock_json # Test request mocked API study = Study(TEST_STUDY_KEY) trial_from_mocked = study.new_trial() # Assert assert_is_not_none(trial_from_real) assert_is_not_none(trial_from_mocked) assert_equal(type(trial_from_real._study_key), type(trial_from_mocked._study_key)) assert_equal(type(trial_from_real._trial_key), type(trial_from_mocked._trial_key)) assert_equal(type(trial_from_real.values), type(trial_from_mocked.values))
def test_new_trial_missing_values(self): self.mock_put.return_value.ok = True # Create mock mock_json = {'trial_key': '1ab23456cdef7gh8i9jk012l'} self.mock_put.return_value = Mock() self.mock_put.return_value.json.return_value = mock_json # Test trial request study_key = '9zy87654xggf3gh2i1jk098l' study = Study(study_key) trial = study.new_trial() # Assert return None assert_true(self.mock_put.called) assert_is_none(trial)
def test_new_trial_missing_trial_key(self): self.mock_put.return_value.ok = True # Create mock mock_json = {'values': {'test_param': 1.234}} self.mock_put.return_value = Mock() self.mock_put.return_value.json.return_value = mock_json # Test trial request study_key = '9zy87654xggf3gh2i1jk098l' study = Study(study_key) trial = study.new_trial() # Assert return None assert_true(self.mock_put.called) assert_is_none(trial)
def test_new_trial_ok(self): self.mock_put.return_value.ok = False # Create mock mock_json = {'trial_key': '1ab23456cdef7gh8i9jk012l', 'values': {'test_param': 1.234}} self.mock_put.return_value = Mock() self.mock_put.return_value.json.return_value = mock_json # Test trial request study_key = '9zy87654xggf3gh2i1jk098l' study = Study(study_key) trial = study.new_trial() # Assert called correctly self.mock_put.assert_called_with(urljoin(API_URL, 'studies/{}/trials/new_trial'.format(study_key))) # Assert correct trial is returned assert_is_not_none(trial) assert_equal(trial.study_key, study_key) assert_equal(trial.trial_key, mock_json['trial_key']) assert_equal(trial.values, mock_json['values'])
def new_trial(study_key: str): study = Study(study_key) trial = study.new_trial() return trial