1717package com .google .cloud .firestore .it ;
1818
1919import static com .google .cloud .firestore .FieldValue .vector ;
20- import static com .google .cloud .firestore .LocalFirestoreHelper .query ;
2120import static com .google .cloud .firestore .it .ITQueryTest .map ;
2221import static com .google .cloud .firestore .it .TestHelper .isRunningAgainstFirestoreEmulator ;
2322import static com .google .cloud .firestore .pipeline .expressions .AggregateFunction .count ;
5150import static com .google .cloud .firestore .pipeline .expressions .Expression .log ;
5251import static com .google .cloud .firestore .pipeline .expressions .Expression .logicalMaximum ;
5352import static com .google .cloud .firestore .pipeline .expressions .Expression .logicalMinimum ;
54- import static com .google .cloud .firestore .pipeline .expressions .Expression .mapEntries ;
55- import static com .google .cloud .firestore .pipeline .expressions .Expression .mapKeys ;
5653import static com .google .cloud .firestore .pipeline .expressions .Expression .mapMerge ;
5754import static com .google .cloud .firestore .pipeline .expressions .Expression .mapRemove ;
58- import static com .google .cloud .firestore .pipeline .expressions .Expression .mapSet ;
59- import static com .google .cloud .firestore .pipeline .expressions .Expression .mapValues ;
6055import static com .google .cloud .firestore .pipeline .expressions .Expression .notEqual ;
6156import static com .google .cloud .firestore .pipeline .expressions .Expression .nullValue ;
6257import static com .google .cloud .firestore .pipeline .expressions .Expression .or ;
8681import com .google .cloud .Timestamp ;
8782import com .google .cloud .firestore .Blob ;
8883import com .google .cloud .firestore .CollectionReference ;
89- import com .google .cloud .firestore .DocumentReference ;
9084import com .google .cloud .firestore .Firestore ;
9185import com .google .cloud .firestore .FirestoreOptions ;
9286import com .google .cloud .firestore .GeoPoint ;
112106import com .google .common .collect .ImmutableList ;
113107import com .google .common .collect .ImmutableMap ;
114108import com .google .common .collect .Lists ;
115- import java .util .Arrays ;
116109import java .util .Date ;
117110import java .util .HashMap ;
118111import java .util .List ;
@@ -1384,7 +1377,8 @@ public void testMapGet() throws Exception {
13841377 map ("hugoAward" , true , "title" , "The Hitchhiker's Guide to the Galaxy" ),
13851378 map ("hugoAward" , true , "title" , "Dune" )));
13861379 }
1387- @ Test
1380+
1381+ @ Test
13881382 public void testMapSet () throws Exception {
13891383 Map <String , Object > docData = new HashMap <>();
13901384 docData .put ("existingField" , ImmutableMap .of ("foo" , 1L ));
@@ -1400,18 +1394,30 @@ public void testMapSet() throws Exception {
14001394 Expression .mapSet (constant (ImmutableMap .of ()), "a" , 1 ).as ("simple" ),
14011395 Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "b" , 2 ).as ("add" ),
14021396 Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "a" , 2 ).as ("overwrite" ),
1403- Expression .mapSet (constant (ImmutableMap .of ("a" , 1 , "b" , 2 )), "a" , 3 , "c" , 4 ).as ("multi" ),
1404- Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "a" , field ("non_existent" )).as ("remove" ),
1397+ Expression .mapSet (constant (ImmutableMap .of ("a" , 1 , "b" , 2 )), "a" , 3 , "c" , 4 )
1398+ .as ("multi" ),
1399+ Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "a" , field ("non_existent" ))
1400+ .as ("remove" ),
14051401 Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "b" , null ).as ("setNull" ),
1406- Expression .mapSet (constant (ImmutableMap .of ("a" , ImmutableMap .of ("b" , 1 ))), "a.b" , 2 ).as ("setDotted" ),
1402+ Expression .mapSet (constant (ImmutableMap .of ("a" , ImmutableMap .of ("b" , 1 ))), "a.b" , 2 )
1403+ .as ("setDotted" ),
14071404 Expression .mapSet (constant (ImmutableMap .of ()), "" , "empty" ).as ("setEmptyKey" ),
1408- Expression .mapSet (constant (ImmutableMap .of ("a" , 1 )), "b" , Expression .add (constant (1 ), constant (2 ))).as ("setExprVal" ),
1409- Expression .mapSet (constant (ImmutableMap .of ()), "obj" , constant (ImmutableMap .of ("hidden" , true ))).as ("setNestedMap" ),
1410- Expression .mapSet (constant (ImmutableMap .of ()), "~!@#$%^&*()_+" , "special" ).as ("setSpecialChars" ),
1411-
1405+ Expression .mapSet (
1406+ constant (ImmutableMap .of ("a" , 1 )),
1407+ "b" ,
1408+ Expression .add (constant (1 ), constant (2 )))
1409+ .as ("setExprVal" ),
1410+ Expression .mapSet (
1411+ constant (ImmutableMap .of ()),
1412+ "obj" ,
1413+ constant (ImmutableMap .of ("hidden" , true )))
1414+ .as ("setNestedMap" ),
1415+ Expression .mapSet (constant (ImmutableMap .of ()), "~!@#$%^&*()_+" , "special" )
1416+ .as ("setSpecialChars" ),
14121417 field ("existingField" ).mapSet ("instanceKey" , 100 ).as ("instanceSetField" ),
1413- constant (ImmutableMap .of ("x" , 1 )).mapSet (constant ("y" ), constant (2 )).as ("instanceSetConstant" )
1414- )
1418+ constant (ImmutableMap .of ("x" , 1 ))
1419+ .mapSet (constant ("y" ), constant (2 ))
1420+ .as ("instanceSetConstant" ))
14151421 .execute ()
14161422 .get ();
14171423
@@ -1426,17 +1432,19 @@ public void testMapSet() throws Exception {
14261432 assertThat ((Map <?, ?>) data .get ("multi" )).containsExactly ("a" , 3L , "b" , 2L , "c" , 4L );
14271433 assertThat ((Map <?, ?>) data .get ("remove" )).isEmpty ();
14281434 assertThat ((Map <?, ?>) data .get ("setNull" )).containsExactly ("a" , 1L , "b" , null );
1429-
1435+
14301436 Map <?, ?> setDotted = (Map <?, ?>) data .get ("setDotted" );
14311437 assertThat (setDotted ).containsEntry ("a.b" , 2L );
14321438 assertThat ((Map <?, ?>) setDotted .get ("a" )).containsExactly ("b" , 1L );
14331439
14341440 assertThat ((Map <?, ?>) data .get ("setEmptyKey" )).containsExactly ("" , "empty" );
14351441 assertThat ((Map <?, ?>) data .get ("setExprVal" )).containsExactly ("a" , 1L , "b" , 3L );
1436- assertThat ((Map <?, ?>) data .get ("setNestedMap" )).isEqualTo (ImmutableMap .of ("obj" , ImmutableMap .of ("hidden" , true )));
1442+ assertThat ((Map <?, ?>) data .get ("setNestedMap" ))
1443+ .isEqualTo (ImmutableMap .of ("obj" , ImmutableMap .of ("hidden" , true )));
14371444 assertThat ((Map <?, ?>) data .get ("setSpecialChars" )).containsExactly ("~!@#$%^&*()_+" , "special" );
14381445
1439- assertThat ((Map <?, ?>) data .get ("instanceSetField" )).containsExactly ("foo" , 1L , "instanceKey" , 100L );
1446+ assertThat ((Map <?, ?>) data .get ("instanceSetField" ))
1447+ .containsExactly ("foo" , 1L , "instanceKey" , 100L );
14401448 assertThat ((Map <?, ?>) data .get ("instanceSetConstant" )).containsExactly ("x" , 1L , "y" , 2L );
14411449 }
14421450
@@ -1455,11 +1463,10 @@ public void testMapKeys() throws Exception {
14551463 Expression .mapKeys ("existingField" ).as ("existingKeys" ),
14561464 Expression .mapKeys (constant (ImmutableMap .of ("a" , 1 , "b" , 2 ))).as ("keys" ),
14571465 Expression .mapKeys (constant (ImmutableMap .of ())).as ("empty_keys" ),
1458- Expression .mapKeys (constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true )))). as ( "nested_keys" ),
1459-
1466+ Expression .mapKeys (constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true ))))
1467+ . as ( "nested_keys" ),
14601468 field ("existingField" ).mapKeys ().as ("instanceExistingKeys" ),
1461- constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapKeys ().as ("instanceKeys" )
1462- )
1469+ constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapKeys ().as ("instanceKeys" ))
14631470 .execute ()
14641471 .get ();
14651472
@@ -1468,7 +1475,7 @@ public void testMapKeys() throws Exception {
14681475 Map <String , Object > data = resultList .get (0 ).getData ();
14691476
14701477 assertThat ((List <?>) data .get ("existingKeys" )).containsExactly ("foo" );
1471- assertThat ((List <?>) data .get ("keys" )).containsExactly ("a" , "b" );
1478+ assertThat ((List <?>) data .get ("keys" )).containsExactly ("a" , "b" );
14721479 assertThat ((List <?>) data .get ("empty_keys" )).isEmpty ();
14731480 assertThat ((List <?>) data .get ("nested_keys" )).containsExactly ("a" );
14741481
@@ -1491,11 +1498,11 @@ public void testMapValues() throws Exception {
14911498 Expression .mapValues ("existingField" ).as ("existingValues" ),
14921499 Expression .mapValues (constant (ImmutableMap .of ("a" , 1 , "b" , 2 ))).as ("values" ),
14931500 Expression .mapValues (constant (ImmutableMap .of ())).as ("empty_values" ),
1494- Expression .mapValues (constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true )))).as ("nested_values" ),
1495-
1501+ Expression .mapValues (
1502+ constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true ))))
1503+ .as ("nested_values" ),
14961504 field ("existingField" ).mapValues ().as ("instanceExistingValues" ),
1497- constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapValues ().as ("instanceValues" )
1498- )
1505+ constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapValues ().as ("instanceValues" ))
14991506 .execute ()
15001507 .get ();
15011508
@@ -1506,7 +1513,8 @@ public void testMapValues() throws Exception {
15061513 assertThat ((List <?>) data .get ("existingValues" )).containsExactly (1L );
15071514 assertThat ((List <?>) data .get ("values" )).containsExactly (1L , 2L );
15081515 assertThat ((List <?>) data .get ("empty_values" )).isEmpty ();
1509- assertThat ((List <?>) data .get ("nested_values" )).containsExactly (ImmutableMap .of ("nested" , true ));
1516+ assertThat ((List <?>) data .get ("nested_values" ))
1517+ .containsExactly (ImmutableMap .of ("nested" , true ));
15101518
15111519 assertThat ((List <?>) data .get ("instanceExistingValues" )).containsExactly (1L );
15121520 assertThat ((List <?>) data .get ("instanceValues" )).containsExactly (10L , 20L );
@@ -1527,11 +1535,11 @@ public void testMapEntries() throws Exception {
15271535 Expression .mapEntries ("existingField" ).as ("existingEntries" ),
15281536 Expression .mapEntries (constant (ImmutableMap .of ("a" , 1 , "b" , 2 ))).as ("entries" ),
15291537 Expression .mapEntries (constant (ImmutableMap .of ())).as ("empty_entries" ),
1530- Expression .mapEntries (constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true )))).as ("nested_entries" ),
1531-
1538+ Expression .mapEntries (
1539+ constant (ImmutableMap .of ("a" , ImmutableMap .of ("nested" , true ))))
1540+ .as ("nested_entries" ),
15321541 field ("existingField" ).mapEntries ().as ("instanceExistingEntries" ),
1533- constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapEntries ().as ("instanceEntries" )
1534- )
1542+ constant (ImmutableMap .of ("x" , 10 , "y" , 20 )).mapEntries ().as ("instanceEntries" ))
15351543 .execute ()
15361544 .get ();
15371545
@@ -1545,7 +1553,7 @@ public void testMapEntries() throws Exception {
15451553 @ SuppressWarnings ("unchecked" )
15461554 List <Map <String , Object >> entries = (List <Map <String , Object >>) data .get ("entries" );
15471555 assertThat (entries ).hasSize (2 );
1548-
1556+
15491557 // Map entry order is not guaranteed, so we check containment instead of strict ordering
15501558 assertThat (entries ).contains (ImmutableMap .of ("k" , "a" , "v" , 1L ));
15511559 assertThat (entries ).contains (ImmutableMap .of ("k" , "b" , "v" , 2L ));
@@ -1558,11 +1566,13 @@ public void testMapEntries() throws Exception {
15581566 .containsExactly (ImmutableMap .of ("k" , "foo" , "v" , 1L ));
15591567
15601568 @ SuppressWarnings ("unchecked" )
1561- List <Map <String , Object >> instanceEntries = (List <Map <String , Object >>) data .get ("instanceEntries" );
1569+ List <Map <String , Object >> instanceEntries =
1570+ (List <Map <String , Object >>) data .get ("instanceEntries" );
15621571 assertThat (instanceEntries ).hasSize (2 );
15631572 assertThat (instanceEntries ).contains (ImmutableMap .of ("k" , "x" , "v" , 10L ));
15641573 assertThat (instanceEntries ).contains (ImmutableMap .of ("k" , "y" , "v" , 20L ));
15651574 }
1575+
15661576 @ Test
15671577 public void testDataManipulationExpressions () throws Exception {
15681578 List <PipelineResult > results =
@@ -2933,5 +2943,4 @@ public void disallowDuplicateAliasesAcrossStages() {
29332943 });
29342944 assertThat (exception ).hasMessageThat ().contains ("Duplicate alias or field name" );
29352945 }
2936-
29372946}
0 commit comments