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.
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
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
20 class ServicesTest(unittest
.TestCase
):
23 # Initialize blob dictionary to return.
27 blob
['json_str'] = open(path
, 'r').read()
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
)
35 # Get BlobInfo of sample file.
36 blob
['blob_info'] = BlobInfo
.get(files
.blobstore
.get_blob_key(file_name
))
41 self
.testbed
= testbed
.Testbed()
42 self
.testbed
.activate()
43 self
.testbed
.init_all_stubs()
46 self
.correct_blob
= ServicesTest
.CreateBlob('testdata/sample.json')
47 self
.error_blob
= ServicesTest
.CreateBlob('testdata/error_sample.json')
50 self
.testbed
.deactivate()
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'])
58 self
.assertEqual(services
.GetProfiler(run_id
), correct_blob
['json_str'])
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)
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'])
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
)
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)
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
)