Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 import json | |
| 6 import services | |
|
Dai Mikurube (NOT FULLTIME)
2013/09/24 07:22:11
Better to split standard libraries and your librar
junjianx
2013/09/24 08:11:21
Done.
| |
| 7 import unittest | |
| 8 | |
| 9 from google.appengine.api import files | |
| 10 from google.appengine.ext import ndb | |
| 11 from google.appengine.ext import testbed | |
| 12 from google.appengine.ext.blobstore import BlobInfo | |
| 13 | |
| 14 | |
| 15 class ServicesTest(unittest.TestCase): | |
| 16 @staticmethod | |
| 17 def CreateBlob(path): | |
| 18 # Initialize blob dictionary to return. | |
| 19 blob = {} | |
| 20 | |
| 21 # Read sample file. | |
| 22 blob['json_str'] = open(path, 'r').read() | |
| 23 | |
| 24 # Create file in blobstore according to sample file. | |
| 25 file_name = files.blobstore.create(mime_type='text/plain') | |
| 26 with files.open(file_name, 'a') as f: | |
| 27 f.write(blob['json_str']) | |
| 28 files.finalize(file_name) | |
| 29 | |
| 30 # Get BlobInfo of sample file. | |
| 31 blob['blob_info'] = BlobInfo.get(files.blobstore.get_blob_key(file_name)) | |
| 32 | |
| 33 return blob | |
| 34 | |
| 35 def setUp(self): | |
| 36 self.testbed = testbed.Testbed() | |
| 37 self.testbed.activate() | |
| 38 self.testbed.init_all_stubs() | |
| 39 | |
| 40 # Read sample file. | |
| 41 self.correct_blob = ServicesTest.CreateBlob('testdata/sample.json') | |
| 42 self.error_blob = ServicesTest.CreateBlob('testdata/error_sample.json') | |
| 43 | |
| 44 def tearDown(self): | |
| 45 self.testbed.deactivate() | |
| 46 | |
| 47 def testProfiler(self): | |
| 48 correct_blob = self.correct_blob | |
| 49 # Call services function to create Profiler entity. | |
| 50 run_id = services.CreateProfiler(correct_blob['blob_info']) | |
| 51 | |
| 52 # Test GetProfiler | |
| 53 self.assertEqual(services.GetProfiler(run_id), correct_blob['json_str']) | |
| 54 | |
| 55 # Create Profiler entity with the same file again and check uniqueness. | |
| 56 services.CreateProfiler(correct_blob['blob_info']) | |
| 57 self.assertEqual(services.Profiler.query().count(), 1) | |
| 58 | |
| 59 def testTemplate(self): | |
| 60 correct_blob = self.correct_blob | |
| 61 # Call services function to create template entities. | |
| 62 services.CreateTemplates(correct_blob['blob_info']) | |
| 63 | |
| 64 # Test templates being stored in database correctly. | |
| 65 json_obj = json.loads(correct_blob['json_str']) | |
| 66 for content in json_obj['templates'].values(): | |
| 67 template_entity = ndb.Key('Template', json.dumps(content)).get() | |
| 68 self.assertEqual(template_entity.content, content) | |
| 69 | |
| 70 # Create template entities with the same file again and check uniqueness. | |
| 71 services.CreateTemplates(correct_blob['blob_info']) | |
| 72 self.assertEqual(services.Template.query().count(), 2) | |
| 73 | |
| 74 def testErrorBlob(self): | |
| 75 error_blob = self.error_blob | |
| 76 # Test None when default template not indicated or found in templates. | |
| 77 dflt_tmpl = services.CreateTemplates(error_blob['blob_info']) | |
| 78 self.assertIsNone(dflt_tmpl) | |
| OLD | NEW |