Example #1
0
 def testEquality(self):
     i1 = QgsInterval()
     i2 = QgsInterval()
     self.assertEqual(i1, i2)
     i1 = QgsInterval(5)
     self.assertNotEqual(i1, i2)
     i1.setValid(False)
     i2 = QgsInterval(5)
     self.assertNotEqual(i1, i2)
     i1 = QgsInterval(5)
     self.assertEqual(i1, i2)
     i1.setSeconds(6)
     self.assertNotEqual(i1, i2)
Example #2
0
 def testFromString(self):
     i = QgsInterval.fromString('1 Year 1 Month 1 Week 1 Hour 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('1 Year, 1 Month, 1 Week, 1 Hour, 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('1 Year; 1 Month; 1 Week; 1 Hour; 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('1 Year. 1 Month. 1 Week. 1 Hour. 1 Minute.')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('2 Years')
     self.assertTrue(i.isValid())
     self.assertEqual(i.years(), 2)
     i = QgsInterval.fromString('30 month')
     self.assertTrue(i.isValid())
     self.assertEqual(i.months(), 30)
     i = QgsInterval.fromString(' 40 MONTHS ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.months(), 40)
     i = QgsInterval.fromString('2.5 weeks')
     self.assertTrue(i.isValid())
     self.assertEqual(i.weeks(), 2.5)
     i = QgsInterval.fromString('2.5 WEEK')
     self.assertTrue(i.isValid())
     self.assertEqual(i.weeks(), 2.5)
     i = QgsInterval.fromString('1 Day')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 24 * 60 * 60)
     i = QgsInterval.fromString('2  dAys')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 48 * 60 * 60)
     i = QgsInterval.fromString('1 hours')
     self.assertTrue(i.isValid())
     self.assertEqual(i.hours(), 1)
     i = QgsInterval.fromString('1.7 HoURS ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.hours(), 1.7)
     i = QgsInterval.fromString('2 minutes')
     self.assertTrue(i.isValid())
     self.assertEqual(i.minutes(), 2)
     i = QgsInterval.fromString('123 MiNuTe ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.minutes(), 123)
     i = QgsInterval.fromString('5 Seconds')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 5)
     i = QgsInterval.fromString('5 second')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 5)
     i = QgsInterval.fromString('bad')
     self.assertFalse(i.isValid())
Example #3
0
 def testEquality(self):
     i1 = QgsInterval()
     i2 = QgsInterval()
     self.assertEqual(i1, i2)
     i1 = QgsInterval(5)
     self.assertNotEqual(i1, i2)
     i1.setValid(False)
     i2 = QgsInterval(5)
     self.assertNotEqual(i1, i2)
     i1 = QgsInterval(5)
     self.assertEqual(i1, i2)
     i1.setSeconds(6)
     self.assertNotEqual(i1, i2)
Example #4
0
 def testConversions(self):
     i = QgsInterval()
     # conversions
     i.setYears(1)
     self.assertEqual(round(i.months()), 12)
     self.assertEqual(round(i.weeks()), 52)
     self.assertEqual(round(i.days()), 365)
     i.setDays(5)
     self.assertEqual(i.hours(), 5 * 24)
     self.assertEqual(i.minutes(), 5 * 24 * 60)
     self.assertEqual(i.seconds(), 5 * 24 * 60 * 60)
Example #5
0
    def testSettersGetters(self):
        # setters and getters
        i = QgsInterval()
        i.setSeconds(60)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 60.0)

        i = QgsInterval()
        i.setMinutes(10)
        self.assertTrue(i.isValid())
        self.assertEqual(i.minutes(), 10.0)

        i = QgsInterval()
        i.setHours(5)
        self.assertTrue(i.isValid())
        self.assertEqual(i.hours(), 5.0)

        i = QgsInterval()
        i.setDays(70)
        self.assertTrue(i.isValid())
        self.assertEqual(i.days(), 70.0)

        i = QgsInterval()
        i.setWeeks(9)
        self.assertTrue(i.isValid())
        self.assertEqual(i.weeks(), 9.0)

        i = QgsInterval()
        i.setMonths(4)
        self.assertTrue(i.isValid())
        self.assertEqual(i.months(), 4.0)

        i = QgsInterval()
        i.setYears(8)
        self.assertTrue(i.isValid())
        self.assertEqual(i.years(), 8.0)
