def test_date_dim_with_monthly_interval(self):
        queries = (
            mock_dataset.query.widget(
                f.HighCharts().axis(f.HighCharts.LineSeries(mock_dataset.fields.votes))
            )
            .dimension(f.month(mock_dataset.fields.timestamp))
            .reference(f.DayOverDay(mock_dataset.fields.timestamp))
            .sql
        )

        self.assertEqual(2, len(queries))

        with self.subTest("base query is same as without reference"):
            self.assertEqual(
                "SELECT "
                'TRUNC("timestamp",\'MM\') "$timestamp",'
                'SUM("votes") "$votes" '
                'FROM "politics"."politician" '
                'GROUP BY "$timestamp" '
                'ORDER BY "$timestamp"',
                str(queries[0]),
            )

        with self.subTest(
            "reference query is same as base query with filter on reference dimension shifted"
        ):
            self.assertEqual(
                "SELECT "
                "TRUNC(TIMESTAMPADD('day',1,TRUNC(\"timestamp\",'MM')),'MM') \"$timestamp\","
                'SUM("votes") "$votes_dod" '
                'FROM "politics"."politician" '
                'GROUP BY "$timestamp" '
                'ORDER BY "$timestamp"',
                str(queries[1]),
            )
    def test_build_query_with_datetime_interval_monthly(self):
        queries = (mock_dataset.query().widget(
            f.ReactTable(mock_dataset.fields.votes)).dimension(
                f.month(mock_dataset.fields.timestamp)).sql)

        self.assertEqual(len(queries), 1)

        self.assertEqual(
            "SELECT "
            'TRUNC("timestamp",\'MM\') "$timestamp",'
            'SUM("votes") "$votes" '
            'FROM "politics"."politician" '
            'GROUP BY "$timestamp" '
            'ORDER BY "$timestamp" '
            'LIMIT 200000',
            str(queries[0]),
        )
from unittest import TestCase

from datetime import date

import fireant as f
from fireant import Rollup
from fireant.tests.dataset.mocks import mock_dataset

timestamp_daily = f.day(mock_dataset.fields.timestamp)
timestamp_monthly = f.month(mock_dataset.fields.timestamp)


# noinspection SqlDialectInspection,SqlNoDataSourceInspection
class QueryBuilderDatetimeReferenceTests(TestCase):
    maxDiff = None

    def test_reference_with_no_dimensions_or_filters_creates_same_query(self):
        # TODO reduce this to a single query
        queries = (
            mock_dataset.query.widget(
                f.HighCharts().axis(f.HighCharts.LineSeries(mock_dataset.fields.votes))
            )
            .reference(f.DayOverDay(mock_dataset.fields.timestamp))
            .sql
        )

        self.assertEqual(2, len(queries))

        with self.subTest("base query is same as without reference"):
            self.assertEqual(
                "SELECT " 'SUM("votes") "$votes" ' 'FROM "politics"."politician"',
示例#4
0
 def test_date_value_with_month_interval_is_returned_as_date_string_to_the_month_and_year(
     self, ):
     for d in (date(2019, 1, 1), datetime(2019, 1, 1, 12, 30, 2)):
         with self.subTest("with " + d.__class__.__name__):
             self.assertEqual("Jan 2019",
                              formats.display_value(d, month(date_field)))