code: def testVariant(self):
      def create_constant_variant(value):
          return constant_op.constant(
              tensor_pb2.TensorProto(
                  dtype=dtypes.variant.as_datatype_enum,
                  tensor_shape=tensor_shape.TensorShape([]).as_proto(),
                  variant_val=[
                      tensor_pb2.VariantTensorDataProto(
                          # Match registration in variant_op_registry.cc
                          type_name=b"int",
                          metadata=np.array(
                              value, dtype=np.int32
                          ).tobytes(),
                      )
                  ],
              )
          )
  
      # TODO(ebrevdo): Re-enable use_gpu=True once non-DMA Variant
      # copying between CPU and GPU is supported.
      with self.session(use_gpu=False):
          num_tests = 127
          values = list(range(100))
          variant_consts = [create_constant_variant(x) for x in values]
          sum_count_indices = np.random.randint(1, 29, size=num_tests)
          sum_indices = [
              np.random.randint(100, size=count)
              for count in sum_count_indices
          ]
          expected_sums = [np.sum(x) for x in sum_indices]
          variant_sums = [
              math_ops.add_n([variant_consts[i] for i in x])
              for x in sum_indices
          ]
  
          # We use as_string() to get the Variant DebugString for the
          # variant_sums; we know its value so we can check via string equality
          # here.
          #
          # Right now, non-numpy-compatible objects cannot be returned from a
          # session.run call; similarly, objects that can't be converted to
          # native numpy types cannot be passed to ops.convert_to_tensor.
          variant_sums_string = string_ops.as_string(variant_sums)
          self.assertAllEqual(
              variant_sums_string,
              [
                  "Variant<type: int value: {}>".format(s).encode(
                      "utf-8"
                  )
                  for s in expected_sums
              ],
          )
  
file_path: tensorflow/tensorflow/python/kernel_tests/math_ops/aggregate_ops_test.py
imports: import numpy
  from tensorflow.core.framework import tensor_pb2
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import string_ops
  from tensorflow.python.platform import test
label_desc: Use image node_id 5 for calling the AddNTest obj's underlying member method code with example usage: obj.testVariant() and returns: constant_op