~barry/autopilot/lp1488175

« back to all changes in this revision

Viewing changes to autopilot/tests/unit/test_types.py

  • Committer: CI bot
  • Author(s): Leonardo Arias Fonseca, Thomi Richards, Christopher Lee, Leo Arias, nskaggs
  • Date: 2014-10-31 21:49:56 UTC
  • mfrom: (493.1.26 trunk)
  • Revision ID: ps-jenkins@lists.canonical.com-20141031214956-oaptmny2t0vgj973
Autopilot release:
  - Add support for large timestamps
  - Add per-test timeouts
  - Add press duration to touch clicks
  - Improved logging
  - Make own autopilot functional test runnable with testtools runner.   
Approved by: Nicholas Skaggs, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#
19
19
 
20
20
from datetime import datetime, time
21
 
from testscenarios import TestWithScenarios
 
21
from testscenarios import TestWithScenarios, multiply_scenarios
22
22
from testtools import TestCase
23
23
from testtools.matchers import Equals, IsInstance, NotEquals, raises
24
24
 
25
25
import dbus
26
26
from unittest.mock import patch, Mock
27
27
 
 
28
from autopilot.tests.functional.fixtures import Timezone
28
29
from autopilot.introspection.types import (
29
30
    Color,
30
31
    create_value_instance,
51
52
from autopilot.introspection.dbus import DBusIntrospectionObject
52
53
from autopilot.utilities import compatible_repr
53
54
 
 
55
from dateutil import tz
 
56
 
54
57
 
55
58
class PlainTypeTests(TestWithScenarios, TestCase):
56
59
 
281
284
        self.assertEqual(repr(c), str(c))
282
285
 
283
286
 
284
 
class DateTimeTests(TestCase):
 
287
def unable_to_handle_timestamp(timestamp):
 
288
    """Return false if the platform can handle timestamps larger than 32bit
 
289
    limit.
 
290
 
 
291
    """
 
292
    try:
 
293
        datetime.fromtimestamp(timestamp)
 
294
        return False
 
295
    except:
 
296
        return True
 
297
 
 
298
 
 
299
class DateTimeCreationTests(TestCase):
 
300
 
 
301
    timestamp = 1405382400  # No significance, just a timestamp
285
302
 
286
303
    def test_can_construct_datetime(self):
287
 
        dt = DateTime(1377209927)
 
304
        dt = DateTime(self.timestamp)
288
305
        self.assertThat(dt, IsInstance(dbus.Array))
289
306
 
290
307
    def test_datetime_has_slice_access(self):
291
 
        dt = DateTime(1377209927)
292
 
 
293
 
        self.assertThat(dt[0], Equals(1377209927))
 
308
        dt = DateTime(self.timestamp)
 
309
        self.assertThat(dt[0], Equals(self.timestamp))
294
310
 
295
311
    def test_datetime_has_properties(self):
296
 
        dt = DateTime(1377209927)
 
312
        dt = DateTime(self.timestamp)
297
313
 
298
314
        self.assertTrue(hasattr(dt, 'timestamp'))
299
315
        self.assertTrue(hasattr(dt, 'year'))
303
319
        self.assertTrue(hasattr(dt, 'minute'))
304
320
        self.assertTrue(hasattr(dt, 'second'))
305
321
 
 
322
    def test_repr(self):
 
323
        # Use a well known timezone for comparison
 
324
        self.useFixture(Timezone('UTC'))
 
325
        dt = DateTime(self.timestamp)
 
326
        observed = repr(dt)
 
327
 
 
328
        expected = "DateTime({:%Y-%m-%d %H:%M:%S})".format(
 
329
            datetime.fromtimestamp(self.timestamp)
 
330
        )
 
331
        self.assertEqual(expected, observed)
 
332
 
 
333
    def test_repr_equals_str(self):
 
334
        dt = DateTime(self.timestamp)
 
335
        self.assertEqual(repr(dt), str(dt))
 
336
 
 
337
    def test_can_create_DateTime_using_large_timestamp(self):
 
338
        """Must be able to create a DateTime object using a timestamp larger
 
339
        than the 32bit time_t limit.
 
340
 
 
341
        """
 
342
        # Use a well known timezone for comparison
 
343
        self.useFixture(Timezone('UTC'))
 
344
        large_timestamp = 2**32+1
 
345
        dt = DateTime(large_timestamp)
 
346
 
 
347
        self.assertEqual(dt.year, 2106)
 
348
        self.assertEqual(dt.month, 2)
 
349
        self.assertEqual(dt.day, 7)
 
350
        self.assertEqual(dt.hour, 6)
 
351
        self.assertEqual(dt.minute, 28)
 
352
        self.assertEqual(dt.second, 17)
 
353
        self.assertEqual(dt.timestamp, large_timestamp)
 
354
 
 
355
 
 
356
class DateTimeTests(TestWithScenarios, TestCase):
 
357
 
 
358
    timestamps = [
 
359
        # This timestamp uncovered an issue during development.
 
360
        ('Explicit US/Pacific test', dict(
 
361
            timestamp=1090123200
 
362
        )),
 
363
        ('September 2014', dict(
 
364
            timestamp=1411992000
 
365
        )),
 
366
 
 
367
        ('NZ DST example', dict(
 
368
            timestamp=2047570047
 
369
        )),
 
370
 
 
371
        ('Winter', dict(
 
372
            timestamp=1389744000
 
373
        )),
 
374
 
 
375
        ('Summer', dict(
 
376
            timestamp=1405382400
 
377
        )),
 
378
 
 
379
        ('32bit max', dict(
 
380
            timestamp=2**32+1
 
381
        )),
 
382
 
 
383
        ('32bit limit', dict(
 
384
            timestamp=2983579200
 
385
        )),
 
386
 
 
387
    ]
 
388
 
 
389
    timezones = [
 
390
        ('UTC', dict(
 
391
            timezone='UTC'
 
392
        )),
 
393
 
 
394
        ('London', dict(
 
395
            timezone='Europe/London'
 
396
        )),
 
397
 
 
398
        ('New Zealand', dict(
 
399
            timezone='NZ',
 
400
        )),
 
401
 
 
402
        ('Pacific', dict(
 
403
            timezone='US/Pacific'
 
404
        )),
 
405
 
 
406
        ('Hongkong', dict(
 
407
            timezone='Hongkong'
 
408
        )),
 
409
 
 
410
        ('Moscow', dict(
 
411
            timezone='Europe/Moscow'
 
412
        )),
 
413
 
 
414
        ('Copenhagen', dict(
 
415
            timezone='Europe/Copenhagen',
 
416
        )),
 
417
    ]
 
418
 
 
419
    scenarios = multiply_scenarios(timestamps, timezones)
 
420
 
 
421
    def skip_if_timestamp_too_large(self, timestamp):
 
422
        if unable_to_handle_timestamp(self.timestamp):
 
423
            self.skip("Timestamp to large for platform time_t")
 
424
 
306
425
    def test_datetime_properties_have_correct_values(self):
307
 
        dt = DateTime(1377209927)
308
 
        dt_with_tz = datetime.fromtimestamp(1377209927)
309
 
 
310
 
        self.assertThat(dt.timestamp, Equals(dt_with_tz.timestamp()))
311
 
        self.assertThat(dt.year, Equals(dt_with_tz.year))
312
 
        self.assertThat(dt.month, Equals(dt_with_tz.month))
313
 
        self.assertThat(dt.day, Equals(dt_with_tz.day))
314
 
        self.assertThat(dt.hour, Equals(dt_with_tz.hour))
315
 
        self.assertThat(dt.minute, Equals(dt_with_tz.minute))
316
 
        self.assertThat(dt.second, Equals(dt_with_tz.second))
 
426
        self.skip_if_timestamp_too_large(self.timestamp)
 
427
        self.useFixture(Timezone(self.timezone))
 
428
 
 
429
        dt1 = DateTime(self.timestamp)
 
430
        dt2 = datetime.fromtimestamp(self.timestamp, tz.gettz())
 
431
 
 
432
        self.assertThat(dt1.year, Equals(dt2.year))
 
433
        self.assertThat(dt1.month, Equals(dt2.month))
 
434
        self.assertThat(dt1.day, Equals(dt2.day))
 
435
        self.assertThat(dt1.hour, Equals(dt2.hour))
 
436
        self.assertThat(dt1.minute, Equals(dt2.minute))
 
437
        self.assertThat(dt1.second, Equals(dt2.second))
 
438
        self.assertThat(dt1.timestamp, Equals(dt2.timestamp()))
317
439
 
318
440
    def test_equality_with_datetime(self):
319
 
        dt1 = DateTime(1377209927)
320
 
        dt2 = DateTime(1377209927)
 
441
        self.skip_if_timestamp_too_large(self.timestamp)
 
442
        self.useFixture(Timezone(self.timezone))
 
443
 
 
444
        dt1 = DateTime(self.timestamp)
 
445
        dt2 = datetime(
 
446
            dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, dt1.second
 
447
        )
321
448
 
322
449
        self.assertThat(dt1, Equals(dt2))
323
450
 
324
451
    def test_equality_with_list(self):
325
 
        dt1 = DateTime(1377209927)
326
 
        dt2 = [1377209927]
 
452
        self.skip_if_timestamp_too_large(self.timestamp)
 
453
        self.useFixture(Timezone(self.timezone))
 
454
 
 
455
        dt1 = DateTime(self.timestamp)
 
456
        dt2 = [self.timestamp]
327
457
 
328
458
        self.assertThat(dt1, Equals(dt2))
329
459
 
330
 
    def test_equality_with_datetime_timestamp(self):
331
 
        # DateTime no longer assumes UTC and uses local TZ.
332
 
        dt1 = DateTime(1377209927)
333
 
        dt2 = datetime.fromtimestamp(1377209927)
334
 
        dt3 = datetime.fromtimestamp(1377209928)
 
460
    def test_equality_with_datetime_object(self):
 
461
        self.skip_if_timestamp_too_large(self.timestamp)
 
462
        self.useFixture(Timezone(self.timezone))
 
463
 
 
464
        dt1 = DateTime(self.timestamp)
 
465
        dt2 = datetime.fromtimestamp(self.timestamp, tz.gettz())
 
466
        dt3 = datetime.fromtimestamp(self.timestamp + 1, tz.gettz())
335
467
 
336
468
        self.assertThat(dt1, Equals(dt2))
337
469
        self.assertThat(dt1, NotEquals(dt3))
338
470
 
339
471
    def test_can_convert_to_datetime(self):
340
 
        dt1 = DateTime(1377209927)
 
472
        self.skip_if_timestamp_too_large(self.timestamp)
341
473
 
 
474
        dt1 = DateTime(self.timestamp)
342
475
        self.assertThat(dt1.datetime, IsInstance(datetime))
343
476
 
344
 
    def test_repr(self):
345
 
        expected = repr_type(
346
 
            u"DateTime({:%Y-%m-%d %H:%M:%S})".format(
347
 
                datetime.fromtimestamp(1377209927)
348
 
            )
349
 
        )
350
 
        dt = DateTime(1377209927)
351
 
        observed = repr(dt)
352
 
        self.assertEqual(expected, observed)
353
 
 
354
 
    def test_repr_equals_str(self):
355
 
        dt = DateTime(1377209927)
356
 
        self.assertEqual(repr(dt), str(dt))
357
 
 
358
477
 
359
478
class TimeTests(TestCase):
360
479