xref: /aosp_15_r20/external/federated-compute/fcp/artifact_building/test_utils_test.py (revision 14675a029014e728ec732f129a32e299b2da0601)
1# Copyright 2022 Google LLC
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"""Tests for test_utils.py."""
15
16from absl.testing import absltest
17
18import tensorflow as tf
19
20from fcp.artifact_building import checkpoint_utils
21from fcp.artifact_building import test_utils
22from fcp.protos import plan_pb2
23
24
25class TestUtilsTest(absltest.TestCase):
26
27  def test_set_savepoint(self):
28    checkpoint_op = plan_pb2.CheckpointOp()
29    graph = tf.Graph()
30    with graph.as_default():
31      v = tf.compat.v1.get_variable('v', initializer=tf.constant(1))
32      saver = checkpoint_utils.create_deterministic_saver([v])
33      test_utils.set_checkpoint_op(checkpoint_op, saver)
34      self.assertTrue(checkpoint_op.HasField('saver_def'))
35      self.assertNotIn(':', checkpoint_op.saver_def.save_tensor_name)
36
37  def test_set_savepoint_no_saver(self):
38    checkpoint_op = plan_pb2.CheckpointOp()
39    test_utils.set_checkpoint_op(checkpoint_op, None)
40    self.assertEqual(plan_pb2.CheckpointOp(), checkpoint_op)
41
42
43if __name__ == '__main__':
44  absltest.main()
45