Skip to content

Commit

Permalink
fix: datetime matchers now generate a value if one is not given
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Mar 12, 2020
1 parent debebd7 commit a910840
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/v3/e2e/package.json
Expand Up @@ -6,7 +6,7 @@
"test": "npm run test:consumer && npm run test:publish && npm run test:provider",
"test:consumer": "./node_modules/.bin/mocha test/consumer.spec.js",
"test:publish": "node test/publish.js",
"test:provider": "LOG_LEVEL=debug ./node_modules/.bin/mocha test/provider.spec.js",
"test:provider": "./node_modules/.bin/mocha test/provider.spec.js",
"can-i-deploy": "npm run can-i-deploy:consumer && npm run can-i-deploy:provider",
"can-i-deploy:consumer": "$(find ../../ -name pact-broker | grep -e 'bin/pact-broker$' | head -n 1) can-i-deploy --pacticipant 'Matching Service' --latest --broker-base-url https://test.pact.dius.com.au --broker-username dXfltyFMgNOFZAxr8io9wJ37iUpY42M --broker-password O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1",
"can-i-deploy:provider": "$(find ../../ -name pact-broker | grep -e 'bin/pact-broker$' | head -n 1) can-i-deploy --pacticipant 'Animal Profile Service' --latest --broker-base-url https://test.pact.dius.com.au --broker-username dXfltyFMgNOFZAxr8io9wJ37iUpY42M --broker-password O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1",
Expand Down
2 changes: 1 addition & 1 deletion examples/v3/e2e/test/consumer.spec.js
Expand Up @@ -51,7 +51,7 @@ describe("Pact V3", () => {
// API we don't care about
const animalBodyExpectation = {
id: integer(1),
available_from: timestamp("yyyy-MM-dd'T'HH:mm:ss.SZ"),
available_from: timestamp("yyyy-MM-dd'T'HH:mm:ss.SSSX"),
first_name: string("Billy"),
last_name: string("Goat"),
animal: string("goat"),
Expand Down
22 changes: 15 additions & 7 deletions examples/v3/e2e/test/provider.spec.js
Expand Up @@ -32,32 +32,40 @@ describe("Pact Verification", () => {
},

stateHandlers: {
"Has no animals": (setup) => {
"Has no animals": setup => {
if (setup) {
animalRepository.clear()
return Promise.resolve({description: `Animals removed to the db`})
return Promise.resolve({ description: `Animals removed to the db` })
}
},
"Has some animals": (setup) => {
"Has some animals": setup => {
if (setup) {
importData()
return Promise.resolve({description: `Animals added to the db`, count: animalRepository.count()})
return Promise.resolve({
description: `Animals added to the db`,
count: animalRepository.count(),
})
}
},
"Has an animal with ID": (setup, parameters) => {
if (setup) {
importData()
animalRepository.first().id = parameters.id
return Promise.resolve({description: `Animal with ID ${parameters.id} added to the db`, id: parameters.id})
return Promise.resolve({
description: `Animal with ID ${parameters.id} added to the db`,
id: parameters.id,
})
}
},
"is not authenticated": () => {
token = ""
return Promise.resolve({description: `Invalid bearer token generated`})
return Promise.resolve({
description: `Invalid bearer token generated`,
})
},
"is authenticated": () => {
token = "1234"
return Promise.resolve({description: `Bearer token generated`})
return Promise.resolve({ description: `Bearer token generated` })
},
},

Expand Down
16 changes: 7 additions & 9 deletions src/v3/matchers.ts
@@ -1,4 +1,5 @@
import * as R from "ramda"
const PactNative = require("../../src/v3/native")

/**
* Value must match the given template
Expand Down Expand Up @@ -239,13 +240,12 @@ export function equal(value: any) {
* @param format Datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date and time will be generated.
*/
export function timestamp(format: string, example: string) {
export function timestamp(format: string, example?: string) {
return {
format, // TODO: do we need this and "timestamp: format" ?
"pact:generator:type": "DateTime",
"pact:matcher:type": "timestamp",
timestamp: format,
value: example,
value: example || PactNative.generate_datetime_string(format),
}
}

Expand All @@ -254,13 +254,12 @@ export function timestamp(format: string, example: string) {
* @param format Time format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system time will be generated.
*/
export function time(format: string, example: string) {
export function time(format: string, example?: string) {
return {
format,
"pact:generator:type": "Time",
"pact:matcher:type": "time",
time: format,
value: example,
value: example || PactNative.generate_datetime_string(format),
}
}

Expand All @@ -269,13 +268,12 @@ export function time(format: string, example: string) {
* @param format Date format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date will be generated.
*/
export function date(format: any, example: any) {
export function date(format: any, example?: string) {
return {
date: format,
format,
"pact:generator:type": "Date",
"pact:matcher:type": "date",
value: example,
value: example || PactNative.generate_datetime_string(format),
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/v3/native/src/lib.rs
Expand Up @@ -11,6 +11,7 @@ use pact_matching::models::provider_states::ProviderState;
use pact_matching::models::json_utils::json_to_string;
use pact_matching::models::matchingrules::{MatchingRules, MatchingRule, Category, RuleLogic};
use pact_matching::models::generators::{Generators, GeneratorCategory, Generator};
use pact_matching::time_utils::generate_string;
use pact_mock_server::server_manager::ServerManager;
use env_logger::{Builder, Target};
use uuid::Uuid;
Expand Down Expand Up @@ -152,6 +153,15 @@ fn generator_from_js_object<'a>(obj: Handle<JsObject>, ctx: &mut CallContext<JsP
}
}

fn generate_datetime_string(mut cx: FunctionContext) -> JsResult<JsString> {
let format = cx.argument::<JsString>(0)?;
let result = generate_string(&format.value());
match result {
Ok(value) => Ok(cx.string(value)),
Err(err) => cx.throw_error(err)
}
}

declare_types! {
pub class JsPact for Pact {
init(mut cx) {
Expand Down Expand Up @@ -481,5 +491,6 @@ register_module!(mut m, {
m.export_function("init", init)?;
m.export_class::<JsPact>("Pact")?;
m.export_function("verify_provider", verify::verify_provider)?;
m.export_function("generate_datetime_string", generate_datetime_string)?;
Ok(())
});

0 comments on commit a910840

Please sign in to comment.