1 # Copyright (C) 2013 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
33 from google.appengine.ext import db
34 from google.appengine.ext import testbed
37 class DataStoreFileTest(unittest.TestCase):
39 self.testbed = testbed.Testbed()
40 self.testbed.activate()
41 self.testbed.init_datastore_v3_stub()
43 self.test_file = datastorefile.DataStoreFile()
46 self.testbed.deactivate()
48 def testSaveLoadDeleteData(self):
49 test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3
51 self.assertTrue(self.test_file.save_data(test_data))
52 self.assertEqual(test_data, self.test_file.data)
54 self.assertEqual(test_data, self.test_file.load_data())
55 self.assertEqual(test_data, self.test_file.data)
57 self.test_file.delete_data()
58 self.assertFalse(self.test_file.load_data())
60 def testLoadDataInvalidKey(self):
61 test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3
63 self.assertTrue(self.test_file.save_data(test_data))
64 self.assertEqual(test_data, self.test_file.data)
66 self.test_file.delete_data()
67 self.assertEqual('', self.test_file.load_data())
69 def testLoadDataNoKeys(self):
70 # This should never happen.
71 self.assertEqual(None, self.test_file.load_data())
73 def testSaveData(self):
74 self.assertFalse(self.test_file.save_data(None))
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))
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)
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)
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)
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)
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])
111 chunk_indices = self.test_file._get_chunk_indices(data_length)
112 self.assertEqual(len(chunk_indices), 4)
115 if __name__ == '__main__':