xref: /aosp_15_r20/external/federated-compute/fcp/tensorflow/example_selector_fuser_test.py (revision 14675a029014e728ec732f129a32e299b2da0601)
1# Copyright 2021 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
15import tensorflow as tf
16
17from fcp.protos import plan_pb2
18from fcp.tensorflow import test_selector_pb2
19
20import fcp.tensorflow.example_selector_fuser as fuser
21
22
23class ExampleSelectorFuserTest(tf.test.TestCase):
24
25  def test_example_selector_fuser(self):
26    selector = plan_pb2.ExampleSelector(collection_uri='app:/test_collection')
27    criteria = test_selector_pb2.TestCriteria(max_examples=10)
28    selector.criteria.Pack(criteria)
29    resumption_token = test_selector_pb2.ResumptionToken(last_index=25)
30    fused_selector_tensor = fuser.example_selector_fuser(
31        tf.convert_to_tensor(selector.SerializeToString(), dtype=tf.string),
32        tf.convert_to_tensor(
33            'type.googleapis.com/fcp.ResumptionToken', dtype=tf.string),
34        tf.convert_to_tensor(
35            resumption_token.SerializeToString(), dtype=tf.string))
36
37    fused_selector = plan_pb2.ExampleSelector()
38    fused_selector.ParseFromString(fused_selector_tensor.numpy())
39    assert fused_selector.collection_uri == 'app:/test_collection'
40    unpacked_criteria = test_selector_pb2.TestCriteria()
41    assert fused_selector.criteria.Unpack(unpacked_criteria)
42    assert unpacked_criteria.max_examples == 10
43
44    unpacked_token = test_selector_pb2.ResumptionToken()
45    assert fused_selector.resumption_token.Unpack(unpacked_token)
46    assert unpacked_token.last_index == 25
47
48  def test_example_selector_fuser_error(self):
49    resumption_token = test_selector_pb2.ResumptionToken(last_index=25)
50    with self.assertRaises(tf.errors.InvalidArgumentError):
51      fuser.example_selector_fuser(
52          tf.convert_to_tensor(b'1234', dtype=tf.string),
53          tf.convert_to_tensor(
54              'type.googleapis.com/fcp.ResumptionToken', dtype=tf.string),
55          tf.convert_to_tensor(
56              resumption_token.SerializeToString(), dtype=tf.string))
57
58
59if __name__ == '__main__':
60  tf.test.main()
61