12822198d5cc4de1de86e4a09c1b873e9dc733c3
[WebKit-https.git] / Websites / webkit-perf.appspot.com / models_unittest.py
1 #!/usr/bin/env python
2 # Copyright (C) 2012 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import models
31 import unittest
32
33 from datetime import datetime
34 from google.appengine.api import memcache
35 from google.appengine.ext import testbed
36
37
38 class HelperTests(unittest.TestCase):
39     def setUp(self):
40         self.testbed = testbed.Testbed()
41         self.testbed.activate()
42         self.testbed.init_datastore_v3_stub()
43
44     def tearDown(self):
45         self.testbed.deactivate()
46
47     def _assert_there_is_exactly_one_id_holder_and_matches(self, id):
48         id_holders = models.NumericIdHolder.all().fetch(5)
49         self.assertEqual(len(id_holders), 1)
50         self.assertTrue(id_holders[0])
51         self.assertEqual(id_holders[0].key().id(), id)
52
53     def test_create_in_transaction_with_numeric_id_holder(self):
54
55         def execute(id):
56             return models.Branch(id=id, name='some branch', key_name='some-branch').put()
57
58         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
59         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
60
61         self.assertTrue(models.create_in_transaction_with_numeric_id_holder(execute))
62
63         branches = models.Branch.all().fetch(5)
64         self.assertEqual(len(branches), 1)
65         self.assertEqual(branches[0].name, 'some branch')
66         self.assertEqual(branches[0].key().name(), 'some-branch')
67
68         self._assert_there_is_exactly_one_id_holder_and_matches(branches[0].id)
69
70     def test_failing_in_create_in_transaction_with_numeric_id_holder(self):
71
72         def execute(id):
73             return None
74
75         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
76         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
77
78         self.assertFalse(models.create_in_transaction_with_numeric_id_holder(execute))
79
80         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
81         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
82
83     def test_raising_in_create_in_transaction_with_numeric_id_holder(self):
84
85         def execute(id):
86             raise TypeError
87             return None
88
89         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
90         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
91
92         self.assertRaises(TypeError, models.create_in_transaction_with_numeric_id_holder, (execute))
93
94         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
95         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
96
97     def test_delete_model_with_numeric_id_holder(self):
98
99         def execute(id):
100             return models.Branch(id=id, name='some branch', key_name='some-branch').put()
101
102         branch = models.Branch.get(models.create_in_transaction_with_numeric_id_holder(execute))
103         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 1)
104
105         models.delete_model_with_numeric_id_holder(branch)
106
107         self.assertEqual(len(models.Branch.all().fetch(5)), 0)
108         self.assertEqual(len(models.NumericIdHolder.all().fetch(5)), 0)
109
110     def test_model_from_numeric_id(self):
111
112         def execute(id):
113             return models.Branch(id=id, name='some branch', key_name='some-branch').put()
114
115         branch = models.Branch.get(models.create_in_transaction_with_numeric_id_holder(execute))
116
117         self.assertEqual(models.model_from_numeric_id(branch.id, models.Branch).key(), branch.key())
118         self.assertEqual(models.model_from_numeric_id(branch.id + 1, models.Branch), None)
119         models.delete_model_with_numeric_id_holder(branch)
120         self.assertEqual(models.model_from_numeric_id(branch.id, models.Branch), None)
121
122
123 class BuilderTests(unittest.TestCase):
124     def setUp(self):
125         self.testbed = testbed.Testbed()
126         self.testbed.activate()
127         self.testbed.init_datastore_v3_stub()
128
129     def tearDown(self):
130         self.testbed.deactivate()
131
132     def test_create(self):
133         builder_key = models.Builder.create('some builder', 'some password')
134         self.assertTrue(builder_key)
135         builder = models.Builder.get(builder_key)
136         self.assertEqual(builder.key().name(), 'some builder')
137         self.assertEqual(builder.name, 'some builder')
138         self.assertEqual(builder.password, models.Builder._hashed_password('some password'))
139
140     def test_update_password(self):
141         builder = models.Builder.get(models.Builder.create('some builder', 'some password'))
142         self.assertEqual(builder.password, models.Builder._hashed_password('some password'))
143         builder.update_password('other password')
144         self.assertEqual(builder.password, models.Builder._hashed_password('other password'))
145
146         # Make sure it's saved
147         builder = models.Builder.get(builder.key())
148         self.assertEqual(builder.password, models.Builder._hashed_password('other password'))
149
150     def test_hashed_password(self):
151         self.assertNotEqual(models.Builder._hashed_password('some password'), 'some password')
152         self.assertFalse('some password' in models.Builder._hashed_password('some password'))
153         self.assertEqual(len(models.Builder._hashed_password('some password')), 64)
154
155     def test_authenticate(self):
156         builder = models.Builder.get(models.Builder.create('some builder', 'some password'))
157         self.assertTrue(builder.authenticate('some password'))
158         self.assertFalse(builder.authenticate('bad password'))
159
160
161 class ReportLog(unittest.TestCase):
162     def setUp(self):
163         self.testbed = testbed.Testbed()
164         self.testbed.activate()
165         self.testbed.init_datastore_v3_stub()
166
167     def tearDown(self):
168         self.testbed.deactivate()
169
170     def _create_log_with_payload(self, payload):
171         return models.ReportLog(timestamp=datetime.now(), headers='some headers', payload=payload)
172
173     def test_parsed_payload(self):
174         log = self._create_log_with_payload('')
175         self.assertFalse('_parsed' in log.__dict__)
176         self.assertEqual(log._parsed_payload(), False)
177         self.assertEqual(log._parsed, False)
178
179         log = self._create_log_with_payload('{"key": "value", "another key": 1}')
180         self.assertEqual(log._parsed_payload(), {"key": "value", "another key": 1})
181         self.assertEqual(log._parsed, {"key": "value", "another key": 1})
182
183     def test_get_value(self):
184         log = self._create_log_with_payload('{"string": "value", "integer": 1, "float": 1.1}')
185         self.assertEqual(log.get_value('string'), 'value')
186         self.assertEqual(log.get_value('integer'), 1)
187         self.assertEqual(log.get_value('float'), 1.1)
188         self.assertEqual(log.get_value('bad'), None)
189
190     def test_results(self):
191         log = self._create_log_with_payload('{"results": 123}')
192         self.assertEqual(log.results(), 123)
193
194         log = self._create_log_with_payload('{"key": "value"}')
195         self.assertEqual(log.results(), None)
196
197     def test_builder(self):
198         log = self._create_log_with_payload('{"key": "value"}')
199         self.assertEqual(log.builder(), None)
200
201         builder_name = "Chromium Mac Release (Perf)"
202         log = self._create_log_with_payload('{"builder-name": "%s"}' % builder_name)
203         self.assertEqual(log.builder(), None)
204
205         builder_key = models.Builder.create(builder_name, 'some password')
206         log = self._create_log_with_payload('{"builder-name": "%s"}' % builder_name)
207         self.assertEqual(log.builder().key(), builder_key)
208
209     # FIXME test_branch and test_platform
210
211     def test_build_number(self):
212         log = self._create_log_with_payload('{"build-number": 123}')
213         self.assertEqual(log.build_number(), 123)
214
215         log = self._create_log_with_payload('{"key": "value"}')
216         self.assertEqual(log.build_number(), None)
217
218     def test_webkit_revision(self):
219         log = self._create_log_with_payload('{"key": "value"}')
220         self.assertEqual(log.webkit_revision(), None)
221
222         log = self._create_log_with_payload('{"webkit-revision": 123}')
223         self.assertEqual(log.webkit_revision(), 123)
224
225     def chromium_revision(self):
226         log = self._create_log_with_payload('{"chromium-revision": 123}')
227         self.assertEqual(log.webkit_revision(), 123)
228
229         log = self._create_log_with_payload('{"key": "value"}')
230         self.assertEqual(log.webkit_revision(), None)
231
232
233 class PersistentCacheTests(unittest.TestCase):
234     def setUp(self):
235         self.testbed = testbed.Testbed()
236         self.testbed.activate()
237         self.testbed.init_datastore_v3_stub()
238         self.testbed.init_memcache_stub()
239
240     def tearDown(self):
241         self.testbed.deactivate()
242
243     def _assert_persistent_cache(self, name, value):
244         self.assertEqual(models.PersistentCache.get_by_key_name(name).value, value)
245         self.assertEqual(memcache.get(name), value)
246
247     def test_set(self):
248         self.assertEqual(len(models.PersistentCache.all().fetch(5)), 0)
249
250         models.PersistentCache.set_cache('some-cache', 'some data')
251         self._assert_persistent_cache('some-cache', 'some data')
252
253         models.PersistentCache.set_cache('some-cache', 'some other data')
254
255         self._assert_persistent_cache('some-cache', 'some other data')
256
257     def test_get(self):
258         self.assertEqual(memcache.get('some-cache'), None)
259         self.assertEqual(models.PersistentCache.get_cache('some-cache'), None)
260
261         models.PersistentCache.set_cache('some-cache', 'some data')
262
263         self.assertEqual(memcache.get('some-cache'), 'some data')
264         self.assertEqual(models.PersistentCache.get_cache('some-cache'), 'some data')
265
266         memcache.delete('some-cache')
267         self.assertEqual(memcache.get('some-cache'), None)
268         self.assertEqual(models.PersistentCache.get_cache('some-cache'), 'some data')
269
270
271 if __name__ == '__main__':
272     unittest.main()