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 29import unittest 30 31import datastorefile 32 33from google.appengine.ext import db 34from google.appengine.ext import testbed 35 36 37class 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 testSaveData(self): 61 self.assertFalse(self.test_file.save_data(None)) 62 63 too_big_data = 'x' * (datastorefile.MAX_DATA_ENTRY_PER_FILE * datastorefile.MAX_ENTRY_LEN + 1) 64 self.assertFalse(self.test_file.save_data(too_big_data)) 65 66 test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 5 67 self.assertTrue(self.test_file.save_data(test_data)) 68 nchunks = datastorefile.DataEntry.all().count() 69 nkeys = len(self.test_file.data_keys) + len(self.test_file.new_data_keys) 70 self.assertEqual(nkeys, nchunks) 71 72 def testSaveDataKeyReuse(self): 73 test_data = 'x' * datastorefile.MAX_ENTRY_LEN * 5 74 self.assertTrue(self.test_file.save_data(test_data)) 75 nchunks = datastorefile.DataEntry.all().count() 76 nkeys = len(self.test_file.data_keys) + len(self.test_file.new_data_keys) 77 self.assertEqual(nkeys, nchunks) 78 79 smaller_data = 'x' * datastorefile.MAX_ENTRY_LEN * 3 80 self.assertTrue(self.test_file.save_data(smaller_data)) 81 nchunks = datastorefile.DataEntry.all().count() 82 nkeys_before = len(self.test_file.data_keys) + len(self.test_file.new_data_keys) 83 self.assertEqual(nkeys_before, nchunks) 84 85 self.assertTrue(self.test_file.save_data(smaller_data)) 86 nchunks = datastorefile.DataEntry.all().count() 87 nkeys_after = len(self.test_file.data_keys) + len(self.test_file.new_data_keys) 88 self.assertEqual(nkeys_after, nchunks) 89 self.assertNotEqual(nkeys_before, nkeys_after) 90 91 def testGetChunkIndices(self): 92 data_length = datastorefile.MAX_ENTRY_LEN * 3 93 chunk_indices = self.test_file._get_chunk_indices(data_length) 94 self.assertEqual(len(chunk_indices), 3) 95 self.assertNotEqual(chunk_indices[0], chunk_indices[-1]) 96 97 data_length += 1 98 chunk_indices = self.test_file._get_chunk_indices(data_length) 99 self.assertEqual(len(chunk_indices), 4) 100 101 102if __name__ == '__main__': 103 unittest.main() 104