Example #6
0
    def testIntervalConstructor(self):
        """ Test QgsInterval constructor """

        # invalid interval
        i = QgsInterval()
        self.assertFalse(i.isValid())
        i.setValid(True)
        self.assertTrue(i.isValid())
        i.setValid(False)
        self.assertFalse(i.isValid())
        # setting a duration should make interval valid
        i.setSeconds(5)
        self.assertTrue(i.isValid())

        # constructor with duration
        i = QgsInterval(56)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 56)
Example #7
0
    def testDateTime(self):
        """ Test calculation of aggregates on date/datetime fields"""

        layer = QgsVectorLayer("Point?field=flddate:date&field=flddatetime:datetime", "layer", "memory")
        pr = layer.dataProvider()

        # must be same length:
        datetime_values = [QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                           QDateTime(QDate(2011, 1, 5), QTime(15, 3, 1)),
                           QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                           QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                           QDateTime(QDate(2019, 12, 28), QTime(23, 10, 1)),
                           QDateTime(),
                           QDateTime(QDate(1998, 1, 2), QTime(1, 10, 54)),
                           QDateTime(),
                           QDateTime(QDate(2011, 1, 5), QTime(11, 10, 54))]
        date_values = [QDate(2015, 3, 4),
                       QDate(2015, 3, 4),
                       QDate(2019, 12, 28),
                       QDate(),
                       QDate(1998, 1, 2),
                       QDate(),
                       QDate(2011, 1, 5),
                       QDate(2011, 1, 5),
                       QDate(2011, 1, 5)]
        self.assertEqual(len(datetime_values), len(date_values))

        features = []
        for i in range(len(datetime_values)):
            f = QgsFeature()
            f.setFields(layer.fields())
            f.setAttributes([date_values[i], datetime_values[i]])
            features.append(f)
        assert pr.addFeatures(features)

        tests = [[QgsAggregateCalculator.Count, 'flddatetime', 9],
                 [QgsAggregateCalculator.Count, 'flddate', 9],
                 [QgsAggregateCalculator.CountDistinct, 'flddatetime', 6],
                 [QgsAggregateCalculator.CountDistinct, 'flddate', 5],
                 [QgsAggregateCalculator.CountMissing, 'flddatetime', 2],
                 [QgsAggregateCalculator.CountMissing, 'flddate', 2],
                 [QgsAggregateCalculator.Min, 'flddatetime', QDateTime(QDate(1998, 1, 2), QTime(1, 10, 54))],
                 [QgsAggregateCalculator.Min, 'flddate', QDateTime(QDate(1998, 1, 2), QTime(0, 0, 0))],
                 [QgsAggregateCalculator.Max, 'flddatetime', QDateTime(QDate(2019, 12, 28), QTime(23, 10, 1))],
                 [QgsAggregateCalculator.Max, 'flddate', QDateTime(QDate(2019, 12, 28), QTime(0, 0, 0))],

                 [QgsAggregateCalculator.Range, 'flddatetime', QgsInterval(693871147)],
                 [QgsAggregateCalculator.Range, 'flddate', QgsInterval(693792000)],

                 [QgsAggregateCalculator.ArrayAggregate, 'flddatetime', [None if v.isNull() else v for v in datetime_values]],
                 [QgsAggregateCalculator.ArrayAggregate, 'flddate', [None if v.isNull() else v for v in date_values]],
                 ]

        agg = QgsAggregateCalculator(layer)
        for t in tests:
            val, ok = agg.calculate(t[0], t[1])
            self.assertTrue(ok)
            self.assertEqual(val, t[2])

        # bad tests - the following stats should not be calculatable for string fields
        for t in [QgsAggregateCalculator.Sum,
                  QgsAggregateCalculator.Mean,
                  QgsAggregateCalculator.Median,
                  QgsAggregateCalculator.StDev,
                  QgsAggregateCalculator.StDevSample,
                  QgsAggregateCalculator.Minority,
                  QgsAggregateCalculator.Majority,
                  QgsAggregateCalculator.FirstQuartile,
                  QgsAggregateCalculator.ThirdQuartile,
                  QgsAggregateCalculator.InterQuartileRange,
                  QgsAggregateCalculator.StringMinimumLength,
                  QgsAggregateCalculator.StringMaximumLength,
                  ]:
            val, ok = agg.calculate(t, 'flddatetime')
            self.assertFalse(ok)
