Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scalars: add HTTP-level tests of download behavior #3793

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 61 additions & 60 deletions tensorboard/plugins/scalar/scalars_plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import collections.abc
import csv
import functools
import json
import os.path
import unittest

from six import StringIO
from six.moves import xrange # pylint: disable=redefined-builtin
import tensorflow as tf
from werkzeug import test as werkzeug_test
from werkzeug import wrappers

from tensorboard import errors
from tensorboard.backend import application
Expand Down Expand Up @@ -168,70 +171,37 @@ def test_index(self, plugin):
plugin.index_impl("eid"),
)

@with_runs(
[_RUN_WITH_LEGACY_SCALARS, _RUN_WITH_SCALARS, _RUN_WITH_HISTOGRAM]
)
def _test_scalars_json(self, plugin, run_name, tag_name, should_work=True):
if should_work:
(data, mime_type) = plugin.scalars_impl(
tag_name, run_name, "eid", scalars_plugin.OutputFormat.JSON
)
self.assertEqual("application/json", mime_type)
self.assertEqual(len(data), self._STEPS)
else:
with self.assertRaises(errors.NotFoundError):
plugin.scalars_impl(
tag_name, run_name, "eid", scalars_plugin.OutputFormat.JSON,
)

@with_runs(
[_RUN_WITH_LEGACY_SCALARS, _RUN_WITH_SCALARS, _RUN_WITH_HISTOGRAM]
)
def _test_scalars_csv(self, plugin, run_name, tag_name, should_work=True):
if should_work:
(data, mime_type) = plugin.scalars_impl(
tag_name, run_name, "eid", scalars_plugin.OutputFormat.CSV
)
self.assertEqual("text/csv", mime_type)
s = StringIO(data)
reader = csv.reader(s)
self.assertEqual(["Wall time", "Step", "Value"], next(reader))
self.assertEqual(len(list(reader)), self._STEPS)
else:
with self.assertRaises(errors.NotFoundError):
plugin.scalars_impl(
tag_name, run_name, "eid", scalars_plugin.OutputFormat.CSV,
)

def test_scalars_json_with_legacy_scalars(self):
self._test_scalars_json(
self._RUN_WITH_LEGACY_SCALARS, self._LEGACY_SCALAR_TAG
)

def test_scalars_json_with_scalars(self):
self._test_scalars_json(
self._RUN_WITH_SCALARS, "%s/scalar_summary" % self._SCALAR_TAG
)

def test_scalars_json_with_histogram(self):
self._test_scalars_json(
self._RUN_WITH_HISTOGRAM, self._HISTOGRAM_TAG, should_work=False
)

def test_scalars_csv_with_legacy_scalars(self):
self._test_scalars_csv(
self._RUN_WITH_LEGACY_SCALARS, self._LEGACY_SCALAR_TAG
@with_runs([_RUN_WITH_LEGACY_SCALARS])
def test_scalars_with_legacy_scalars(self, plugin):
data, mime_type = plugin.scalars_impl(
self._LEGACY_SCALAR_TAG,
self._RUN_WITH_LEGACY_SCALARS,
"eid",
scalars_plugin.OutputFormat.JSON,
)
self.assertEqual("application/json", mime_type)
self.assertEqual(len(data), self._STEPS)

def test_scalars_csv_with_scalars(self):
self._test_scalars_csv(
self._RUN_WITH_SCALARS, "%s/scalar_summary" % self._SCALAR_TAG
@with_runs([_RUN_WITH_SCALARS])
def test_scalars_with_scalars(self, plugin):
data, mime_type = plugin.scalars_impl(
"%s/scalar_summary" % self._SCALAR_TAG,
self._RUN_WITH_SCALARS,
"eid",
scalars_plugin.OutputFormat.JSON,
)
self.assertEqual("application/json", mime_type)
self.assertEqual(len(data), self._STEPS)

def test_scalars_csv_with_histogram(self):
self._test_scalars_csv(
self._RUN_WITH_HISTOGRAM, self._HISTOGRAM_TAG, should_work=False
)
@with_runs([_RUN_WITH_HISTOGRAM])
def test_scalars_with_histogram(self, plugin):
with self.assertRaises(errors.NotFoundError):
plugin.scalars_impl(
self._HISTOGRAM_TAG,
self._RUN_WITH_HISTOGRAM,
"eid",
scalars_plugin.OutputFormat.JSON,
)

@with_runs([_RUN_WITH_LEGACY_SCALARS])
def test_active_with_legacy_scalars(self, plugin):
Expand Down Expand Up @@ -260,6 +230,37 @@ def test_active_with_all(self, plugin):
else:
self.assertTrue(plugin.is_active())

@with_runs([_RUN_WITH_SCALARS])
def test_download_url_json(self, plugin):
wsgi_app = application.TensorBoardWSGI([plugin])
server = werkzeug_test.Client(wsgi_app, wrappers.BaseResponse)
response = server.get(
"/data/plugin/scalars/scalars?run=%s&tag=%s"
% (self._RUN_WITH_SCALARS, "%s/scalar_summary" % self._SCALAR_TAG,)
)
self.assertEqual(200, response.status_code)
self.assertEqual("application/json", response.headers["Content-Type"])
payload = json.loads(response.get_data())
self.assertEqual(len(payload), self._STEPS)

@with_runs([_RUN_WITH_SCALARS])
def test_download_url_csv(self, plugin):
wsgi_app = application.TensorBoardWSGI([plugin])
server = werkzeug_test.Client(wsgi_app, wrappers.BaseResponse)
response = server.get(
"/data/plugin/scalars/scalars?run=%s&tag=%s&format=csv"
% (self._RUN_WITH_SCALARS, "%s/scalar_summary" % self._SCALAR_TAG,)
)
self.assertEqual(200, response.status_code)
self.assertEqual(
"text/csv; charset=utf-8", response.headers["Content-Type"]
)
payload = response.get_data()
s = StringIO(payload.decode("utf-8"))
reader = csv.reader(s)
self.assertEqual(["Wall time", "Step", "Value"], next(reader))
self.assertEqual(len(list(reader)), self._STEPS)


if __name__ == "__main__":
tf.test.main()