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