|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +import unittest |
| 18 | + |
| 19 | +from google.protobuf import duration_pb2 |
| 20 | +from google.protobuf import timestamp_pb2 |
| 21 | + |
| 22 | +from apache_beam.utils import proto_utils |
| 23 | +from apache_beam.utils.timestamp import MAX_TIMESTAMP |
| 24 | + |
| 25 | + |
| 26 | +class TestProtoUtils(unittest.TestCase): |
| 27 | + def test_from_micros_duration(self): |
| 28 | + ts = proto_utils.from_micros(duration_pb2.Duration, MAX_TIMESTAMP.micros) |
| 29 | + expected = duration_pb2.Duration( |
| 30 | + seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) |
| 31 | + self.assertEqual(ts, expected) |
| 32 | + |
| 33 | + def test_from_micros_timestamp(self): |
| 34 | + ts = proto_utils.from_micros(timestamp_pb2.Timestamp, MAX_TIMESTAMP.micros) |
| 35 | + expected = timestamp_pb2.Timestamp( |
| 36 | + seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) |
| 37 | + self.assertEqual(ts, expected) |
| 38 | + |
| 39 | + def test_to_micros_duration(self): |
| 40 | + dur = duration_pb2.Duration( |
| 41 | + seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) |
| 42 | + ts = proto_utils.to_micros(dur) |
| 43 | + expected = MAX_TIMESTAMP.micros |
| 44 | + self.assertEqual(ts, expected) |
| 45 | + |
| 46 | + def test_to_micros_timestamp(self): |
| 47 | + dur = timestamp_pb2.Timestamp( |
| 48 | + seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) |
| 49 | + ts = proto_utils.to_micros(dur) |
| 50 | + expected = MAX_TIMESTAMP.micros |
| 51 | + self.assertEqual(ts, expected) |
| 52 | + |
| 53 | + def test_round_trip_duration(self): |
| 54 | + expected = 919336704 |
| 55 | + dur = proto_utils.from_micros(duration_pb2.Duration, expected) |
| 56 | + ms = proto_utils.to_micros(dur) |
| 57 | + self.assertEqual(ms, expected) |
| 58 | + |
| 59 | + def test_round_trip_timestamp(self): |
| 60 | + expected = 919336704 |
| 61 | + ts = proto_utils.from_micros(timestamp_pb2.Timestamp, expected) |
| 62 | + ms = proto_utils.to_micros(ts) |
| 63 | + self.assertEqual(ms, expected) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + unittest.main() |
0 commit comments