Skip to content

Commit 16f231b

Browse files
committed
CSHARP-5391: Add spec test for $$type operator with "number"
1 parent d2802b6 commit 16f231b

File tree

3 files changed

+250
-5
lines changed

3 files changed

+250
-5
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
{
2+
"description": "operator-type-number_alias",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0"
8+
}
9+
},
10+
{
11+
"database": {
12+
"id": "database0",
13+
"client": "client0",
14+
"databaseName": "test"
15+
}
16+
},
17+
{
18+
"collection": {
19+
"id": "collection0",
20+
"database": "database0",
21+
"collectionName": "coll0"
22+
}
23+
}
24+
],
25+
"initialData": [
26+
{
27+
"collectionName": "coll0",
28+
"databaseName": "test",
29+
"documents": []
30+
}
31+
],
32+
"tests": [
33+
{
34+
"description": "type number alias matches int32",
35+
"operations": [
36+
{
37+
"name": "insertOne",
38+
"object": "collection0",
39+
"arguments": {
40+
"document": {
41+
"_id": 1,
42+
"x": {
43+
"$numberInt": "2147483647"
44+
}
45+
}
46+
}
47+
},
48+
{
49+
"name": "find",
50+
"object": "collection0",
51+
"arguments": {
52+
"filter": {
53+
"_id": 1
54+
},
55+
"limit": 1
56+
},
57+
"expectResult": [
58+
{
59+
"_id": 1,
60+
"x": {
61+
"$$type": "number"
62+
}
63+
}
64+
]
65+
}
66+
]
67+
},
68+
{
69+
"description": "type number alias matches int64",
70+
"operations": [
71+
{
72+
"name": "insertOne",
73+
"object": "collection0",
74+
"arguments": {
75+
"document": {
76+
"_id": 1,
77+
"x": {
78+
"$numberLong": "9223372036854775807"
79+
}
80+
}
81+
}
82+
},
83+
{
84+
"name": "find",
85+
"object": "collection0",
86+
"arguments": {
87+
"filter": {
88+
"_id": 1
89+
},
90+
"limit": 1
91+
},
92+
"expectResult": [
93+
{
94+
"_id": 1,
95+
"x": {
96+
"$$type": "number"
97+
}
98+
}
99+
]
100+
}
101+
]
102+
},
103+
{
104+
"description": "type number alias matches double",
105+
"operations": [
106+
{
107+
"name": "insertOne",
108+
"object": "collection0",
109+
"arguments": {
110+
"document": {
111+
"_id": 1,
112+
"x": {
113+
"$numberDouble": "2.71828"
114+
}
115+
}
116+
}
117+
},
118+
{
119+
"name": "find",
120+
"object": "collection0",
121+
"arguments": {
122+
"filter": {
123+
"_id": 1
124+
},
125+
"limit": 1
126+
},
127+
"expectResult": [
128+
{
129+
"_id": 1,
130+
"x": {
131+
"$$type": "number"
132+
}
133+
}
134+
]
135+
}
136+
]
137+
},
138+
{
139+
"description": "type number alias matches decimal128",
140+
"operations": [
141+
{
142+
"name": "insertOne",
143+
"object": "collection0",
144+
"arguments": {
145+
"document": {
146+
"_id": 1,
147+
"x": {
148+
"$numberDecimal": "3.14159"
149+
}
150+
}
151+
}
152+
},
153+
{
154+
"name": "find",
155+
"object": "collection0",
156+
"arguments": {
157+
"filter": {
158+
"_id": 1
159+
},
160+
"limit": 1
161+
},
162+
"expectResult": [
163+
{
164+
"_id": 1,
165+
"x": {
166+
"$$type": "number"
167+
}
168+
}
169+
]
170+
}
171+
]
172+
}
173+
]
174+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
description: operator-type-number_alias
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
- database:
9+
id: &database0 database0
10+
client: *client0
11+
databaseName: &database0Name test
12+
- collection:
13+
id: &collection0 collection0
14+
database: *database0
15+
collectionName: &collection0Name coll0
16+
17+
initialData:
18+
- collectionName: *collection0Name
19+
databaseName: *database0Name
20+
documents: []
21+
22+
tests:
23+
-
24+
description: type number alias matches int32
25+
operations:
26+
- name: insertOne
27+
object: *collection0
28+
arguments:
29+
document: { _id: 1, x: { $numberInt: "2147483647" } }
30+
- &find
31+
name: find
32+
object: *collection0
33+
arguments:
34+
filter: { _id: 1 }
35+
limit: 1
36+
expectResult:
37+
- { _id: 1, x: { $$type: "number" } }
38+
-
39+
description: type number alias matches int64
40+
operations:
41+
- name: insertOne
42+
object: *collection0
43+
arguments:
44+
document: { _id: 1, x: { $numberLong: "9223372036854775807" } }
45+
- *find
46+
-
47+
description: type number alias matches double
48+
operations:
49+
- name: insertOne
50+
object: *collection0
51+
arguments:
52+
document: { _id: 1, x: { $numberDouble: "2.71828" } }
53+
- *find
54+
-
55+
description: type number alias matches decimal128
56+
operations:
57+
- name: insertOne
58+
object: *collection0
59+
arguments:
60+
document: { _id: 1, x: { $numberDecimal: "3.14159" } }
61+
- *find

tests/MongoDB.Driver.Tests/UnifiedTestOperations/Matchers/UnifiedValueMatcher.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Collections;
1718
using System.Collections.Generic;
1819
using System.Linq;
1920
using FluentAssertions;
@@ -27,6 +28,7 @@ namespace MongoDB.Driver.Tests.UnifiedTestOperations.Matchers
2728
public class UnifiedValueMatcher
2829
{
2930
private UnifiedEntityMap _entityMap;
31+
private static readonly HashSet<string> NumericTypes = ["int", "long", "double", "decimal"];
3032

3133
public UnifiedValueMatcher(UnifiedEntityMap entityMap)
3234
{
@@ -193,22 +195,30 @@ private void AssertValuesMatch(BsonValue actual, BsonValue expected, bool isRoot
193195
private void AssertExpectedType(BsonValue actual, BsonValue expectedTypes)
194196
{
195197
var actualTypeName = GetBsonTypeNameAsString(actual.BsonType);
196-
List<string> expectedTypeNames;
197198

198199
if (expectedTypes.IsString)
199200
{
200-
expectedTypeNames = new List<string> { expectedTypes.AsString };
201+
var expectedType = expectedTypes.AsString;
202+
if (expectedType == "number")
203+
{
204+
NumericTypes.Should().Contain(actualTypeName);
205+
}
206+
else
207+
{
208+
actualTypeName.Should().Be(expectedType);
209+
}
201210
}
202211
else if (expectedTypes.IsBsonArray)
203212
{
204-
expectedTypeNames = expectedTypes.AsBsonArray.Select(t => t.AsString).ToList();
213+
expectedTypes.AsBsonArray.Any(t =>
214+
t.AsString == actualTypeName ||
215+
(t.AsString == "number" && NumericTypes.Contains(actualTypeName)))
216+
.Should().BeTrue();
205217
}
206218
else
207219
{
208220
throw new FormatException($"Unexpected $$type value BsonType: '{expectedTypes.BsonType}'.");
209221
}
210-
211-
actualTypeName.Should().BeOneOf(expectedTypeNames);
212222
}
213223

214224
private string GetBsonTypeNameAsString(BsonType bsonType)

0 commit comments

Comments
 (0)