Skip to content

Firestore: Migrate unit tests to assertThrows() #3562

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

Merged
merged 4 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,24 +33,24 @@ public void pathWithArray() {
assertEquals("a.b.c", fieldPath.toString());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void emptyPathIsInvalid() {
FieldPath.fromDotSeparatedPath("");
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath(""));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void emptyFirstSegmentIsInvalid() {
FieldPath.fromDotSeparatedPath(".a");
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath(".a"));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void emptyLastSegmentIsInvalid() {
FieldPath.fromDotSeparatedPath("a.");
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath("a."));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void emptyMiddleSegmentIsInvalid() {
FieldPath.fromDotSeparatedPath("a..b");
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath("a..b"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.firebase.firestore.testutil.TestUtil.version;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import com.google.firebase.firestore.core.Query;
import com.google.firebase.firestore.core.Target;
Expand Down Expand Up @@ -219,55 +220,59 @@ public void testReadsWithoutDocumentOrQuery() throws IOException, JSONException
verifyAllElements(bundleReader);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testThrowsWithoutLengthPrefix() throws IOException, JSONException {
String bundle = "{metadata: 'no length prefix' }";

BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));

bundleReader.getBundleMetadata();
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testThrowsWithMissingBrackets() throws IOException, JSONException {
String bundle = "3abc";

BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
bundleReader.getBundleMetadata();

assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
}

@Test(expected = JSONException.class)
@Test
public void testThrowsWithInvalidJSON() throws IOException, JSONException {
String bundle = "3{abc}";

BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
bundleReader.getBundleMetadata();

assertThrows(JSONException.class, () -> bundleReader.getBundleMetadata());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testThrowsWhenSecondElementIsMissing() throws IOException, JSONException {
TestBundleBuilder bundleBuilder = new TestBundleBuilder(TEST_PROJECT);
String bundle =
bundleBuilder.build("bundle-1", /* createTimeMicros= */ 6000000L, /* version= */ 1) + "foo";

BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
bundleReader.getNextElement();

assertThrows(IllegalArgumentException.class, () -> bundleReader.getNextElement());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testThrowsWhenBundleDoesNotContainEnoughData() throws IOException, JSONException {
String bundle = "3{}";

BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
bundleReader.getBundleMetadata();

assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testWhenFirstElementIsNotBundleMetadata() throws IOException, JSONException {
String json =
String.format(
Expand All @@ -283,7 +288,7 @@ public void testWhenFirstElementIsNotBundleMetadata() throws IOException, JSONEx
BundleReader bundleReader =
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));

bundleReader.getBundleMetadata();
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.firebase.firestore.testutil.TestUtil.orderBy;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import com.google.firebase.firestore.core.Query;
import com.google.firebase.firestore.core.Target;
Expand Down Expand Up @@ -596,32 +597,32 @@ public void testDecodesEndBeforeQuery() throws JSONException {
assertDecodesNamedQuery(json, query);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testDoesNotDecodeOffset() throws JSONException {
String json = "{ from: [ { collectionId: 'coll' } ], offset: 5 }";
Query query = TestUtil.query("coll");
assertDecodesNamedQuery(json, query);
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testDoesNotDecodeSelect() throws JSONException {
String json = "{ from: [ { collectionId: 'coll' } ], select: [] }";
Query query = TestUtil.query("coll");
assertDecodesNamedQuery(json, query);
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testDoesNotDecodeMissingCollection() throws JSONException {
String json = "{ from: [ ] }";
Query query = TestUtil.query("coll");
assertDecodesNamedQuery(json, query);
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testDoesNotDecodeMultipleCollections() throws JSONException {
String json = "{ from: [ { collectionId: 'c1' }, { collectionId: 'c2' } ] }";
Query query = TestUtil.query("coll");
assertDecodesNamedQuery(json, query);
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
}

// BundleMetadata tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;

import com.google.firebase.firestore.testutil.ComparatorTester;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -57,8 +59,9 @@ public void testComparison() {
.testCompare();
}

@Test(expected = Throwable.class)
@Test
public void testUnevenNumberOfSegmentsAreRejected() {
DocumentKey.fromSegments(Collections.singletonList("a"));
List<String> segments = Collections.singletonList("a");
assertThrows(Throwable.class, () -> DocumentKey.fromSegments(segments));
}
}