Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / TestResultServer / model / datastorefile_test.py
1 # Copyright (C) 2013 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #     * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #     * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #     * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 import unittest
30
31 import datastorefile
32
33 from google.appengine.ext import db
34 from google.appengine.ext import testbed
35
36
37 class DataStoreFileTest(unittest.TestCase):
38     def setUp(self):
39         self.testbed = testbed.Testbed()
40         self.testbed.activate()
41         self.testbed.init_datastore_v3_stub()
42
43         self.test_file = datastorefile.DataStoreFile()
44
45     def tearDown(self):
46         self.testbed.deactivate()
47
48     def testSaveLoadDeleteData(self):
49         test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3
50
51         self.assertTrue(self.test_file.save_data(test_data))
52         self.assertEqual(test_data, self.test_file.data)
53
54         self.assertEqual(test_data, self.test_file.load_data())
55         self.assertEqual(test_data, self.test_file.data)
56
57         self.test_file.delete_data()
58         self.assertFalse(self.test_file.load_data())
59
60     def testLoadDataInvalidKey(self):
61         test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3
62
63         self.assertTrue(self.test_file.save_data(test_data))
64         self.assertEqual(test_data, self.test_file.data)
65
66         self.test_file.delete_data()
67         self.assertEqual('', self.test_file.load_data())
68
69     def testLoadDataNoKeys(self):
70         # This should never happen.
71         self.assertEqual(None, self.test_file.load_data())
72
73     def testSaveData(self):
74         self.assertFalse(self.test_file.save_data(None))
75
76         too_big_data = 'x' * (datastorefile.MAX_DATA_ENTRY_PER_FILE * datastorefile.MAX_ENTRY_LEN + 1)
77         self.assertFalse(self.test_file.save_data(too_big_data))
78
79         test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 5
80         self.assertTrue(self.test_file.save_data(test_data))
81         nchunks = datastorefile.DataEntry.all().count()
82         nkeys = len(self.test_file.data_keys) + len(self.test_file.new_data_keys)
83         self.assertEqual(nkeys, nchunks)
84
85     def testSaveDataKeyReuse(self):
86         test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 5
87         self.assertTrue(self.test_file.save_data(test_data))
88         nchunks = datastorefile.DataEntry.all().count()
89         nkeys = len(self.test_file.data_keys) + len(self.test_file.new_data_keys)
90         self.assertEqual(nkeys, nchunks)
91
92         smaller_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3
93         self.assertTrue(self.test_file.save_data(smaller_data))
94         nchunks = datastorefile.DataEntry.all().count()
95         nkeys_before = len(self.test_file.data_keys) + len(self.test_file.new_data_keys)
96         self.assertEqual(nkeys_before, nchunks)
97
98         self.assertTrue(self.test_file.save_data(smaller_data))
99         nchunks = datastorefile.DataEntry.all().count()
100         nkeys_after = len(self.test_file.data_keys) + len(self.test_file.new_data_keys)
101         self.assertEqual(nkeys_after, nchunks)
102         self.assertNotEqual(nkeys_before, nkeys_after)
103
104     def testGetChunkIndices(self):
105         data_length = datastorefile.MAX_ENTRY_LEN * 3
106         chunk_indices = self.test_file._get_chunk_indices(data_length)
107         self.assertEqual(len(chunk_indices), 3)
108         self.assertNotEqual(chunk_indices[0], chunk_indices[-1])
109
110         data_length += 1
111         chunk_indices = self.test_file._get_chunk_indices(data_length)
112         self.assertEqual(len(chunk_indices), 4)
113
114
115 if __name__ == '__main__':
116     unittest.main()