def test_query_cond_not_contains(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.not_contains, 1) # Make sure a valid operation works q2 = Field("test").not_contains("val") self.assertEqual(str(q2), "testNOT LIKEval")
def test_query_cond_not_equals(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.ne, dt) # Make sure a valid operation works (str) q2 = Field("test").ne("test") self.assertEqual(str(q2), "test!=test")
def test_query_cond_ends_with(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.ends_with, 1) # Make sure a valid operation works q2 = Field("test").ends_with("val") self.assertEqual(str(q2), "testENDSWITHval")
def get_order_field(sort_str): sort_direction = sort_str.split(':')[1] if sort_direction: column = sort_str.split(':')[0] if sort_direction == 'asc': return Field(column).order(Order.asc) elif sort_direction == 'desc': return Field(column).order(Order.desc) else: return Field(sort_str).order(Order.asc)
def test_complex_query_bitwise(self): start = dt(2016, 2, 1) end = dt(2016, 2, 10) q = (Field("f1").eq("val1") & Field("f2").between(start, end) ^ Field("f3").eq("val3")) self.assertEqual( str(q), 'f1=val1^f2BETWEENjavascript:gs.dateGenerate("2016-02-01 00:00:00")' '@javascript:gs.dateGenerate("2016-02-10 00:00:00")^NQf3=val3', )
def get_sn_string_filter(value: str, field: Field): # Service Now supports limited wildcards if value.count('*') > 1: # If more than 1 wildcard, we can't process it raise BadRequestException( f'{field.name} contains multiple wildcards. May only contain one at the start or end.' ) elif value.endswith('*'): return field.ends_with(value[:-1]) elif value.startswith('*'): return field.starts_with(value[1:]) else: return field.eq(value)
def test_query_cond_not_in(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.not_in, [dt]) self.assertRaises(QueryTypeError, q1.not_in, "single") # Make sure a valid operation works (string list) q3 = Field("test").not_in(["foo", "bar"]) self.assertEqual(str(q3), "testNOT INfoo,bar") # Make sure a valid operation works (int list) q3 = Field("test").not_in([1, 2]) self.assertEqual(str(q3), "testNOT IN1,2")
def get_sn_filters(self): filters = list() for field in get_own_fields(self.__class__): value = getattr(self, field.name) if value is None: continue if field.type == str: filters.append(get_sn_string_filter(value, Field(field.name))) elif field.type == datetime or field.type in (int, float): filters.append(get_numeric_filter(value, Field(field.name))) else: raise Exception( f'Unexpected type: {field.type}, field: {field.name}') return filters
def test_query_cond_less_than(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.lt, "a") # Make sure a valid operation works q2 = Field("test").lt(1) self.assertEqual(str(q2), "test<1") # Make sure naive dates are assumed as UTC q3 = Field("test").lt(dt(2016, 2, 1)) self.assertEqual( str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 00:00:00")') # Make sure tz-aware dates are converted to UTC (UTC+1) q3 = (Field("test").lt(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))) self.assertEqual( str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 02:00:00")')
def test_query_cond_between(self): # Make sure between with str arguments fails q1 = Field("test") self.assertRaises(QueryTypeError, q1.between, "start", "end") # Make sure between with int arguments works q2 = Field("test").between(1, 2) self.assertEqual(str(q2), "testBETWEEN1@2") # Make sure between with dates works start = dt(1970, 1, 1) end = dt(1970, 1, 2) q2 = Field("test").between(start, end) self.assertEqual( str(q2), 'testBETWEENjavascript:gs.dateGenerate("1970-01-01 00:00:00")' '@javascript:gs.dateGenerate("1970-01-02 00:00:00")', )
def test_query_cond_not_on(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.not_on, "a") q1 = Field("test") self.assertRaises(QueryTypeError, q1.not_on, 1) # Make sure naive dates are assumed as UTC q2 = Field("test").not_on(dt(2016, 2, 1)) self.assertEqual( str(q2), 'testNOTONcustom@javascript:gs.dateGenerate("2016-02-01", "start")@javascript:gs.dateGenerate("2016-02-01", "end")' ) q3 = Field("test").not_on(DateTimeOn.today) self.assertEqual( str(q3), 'testNOTONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()' )
def test_query_logical_bitwise_nq(self): # Make sure AND() operator between expressions works q = (Field("test").eq("test") ^ Field("test2").eq("test")) self.assertEqual(str(q), "test=test^NQtest2=test")
def test_query_logical_bitwise_or(self): # Make sure AND() operator between expressions works q = ((Field("test") == "test") | (Field("test2") == "test")) self.assertEqual(str(q), "test=test^ORtest2=test")
def test_query_order_ascending(self): # :meth:`order_descending` should generate ORDERBY<field> q = (Field("foo").eq("bar") & Field("foo2").order(Order.asc)) self.assertEqual(str(q), "foo=bar^ORDERBYfoo2")
def test_query_cond_is_not_empty(self): # Make sure a valid operation works q = Field("test").is_not_empty() self.assertEqual(str(q), "testISNOTEMPTY")