1 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 # This file is expected to be used under another directory to use, 6 # so we disable checking import path of GAE tools from this directory. 7 # pylint: disable=F0401,E0611 8 9 import json 10 import unittest 11 12 from google.appengine.api import files 13 from google.appengine.ext import ndb 14 from google.appengine.ext import testbed 15 from google.appengine.ext.blobstore import BlobInfo 16 17 import services 18 19 20 class ServicesTest(unittest.TestCase): 21 @staticmethod 22 def CreateBlob(path): 23 # Initialize blob dictionary to return. 24 blob = {} 25 26 # Read sample file. 27 blob['json_str'] = open(path, 'r').read() 28 29 # Create file in blobstore according to sample file. 30 file_name = files.blobstore.create(mime_type='text/plain') 31 with files.open(file_name, 'a') as f: 32 f.write(blob['json_str']) 33 files.finalize(file_name) 34 35 # Get BlobInfo of sample file. 36 blob['blob_info'] = BlobInfo.get(files.blobstore.get_blob_key(file_name)) 37 38 return blob 39 40 def setUp(self): 41 self.testbed = testbed.Testbed() 42 self.testbed.activate() 43 self.testbed.init_all_stubs() 44 45 # Read sample file. 46 self.correct_blob = ServicesTest.CreateBlob('testdata/sample.json') 47 self.error_blob = ServicesTest.CreateBlob('testdata/error_sample.json') 48 49 def tearDown(self): 50 self.testbed.deactivate() 51 52 def testProfiler(self): 53 correct_blob = self.correct_blob 54 # Call services function to create Profiler entity. 55 run_id = services.CreateProfiler(correct_blob['blob_info']) 56 57 # Test GetProfiler 58 self.assertEqual(services.GetProfiler(run_id), correct_blob['json_str']) 59 60 # Create Profiler entity with the same file again and check uniqueness. 61 services.CreateProfiler(correct_blob['blob_info']) 62 self.assertEqual(services.Profiler.query().count(), 1) 63 64 def testTemplate(self): 65 correct_blob = self.correct_blob 66 # Call services function to create template entities. 67 services.CreateTemplates(correct_blob['blob_info']) 68 69 # Test templates being stored in database correctly. 70 json_obj = json.loads(correct_blob['json_str']) 71 for content in json_obj['templates'].values(): 72 template_entity = ndb.Key('Template', json.dumps(content)).get() 73 self.assertEqual(template_entity.content, content) 74 75 # Create template entities with the same file again and check uniqueness. 76 services.CreateTemplates(correct_blob['blob_info']) 77 self.assertEqual(services.Template.query().count(), 2) 78 79 def testErrorBlob(self): 80 error_blob = self.error_blob 81 # Test None when default template not indicated or found in templates. 82 dflt_tmpl = services.CreateTemplates(error_blob['blob_info']) 83 self.assertIsNone(dflt_tmpl) 84