Example #8
0
 def testConversions(self):
     i = QgsInterval()
     # conversions
     i.setYears(1)
     self.assertEqual(round(i.months()), 12)
     self.assertEqual(round(i.weeks()), 52)
     self.assertEqual(round(i.days()), 365)
     i.setDays(5)
     self.assertEqual(i.hours(), 5 * 24)
     self.assertEqual(i.minutes(), 5 * 24 * 60)
     self.assertEqual(i.seconds(), 5 * 24 * 60 * 60)
Example #9
0
    def testGettersDurationUnitChange(self):
        i = QgsInterval()
        self.assertEqual(i.originalDuration(), 0.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalUnknownUnit)
        i.setYears(1)
        self.assertEqual(i.years(), 1.0)
        i.setMonths(3)
        self.assertEqual(i.months(), 3.0)
        i.setWeeks(1)
        self.assertEqual(i.weeks(), 1.0)
        i.setDays(4)
        self.assertEqual(i.days(), 4.0)
        i.setHours(22.3)
        self.assertEqual(i.hours(), 22.3)
        i.setMinutes(11)
        self.assertEqual(i.minutes(), 11.0)
        i.setSeconds(100)
        self.assertEqual(i.seconds(), 100.0)

        i = QgsInterval(0, 0, 0, 0, 0, 1, 30)
        self.assertEqual(i.minutes(), 1.5)
        i.setDays(45)
        self.assertEqual(i.months(), 1.5)
Example #10
0
 def testSettersDurationUnitChange(self):
     i = QgsInterval()
     self.assertEqual(i.originalDuration(), 0.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalUnknownUnit)
     i.setYears(1)
     self.assertEqual(i.originalDuration(), 1.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalYears)
     i.setMonths(3)
     self.assertEqual(i.originalDuration(), 3.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMonths)
     i.setWeeks(1)
     self.assertEqual(i.originalDuration(), 1.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalWeeks)
     i.setDays(4)
     self.assertEqual(i.originalDuration(), 4.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalDays)
     i.setHours(22.3)
     self.assertEqual(i.originalDuration(), 22.3)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalHours)
     i.setMinutes(11)
     self.assertEqual(i.originalDuration(), 11.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMinutes)
     i.setSeconds(100)
     self.assertEqual(i.originalDuration(), 100.0)
     self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)
Example #11
0
    def testIntervalDurationUnitSetting(self):
        i = QgsInterval()
        self.assertEqual(i.originalDuration(), 0.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalUnknownUnit)
        i = QgsInterval(2, QgsUnitTypes.TemporalMilliseconds)
        self.assertEqual(i.originalDuration(), 2.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMilliseconds)
        i = QgsInterval(34.56, QgsUnitTypes.TemporalSeconds)
        self.assertEqual(i.originalDuration(), 34.56)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)
        i = QgsInterval(10, QgsUnitTypes.TemporalMinutes)
        self.assertEqual(i.originalDuration(), 10.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMinutes)
        i = QgsInterval(10.012, QgsUnitTypes.TemporalHours)
        self.assertEqual(i.originalDuration(), 10.012)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalHours)
        i = QgsInterval(10.32, QgsUnitTypes.TemporalDays)
        self.assertEqual(i.originalDuration(), 10.32)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalDays)
        i = QgsInterval(100, QgsUnitTypes.TemporalWeeks)
        self.assertEqual(i.originalDuration(), 100.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalWeeks)
        i = QgsInterval(1000, QgsUnitTypes.TemporalMonths)
        self.assertEqual(i.originalDuration(), 1000.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMonths)
        i = QgsInterval(500.005, QgsUnitTypes.TemporalYears)
        self.assertEqual(i.originalDuration(), 500.005)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalYears)
        i = QgsInterval(0.02, QgsUnitTypes.TemporalDecades)
        self.assertEqual(i.originalDuration(), 0.02)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalDecades)
        i = QgsInterval(0.2, QgsUnitTypes.TemporalCenturies)
        self.assertEqual(i.originalDuration(), 0.2)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalCenturies)

        i = QgsInterval(10)
        self.assertEqual(i.originalDuration(), 10.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)
        i = QgsInterval(0)
        self.assertEqual(i.originalDuration(), 0.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)

        i = QgsInterval(1, 0, 0, 0, 0, 0, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalYears)
        i = QgsInterval(0, 1, 0, 0, 0, 0, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMonths)
        i = QgsInterval(0, 0, 1, 0, 0, 0, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalWeeks)
        i = QgsInterval(0, 0, 0, 1, 0, 0, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalDays)
        i = QgsInterval(0, 0, 0, 0, 1, 0, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalHours)
        i = QgsInterval(0, 0, 0, 0, 0, 1, 0)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalMinutes)
        i = QgsInterval(0, 0, 0, 0, 0, 0, 1)
        self.assertEqual(i.originalDuration(), 1.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)
        i = QgsInterval(0, 0, 0, 0, 0, 0, 0)

        # we may as well treat this the same as if 0 seconds was explicitly specified!
        self.assertEqual(i.originalDuration(), 0.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalSeconds)

        i = QgsInterval(0, 0, 0, 0, 0, 1, 1)
        self.assertEqual(i.originalDuration(), 0.0)
        self.assertEqual(i.originalUnit(), QgsUnitTypes.TemporalUnknownUnit)
