Initial commit
This commit is contained in:
45
betterproto/tests/generate.py
Normal file
45
betterproto/tests/generate.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
import os # isort: skip
|
||||
|
||||
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
|
||||
|
||||
|
||||
import subprocess
|
||||
import importlib
|
||||
from typing import Generator, Tuple
|
||||
|
||||
from google.protobuf.json_format import Parse
|
||||
from google.protobuf import symbol_database
|
||||
from google.protobuf.descriptor_pool import DescriptorPool
|
||||
|
||||
root = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
def get_files(end: str) -> Generator[Tuple[str, str], None, None]:
|
||||
for r, dirs, files in os.walk(root):
|
||||
for filename in [f for f in files if f.endswith(end)]:
|
||||
parts = os.path.splitext(filename)[0].split("-")
|
||||
yield [parts[0], os.path.join(r, filename)]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.chdir(root)
|
||||
|
||||
for base, filename in get_files(".proto"):
|
||||
subprocess.run(
|
||||
f"protoc --python_out=. {os.path.basename(filename)}", shell=True
|
||||
)
|
||||
subprocess.run(
|
||||
f"protoc --plugin=protoc-gen-custom=../../protoc-gen-betterpy.py --custom_out=. {os.path.basename(filename)}",
|
||||
shell=True,
|
||||
)
|
||||
|
||||
for base, filename in get_files(".json"):
|
||||
# Reset the internal symbol database so we can import the `Test` message
|
||||
# multiple times. Ugh.
|
||||
sym = symbol_database.Default()
|
||||
sym.pool = DescriptorPool()
|
||||
imported = importlib.import_module(f"{base}_pb2")
|
||||
out = filename.replace(".json", ".bin")
|
||||
serialized = Parse(open(filename).read(), imported.Test()).SerializeToString()
|
||||
open(out, "wb").write(serialized)
|
||||
3
betterproto/tests/int32-negative.json
Normal file
3
betterproto/tests/int32-negative.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"count": -150
|
||||
}
|
||||
3
betterproto/tests/int32.json
Normal file
3
betterproto/tests/int32.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"count": 150
|
||||
}
|
||||
7
betterproto/tests/int32.proto
Normal file
7
betterproto/tests/int32.proto
Normal file
@@ -0,0 +1,7 @@
|
||||
syntax = "proto3";
|
||||
|
||||
// Some documentation about the Test message.
|
||||
message Test {
|
||||
// Some documentation about the count.
|
||||
int32 count = 1;
|
||||
}
|
||||
5
betterproto/tests/nested.json
Normal file
5
betterproto/tests/nested.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"nested": {
|
||||
"count": 150
|
||||
}
|
||||
}
|
||||
17
betterproto/tests/nested.proto
Normal file
17
betterproto/tests/nested.proto
Normal file
@@ -0,0 +1,17 @@
|
||||
syntax = "proto3";
|
||||
|
||||
// A test message with a nested message inside of it.
|
||||
message Test {
|
||||
// This is the nested type.
|
||||
message Nested {
|
||||
// Stores a simple counter.
|
||||
int32 count = 1;
|
||||
}
|
||||
|
||||
Nested nested = 1;
|
||||
Sibling sibling = 2;
|
||||
}
|
||||
|
||||
message Sibling {
|
||||
int32 foo = 1;
|
||||
}
|
||||
3
betterproto/tests/repeated.json
Normal file
3
betterproto/tests/repeated.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"names": ["one", "two", "three"]
|
||||
}
|
||||
5
betterproto/tests/repeated.proto
Normal file
5
betterproto/tests/repeated.proto
Normal file
@@ -0,0 +1,5 @@
|
||||
syntax = "proto3";
|
||||
|
||||
message Test {
|
||||
repeated string names = 1;
|
||||
}
|
||||
3
betterproto/tests/repeatedpacked.json
Normal file
3
betterproto/tests/repeatedpacked.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"counts": [1, 2, -1, -2]
|
||||
}
|
||||
5
betterproto/tests/repeatedpacked.proto
Normal file
5
betterproto/tests/repeatedpacked.proto
Normal file
@@ -0,0 +1,5 @@
|
||||
syntax = "proto3";
|
||||
|
||||
message Test {
|
||||
repeated int32 counts = 1;
|
||||
}
|
||||
4
betterproto/tests/signed-negative.json
Normal file
4
betterproto/tests/signed-negative.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"signed_32": -150,
|
||||
"signed_64": -150
|
||||
}
|
||||
4
betterproto/tests/signed.json
Normal file
4
betterproto/tests/signed.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"signed_32": 150,
|
||||
"signed_64": 150
|
||||
}
|
||||
6
betterproto/tests/signed.proto
Normal file
6
betterproto/tests/signed.proto
Normal file
@@ -0,0 +1,6 @@
|
||||
syntax = "proto3";
|
||||
|
||||
message Test {
|
||||
sint32 signed_32 = 1;
|
||||
sint64 signed_64 = 2;
|
||||
}
|
||||
23
betterproto/tests/test_inputs.py
Normal file
23
betterproto/tests/test_inputs.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import importlib
|
||||
import pytest
|
||||
import json
|
||||
|
||||
from generate import get_files
|
||||
|
||||
inputs = get_files(".bin")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name,filename", inputs)
|
||||
def test_sample(name: str, filename: str) -> None:
|
||||
imported = importlib.import_module(name)
|
||||
data_binary = open(filename, "rb").read()
|
||||
data_dict = json.loads(open(filename.replace(".bin", ".json")).read())
|
||||
t1 = imported.Test().parse(data_binary)
|
||||
t2 = imported.Test().from_dict(data_dict)
|
||||
print(t1)
|
||||
print(t2)
|
||||
assert t1 == t2
|
||||
assert bytes(t1) == data_binary
|
||||
assert bytes(t2) == data_binary
|
||||
assert t1.to_dict() == data_dict
|
||||
assert t2.to_dict() == data_dict
|
||||
Reference in New Issue
Block a user