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