Example #12
0
 def testFromUnits(self):
     i = QgsInterval(2, 0, 0, 0, 0, 0, 0)
     self.assertEqual(i.seconds(), 63115200.0)
     i = QgsInterval(0, 2, 0, 0, 0, 0, 0)
     self.assertEqual(i.seconds(), 5184000.0)
     i = QgsInterval(0, 0, 2, 0, 0, 0, 0)
     self.assertEqual(i.seconds(), 1209600.0)
     i = QgsInterval(0, 0, 0, 2, 0, 0, 0)
     self.assertEqual(i.seconds(), 172800.0)
     i = QgsInterval(0, 0, 0, 0, 2, 0, 0)
     self.assertEqual(i.seconds(), 7200.0)
     i = QgsInterval(0, 0, 0, 0, 0, 2, 0)
     self.assertEqual(i.seconds(), 120.0)
     i = QgsInterval(0, 0, 0, 0, 0, 0, 2)
     self.assertEqual(i.seconds(), 2)
     i = QgsInterval(1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 2)
     self.assertEqual(i.seconds(), 56342192.0)
Example #13
0
    def testIntervalConstructor(self):
        """ Test QgsInterval constructor """

        # invalid interval
        i = QgsInterval()
        self.assertFalse(i.isValid())
        i.setValid(True)
        self.assertTrue(i.isValid())
        i.setValid(False)
        self.assertFalse(i.isValid())
        # setting a duration should make interval valid
        i.setSeconds(5)
        self.assertTrue(i.isValid())

        # constructor with duration
        i = QgsInterval(56)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 56)

        # constructor with unit type
        i = QgsInterval(56, QgsUnitTypes.TemporalMilliseconds)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 0.056)
Example #14
0
    def testIndividualStats(self):
        # tests calculation of statistics one at a time, to make sure statistic calculations are not
        # dependent on each other
        tests = [
            {
                'stat': QgsDateTimeStatisticalSummary.Count,
                'expected': 9
            },
            {
                'stat': QgsDateTimeStatisticalSummary.CountDistinct,
                'expected': 6
            },
            {
                'stat': QgsDateTimeStatisticalSummary.CountMissing,
                'expected': 2
            },
            {
                'stat': QgsDateTimeStatisticalSummary.Min,
                'expected': QDateTime(QDate(1998, 1, 2), QTime(1, 10, 54))
            },
            {
                'stat': QgsDateTimeStatisticalSummary.Max,
                'expected': QDateTime(QDate(2019, 12, 28), QTime(23, 10, 1))
            },
            {
                'stat': QgsDateTimeStatisticalSummary.Range,
                'expected': QgsInterval(693871147)
            },
        ]

        # we test twice, once with values added as a list and once using values
        # added one-at-a-time
        s = QgsDateTimeStatisticalSummary()
        s3 = QgsDateTimeStatisticalSummary()
        for t in tests:
            # test constructor
            s2 = QgsDateTimeStatisticalSummary(t['stat'])
            self.assertEqual(s2.statistics(), t['stat'])

            s.setStatistics(t['stat'])
            self.assertEqual(s.statistics(), t['stat'])
            s3.setStatistics(t['stat'])

            dates = [
                QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                QDateTime(QDate(2011, 1, 5), QTime(15, 3, 1)),
                QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                QDateTime(QDate(2015, 3, 4), QTime(11, 10, 54)),
                QDateTime(QDate(2019, 12, 28), QTime(23, 10, 1)),
                QDateTime(),
                QDateTime(QDate(1998, 1, 2), QTime(1, 10, 54)),
                QDateTime(),
                QDateTime(QDate(2011, 1, 5), QTime(11, 10, 54))
            ]
            s.calculate(dates)
            s3.reset()
            for d in dates:
                s3.addValue(d)
            s3.finalize()

            self.assertEqual(s.statistic(t['stat']), t['expected'])
            self.assertEqual(s3.statistic(t['stat']), t['expected'])

            # display name
            self.assertTrue(
                len(QgsDateTimeStatisticalSummary.displayName(t['stat'])) > 0)
