Skip to content
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

Sync valid-pass unified format tests #1342

Merged
merged 6 commits into from
Mar 25, 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
13 changes: 13 additions & 0 deletions src/bson_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ pub(crate) fn get_int(val: &Bson) -> Option<i64> {
}
}

/// Coerce numeric types into an `f64` if it would be lossless to do so. If this Bson is not numeric
/// or the conversion would be lossy (e.g. 1.5 -> 1), this returns `None`.
#[cfg(test)]
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn get_double(val: &Bson) -> Option<f64> {
match *val {
Bson::Int32(i) => Some(f64::from(i)),
Bson::Int64(i) if i == i as f64 as i64 => Some(i as f64),
Bson::Double(f) => Some(f),
_ => None,
}
}

/// Coerce numeric types into an `i64` if it would be lossless to do so. If this Bson is not numeric
/// or the conversion would be lossy (e.g. 1.5 -> 1), this returns `None`.
pub(crate) fn get_int_raw(val: RawBsonRef<'_>) -> Option<i64> {
Expand Down
24 changes: 21 additions & 3 deletions src/test/spec/json/unified-test-format/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SCHEMA=../schema-1.13.json
SCHEMA=../schema-1.23.json

.PHONY: all invalid valid-fail valid-pass versioned-api load-balancers gridfs transactions crud collection-management sessions command-logging-and-monitoring client-side-operations-timeout HAS_AJV
.PHONY: all invalid valid-fail valid-pass atlas-data-lake versioned-api load-balancers gridfs transactions transactions-convenient-api crud collection-management read-write-concern retryable-reads retryable-writes sessions command-logging-and-monitoring client-side-operations-timeout HAS_AJV

all: invalid valid-fail valid-pass versioned-api load-balancers gridfs transactions change-streams crud collection-management sessions command-logging-and-monitoring client-side-operations-timeout
all: invalid valid-fail valid-pass atlas-data-lake versioned-api load-balancers gridfs transactions transactions-convenient-api change-streams crud collection-management read-write-concern retryable-reads retryable-writes sessions command-logging-and-monitoring client-side-operations-timeout client-side-encryption

invalid: HAS_AJV
@# Redirect stdout to hide expected validation errors
Expand All @@ -14,6 +14,9 @@ valid-fail: HAS_AJV
valid-pass: HAS_AJV
@ajv test -s $(SCHEMA) -d "valid-pass/*.yml" --valid

atlas-data-lake: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../atlas-data-lake-testing/tests/unified/*.yml" --valid

versioned-api: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../versioned-api/tests/*.yml" --valid

Expand All @@ -26,6 +29,9 @@ gridfs: HAS_AJV
transactions: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../transactions/tests/unified/*.yml" --valid

transactions-convenient-api: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../transactions-convenient-api/tests/unified/*.yml" --valid

change-streams: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../change-streams/tests/unified/*.yml" --valid

Expand All @@ -38,13 +44,25 @@ crud: HAS_AJV
collection-management: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../collection-management/tests/*.yml" --valid

read-write-concern: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../read-write-concern/tests/operation/*.yml" --valid

retryable-reads: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../retryable-reads/tests/unified/*.yml" --valid

retryable-writes: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../retryable-writes/tests/unified/*.yml" --valid

sessions: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../sessions/tests/*.yml" --valid

command-logging-and-monitoring: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../command-logging-and-monitoring/tests/logging/*.yml" --valid
@ajv test -s $(SCHEMA) -d "../../command-logging-and-monitoring/tests/monitoring/*.yml" --valid

client-side-encryption: HAS_AJV
@ajv test -s $(SCHEMA) -d "../../client-side-encryption/tests/unified/*.yml" --valid

HAS_AJV:
@if ! command -v ajv > /dev/null; then \
echo 'Error: need "npm install -g ajv-cli"' 1>&2; \
Expand Down
32 changes: 32 additions & 0 deletions src/test/spec/json/unified-test-format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Unified Test Format Tests

______________________________________________________________________

## Introduction

This directory contains tests for the Unified Test Format's schema and test runner implementation(s). Tests are
organized in the following directories:

- `invalid`: These files do not validate against the schema and are used to test the schema itself.
- `valid-pass`: These files validate against the schema and should pass when executed with a test runner.
- `valid-fail`: These files validate against the schema but should produce runtime errors or failures when executed with
a test runner. Some do so by violating the "SHOULD" and "SHOULD NOT" guidance in the spec (e.g. referencing an
undefined entity).

## Validating Test Files

JSON and YAML test files can be validated using [Ajv](https://ajv.js.org/) and a schema from the parent directory (e.g.
`schema-1.0.json`).

Test files can be validated individually like so:

```bash
ajv -s ../schema-1.0.json -d path/to/test.yml
```

Ajv can also be used to assert the validity of test files:

```bash
ajv test -s ../schema-1.0.json -d "invalid/*.yml" --invalid
ajv test -s ../schema-1.0.json -d "valid-*/*.yml" --valid
```
39 changes: 0 additions & 39 deletions src/test/spec/json/unified-test-format/README.rst

This file was deleted.

Loading