blob: 815af4be487bdd7eb96e913d8b62e0490454942a [file] [log] [blame]
xixuan82753172017-08-07 09:22:50 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for datastore unittests."""
6
7import datetime
8import unittest
9
10import datastore_client
11
12from google.appengine.api import memcache
13from google.appengine.ext import ndb
14from google.appengine.ext import testbed
15import pytz
16
17
18class DatastoreTestCase(unittest.TestCase):
19
20 def setUp(self):
21 self.testbed = testbed.Testbed()
22 self.testbed.activate()
23 self.addCleanup(self.testbed.deactivate)
24 self.testbed.init_datastore_v3_stub()
25 self.testbed.init_memcache_stub()
26 ndb.get_context().clear_cache()
27
28 def testSetLastExec(self):
29 last_exec_store = datastore_client.LastExecutionRecordStore()
30 exec_time = datetime.datetime(2017, 8, 1, 0)
31 last_exec_store.set_last_execute_time('nightly', exec_time)
32 self.assertEqual(last_exec_store.get_last_execute_time('nightly'),
33 exec_time.replace(tzinfo=pytz.utc))
34
35 def testDeleteLastExec(self):
36 last_exec_store = datastore_client.LastExecutionRecordStore()
37 exec_time = datetime.datetime(2017, 8, 1, 0)
38 last_exec_store.set_last_execute_time('nightly', exec_time)
39 last_exec_store.del_last_execute_time('nightly')
40 self.assertEqual(last_exec_store.get_last_execute_time('nightly'),
41 None)
42
43 def testDeleteLastExecWithNonExistentKey(self):
44 last_exec_store = datastore_client.LastExecutionRecordStore()
45 exec_time = datetime.datetime(2017, 8, 1, 0)
46 last_exec_store.del_last_execute_time('nightly')
47
48 def testGetAllLastExec(self):
49 last_exec_store = datastore_client.LastExecutionRecordStore()
50 exec_time = datetime.datetime(2017, 8, 1, 0)
51 event_types = ['nightly', 'weekly']
52 exec_time_records = [datastore_client.LastExecutionRecord(
53 key=ndb.Key(datastore_client.LastExecutionRecord, e),
54 event_type=e,
55 exec_time=exec_time.replace(tzinfo=pytz.utc)) for e in event_types]
56
57 for e in event_types:
58 last_exec_store.set_last_execute_time(e, exec_time)
59
60 last_exec_times = last_exec_store.get_all()
61 self.assertEqual(last_exec_times, exec_time_records)
62
63 def testDeleteAllLastExec(self):
64 last_exec_store = datastore_client.LastExecutionRecordStore()
65 exec_time = datetime.datetime(2017, 8, 1, 0)
66 event_types = ['nightly', 'weekly']
67
68 for e in event_types:
69 last_exec_store.set_last_execute_time(e, exec_time)
70
71 last_exec_store.delete_all()
72 for e in event_types:
73 self.assertEqual(last_exec_store.get_last_execute_time(e), None)
74
75if __name__ == '__main__':
76 unittest.main()