Skip to content

Commit 982f604

Browse files
authored
Move unit tests to local protos (#288)
The unit tests in this repo use Protobuf definitions from the conformance suite, which works but it makes debugging issues a bit difficult. It is extremely helpful to have local protos in the repo that can be changed for tests to debug issues. This follows the example in protovalidate-go by creating a `validations.proto` and copies the definitions currently being used from conformance into this proto.
1 parent 8c767e9 commit 982f604

File tree

7 files changed

+293
-12
lines changed

7 files changed

+293
-12
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ clean: ## Delete intermediate build artifacts
3333
.PHONY: generate
3434
generate: $(BIN)/buf $(BIN)/license-header ## Regenerate code and license headers
3535
rm -rf gen
36-
buf generate buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION)
37-
buf generate buf.build/bufbuild/protovalidate-testing:$(PROTOVALIDATE_VERSION)
36+
$(BIN)/buf generate buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION)
37+
$(BIN)/buf generate buf.build/bufbuild/protovalidate-testing:$(PROTOVALIDATE_VERSION)
38+
$(BIN)/buf generate
3839
$(ADD_LICENSE_HEADER)
3940

4041
.PHONY: format
@@ -44,7 +45,7 @@ format: install $(BIN)/license-header ## Format code
4445
pipenv run ruff check --fix protovalidate tests
4546

4647
.PHONY: test
47-
test: $(BIN)/protovalidate-conformance generate install ## Run unit tests
48+
test: generate install ## Run unit tests
4849
pipenv run pytest
4950

5051
.PHONY: conformance

buf.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Generated by buf. DO NOT EDIT.
2+
version: v2
3+
deps:
4+
- name: buf.build/bufbuild/protovalidate
5+
commit: 0409229c37804d6187ee0806eb4eebce
6+
digest: b5:795db9d3a6e066dc61d99ac651fa7f136171869abe2211ca272dd84aada7bc4583b9508249fa5b61300a5b1fe8b6dbf6edbc088aa0345d1ccb9fff705e3d48e9
7+
- name: buf.build/bufbuild/protovalidate-testing
8+
commit: 5acbe1f3c8f24ced9466b9ccccad4cb0
9+
digest: b5:5e9d54d19ce3d9d368f4b1b5ee4f20094d1c33d0f2dca19536339335c2e70d5ffedbd4fa28e290b59ecae0671c9d2dc20b6b8ebba5a9ac76cbf5f9d2af655ef4

buf.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: v2
2+
modules:
3+
- path: proto
4+
deps:
5+
- buf.build/bufbuild/protovalidate
6+
- buf.build/bufbuild/protovalidate-testing
7+
lint:
8+
use:
9+
- STANDARD
10+
ignore_only:
11+
PROTOVALIDATE:
12+
- proto/tests/example/v1/validations.proto
13+
breaking:
14+
use:
15+
- FILE

gen/tests/example/v1/validations_pb2.py

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/tests/example/v1/validations_pb2.pyi

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2023-2025 Buf Technologies, Inc.
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+
15+
syntax = "proto3";
16+
17+
package tests.example.v1;
18+
19+
import "buf/validate/validate.proto";
20+
import "google/protobuf/timestamp.proto";
21+
22+
message DoubleFinite {
23+
double val = 1 [(buf.validate.field).double.finite = true];
24+
}
25+
26+
message SFixed64ExLTGT {
27+
sfixed64 val = 1 [(buf.validate.field).sfixed64 = {
28+
lt: 0
29+
gt: 10
30+
}];
31+
}
32+
33+
message TestOneofMsg {
34+
bool val = 1 [(buf.validate.field).bool.const = true];
35+
}
36+
37+
message Oneof {
38+
oneof o {
39+
string x = 1 [(buf.validate.field).string.prefix = "foo"];
40+
int32 y = 2 [(buf.validate.field).int32.gt = 0];
41+
TestOneofMsg z = 3;
42+
}
43+
}
44+
45+
message TimestampGTNow {
46+
google.protobuf.Timestamp val = 1 [(buf.validate.field).timestamp.gt_now = true];
47+
}
48+
49+
message MapMinMax {
50+
map<string, bool> val = 1 [(buf.validate.field).map = {
51+
min_pairs: 2
52+
max_pairs: 4
53+
}];
54+
}
55+
56+
message MapKeys {
57+
map<sint64, string> val = 1 [(buf.validate.field).map.keys.sint64.lt = 0];
58+
}
59+
60+
message Embed {
61+
int64 val = 1 [(buf.validate.field).int64.gt = 0];
62+
}
63+
message RepeatedEmbedSkip {
64+
repeated Embed val = 1 [(buf.validate.field).repeated.items.ignore = IGNORE_ALWAYS];
65+
}

