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 # Usage: PYTHON_PATH=/path/to/appengine_sdk python loghandler_unittest.py. 30 31 import dev_appserver 32 dev_appserver.fix_sys_path() 33 34 import unittest 35 import webapp2 36 37 from google.appengine.ext import testbed 38 39 import main 40 41 42 class TestHandlers(unittest.TestCase): 43 def setUp(self): 44 self.testbed = testbed.Testbed() 45 self.testbed.activate() 46 self.testbed.init_datastore_v3_stub() 47 self.testbed.init_memcache_stub() 48 49 def test_update_log(self): 50 request = webapp2.Request.blank('/updatelog') 51 request.method = 'POST' 52 request.POST[main.LOG_PARAM] = 'data to log' 53 request.POST[main.NEW_ENTRY_PARAM] = 'on' 54 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off' 55 56 response = request.get_response(main.app) 57 self.assertEqual(response.status_int, 200) 58 self.assertEqual(response.body, 'Wrote new log entry.') 59 60 response = request.get_response(main.app) 61 self.assertEqual(response.status_int, 200) 62 self.assertEqual(response.body, 'Wrote new log entry.') 63 64 request = webapp2.Request.blank('/updatelog') 65 request.method = 'POST' 66 request.POST[main.LOG_PARAM] = 'data to log' 67 request.POST[main.NEW_ENTRY_PARAM] = 'off' 68 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off' 69 70 response = request.get_response(main.app) 71 self.assertEqual(response.status_int, 200) 72 self.assertEqual(response.body, 'Added to existing log entry.') 73 74 request = webapp2.Request.blank('/updatelog') 75 request.method = 'POST' 76 request.POST[main.LOG_PARAM] = 'data to log' 77 request.POST[main.NEW_ENTRY_PARAM] = 'off' 78 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'on' 79 80 response = request.get_response(main.app) 81 self.assertEqual(response.status_int, 200) 82 self.assertEqual(response.body, 'Wrote new no needs rebaseline log.') 83 84 response = request.get_response(main.app) 85 self.assertEqual(response.status_int, 200) 86 self.assertEqual(response.body, 'Overwrote existing no needs rebaseline log.') 87 88 request = webapp2.Request.blank('/updatelog') 89 request.method = 'POST' 90 request.POST[main.LOG_PARAM] = 'data to log' 91 request.POST[main.NEW_ENTRY_PARAM] = 'off' 92 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off' 93 94 response = request.get_response(main.app) 95 self.assertEqual(response.status_int, 200) 96 self.assertEqual(response.body, 'Previous entry was a no need rebaseline log. Writing a new log.') 97 98 def test_update_log_first_entry_without_new_entry_param(self): 99 request = webapp2.Request.blank('/updatelog') 100 request.method = 'POST' 101 request.POST[main.LOG_PARAM] = 'data to log' 102 request.POST[main.NEW_ENTRY_PARAM] = 'off' 103 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'off' 104 105 response = request.get_response(main.app) 106 self.assertEqual(response.status_int, 200) 107 self.assertEqual(response.body, 'Wrote new log entry.') 108 109 def test_update_log_first_entry_no_needs_rebaseline_param(self): 110 request = webapp2.Request.blank('/updatelog') 111 request.method = 'POST' 112 request.POST[main.LOG_PARAM] = 'data to log' 113 request.POST[main.NEW_ENTRY_PARAM] = 'off' 114 request.POST[main.NO_NEEDS_REBASELINE_PARAM] = 'on' 115 116 response = request.get_response(main.app) 117 self.assertEqual(response.status_int, 200) 118 self.assertEqual(response.body, 'Wrote new log entry.') 119 120 121 if __name__ == '__main__': 122 unittest.main() 123