def test_chained_function(self): field1 = Field('foo').negate() field2 = field1.eq('bar') self.assertEqual('NOT "foo"', str(field1)) self.assertEqual('NOT "foo"=\'bar\'', str(field2)) self.assertIsNot(field1, field2)
def test_negate(self): c1 = Field('foo') >= 1 c2 = c1.negate() self.assertEqual('"foo">=1', str(c1)) self.assertEqual('NOT "foo">=1', str(c2))
def test_trunc_year(self): result = SnowflakeDatabase().trunc_date(Field('date'), annually) self.assertEqual('TRUNC("date",\'Y\')', str(result))
def test__case_with_field_is_not_aggregate(self): v = Case().when(Field("foo") == 1, 1).when(Field("foo") == 2, 2).else_(3) self.assertFalse(v.is_aggregate)
def test__mixed_case_is_not_aggregate(self): v = (Case().when(Field("foo") == 1, fn.Sum(Field("bar"))).when( Field("foo") == 2, Field("fiz"))) self.assertFalse(v.is_aggregate)
def test__func_arithmetic_constant_is_not_aggregate(self): v = 1 / fn.Sum(Field("foo")) self.assertTrue(v.is_aggregate)
def test__agg_func_arithmetic_is_aggregate(self): v = fn.Sum(Field("foo")) / fn.Sum(Field("foo")) self.assertTrue(v.is_aggregate)
def test__field_arithmetic_constant_is_not_aggregate(self): v = Field("foo") + 1 self.assertFalse(v.is_aggregate)
def test_tabled_eq_fields_equally_hashed(self): client_name1 = Field(name="name", table=Table("clients")) client_name2 = Field(name="name", table=Table("clients")) self.assertTrue(hash(client_name1) == hash(client_name2))
def test__criterion_for_with_value(self): table = Table('a') c = (Field('foo') > 1).for_(table) self.assertEqual(c.left, table) self.assertEqual(c.tables_, {table})
def test__criterion_is_null_with_alias(self): c1 = Field("foo").isnull().as_('alias') c2 = Field("foo", table=self.t).isnull().as_('alias') self.assertEqual('"foo" IS NULL "alias"', str(c1)) self.assertEqual('"crit"."foo" IS NULL "alias"', str(c2))
def test__criterion_is_null(self): c1 = Field("foo").isnull() c2 = Field("foo", table=self.t).isnull() self.assertEqual('"foo" IS NULL', str(c1)) self.assertEqual('"crit"."foo" IS NULL', str(c2))
def test_with_generator(self): crit = Criterion.all(Field(letter) for letter in "abcd") self.assertEqual(str(crit), '"a" AND "b" AND "c" AND "d"')
def test_multiple_args_returned_in_chain_of_ors(self): crit = Criterion.all([Field("a"), Field("b"), Field("c"), Field("d")]) self.assertEqual(str(crit), '"a" AND "b" AND "c" AND "d"')
def test_single_arg_returns_self(self): f = Field("a") crit = Criterion.all([f]) self.assertEqual(str(f), str(crit))
def test__criterion_eq_right(self): c1 = 1 == Field("foo") c2 = -1 == Field("foo", table=self.t) self.assertEqual('"foo"=1', str(c1)) self.assertEqual('"crit"."foo"=-1', str(c2))
def test__field_arithmetic_is_not_aggregate(self): v = Field("foo") + Field("bar") self.assertFalse(v.is_aggregate)
def test_tabled_ne_fields_differently_hashed(self): customer_name = Field(name="name", table=Table("customers")) client_name = Field(name="name", table=Table("clients")) self.assertTrue(hash(customer_name) != hash(client_name))
def test__negative_agg_func_is_aggregate(self): v = Negative(fn.Sum(Field("foo"))) self.assertTrue(v.is_aggregate)
def test_non_tabled_aliased_ne_fields_differently_hashed(self): self.assertTrue( hash(Field(name="A", alias="my_a1")) != hash( Field(name="A", alias="my_a2")))
def test__mixed_func_arithmetic_is_not_aggregate(self): v = Field("foo") / fn.Sum(Field("foo")) self.assertFalse(v.is_aggregate)
def test_non_tabled_eq_fields_equally_hashed(self): self.assertTrue(hash(Field(name="A")) == hash(Field(name="A")))
def test__agg_case_is_aggregate(self): v = (Case().when(Field("foo") == 1, fn.Sum(Field("bar"))).when( Field("foo") == 2, fn.Sum(Field("fiz"))).else_(fn.Sum(Field("fiz")))) self.assertTrue(v.is_aggregate)
def test_non_tabled_ne_fields_differently_hashed(self): self.assertTrue(hash(Field(name="A")) != hash(Field(name="B")))
def test__case_mixed_constant_is_not_aggregate(self): v = (Case().when(Field("foo") == 1, fn.Sum(Field("bar"))).when( Field("foo") == 2, fn.Sum(Field("fiz"))).else_(1)) self.assertTrue(v.is_aggregate)
def test_when_alias_specified(self): c1 = Field("foo", alias="bar") self.assertEqual('bar', str(c1.alias)) c1 = Field("foo").as_("bar") self.assertEqual('bar', str(c1.alias))
def test__case_with_single_aggregate_field_is_not_aggregate(self): v = (Case().when(Field("foo") == 1, 1).when(fn.Sum(Field("foo")) == 2, 2).else_(3)) self.assertFalse(v.is_aggregate)
def test_round_date(self): result = Vertica().round_date(Field('date'), 'XX') self.assertEqual('ROUND("date",\'XX\')', str(result))
def test_date_add_day(self): result = SnowflakeDatabase().date_add(Field('date'), 'day', 1) self.assertEqual('TIMESTAMPADD(\'day\',1,"date")', str(result))
def test__criterion_replace_table_with_value(self): table = Table("a") c = (Field("foo") > 1).replace_table(None, table) self.assertEqual(c.left, table) self.assertEqual(c.tables_, {table})
def test__field_is_not_aggregate(self): v = Field("foo") self.assertFalse(v.is_aggregate)
def test_with_generator(self): crit = Criterion.any(Field(letter) for letter in "abcd") self.assertEqual(str(crit), '"a" OR "b" OR "c" OR "d"')