Skip to content

Move unit tests to local protos #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ clean: ## Delete intermediate build artifacts
.PHONY: generate
generate: $(BIN)/buf $(BIN)/license-header ## Regenerate code and license headers
rm -rf gen
buf generate buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION)
buf generate buf.build/bufbuild/protovalidate-testing:$(PROTOVALIDATE_VERSION)
$(BIN)/buf generate buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION)
$(BIN)/buf generate buf.build/bufbuild/protovalidate-testing:$(PROTOVALIDATE_VERSION)
$(BIN)/buf generate
Comment on lines +36 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's helpful to have local proto files for debugging. But PROTOVALIDATE_VERSION and buf.lock competing over the exact version seems like a footgun.

Maybe the least controversial thing to do is to cut a protovalidate release that contains the new test protos?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not following. pv-go is doing this exact thing with no issues. I would really prefer for this to follow that pattern. Being able to change protos while debugging issues is much quicker than constantly having to cut protovalidate releases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that we haven't had an issue with it so far. I didn't realize the Go implementation already does it. LGTM, and hopefully find a better solution down the road.

$(ADD_LICENSE_HEADER)

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

.PHONY: test
test: $(BIN)/protovalidate-conformance generate install ## Run unit tests
test: generate install ## Run unit tests
pipenv run pytest

.PHONY: conformance
Expand Down
9 changes: 9 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by buf. DO NOT EDIT.
version: v2
deps:
- name: buf.build/bufbuild/protovalidate
commit: 0409229c37804d6187ee0806eb4eebce
digest: b5:795db9d3a6e066dc61d99ac651fa7f136171869abe2211ca272dd84aada7bc4583b9508249fa5b61300a5b1fe8b6dbf6edbc088aa0345d1ccb9fff705e3d48e9
- name: buf.build/bufbuild/protovalidate-testing
commit: 5acbe1f3c8f24ced9466b9ccccad4cb0
digest: b5:5e9d54d19ce3d9d368f4b1b5ee4f20094d1c33d0f2dca19536339335c2e70d5ffedbd4fa28e290b59ecae0671c9d2dc20b6b8ebba5a9ac76cbf5f9d2af655ef4
15 changes: 15 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: v2
modules:
- path: proto
deps:
- buf.build/bufbuild/protovalidate
- buf.build/bufbuild/protovalidate-testing
lint:
use:
- STANDARD
ignore_only:
PROTOVALIDATE:
- proto/tests/example/v1/validations.proto
breaking:
use:
- FILE
97 changes: 97 additions & 0 deletions gen/tests/example/v1/validations_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions gen/tests/example/v1/validations_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions proto/tests/example/v1/validations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package tests.example.v1;

import "buf/validate/validate.proto";
import "google/protobuf/timestamp.proto";

message DoubleFinite {
double val = 1 [(buf.validate.field).double.finite = true];
}

message SFixed64ExLTGT {
sfixed64 val = 1 [(buf.validate.field).sfixed64 = {
lt: 0
gt: 10
}];
}

message TestOneofMsg {
bool val = 1 [(buf.validate.field).bool.const = true];
}

message Oneof {
oneof o {
string x = 1 [(buf.validate.field).string.prefix = "foo"];
int32 y = 2 [(buf.validate.field).int32.gt = 0];
TestOneofMsg z = 3;
}
}

message TimestampGTNow {
google.protobuf.Timestamp val = 1 [(buf.validate.field).timestamp.gt_now = true];
}

message MapMinMax {
map<string, bool> val = 1 [(buf.validate.field).map = {
min_pairs: 2
max_pairs: 4
}];
}

message MapKeys {
map<sint64, string> val = 1 [(buf.validate.field).map.keys.sint64.lt = 0];
}

message Embed {
int64 val = 1 [(buf.validate.field).int64.gt = 0];
}
message RepeatedEmbedSkip {
repeated Embed val = 1 [(buf.validate.field).repeated.items.ignore = IGNORE_ALWAYS];
}
18 changes: 9 additions & 9 deletions tests/validate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import unittest

import protovalidate
from buf.validate.conformance.cases import maps_pb2, numbers_pb2, oneofs_pb2, repeated_pb2, wkt_timestamp_pb2
from gen.tests.example.v1 import validations_pb2


class TestValidate(unittest.TestCase):
def test_ninf(self):
msg = numbers_pb2.DoubleFinite()
msg = validations_pb2.DoubleFinite()
msg.val = float("-inf")
violations = protovalidate.collect_violations(msg)
self.assertEqual(len(violations), 1)
Expand All @@ -29,7 +29,7 @@ def test_ninf(self):
self.assertEqual(violations[0].rule_value, True)

def test_map_key(self):
msg = maps_pb2.MapKeys()
msg = validations_pb2.MapKeys()
msg.val[1] = "a"
violations = protovalidate.collect_violations(msg)
self.assertEqual(len(violations), 1)
Expand All @@ -38,18 +38,18 @@ def test_map_key(self):
self.assertEqual(violations[0].rule_value, 0)

def test_sfixed64(self):
msg = numbers_pb2.SFixed64ExLTGT(val=11)
msg = validations_pb2.SFixed64ExLTGT(val=11)
protovalidate.validate(msg)

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

def test_oneofs(self):
msg1 = oneofs_pb2.Oneof()
msg1 = validations_pb2.Oneof()
msg1.y = 123
protovalidate.validate(msg1)

msg2 = oneofs_pb2.Oneof()
msg2 = validations_pb2.Oneof()
msg2.z.val = True
protovalidate.validate(msg2)

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

def test_repeated(self):
msg = repeated_pb2.RepeatedEmbedSkip()
msg = validations_pb2.RepeatedEmbedSkip()
msg.val.add(val=-1)
protovalidate.validate(msg)

violations = protovalidate.collect_violations(msg)
assert len(violations) == 0

def test_maps(self):
msg = maps_pb2.MapMinMax()
msg = validations_pb2.MapMinMax()
try:
protovalidate.validate(msg)
except protovalidate.ValidationError as e:
Expand All @@ -78,7 +78,7 @@ def test_maps(self):
assert len(violations) == 1

def test_timestamp(self):
msg = wkt_timestamp_pb2.TimestampGTNow()
msg = validations_pb2.TimestampGTNow()
protovalidate.validate(msg)

violations = protovalidate.collect_violations(msg)
Expand Down
Loading