Skip to content

Commit

Permalink
chore: add simple enum test with repeated field pactflow#27
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen authored and opicaud committed Sep 6, 2023
1 parent cfd51a9 commit dd612ab
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 46 deletions.
72 changes: 32 additions & 40 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod protobuf;
mod message_builder;
pub mod message_decoder;
pub mod utils;
mod matching;
pub mod matching;
pub mod mock_server;
pub mod tcp;
pub mod dynamic_message;
Expand Down
2 changes: 1 addition & 1 deletion src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) fn compare(
skip_all,
fields(%path)
)]
fn compare_message(
pub fn compare_message(
path: DocPath,
expected_message_fields: &[ProtobufField],
actual_message_fields: &[ProtobufField],
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,15 @@ pub(crate) fn parse_pact_from_request_json(pact_json: &str, source: &str) -> any
}

/// Lookup up the interaction in the Pact file, given the ID
pub(crate) fn lookup_interaction_by_id<'a>(
pub fn lookup_interaction_by_id<'a>(
interaction_key: &str,
pact: &'a V4Pact
) -> Option<&'a Box<dyn V4Interaction + Send + Sync + RefUnwindSafe>> {
pact.interactions.iter()
.find(|i| i.unique_key() == interaction_key)
}

pub(crate) fn lookup_interaction_config(interaction: &dyn V4Interaction) -> Option<HashMap<String, serde_json::Value>> {
pub fn lookup_interaction_config(interaction: &dyn V4Interaction) -> Option<HashMap<String, serde_json::Value>> {
interaction.plugin_config().iter()
.find_map(|(key, value)| {
if key.as_str() == "protobuf" {
Expand Down Expand Up @@ -372,7 +372,7 @@ pub(crate) fn lookup_service_descriptors_for_interaction(
}

/// Get the encoded Protobuf descriptors from the Pact level configuration for the message key
pub(crate) fn get_descriptors_for_interaction(
pub fn get_descriptors_for_interaction(
message_key: &str,
plugin_config: &BTreeMap<String, serde_json::Value>
) -> anyhow::Result<FileDescriptorSet> {
Expand Down
20 changes: 20 additions & 0 deletions tests/enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";

enum Values {
NONE = 0;
A = 1;
B = 2;
C = 3;
}

message MessageIn {
repeated Values in = 1;
}

message MessageOut {
repeated Values out = 1;
}

service Test {
rpc GetTest(MessageIn) returns (MessageOut) {}
}
90 changes: 90 additions & 0 deletions tests/enum_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::path::Path;

use expectest::prelude::*;
use maplit::hashmap;
use pact_consumer::builders::PactBuilderAsync;
use pact_matching::{BodyMatchResult, CoreMatchingContext, DiffConfig};
use pact_models::json_utils::json_to_string;
use pact_models::path_exp::DocPath;
use prost::encoding::WireType;
use serde_json::json;
use pact_protobuf_plugin::matching::compare_message;
use pact_protobuf_plugin::message_decoder::{ProtobufField, ProtobufFieldData};
use pact_protobuf_plugin::utils::{find_message_type_by_name, get_descriptors_for_interaction, lookup_interaction_config};

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn repeated_enum_test() {
let mut pact_builder = PactBuilderAsync::new_v4("repeated_enum", "protobuf-plugin");
pact_builder
.using_plugin("protobuf", None).await
.message_interaction("get a list of enums", |mut i| async move {
let proto_file = Path::new("tests/enum.proto")
.canonicalize().unwrap().to_string_lossy().to_string();
i.contents_from(json!({
"pact:proto": proto_file,
"pact:message-type": "MessageIn",
"pact:content-type": "application/protobuf",

"in": [
"matching(equalTo, 'A')"
]
})).await;
i
})
.await;

let pact = pact_builder.build().as_v4_pact().unwrap();
let plugin_config = pact.plugin_data.iter()
.find(|data| data.name == "protobuf")
.map(|data| &data.configuration)
.unwrap()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

for message in pact_builder.messages() {
let interaction_config = lookup_interaction_config(&message).unwrap();
let descriptor_key = interaction_config.get("descriptorKey")
.map(json_to_string).unwrap();
let fds = get_descriptors_for_interaction(descriptor_key.as_str(),
&plugin_config).unwrap();
let path = DocPath::root();
let context = CoreMatchingContext::new(DiffConfig::NoUnexpectedKeys,
&message.contents.matching_rules.rules_for_category("body").unwrap(), &hashmap!{});
let (message_descriptor, fs) = find_message_type_by_name("MessageIn", &fds).unwrap();
let enum_descriptor = fs.enum_type.first().unwrap();
let expected = vec![
ProtobufField {
field_num: 1,
field_name: "in".to_string(),
wire_type: WireType::LengthDelimited,
data: ProtobufFieldData::Enum(1, enum_descriptor.clone())
}
];
let actual = vec![
ProtobufField {
field_num: 1,
field_name: "in".to_string(),
wire_type: WireType::LengthDelimited,
data: ProtobufFieldData::Enum(1, enum_descriptor.clone())
},
// ProtobufField {
// field_num: 1,
// field_name: "in".to_string(),
// wire_type: WireType::LengthDelimited,
// data: ProtobufFieldData::Enum(1, enum_descriptor.clone())
// }
];

let result = compare_message(
path,
&expected,
&actual,
&context,
&message_descriptor,
&fds,
).unwrap();

expect!(result).to(be_equal_to(BodyMatchResult::Ok));
}
}
3 changes: 2 additions & 1 deletion tests/mock_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn mock_server_with_no_requests() {
}
}

#[test_log::test(tokio::test(flavor = "multi_thread", worker_threads = 2))]
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn each_value_matcher() {
let mut pact_builder = PactBuilderAsync::new_v4("each-value", "protobuf-plugin");
pact_builder
Expand Down Expand Up @@ -164,3 +164,4 @@ async fn each_value_matcher() {
let path = http::uri::PathAndQuery::try_from("/Test/GetValues").unwrap();
grpc.unary(Request::new(message), path, codec).await.unwrap();
}

0 comments on commit dd612ab

Please sign in to comment.