def test_time_filters_validation(self, mock_cache): """Test validation for time filters.""" # Test that form validation succeeds when relative time fields make sense. parameters_list = [ {'firstmjd__gt': 57000, 'firstmjd__lt': 58000, 'lastmjd__gt': 58000, 'lastmjd__lt': 59000}, ] for parameters in parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertTrue(form.is_valid())
def test_classifier_filters_validation(self, mock_cache_get): mock_cache_get.return_value = alerce_classifiers_response parameters_list = [ {'lc_classifier': 'SNIa', 'p_lc_classifier': 0.5}, {'stamp_classifier': 'SN', 'p_stamp_classifier': 0.5} ] for parameters in parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) form.is_valid() self.assertTrue(form.is_valid()) invalid_parameters_list = [ {'lc_classifier': 'SNIa', 'p_stamp_classifier': 0.5}, {'stamp_classifier': 'SN', 'p_lc_classifier': 0.5} ] for parameters in invalid_parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertFalse(form.is_valid()) self.assertIn('Only one of either light curve or stamp classification may be used as a filter.', form.errors['__all__'])
def test_cone_search_validation(self): """Test cross-field validation for cone search filters.""" # Test that validation fails if not all fields are present parameters_list = [{ 'ra': 10, 'dec': 10 }, { 'dec': 10, 'sr': 10 }, { 'ra': 10, 'sr': 10 }] for parameters in parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertFalse(form.is_valid()) self.assertIn( 'All of RA, Dec, and Search Radius must be included to execute a cone search.', form.non_field_errors()) # Test that validation passes when all three fields are present self.base_form_data.update({'ra': 10, 'dec': 10, 'sr': 10}) form = ALeRCEQueryForm(self.base_form_data) self.assertTrue(form.is_valid())
def test_get_classifiers(self, mock_cache_get): mock_cache_get.return_value = None # Ensure cache is not used classifiers = ALeRCEQueryForm._get_classifiers() self.assertIn('early', classifiers.keys()) self.assertIn('late', classifiers.keys()) for classifier in classifiers['early'] + classifiers['late']: self.assertIn('name', classifier.keys()) self.assertIn('id', classifier.keys())
def test_get_stamp_classifier_choices(self, mock_cache_get): mock_cache_get.return_value = alerce_classifiers_response stamp_classifiers = ALeRCEQueryForm._get_stamp_classifier_choices() expected_classifiers = { (None, ''), ('SN', 'SN') } for classifier in expected_classifiers: self.assertIn(classifier, stamp_classifiers)
def test_get_classifiers(self, mock_requests_get, mock_cache_get): mock_response = Response() mock_response._content = str.encode(json.dumps(alerce_classifiers_response)) mock_response.status_code = 200 mock_requests_get.return_value = mock_response with self.subTest('Test that cached response avoids request.'): mock_cache_get.return_value = alerce_classifiers_response classifiers = ALeRCEQueryForm._get_classifiers() mock_requests_get.assert_not_called() for classifier_group in classifiers: self.assertTrue( all(k in classifier_group.keys() for k in ['classifier_name', 'classifier_version', 'classes']) ) with self.subTest('Test that no cached response results in HTTP request.'): mock_cache_get.return_value = None classifiers = ALeRCEQueryForm._get_classifiers() mock_requests_get.assert_called_once()
def test_get_classifiers(self, mock_cache_get): mock_cache_get.return_value = None # Ensure cache is not used expected_classifiers = ['lc_classifier', 'stamp_classifier'] classifiers = ALeRCEQueryForm._get_classifiers() for expected in expected_classifiers: for classifier in classifiers: if expected == classifier['classifier_name']: break else: self.fail(f'Did not find {expected} in classifiers.')
def test_get_light_curve_classifier_choices(self, mock_cache_get): mock_cache_get.return_value = alerce_classifiers_response lc_classifiers = ALeRCEQueryForm._get_light_curve_classifier_choices() expected_classifiers = [ (None, ''), ('SNIa', 'SNIa - transient'), ('QSO', 'QSO - stochastic'), ('LPV', 'LPV - periodic') ] for classifier in expected_classifiers: self.assertIn(classifier, lc_classifiers)
def test_fetch_alerts(self): form = ALeRCEQueryForm({ 'query_name': 'Test', 'broker': 'ALeRCE', 'nobs__gt': 1, 'classearly': 19, 'pclassearly': 0.7, 'mjd__gt': 59148.78219219812 }) form.is_valid() query = form.save() alerts = [ alert for alert in self.broker.fetch_alerts(query.parameters) ] self.assertGreaterEqual(len(alerts), 6) for k in [ 'oid', 'lastmjd', 'mean_magpsf_r', 'mean_magpsf_g', 'pclassrf', 'pclassearly', 'meanra', 'meandec' ]: self.assertIn(k, alerts[0])
def test_time_filters_validation(self): """Test validation for time filters.""" # Test that validation fails when either absolute time filter is paired with relative time filter parameters_list = [{ 'mjd__lt': 58000, 'relative_mjd__gt': 168 }, { 'mjd__gt': 58000, 'relative_mjd__gt': 168 }] for parameters in parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertFalse(form.is_valid()) self.assertIn( 'Cannot filter by both relative and absolute time.', form.non_field_errors()) # Test that mjd__lt and mjd__gt fail when mjd__lt is less than mjd__gt with self.subTest(): parameters = {'mjd__lt': 57000, 'mjd__gt': 57001} parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertFalse(form.is_valid()) self.assertIn( 'Min date of first detection must be earlier than max date of first detection.', form.non_field_errors()) # Test that form validation succeeds when relative time fields make sense and absolute time field is used alone. parameters_list = [{ 'mjd__gt': 57000, 'mjd__lt': 58000 }, { 'relative_mjd__gt': 168 }] for parameters in parameters_list: with self.subTest(): parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertTrue(form.is_valid()) # Test that form validation succeeds when absolute time field is used on its own. with self.subTest(): parameters = {'relative_mjd__gt': 168} parameters.update(self.base_form_data) form = ALeRCEQueryForm(parameters) self.assertTrue(form.is_valid()) # Test that clean_relative_mjd__gt works as expected expected_mjd = Time( datetime.now()).mjd - parameters['relative_mjd__gt'] / 24 self.assertAlmostEqual(form.cleaned_data['relative_mjd__gt'], expected_mjd)