Example #15
0
    def testIntervalConstructor(self):
        """ Test QgsInterval constructor """

        # invalid interval
        i = QgsInterval()
        self.assertFalse(i.isValid())
        i.setValid(True)
        self.assertTrue(i.isValid())
        i.setValid(False)
        self.assertFalse(i.isValid())
        # setting a duration should make interval valid
        i.setSeconds(5)
        self.assertTrue(i.isValid())

        # constructor with duration
        i = QgsInterval(56)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 56)
Example #16
0
    def testSettersGetters(self):
        # setters and getters
        i = QgsInterval()
        i.setSeconds(60)
        self.assertTrue(i.isValid())
        self.assertEqual(i.seconds(), 60.0)

        i = QgsInterval()
        i.setMinutes(10)
        self.assertTrue(i.isValid())
        self.assertEqual(i.minutes(), 10.0)

        i = QgsInterval()
        i.setHours(5)
        self.assertTrue(i.isValid())
        self.assertEqual(i.hours(), 5.0)

        i = QgsInterval()
        i.setDays(70)
        self.assertTrue(i.isValid())
        self.assertEqual(i.days(), 70.0)

        i = QgsInterval()
        i.setWeeks(9)
        self.assertTrue(i.isValid())
        self.assertEqual(i.weeks(), 9.0)

        i = QgsInterval()
        i.setMonths(4)
        self.assertTrue(i.isValid())
        self.assertEqual(i.months(), 4.0)

        i = QgsInterval()
        i.setYears(8)
        self.assertTrue(i.isValid())
        self.assertEqual(i.years(), 8.0)
Example #17
0
    def testEquality(self):
        i1 = QgsInterval()
        i2 = QgsInterval()
        self.assertEqual(i1, i2)
        i1 = QgsInterval(5)
        self.assertNotEqual(i1, i2)
        i1.setValid(False)
        i2 = QgsInterval(5)
        self.assertNotEqual(i1, i2)
        i1 = QgsInterval(5)
        self.assertEqual(i1, i2)
        i1.setSeconds(6)
        self.assertNotEqual(i1, i2)

        # using original units

        i1 = QgsInterval(1, QgsUnitTypes.TemporalMonths)
        self.assertEqual(i1, QgsInterval(1, QgsUnitTypes.TemporalMonths))
        i2 = QgsInterval(720, QgsUnitTypes.TemporalHours)
        self.assertEqual(i2, QgsInterval(720, QgsUnitTypes.TemporalHours))
        # these QgsInterval would be equal if we test on the approximated seconds value alone, but should be treated as not equal
        # as their original units differ and we don't want the odd situation that the QgsInterval objects report equality
        # but i1.months() != i2.months()!!
        self.assertEqual(i1.seconds(), i2.seconds())
        self.assertNotEqual(i1, i2)

        # these should be treated as equal - they have unknown original units, so we are just comparing
        # their computed seconds values
        i1 = QgsInterval(0, 0, 0, 1, 0, 0, 1)
        i2 = QgsInterval(0, 0, 0, 0, 24, 0, 1)
        self.assertEqual(i1, i2)
