예제 #1
0
 def test_float(self):
     FloatModel.objects.create(f1=-27.5, f2=0.33)
     obj = FloatModel.objects.annotate(f1_degrees=Degrees("f1"),
                                       f2_degrees=Degrees("f2")).first()
     self.assertIsInstance(obj.f1_degrees, float)
     self.assertIsInstance(obj.f2_degrees, float)
     self.assertAlmostEqual(obj.f1_degrees, math.degrees(obj.f1))
     self.assertAlmostEqual(obj.f2_degrees, math.degrees(obj.f2))
예제 #2
0
 def test_decimal(self):
     DecimalModel.objects.create(n1=Decimal("-12.9"), n2=Decimal("0.6"))
     obj = DecimalModel.objects.annotate(n1_degrees=Degrees("n1"),
                                         n2_degrees=Degrees("n2")).first()
     self.assertIsInstance(obj.n1_degrees, Decimal)
     self.assertIsInstance(obj.n2_degrees, Decimal)
     self.assertAlmostEqual(obj.n1_degrees, Decimal(math.degrees(obj.n1)))
     self.assertAlmostEqual(obj.n2_degrees, Decimal(math.degrees(obj.n2)))
예제 #3
0
 def test_integer(self):
     IntegerModel.objects.create(small=-20, normal=15, big=-1)
     obj = IntegerModel.objects.annotate(
         small_degrees=Degrees('small'),
         normal_degrees=Degrees('normal'),
         big_degrees=Degrees('big'),
     ).first()
     self.assertIsInstance(obj.small_degrees, float)
     self.assertIsInstance(obj.normal_degrees, float)
     self.assertIsInstance(obj.big_degrees, float)
     self.assertAlmostEqual(obj.small_degrees, math.degrees(obj.small))
     self.assertAlmostEqual(obj.normal_degrees, math.degrees(obj.normal))
     self.assertAlmostEqual(obj.big_degrees, math.degrees(obj.big))
예제 #4
0
 def test_degrees(self):
     """
     Tests degrees function on a column.
     """
     q1 = Author.objects.values("num").annotate(
         num_degrees=Degrees("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, ((tests_author.num) * 180 / " +
         "3.141592653589793) AS num_degrees FROM tests_author",
     )
     self.assertEqual(params, ())
예제 #5
0
 def with_frames(self):
     frames_asc = Frame.objects.filter(sighting=OuterRef('id')).order_by('timestamp')
     frames_desc = Frame.objects.filter(sighting=OuterRef('id')).order_by('-timestamp')
     return self.prefetch_related(
         Prefetch(
             'frames',
             queryset=Frame.objects.with_flight_time(),
         )
     ).annotate(
         frame_count=Count('frames'),
         fa=Subquery(frames_asc.values('altitude')[:1]),
         la=Subquery(frames_desc.values('altitude')[:1]),
         fz=Subquery(frames_asc.values('azimuth')[:1]),
         lz=Subquery(frames_desc.values('azimuth')[:1]),
         arc_length=Degrees(
             ACos(
                 Sin(Radians(F('fa'))) * Sin(Radians(F('la'))) +
                 Cos(Radians(F('fa'))) * Cos(Radians(F('la'))) * Cos(Radians(F('fz') - F('lz')))
             )
         ),
     )
예제 #6
0
 def test_null(self):
     IntegerModel.objects.create()
     obj = IntegerModel.objects.annotate(null_degrees=Degrees('normal')).first()
     self.assertIsNone(obj.null_degrees)