visualization_test.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ==============================================================================
  16. """Tests for dragnn.python.visualization."""
  17. from __future__ import absolute_import
  18. from __future__ import division
  19. from __future__ import print_function
  20. from tensorflow.python.platform import googletest
  21. from dragnn.protos import spec_pb2
  22. from dragnn.protos import trace_pb2
  23. from dragnn.python import visualization
  24. def _get_trace_proto_string():
  25. trace = trace_pb2.MasterTrace()
  26. trace.component_trace.add(
  27. step_trace=[
  28. trace_pb2.ComponentStepTrace(fixed_feature_trace=[]),
  29. ],
  30. # Google Translate says this is "component" in Chinese. (To test UTF-8).
  31. name='零件',)
  32. return trace.SerializeToString()
  33. def _get_master_spec():
  34. return spec_pb2.MasterSpec(
  35. component=[spec_pb2.ComponentSpec(name='jalapeño')])
  36. class VisualizationTest(googletest.TestCase):
  37. def testCanFindScript(self):
  38. script = visualization._load_viz_script()
  39. self.assertIsInstance(script, str)
  40. self.assertTrue(10e3 < len(script) < 10e6,
  41. 'Script size should be between 10k and 10M')
  42. def testSampleTraceSerialization(self):
  43. json = visualization.parse_trace_json(_get_trace_proto_string())
  44. self.assertIsInstance(json, str)
  45. self.assertTrue('component_trace' in json)
  46. def testInteractiveVisualization(self):
  47. widget = visualization.InteractiveVisualization()
  48. widget.initial_html()
  49. widget.show_trace(_get_trace_proto_string())
  50. def testMasterSpecJson(self):
  51. visualization.trace_html(
  52. _get_trace_proto_string(), master_spec=_get_master_spec())
  53. widget = visualization.InteractiveVisualization()
  54. widget.initial_html()
  55. widget.show_trace(_get_trace_proto_string(), master_spec=_get_master_spec())
  56. if __name__ == '__main__':
  57. googletest.main()