def testEquals(self): left = Channel(type=_MyType) right = Channel(type=_MyType) pred = left.future().value == right.future().value actual_pb = pred.encode() self.assertEqual(actual_pb.operator.compare_op.op, placeholder_pb2.ComparisonOperator.Operation.EQUAL)
def testDoubleNegation(self): """Treat `not(not(a))` as `a`.""" channel_1 = Channel(type=_MyType) channel_2 = Channel(type=_MyType) pred = channel_1.future().value < channel_2.future().value not_not_pred = ph.logical_not(ph.logical_not(pred)) channel_to_key_map = { channel_1: 'channel_1_key', channel_2: 'channel_2_key', } actual_pb = not_not_pred.encode_with_keys( lambda channel: channel_to_key_map[channel]) expected_pb = text_format.Parse( """ operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_1_key" } } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_2_key" } } } } } } } } op: LESS_THAN } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb)
def testEncodeWithKeys(self): channel_1 = Channel(type=_MyType) channel_2 = Channel(type=_MyType) pred = channel_1.future().value > channel_2.future().value channel_to_key_map = { channel_1: 'channel_1_key', channel_2: 'channel_2_key', } actual_pb = pred.encode_with_keys( lambda channel: channel_to_key_map[channel]) expected_pb = text_format.Parse( """ operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_1_key" } } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_2_key" } } } } } } } } op: GREATER_THAN } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb)
def testPredicateDependentChannels(self): int1 = Channel(type=standard_artifacts.Integer) int2 = Channel(type=standard_artifacts.Integer) pred1 = int1.future().value == 1 pred2 = int1.future().value == int2.future().value pred3 = ph.logical_not(pred1) pred4 = ph.logical_and(pred1, pred2) self.assertEqual(set(pred1.dependent_channels()), {int1}) self.assertEqual(set(pred2.dependent_channels()), {int1, int2}) self.assertEqual(set(pred3.dependent_channels()), {int1}) self.assertEqual(set(pred4.dependent_channels()), {int1, int2})
def testEncodeWithKeys(self): channel = Channel(type=_MyType) channel_future = channel.future()[0].value actual_pb = channel_future.encode_with_keys( lambda channel: channel.type_name) expected_pb = text_format.Parse( """ operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "MyTypeName" } } } } } } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb) self.assertIsNone(channel_future._key)
def testEncode(self): channel_1 = Channel(type=_MyType) channel_2 = Channel(type=_MyType) pred = channel_1.future().value > channel_2.future().value actual_pb = pred.encode() expected_pb = text_format.Parse( """ operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder {} } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder {} } } } } } } } op: GREATER_THAN } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb)
def testProtoFutureValueOperator(self): test_pb_filepath = os.path.join( os.path.dirname(__file__), 'testdata', 'proto_placeholder_future_value_operator.pbtxt') with open(test_pb_filepath) as text_pb_file: expected_pb = text_format.ParseLines( text_pb_file, placeholder_pb2.PlaceholderExpression()) output_channel = Channel(type=standard_artifacts.Integer) placeholder = output_channel.future()[0].value placeholder._key = '_component.num' self.assertProtoEquals(placeholder.encode(), expected_pb)
def testComparison_greaterThanOrEqual(self): """Treat `a >= b` as `not(a < b)`.""" channel_1 = Channel(type=_MyType) channel_2 = Channel(type=_MyType) pred = channel_1.future().value >= channel_2.future().value channel_to_key_map = { channel_1: 'channel_1_key', channel_2: 'channel_2_key', } actual_pb = pred.encode_with_keys( lambda channel: channel_to_key_map[channel]) expected_pb = text_format.Parse( """ operator { unary_logical_op { expression { operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_1_key" } } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_2_key" } } } } } } } } op: LESS_THAN } } } op: NOT } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb)
def testNestedLogicalOps(self): channel_11 = Channel(type=_MyType) channel_12 = Channel(type=_MyType) channel_21 = Channel(type=_MyType) channel_22 = Channel(type=_MyType) channel_3 = Channel(type=_MyType) pred = ph.logical_or( ph.logical_and( channel_11.future().value >= channel_12.future().value, channel_21.future().value < channel_22.future().value), ph.logical_not(channel_3.future().value == 'foo')) channel_to_key_map = { channel_11: 'channel_11_key', channel_12: 'channel_12_key', channel_21: 'channel_21_key', channel_22: 'channel_22_key', channel_3: 'channel_3_key', } actual_pb = pred.encode_with_keys( lambda channel: channel_to_key_map[channel]) expected_pb = text_format.Parse( """ operator { binary_logical_op { lhs { operator { binary_logical_op { lhs { operator { unary_logical_op { expression { operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_11_key" } } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_12_key" } } } } } } } } op: LESS_THAN } } } op: NOT } } } rhs { operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_21_key" } } } } } } } } rhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_22_key" } } } } } } } } op: LESS_THAN } } } op: AND } } } rhs { operator { unary_logical_op { expression { operator { compare_op { lhs { operator { artifact_value_op { expression { operator { index_op { expression { placeholder { key: "channel_3_key" } } } } } } } } rhs { value { string_value: "foo" } } op: EQUAL } } } op: NOT } } } op: OR } } """, placeholder_pb2.PlaceholderExpression()) self.assertProtoEquals(actual_pb, expected_pb)