visualization_test.py 2.39 KB
Newer Older
Ivan Bogatyy's avatar
Ivan Bogatyy committed
1
# -*- coding: utf-8 -*-
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

Ivan Bogatyy's avatar
Ivan Bogatyy committed
17
18
19
20
21
22
23
"""Tests for dragnn.python.visualization."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.platform import googletest
Ivan Bogatyy's avatar
Ivan Bogatyy committed
24
from dragnn.protos import spec_pb2
Ivan Bogatyy's avatar
Ivan Bogatyy committed
25
26
27
28
29
30
31
32
33
34
from dragnn.protos import trace_pb2
from dragnn.python import visualization


def _get_trace_proto_string():
  trace = trace_pb2.MasterTrace()
  trace.component_trace.add(
      step_trace=[
          trace_pb2.ComponentStepTrace(fixed_feature_trace=[]),
      ],
Ivan Bogatyy's avatar
Ivan Bogatyy committed
35
36
      # Google Translate says this is "component" in Chinese. (To test UTF-8).
      name='零件',)
Ivan Bogatyy's avatar
Ivan Bogatyy committed
37
38
39
  return trace.SerializeToString()


Ivan Bogatyy's avatar
Ivan Bogatyy committed
40
41
42
43
44
def _get_master_spec():
  return spec_pb2.MasterSpec(
      component=[spec_pb2.ComponentSpec(name='jalapeño')])


Ivan Bogatyy's avatar
Ivan Bogatyy committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class VisualizationTest(googletest.TestCase):

  def testCanFindScript(self):
    script = visualization._load_viz_script()
    self.assertIsInstance(script, str)
    self.assertTrue(10e3 < len(script) < 10e6,
                    'Script size should be between 10k and 10M')

  def testSampleTraceSerialization(self):
    json = visualization.parse_trace_json(_get_trace_proto_string())
    self.assertIsInstance(json, str)
    self.assertTrue('component_trace' in json)

  def testInteractiveVisualization(self):
    widget = visualization.InteractiveVisualization()
    widget.initial_html()
    widget.show_trace(_get_trace_proto_string())

Ivan Bogatyy's avatar
Ivan Bogatyy committed
63
64
65
66
67
68
69
  def testMasterSpecJson(self):
    visualization.trace_html(
        _get_trace_proto_string(), master_spec=_get_master_spec())
    widget = visualization.InteractiveVisualization()
    widget.initial_html()
    widget.show_trace(_get_trace_proto_string(), master_spec=_get_master_spec())

Ivan Bogatyy's avatar
Ivan Bogatyy committed
70
71
72

if __name__ == '__main__':
  googletest.main()