Home | History | Annotate | Download | only in model
      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 datastorefile
     30 import time
     31 import testfile
     32 import unittest
     33 
     34 from datetime import datetime
     35 
     36 from google.appengine.datastore import datastore_stub_util
     37 from google.appengine.ext import db
     38 from google.appengine.ext import testbed
     39 
     40 TEST_DATA = [
     41     # master, builder, test_type, name, data; order matters.
     42     ['ChromiumWebKit', 'WebKit Linux', 'layout-tests', 'webkit_linux_results.json', 'a'],
     43     ['ChromiumWebKit', 'WebKit Win7', 'layout-tests', 'webkit_win7_results.json', 'b'],
     44     ['ChromiumWin', 'Win7 (Dbg)', 'unittests', 'win7_dbg_unittests.json', 'c'],
     45 ]
     46 
     47 
     48 class DataStoreFileTest(unittest.TestCase):
     49     def setUp(self):
     50         self.testbed = testbed.Testbed()
     51         self.testbed.activate()
     52 
     53         self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
     54         self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
     55 
     56         test_file = testfile.TestFile()
     57 
     58     def _getAllFiles(self):
     59         return testfile.TestFile.get_files(None, None, None, None, limit=None)
     60 
     61     def _assertFileMatchesData(self, expected_data, actual_file):
     62         actual_fields = [actual_file.master, actual_file.builder, actual_file.test_type, actual_file.name, actual_file.data]
     63         self.assertEqual(expected_data, actual_fields, 'Mismatch between expected fields in file and actual file.')
     64 
     65     def _addFileAndAssert(self, file_data):
     66         _, code = testfile.TestFile.add_file(*file_data)
     67         self.assertEqual(200, code, 'Unable to create file with data: %s' % file_data)
     68 
     69     def testSaveFile(self):
     70         file_data = TEST_DATA[0][:]
     71         self._addFileAndAssert(file_data)
     72 
     73         files = self._getAllFiles()
     74         self.assertEqual(1, len(files))
     75         self._assertFileMatchesData(file_data, files[0])
     76 
     77         _, code = testfile.TestFile.save_file(files[0], None)
     78         self.assertEqual(500, code, 'Expected empty file not to have been saved.')
     79 
     80         files = self._getAllFiles()
     81         self.assertEqual(1, len(files), 'Expected exactly one file to be present.')
     82         self._assertFileMatchesData(file_data, files[0])
     83 
     84     def testAddAndGetFile(self):
     85         for file_data in TEST_DATA:
     86             self._addFileAndAssert(file_data)
     87 
     88         files = self._getAllFiles()
     89         self.assertEqual(len(TEST_DATA), len(files), 'Mismatch between number of test records and number of files in db.')
     90 
     91         for f in files:
     92             fields = [f.master, f.builder, f.test_type, f.name, f.data]
     93             self.assertIn(fields, TEST_DATA)
     94 
     95     def testOverwriteOrAddFile(self):
     96         file_data = TEST_DATA[0][:]
     97         _, code = testfile.TestFile.overwrite_or_add_file(*file_data)
     98         self.assertEqual(200, code, 'Unable to create file with data: %s' % file_data)
     99         files = self._getAllFiles()
    100         self.assertEqual(1, len(files))
    101 
    102         _, code = testfile.TestFile.overwrite_or_add_file(*file_data)
    103         self.assertEqual(200, code, 'Unable to overwrite or create file with data: %s' % file_data)
    104         files = self._getAllFiles()
    105         self.assertEqual(1, len(files))
    106 
    107         file_data = TEST_DATA[1][:]
    108         _, code = testfile.TestFile.overwrite_or_add_file(*file_data)
    109         self.assertEqual(200, code, 'Unable to overwrite or create file with different data: %s' % file_data)
    110         files = self._getAllFiles()
    111         self.assertEqual(2, len(files))
    112 
    113     def testDeleteFile(self):
    114         file_contents = 'x' * datastorefile.MAX_ENTRY_LEN * 2
    115         file_data = ['ChromiumWebKit', 'WebKit Linux', 'layout-tests', 'results.json', file_contents]
    116         self._addFileAndAssert(file_data)
    117 
    118         ndeleted = testfile.TestFile.delete_file(None, 'ChromiumWebKit', 'WebKit Linux', 'layout-tests', 'results.json', None, None)
    119         self.assertEqual(1, ndeleted, 'Expected exactly one file to have been deleted.')
    120 
    121         nfiles = testfile.TestFile.all().count()
    122         self.assertEqual(0, nfiles, 'Expected exactly zero files to be present in db.')
    123 
    124     def testDeleteAll(self):
    125         for file_data in TEST_DATA:
    126             self._addFileAndAssert(file_data)
    127 
    128         files = self._getAllFiles()
    129         self.assertEqual(len(TEST_DATA), len(files))
    130 
    131         files[0]._delete_all()
    132 
    133         files = self._getAllFiles()
    134         self.assertEqual(len(TEST_DATA) - 1, len(files))
    135 
    136 
    137 if __name__ == '__main__':
    138     unittest.main()
    139