|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from parameterized import parameterized |
| 6 | +from library_generation.model.gapic_inputs import parse |
| 7 | + |
| 8 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 9 | +resources_dir = os.path.join(script_dir, "..", "resources") |
| 10 | +build_file = Path(os.path.join(resources_dir, "misc")).resolve() |
| 11 | + |
| 12 | + |
| 13 | +class UtilitiesTest(unittest.TestCase): |
| 14 | + @parameterized.expand( |
| 15 | + [ |
| 16 | + ("BUILD_no_additional_protos.bazel", " "), |
| 17 | + ("BUILD_common_resources.bazel", " google/cloud/common_resources.proto"), |
| 18 | + ("BUILD_comment_common_resources.bazel", " "), |
| 19 | + ("BUILD_locations.bazel", " google/cloud/location/locations.proto"), |
| 20 | + ("BUILD_comment_locations.bazel", " "), |
| 21 | + ("BUILD_iam_policy.bazel", " google/iam/v1/iam_policy.proto"), |
| 22 | + ("BUILD_comment_iam_policy.bazel", " "), |
| 23 | + ( |
| 24 | + "BUILD_iam_locations.bazel", |
| 25 | + " google/cloud/location/locations.proto google/iam/v1/iam_policy.proto", |
| 26 | + ), |
| 27 | + ] |
| 28 | + ) |
| 29 | + def test_gapic_inputs_parse_additional_protos(self, build_name, expected): |
| 30 | + parsed = parse(build_file, "", build_name) |
| 31 | + self.assertEqual( |
| 32 | + expected, |
| 33 | + parsed.additional_protos, |
| 34 | + ) |
| 35 | + |
| 36 | + def test_gapic_inputs_parse_grpc_only_succeeds(self): |
| 37 | + parsed = parse(build_file, "", "BUILD_grpc.bazel") |
| 38 | + self.assertEqual("grpc", parsed.transport) |
| 39 | + |
| 40 | + def test_gapic_inputs_parse_grpc_rest_succeeds(self): |
| 41 | + parsed = parse(build_file, "", "BUILD_grpc_rest.bazel") |
| 42 | + self.assertEqual("grpc+rest", parsed.transport) |
| 43 | + |
| 44 | + def test_gapic_inputs_parse_rest_succeeds(self): |
| 45 | + parsed = parse(build_file, "", "BUILD_rest.bazel") |
| 46 | + self.assertEqual("rest", parsed.transport) |
| 47 | + |
| 48 | + def test_gapic_inputs_parse_empty_include_samples_succeeds(self): |
| 49 | + parsed = parse(build_file, "", "BUILD_include_samples_empty.bazel") |
| 50 | + self.assertEqual("false", parsed.include_samples) |
| 51 | + |
| 52 | + def test_gapic_inputs_parse_include_samples_false_succeeds(self): |
| 53 | + parsed = parse(build_file, "", "BUILD_include_samples_false.bazel") |
| 54 | + self.assertEqual("false", parsed.include_samples) |
| 55 | + |
| 56 | + def test_gapic_inputs_parse_include_samples_true_succeeds(self): |
| 57 | + parsed = parse(build_file, "", "BUILD_include_samples_true.bazel") |
| 58 | + self.assertEqual("true", parsed.include_samples) |
| 59 | + |
| 60 | + def test_gapic_inputs_parse_empty_rest_numeric_enums_succeeds(self): |
| 61 | + parsed = parse(build_file, "", "BUILD_rest_numeric_enums_empty.bazel") |
| 62 | + self.assertEqual("false", parsed.rest_numeric_enum) |
| 63 | + |
| 64 | + def test_gapic_inputs_parse_rest_numeric_enums_false_succeeds(self): |
| 65 | + parsed = parse(build_file, "", "BUILD_rest_numeric_enums_false.bazel") |
| 66 | + self.assertEqual("false", parsed.rest_numeric_enum) |
| 67 | + |
| 68 | + def test_gapic_inputs_parse_rest_numeric_enums_true_succeeds(self): |
| 69 | + parsed = parse(build_file, "", "BUILD_rest_numeric_enums_true.bazel") |
| 70 | + self.assertEqual("true", parsed.rest_numeric_enum) |
| 71 | + |
| 72 | + def test_gapic_inputs_parse_no_gapic_library_returns_proto_only_true(self): |
| 73 | + # include_samples_empty only has a gradle assembly rule |
| 74 | + parsed = parse(build_file, "", "BUILD_include_samples_empty.bazel") |
| 75 | + self.assertEqual("true", parsed.proto_only) |
| 76 | + |
| 77 | + def test_gapic_inputs_parse_with_gapic_library_returns_proto_only_false(self): |
| 78 | + # rest.bazel has a java_gapic_library rule |
| 79 | + parsed = parse(build_file, "", "BUILD_rest.bazel") |
| 80 | + self.assertEqual("false", parsed.proto_only) |
| 81 | + |
| 82 | + def test_gapic_inputs_parse_gapic_yaml_succeeds(self): |
| 83 | + parsed = parse(build_file, "test/versioned/path", "BUILD_gapic_yaml.bazel") |
| 84 | + self.assertEqual("test/versioned/path/test_gapic_yaml.yaml", parsed.gapic_yaml) |
| 85 | + |
| 86 | + def test_gapic_inputs_parse_no_gapic_yaml_returns_empty_string(self): |
| 87 | + parsed = parse(build_file, "test/versioned/path", "BUILD_no_gapic_yaml.bazel") |
| 88 | + self.assertEqual("", parsed.gapic_yaml) |
| 89 | + |
| 90 | + def test_gapic_inputs_parse_service_config_succeeds(self): |
| 91 | + parsed = parse(build_file, "test/versioned/path", "BUILD_service_config.bazel") |
| 92 | + self.assertEqual( |
| 93 | + "test/versioned/path/test_service_config.json", parsed.service_config |
| 94 | + ) |
| 95 | + |
| 96 | + def test_gapic_inputs_parse_service_yaml_relative_target(self): |
| 97 | + parsed = parse( |
| 98 | + build_file, |
| 99 | + "google/cloud/compute/v1", |
| 100 | + "BUILD_service_config_relative_target.bazel", |
| 101 | + ) |
| 102 | + self.assertEqual( |
| 103 | + "google/cloud/compute/v1/compute_grpc_service_config.json", |
| 104 | + parsed.service_config, |
| 105 | + ) |
| 106 | + |
| 107 | + def test_gapic_inputs_parse_no_service_config_returns_empty_string(self): |
| 108 | + parsed = parse( |
| 109 | + build_file, "test/versioned/path", "BUILD_no_service_config.bazel" |
| 110 | + ) |
| 111 | + self.assertEqual("", parsed.service_config) |
| 112 | + |
| 113 | + def test_gapic_inputs_parse_service_yaml_succeeds(self): |
| 114 | + parsed = parse(build_file, "test/versioned/path", "BUILD_service_yaml.bazel") |
| 115 | + self.assertEqual( |
| 116 | + "test/versioned/path/test_service_yaml.yaml", parsed.service_yaml |
| 117 | + ) |
| 118 | + |
| 119 | + def test_gapic_inputs_parse_service_yaml_absolute_target(self): |
| 120 | + parsed = parse(build_file, "", "BUILD_service_yaml_absolute_target.bazel") |
| 121 | + self.assertEqual( |
| 122 | + "google/cloud/videointelligence/videointelligence_v1p3beta1.yaml", |
| 123 | + parsed.service_yaml, |
| 124 | + ) |
| 125 | + |
| 126 | + def test_gapic_inputs_parse_no_service_yaml_returns_empty_string(self): |
| 127 | + parsed = parse(build_file, "test/versioned/path", "BUILD_no_service_yaml.bazel") |
| 128 | + self.assertEqual("", parsed.service_yaml) |
| 129 | + |
| 130 | + def test_gapic_inputs_parse_proto_only_returns_grpc(self): |
| 131 | + parsed = parse(build_file, "test/versioned/path", "BUILD_proto_only.bazel") |
| 132 | + self.assertEqual("grpc", parsed.transport) |
0 commit comments