Skip to content

Commit

Permalink
Added specifying optional intermediate documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbenckhuijsen committed Apr 4, 2024
1 parent a4c98ba commit 68587c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ You can choose to either define the validation files as JSON or as YAML. Both fo
offer equal features.

**Example in JSON:**

```json
{
"_testcollection" : {
"_testcollection": {
"testdoc1": {
"testArray": [
false,
Expand All @@ -56,11 +57,18 @@ offer equal features.
"testDateTime": "2024-03-22T12:13:14.123Z",
"testReference": "testcollection/ref1",
"testText": "Hello world",
"_subcollection" : {
"_subcollection": {
"testdoc2": {
"testText": "Hello Firestore"
}
}
},
"_testdoc3": {
"_subcollection": {
"testdoc4": {
"testText": "Hello Firestore"
}
}
}
}
}
Expand All @@ -86,12 +94,19 @@ _testcollection:
_subcollection:
testdoc2:
testText: "Hello Firestore"
_testdoc3:
_subcollection:
testdoc3:
testText: "Hello Firestore"
```
Note that in this example:
* Collections always need to be prefixed with an underscore "_". This is to let the library
differentiate between the Map datatype and collections.
* Date/time values need to be defined in ISO 8601 format
* In case a document should be skipped for checking (both for existance and the fields), prefix it with an underscore
"_". In this case the document "testcollection/testdoc3" is optional. Firestore allows you to skip definition of all
intermediate documents in a path.
### Use the library in your test ###
Expand Down Expand Up @@ -128,3 +143,7 @@ These will be reviewed and if appropriate merged into the main release.
### Who do I talk to? ###

For contact, see the developers section in the `pom.xml` file.

### Releasing a new version ###

Run: `mvn clean deploy -P release`
16 changes: 13 additions & 3 deletions src/main/java/nl/group9/firestore/unit/FirestoreTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,24 @@ private ApiFuture<?> validateCollection(CollectionReference collectionReference,
for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> fieldEntry = it.next();

DocumentReference doc = collectionReference.document(fieldEntry.getKey());
futures.add(validateDocument(doc, fieldEntry.getValue()));
String documentName = fieldEntry.getKey();
boolean skipCurrent = false;
if (documentName.startsWith("_")) {
documentName = documentName.substring(1);
skipCurrent = true;
}

DocumentReference doc = collectionReference.document(documentName);
futures.add(validateDocument(doc, fieldEntry.getValue(), skipCurrent));
}
return ApiFutures.allAsList(futures);
}

private ApiFuture<?> validateDocument(DocumentReference docRef, JsonNode node) {
private ApiFuture<?> validateDocument(DocumentReference docRef, JsonNode node, boolean skipCurrent) {
ApiFuture<?> childFuture = traverseCollections(docRef::collection, node);
if (skipCurrent) {
return childFuture;
}

ApiFuture<DocumentSnapshot> docFuture = docRef.get();
ApiFuture<DocumentSnapshot> result = ApiFutures.transform(
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/nl/group9/firestore/unit/FirestoreUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public static void fillFirestore() throws Exception {
Map<String, Object> testdoc2Fields = new HashMap<>();
testdoc2Fields.put("testText", "Hello Firestore");
testdoc2.set(testdoc2Fields).get();

// Document with a missing intermediate Document node as parent
DocumentReference testdoc3 = testcollection.document("testdoc3");
CollectionReference subsubcollection = testdoc3.collection("subcollection");
DocumentReference testdoc4 = subsubcollection.document("testdoc4");
Map<String, Object> testdoc4Fields = new HashMap<>();
testdoc4Fields.put("testText", "Hello Firestore");
testdoc4.set(testdoc4Fields).get();
}
}

Expand Down Expand Up @@ -139,6 +147,13 @@ void testYamlInputStream() throws Exception {
}
}

@Test
void emptySubDocument() throws Exception {
try (Firestore firestore = connection()) {
assertFirestoreJson(firestore, asInputStream("json/missing_subdoc.json"));
}
}

@Test
void testArrayDifferentElements() {
testInvalidFile("json/array_diff_element.json", "Field does not have the expected value at testcollection/testdoc1/testArray[0] ==> expected: <true> but was: <false>");
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/json/missing_subdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"_testcollection" : {
"_testdoc3": {
"_subcollection" : {
"testdoc4": {
"testText": "Hello Firestore"
}
}
}
}
}

0 comments on commit 68587c7

Please sign in to comment.