-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5391: Add spec test for $$type operator with "number" #1739
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
{ | ||
"description": "operator-type-number_alias", | ||
"schemaVersion": "1.0", | ||
"createEntities": [ | ||
{ | ||
"client": { | ||
"id": "client0" | ||
} | ||
}, | ||
{ | ||
"database": { | ||
"id": "database0", | ||
"client": "client0", | ||
"databaseName": "test" | ||
} | ||
}, | ||
{ | ||
"collection": { | ||
"id": "collection0", | ||
"database": "database0", | ||
"collectionName": "coll0" | ||
} | ||
} | ||
], | ||
"initialData": [ | ||
{ | ||
"collectionName": "coll0", | ||
"databaseName": "test", | ||
"documents": [] | ||
} | ||
], | ||
"tests": [ | ||
{ | ||
"description": "type number alias matches int32", | ||
"operations": [ | ||
{ | ||
"name": "insertOne", | ||
"object": "collection0", | ||
"arguments": { | ||
"document": { | ||
"_id": 1, | ||
"x": { | ||
"$numberInt": "2147483647" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "find", | ||
"object": "collection0", | ||
"arguments": { | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"limit": 1 | ||
}, | ||
"expectResult": [ | ||
{ | ||
"_id": 1, | ||
"x": { | ||
"$$type": "number" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "type number alias matches int64", | ||
"operations": [ | ||
{ | ||
"name": "insertOne", | ||
"object": "collection0", | ||
"arguments": { | ||
"document": { | ||
"_id": 1, | ||
"x": { | ||
"$numberLong": "9223372036854775807" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "find", | ||
"object": "collection0", | ||
"arguments": { | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"limit": 1 | ||
}, | ||
"expectResult": [ | ||
{ | ||
"_id": 1, | ||
"x": { | ||
"$$type": "number" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "type number alias matches double", | ||
"operations": [ | ||
{ | ||
"name": "insertOne", | ||
"object": "collection0", | ||
"arguments": { | ||
"document": { | ||
"_id": 1, | ||
"x": { | ||
"$numberDouble": "2.71828" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "find", | ||
"object": "collection0", | ||
"arguments": { | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"limit": 1 | ||
}, | ||
"expectResult": [ | ||
{ | ||
"_id": 1, | ||
"x": { | ||
"$$type": "number" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "type number alias matches decimal128", | ||
"operations": [ | ||
{ | ||
"name": "insertOne", | ||
"object": "collection0", | ||
"arguments": { | ||
"document": { | ||
"_id": 1, | ||
"x": { | ||
"$numberDecimal": "3.14159" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "find", | ||
"object": "collection0", | ||
"arguments": { | ||
"filter": { | ||
"_id": 1 | ||
}, | ||
"limit": 1 | ||
}, | ||
"expectResult": [ | ||
{ | ||
"_id": 1, | ||
"x": { | ||
"$$type": "number" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
description: operator-type-number_alias | ||
|
||
schemaVersion: "1.0" | ||
|
||
createEntities: | ||
- client: | ||
id: &client0 client0 | ||
- database: | ||
id: &database0 database0 | ||
client: *client0 | ||
databaseName: &database0Name test | ||
- collection: | ||
id: &collection0 collection0 | ||
database: *database0 | ||
collectionName: &collection0Name coll0 | ||
|
||
initialData: | ||
- collectionName: *collection0Name | ||
databaseName: *database0Name | ||
documents: [] | ||
|
||
tests: | ||
- | ||
description: type number alias matches int32 | ||
operations: | ||
- name: insertOne | ||
object: *collection0 | ||
arguments: | ||
document: { _id: 1, x: { $numberInt: "2147483647" } } | ||
- &find | ||
name: find | ||
object: *collection0 | ||
arguments: | ||
filter: { _id: 1 } | ||
limit: 1 | ||
expectResult: | ||
- { _id: 1, x: { $$type: "number" } } | ||
- | ||
description: type number alias matches int64 | ||
operations: | ||
- name: insertOne | ||
object: *collection0 | ||
arguments: | ||
document: { _id: 1, x: { $numberLong: "9223372036854775807" } } | ||
- *find | ||
- | ||
description: type number alias matches double | ||
operations: | ||
- name: insertOne | ||
object: *collection0 | ||
arguments: | ||
document: { _id: 1, x: { $numberDouble: "2.71828" } } | ||
- *find | ||
- | ||
description: type number alias matches decimal128 | ||
operations: | ||
- name: insertOne | ||
object: *collection0 | ||
arguments: | ||
document: { _id: 1, x: { $numberDecimal: "3.14159" } } | ||
- *find |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
*/ | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
|
@@ -27,6 +28,7 @@ namespace MongoDB.Driver.Tests.UnifiedTestOperations.Matchers | |
public class UnifiedValueMatcher | ||
{ | ||
private UnifiedEntityMap _entityMap; | ||
private static readonly HashSet<string> NumericTypes = ["int", "long", "double", "decimal"]; | ||
|
||
public UnifiedValueMatcher(UnifiedEntityMap entityMap) | ||
{ | ||
|
@@ -193,22 +195,30 @@ private void AssertValuesMatch(BsonValue actual, BsonValue expected, bool isRoot | |
private void AssertExpectedType(BsonValue actual, BsonValue expectedTypes) | ||
{ | ||
var actualTypeName = GetBsonTypeNameAsString(actual.BsonType); | ||
List<string> expectedTypeNames; | ||
|
||
if (expectedTypes.IsString) | ||
{ | ||
expectedTypeNames = new List<string> { expectedTypes.AsString }; | ||
var expectedType = expectedTypes.AsString; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of asserting differently in more than one place keep the existing assert at the end of this method and change how
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I leave it to you to verify that all tests still pass with this proposed refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, does In the json file you are adding it always appears by itself. So the changes to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to also modify the BsonArray case as the "number" value could be used anywhere. Currently there are no instances in our spec tests where it is used as an element of an array but in the future we could have a spec test that uses the "number" value in an array. However, it is untested functionality currently and updating the array use case in the future will be easy so I'll defer it. |
||
if (expectedType == "number") | ||
{ | ||
NumericTypes.Should().Contain(actualTypeName); | ||
} | ||
else | ||
{ | ||
actualTypeName.Should().Be(expectedType); | ||
} | ||
} | ||
else if (expectedTypes.IsBsonArray) | ||
{ | ||
expectedTypeNames = expectedTypes.AsBsonArray.Select(t => t.AsString).ToList(); | ||
expectedTypes.AsBsonArray.Any(t => | ||
t.AsString == actualTypeName || | ||
(t.AsString == "number" && NumericTypes.Contains(actualTypeName))) | ||
.Should().BeTrue(); | ||
} | ||
else | ||
{ | ||
throw new FormatException($"Unexpected $$type value BsonType: '{expectedTypes.BsonType}'."); | ||
} | ||
|
||
actualTypeName.Should().BeOneOf(expectedTypeNames); | ||
adelinowona marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private string GetBsonTypeNameAsString(BsonType bsonType) | ||
|
Uh oh!
There was an error while loading. Please reload this page.