|
| 1 | +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""API tests for the `tensorboard.summary` module. |
| 16 | +
|
| 17 | +These tests are especially important because this module is the standard |
| 18 | +public entry point for end users, so we should be as careful as possible |
| 19 | +to ensure that we export the right things. |
| 20 | +""" |
| 21 | +from __future__ import absolute_import |
| 22 | +from __future__ import division |
| 23 | +from __future__ import print_function |
| 24 | + |
| 25 | +import collections |
| 26 | + |
| 27 | +import tensorflow as tf |
| 28 | +from tensorboard import summary |
| 29 | + |
| 30 | + |
| 31 | +STANDARD_PLUGINS = frozenset([ |
| 32 | + 'audio', |
| 33 | + 'histogram', |
| 34 | + 'image', |
| 35 | + 'pr_curve', |
| 36 | + 'scalar', |
| 37 | + 'text', |
| 38 | +]) |
| 39 | + |
| 40 | +# The subset of `STANDARD_PLUGINS` for which we do not currently have |
| 41 | +# functions to generate a summary protobuf outside of a TensorFlow |
| 42 | +# graph. This set should ideally be empty; any entries here should be |
| 43 | +# considered temporary. |
| 44 | +PLUGINS_WITHOUT_PB_FUNCTIONS = frozenset([ |
| 45 | + 'pr_curve', # TODO(@chihuahua, #445): Fix this. |
| 46 | +]) |
| 47 | + |
| 48 | + |
| 49 | +class SummaryExportsTest(tf.test.TestCase): |
| 50 | + |
| 51 | + def test_each_plugin_has_an_export(self): |
| 52 | + for plugin in STANDARD_PLUGINS: |
| 53 | + self.assertIsInstance(getattr(summary, plugin), collections.Callable) |
| 54 | + |
| 55 | + def test_plugins_export_pb_functions(self): |
| 56 | + for plugin in STANDARD_PLUGINS: |
| 57 | + if plugin not in PLUGINS_WITHOUT_PB_FUNCTIONS: |
| 58 | + self.assertIsInstance( |
| 59 | + getattr(summary, '%s_pb' % plugin), collections.Callable) |
| 60 | + |
| 61 | + def test_all_exports_correspond_to_plugins(self): |
| 62 | + exports = [name for name in dir(summary) if not name.startswith('_')] |
| 63 | + futures = frozenset(('absolute_import', 'division', 'print_function')) |
| 64 | + bad_exports = [ |
| 65 | + name for name in exports |
| 66 | + if name not in futures and not any( |
| 67 | + name == plugin or name.startswith('%s_' % plugin) |
| 68 | + for plugin in STANDARD_PLUGINS) |
| 69 | + ] |
| 70 | + if bad_exports: |
| 71 | + self.fail( |
| 72 | + 'The following exports do not correspond to known standard ' |
| 73 | + 'plugins: %r. Please mark these as private by prepending an ' |
| 74 | + 'underscore to their names, or, if they correspond to a new ' |
| 75 | + 'plugin that you are certain should be part of the public API ' |
| 76 | + 'forever, add that plugin to the STANDARD_PLUGINS set in this ' |
| 77 | + 'module.' % bad_exports) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + tf.test.main() |
0 commit comments