Example #18
0
 def testFromString(self):
     i = QgsInterval.fromString('1 Year 1 Month 1 Week 1 Hour 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('1 Year, 1 Month, 1 Week, 1 Hour, 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('1 Year; 1 Month; 1 Week; 1 Hour; 1 Minute')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString(
         '1 Year. 1 Month. 1 Week. 1 Hour. 1 Minute.')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 34758060)
     i = QgsInterval.fromString('2 Years')
     self.assertTrue(i.isValid())
     self.assertEqual(i.years(), 2)
     i = QgsInterval.fromString('30 month')
     self.assertTrue(i.isValid())
     self.assertEqual(i.months(), 30)
     i = QgsInterval.fromString(' 40 MONTHS ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.months(), 40)
     i = QgsInterval.fromString('2.5 weeks')
     self.assertTrue(i.isValid())
     self.assertEqual(i.weeks(), 2.5)
     i = QgsInterval.fromString('2.5 WEEK')
     self.assertTrue(i.isValid())
     self.assertEqual(i.weeks(), 2.5)
     i = QgsInterval.fromString('1 Day')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 24 * 60 * 60)
     i = QgsInterval.fromString('2  dAys')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 48 * 60 * 60)
     i = QgsInterval.fromString('1 hours')
     self.assertTrue(i.isValid())
     self.assertEqual(i.hours(), 1)
     i = QgsInterval.fromString('1.7 HoURS ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.hours(), 1.7)
     i = QgsInterval.fromString('2 minutes')
     self.assertTrue(i.isValid())
     self.assertEqual(i.minutes(), 2)
     i = QgsInterval.fromString('123 MiNuTe ')
     self.assertTrue(i.isValid())
     self.assertEqual(i.minutes(), 123)
     i = QgsInterval.fromString('5 Seconds')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 5)
     i = QgsInterval.fromString('5 second')
     self.assertTrue(i.isValid())
     self.assertEqual(i.seconds(), 5)
     i = QgsInterval.fromString('bad')
     self.assertFalse(i.isValid())
Example #19
0
    def test_temporal_animation(self):
        """
        Test temporal animation logic
        """
        canvas = QgsMapCanvas()
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)

        controller = QgsTemporalController()
        canvas.setTemporalController(controller)
        controller.updateTemporalRange.emit(
            QgsDateTimeRange(QDateTime(QDate(2020, 1, 2), QTime(1, 2, 3)),
                             QDateTime(QDate(2020, 1, 4), QTime(1, 2, 3))))
        # should be no change
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)

        temporal_no = QgsTemporalNavigationObject()
        temporal_no.setTemporalExtents(
            QgsDateTimeRange(QDateTime(QDate(2020, 1, 2), QTime(1, 2, 3)),
                             QDateTime(QDate(2020, 1, 4), QTime(1, 2, 3))))
        temporal_no.setFrameDuration(QgsInterval(0, 0, 0, 0, 1, 0, 0))

        canvas.setTemporalController(temporal_no)
        controller.updateTemporalRange.emit(
            QgsDateTimeRange(QDateTime(QDate(2020, 1, 2), QTime(1, 2, 3)),
                             QDateTime(QDate(2020, 1, 4), QTime(1, 2, 3))))
        # should be no change
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)

        temporal_no.setFramesPerSecond(30)
        temporal_no.pause()
        temporal_no.setCurrentFrameNumber(6)
        canvas.refresh()

        # should be no change - temporal controller is not in animation mode
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)

        temporal_no.setNavigationMode(QgsTemporalNavigationObject.Animated)
        self.assertEqual(canvas.mapSettings().frameRate(), 30)
        self.assertEqual(canvas.mapSettings().currentFrame(), 6)

        temporal_no.setCurrentFrameNumber(7)
        self.assertEqual(canvas.mapSettings().frameRate(), 30)
        self.assertEqual(canvas.mapSettings().currentFrame(), 6)

        # switch off animation mode
        temporal_no.setNavigationMode(QgsTemporalNavigationObject.FixedRange)
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)

        temporal_no.setNavigationMode(QgsTemporalNavigationObject.Animated)
        self.assertEqual(canvas.mapSettings().frameRate(), 30)
        self.assertEqual(canvas.mapSettings().currentFrame(), 7)

        temporal_no.setNavigationMode(
            QgsTemporalNavigationObject.NavigationOff)
        self.assertEqual(canvas.mapSettings().frameRate(), -1)
        self.assertEqual(canvas.mapSettings().currentFrame(), -1)