def test_validate(self): for t in self.pas_explicit_range: print("PASS validate: {}".format(t)) gd = SolrDaterange.validate(t) for t in self.fail_explicit_range: print("FAIL validate: {}".format(t)) assert_raises(Invalid, SolrDaterange.validate, t)
def test_check_month_day_validity(self): for d in self.fail_date_month: print("FAIL month_day: {}".format(d)) assert_raises(Invalid, SolrDaterange._check_month_day_validity, d) for d in self.pass_date_month: print("PASS month_day: {}".format(d)) res = SolrDaterange._check_month_day_validity(d)
def test_check_date_element(self): for typ in self.fail: print("FAIL date_element typ: {}".format(typ)) for e in self.fail[typ]: print("FAIL date_element: {}".format(e)) assert_raises(Invalid, SolrDaterange._check_date_element, typ, e) for y in self.pas: print("PASS date_element typ: {}".format(typ)) for e in self.pas[typ]: print("PASS date_element: {}".format(e)) assert (SolrDaterange._check_date_element(typ, e) == e)
def test_check_implicit_range(self): for t in self.pas_implicit_range: print("PASS implicit_range: {}".format(t)) gd = SolrDaterange._check_implicit_range(t) check = gd['year'] try: check += '-' + gd['month'] check += '-' + gd['day'] check += 'T' + gd['hour'] check += ':' + gd['minute'] check += ':' + gd['second'] except TypeError: pass assert (check == t) for t in self.fail_implicit_range: print("FAIL implicit: {}".format(t)) assert_raises(Invalid, SolrDaterange._check_implicit_range, t)
def test_check_implicit_range(self): for t in self.pas_implicit_range: print("PASS implicit_range: {}".format(t)) gd = SolrDaterange._check_implicit_range(t) check = gd['year'] try: check += '-' + gd['month'] check += '-' + gd['day'] check += 'T' + gd['hour'] check += ':' + gd['minute'] check += ':' + gd['second'] except TypeError: pass assert(check == t) for t in self.fail_implicit_range: print("FAIL implicit: {}".format(t)) assert_raises(Invalid, SolrDaterange._check_implicit_range, t)
def vali_daterange(values): ''' Initial conversion, 2 possibilities: 1) <values> is a json representation of a list of DateRange-strings (output of validator repeating_text), 2) <values> is one DateRange-string (output from ordinary text field) Both are initially converted to a list of DateRange strings Then add some slack to proper format of timerange before submitting to real validator: + insert trailing "Z" for points in time if necessary + add brackets if necessary ''' def _fix_timestamp(ts): try: fixed = (ts + "Z" if len(ts.split(":")) == 3 and ts[-1] != "Z" else ts) except: pass return fixed def _to_list_of_strings(valuestring): try: values = json.loads(valuestring) except ValueError: values = [valuestring] return(values) values = _to_list_of_strings(values) valid_values = [] for value in values: value = value.strip() try: timestamps = [x.strip('[]') for x in value.split(" TO ")] except ValueError: pass else: timestamps = [_fix_timestamp(ts) for ts in timestamps] value = " TO ".join(timestamps) value = "[" + value + "]" if len(timestamps) == 2 else value valid_values.append(SolrDaterange.validate(value)) valid_values = json.dumps(valid_values) return valid_values