20
20
import com .mongodb .client .MongoCollection ;
21
21
import com .mongodb .client .MongoDatabase ;
22
22
import org .bson .BsonArray ;
23
+ import org .bson .BsonBoolean ;
23
24
import org .bson .BsonDocument ;
24
25
import org .bson .BsonString ;
25
26
import org .bson .BsonValue ;
@@ -70,6 +71,7 @@ public CrudTest(final String filename, final String description, final String da
70
71
@ Before
71
72
public void setUp () {
72
73
assumeFalse (skipTest );
74
+ assumeFalse (description .startsWith ("Aggregate with $listLocalSessions" ));
73
75
database = getMongoClient ().getDatabase (databaseName );
74
76
collection = database .getCollection (getClass ().getName (), BsonDocument .class );
75
77
if (!data .isEmpty ()) {
@@ -91,30 +93,51 @@ public void tearDown() {
91
93
92
94
@ Test
93
95
public void shouldPassAllOutcomes () {
94
- BsonDocument outcome = helper .getOperationResults (definition .getDocument ("operation" ));
95
- BsonDocument expectedOutcome = definition .getDocument ("outcome" );
96
-
97
- if (expectedOutcome .containsKey ("error" )) {
98
- assertEquals ("Expected error" , expectedOutcome .getBoolean ("error" ), outcome .get ("error" ));
96
+ BsonDocument expectedOutcome = definition .getDocument ("outcome" , null );
97
+ // check if v1 test
98
+ if (definition .containsKey ("operation" )) {
99
+ runOperation (expectedOutcome , definition .getDocument ("operation" ),
100
+ expectedOutcome != null && expectedOutcome .containsKey ("result" ) && expectedOutcome .isDocument ("result" )
101
+ ? expectedOutcome .get ("result" ) : null );
102
+ } else { // v2 test
103
+ BsonArray operations = definition .getArray ("operations" );
104
+ for (BsonValue operation : operations ) {
105
+ runOperation (expectedOutcome , operation .asDocument (), operation .asDocument ().get ("result" , null ));
106
+ }
99
107
}
108
+ }
100
109
101
- // Hack to workaround the lack of upsertedCount
102
- BsonValue expectedResult = expectedOutcome .get ("result" );
103
- BsonValue actualResult = outcome .get ("result" );
104
- if (actualResult .isDocument ()
105
- && actualResult .asDocument ().containsKey ("upsertedCount" )
106
- && actualResult .asDocument ().getNumber ("upsertedCount" ).intValue () == 0
107
- && !expectedResult .asDocument ().containsKey ("upsertedCount" )) {
108
- expectedResult .asDocument ().append ("upsertedCount" , actualResult .asDocument ().get ("upsertedCount" ));
110
+ private void runOperation (final BsonDocument expectedOutcome , final BsonDocument operation , final BsonValue expectedResult ) {
111
+ BsonDocument outcome = null ;
112
+ boolean wasException = false ;
113
+ try {
114
+ outcome = helper .getOperationResults (operation );
115
+ } catch (Exception e ) {
116
+ wasException = true ;
109
117
}
110
118
111
- // Hack to workaround the lack of insertedIds
112
- if (expectedResult .isDocument ()
113
- && !expectedResult .asDocument ().containsKey ("insertedIds" )) {
114
- actualResult .asDocument ().remove ("insertedIds" );
119
+ if (operation .getBoolean ("error" , BsonBoolean .FALSE ).getValue ()) {
120
+ assertEquals (operation .containsKey ("error" ), wasException );
115
121
}
116
122
117
- assertEquals (description , expectedResult , actualResult );
123
+ if (expectedResult != null ) {
124
+ // Hack to workaround the lack of upsertedCount
125
+ BsonValue actualResult = outcome .get ("result" );
126
+ if (actualResult .isDocument ()
127
+ && actualResult .asDocument ().containsKey ("upsertedCount" )
128
+ && actualResult .asDocument ().getNumber ("upsertedCount" ).intValue () == 0
129
+ && !expectedResult .asDocument ().containsKey ("upsertedCount" )) {
130
+ expectedResult .asDocument ().append ("upsertedCount" , actualResult .asDocument ().get ("upsertedCount" ));
131
+ }
132
+
133
+ // Hack to workaround the lack of insertedIds
134
+ if (expectedResult .isDocument ()
135
+ && !expectedResult .asDocument ().containsKey ("insertedIds" )) {
136
+ actualResult .asDocument ().remove ("insertedIds" );
137
+ }
138
+
139
+ assertEquals (description , expectedResult , actualResult );
140
+ }
118
141
119
142
if (expectedOutcome .containsKey ("collection" )) {
120
143
assertCollectionEquals (expectedOutcome .getDocument ("collection" ));
@@ -133,7 +156,8 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
133
156
for (BsonValue test : testDocument .getArray ("tests" )) {
134
157
data .add (new Object []{file .getName (), test .asDocument ().getString ("description" ).getValue (),
135
158
testDocument .getString ("database_name" , new BsonString (getDefaultDatabaseName ())).getValue (),
136
- testDocument .getArray ("data" ), test .asDocument (), skipTest (testDocument , test .asDocument (), getServerVersion ())});
159
+ testDocument .getArray ("data" , new BsonArray ()), test .asDocument (),
160
+ skipTest (testDocument , test .asDocument (), getServerVersion ())});
137
161
}
138
162
}
139
163
return data ;
0 commit comments