def test_float_with_precision(self): FloatModel.objects.create(f1=-5.75, f2=Pi()) obj = FloatModel.objects.annotate( f1_round=Round("f1", 1), f2_round=Round("f2", 5), ).first() self.assertIsInstance(obj.f1_round, float) self.assertIsInstance(obj.f2_round, float) self.assertAlmostEqual(obj.f1_round, obj.f1, places=1) self.assertAlmostEqual(obj.f2_round, obj.f2, places=5)
def test_decimal_with_precision(self): DecimalModel.objects.create(n1=Decimal("-5.75"), n2=Pi()) obj = DecimalModel.objects.annotate( n1_round=Round("n1", 1), n2_round=Round("n2", 5), ).first() self.assertIsInstance(obj.n1_round, Decimal) self.assertIsInstance(obj.n2_round, Decimal) self.assertAlmostEqual(obj.n1_round, obj.n1, places=1) self.assertAlmostEqual(obj.n2_round, obj.n2, places=5)
def test_pi(self): """ Tests pi function applied to a column. """ q1 = Author.objects.filter(num=Pi()).values("num") compiler = SQLCompiler(q1.query, self.connection, "default") sql_query, params = compiler.query.as_sql(compiler, self.connection) self.assertEqual( sql_query, "SELECT tests_author.num FROM tests_author WHERE tests_author.num " + "= (3.141592653589793)", ) self.assertEqual(params, ())
def test(self): FloatModel.objects.create(f1=2.5, f2=15.9) obj = FloatModel.objects.annotate(pi=Pi()).first() self.assertIsInstance(obj.pi, float) self.assertAlmostEqual(obj.pi, math.pi, places=5)