tests/validate_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
import unittest
1616

1717
import protovalidate
18-
from buf.validate.conformance.cases import maps_pb2, numbers_pb2, oneofs_pb2, repeated_pb2, wkt_timestamp_pb2
18+
from gen.tests.example.v1 import validations_pb2
1919

2020

2121
class TestValidate(unittest.TestCase):
2222
def test_ninf(self):
23-
msg = numbers_pb2.DoubleFinite()
23+
msg = validations_pb2.DoubleFinite()
2424
msg.val = float("-inf")
2525
violations = protovalidate.collect_violations(msg)
2626
self.assertEqual(len(violations), 1)
@@ -29,7 +29,7 @@ def test_ninf(self):
2929
self.assertEqual(violations[0].rule_value, True)
3030

3131
def test_map_key(self):
32-
msg = maps_pb2.MapKeys()
32+
msg = validations_pb2.MapKeys()
3333
msg.val[1] = "a"
3434
violations = protovalidate.collect_violations(msg)
3535
self.assertEqual(len(violations), 1)
@@ -38,18 +38,18 @@ def test_map_key(self):
3838
self.assertEqual(violations[0].rule_value, 0)
3939

4040
def test_sfixed64(self):
41-
msg = numbers_pb2.SFixed64ExLTGT(val=11)
41+
msg = validations_pb2.SFixed64ExLTGT(val=11)
4242
protovalidate.validate(msg)
4343

4444
violations = protovalidate.collect_violations(msg)
4545
self.assertEqual(len(violations), 0)
4646

4747
def test_oneofs(self):
48-
msg1 = oneofs_pb2.Oneof()
48+
msg1 = validations_pb2.Oneof()
4949
msg1.y = 123
5050
protovalidate.validate(msg1)
5151

52-
msg2 = oneofs_pb2.Oneof()
52+
msg2 = validations_pb2.Oneof()
5353
msg2.z.val = True
5454
protovalidate.validate(msg2)
5555

@@ -58,15 +58,15 @@ def test_oneofs(self):
5858
assert len(violations) == 0
5959

6060
def test_repeated(self):
61-
msg = repeated_pb2.RepeatedEmbedSkip()
61+
msg = validations_pb2.RepeatedEmbedSkip()
6262
msg.val.add(val=-1)
6363
protovalidate.validate(msg)
6464

6565
violations = protovalidate.collect_violations(msg)
6666
assert len(violations) == 0
6767

6868
def test_maps(self):
69-
msg = maps_pb2.MapMinMax()
69+
msg = validations_pb2.MapMinMax()
7070
try:
7171
protovalidate.validate(msg)
7272
except protovalidate.ValidationError as e:
@@ -78,7 +78,7 @@ def test_maps(self):
7878
assert len(violations) == 1
7979

8080
def test_timestamp(self):
81-
msg = wkt_timestamp_pb2.TimestampGTNow()
81+
msg = validations_pb2.TimestampGTNow()
8282
protovalidate.validate(msg)
8383

8484
violations = protovalidate.collect_violations(msg)

0 commit comments

Comments
 (0)