Skip to content

Commit

Permalink
test: edge cases for aggregate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Nov 12, 2024
1 parent 97124c9 commit eb65fd8
Showing 1 changed file with 322 additions and 27 deletions.
349 changes: 322 additions & 27 deletions packages/firestore/e2e/Aggregate/AggregateQuery.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,6 @@ describe('getAggregateFromServer()', function () {
});

describe('count(), average() & sum()', function () {
it('no existing collection responses for average(), sum() & count()', async function () {
const { getAggregateFromServer, collection, getFirestore, count, average, sum } =
firestoreModular;
const firestore = getFirestore();

const colRefNoDocs = collection(firestore, `${COLLECTION}/aggregate-count/no-docs`);

const aggregateSpecNoDocuments = {
countCollection: count(),
averageBar: average('bar'),
sumBaz: sum('baz'),
};

const resultNoDocs = await getAggregateFromServer(colRefNoDocs, aggregateSpecNoDocuments);

const dataNoDocs = resultNoDocs.data();

// average returns null, whilst sum and count return 0
dataNoDocs.countCollection.should.eql(0);
should(dataNoDocs.averageBar).be.null();
dataNoDocs.sumBaz.should.eql(0);
});

it('single path using `string`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, count, average, sum } =
firestoreModular;
Expand All @@ -132,9 +109,9 @@ describe('getAggregateFromServer()', function () {
const colRef = collection(firestore, `${COLLECTION}/aggregate-count/collection`);

await Promise.all([
setDoc(doc(colRef, 'one'), { bar: 0.4, baz: 0.1 }),
setDoc(doc(colRef, 'two'), { bar: 0.5, baz: 0.1 }),
setDoc(doc(colRef, 'three'), { bar: 0.6, baz: 0.1 }),
setDoc(doc(colRef, 'one'), { bar: 0.4, baz: 3 }),
setDoc(doc(colRef, 'two'), { bar: 0.5, baz: 3 }),
setDoc(doc(colRef, 'three'), { bar: 0.6, baz: 3 }),
]);

const aggregateSpec = {
Expand All @@ -150,7 +127,7 @@ describe('getAggregateFromServer()', function () {

data.countCollection.should.eql(3);
data.averageBar.should.eql(0.5);
data.sumBaz.should.eql(0.3);
data.sumBaz.should.eql(9);
// should only return the aggregate field requests
data.should.not.have.property('ignoreThisProperty');
});
Expand Down Expand Up @@ -268,5 +245,323 @@ describe('getAggregateFromServer()', function () {
// should only return the aggregate field requests
data.should.not.have.property('ignoreThisProperty');
});

describe('edge cases for aggregate query', function () {
it('no existing collection responses for average(), sum() & count()', async function () {
const { getAggregateFromServer, collection, getFirestore, count, average, sum } =
firestoreModular;
const firestore = getFirestore();

const colRefNoDocs = collection(firestore, `${COLLECTION}/aggregate-count/no-docs`);

const aggregateSpecNoDocuments = {
countCollection: count(),
averageBar: average('bar'),
sumBaz: sum('baz'),
};

const resultNoDocs = await getAggregateFromServer(colRefNoDocs, aggregateSpecNoDocuments);

const dataNoDocs = resultNoDocs.data();

// average returns null, whilst sum and count return 0
dataNoDocs.countCollection.should.eql(0);
should(dataNoDocs.averageBar).be.null();
dataNoDocs.sumBaz.should.eql(0);
});

it('sum of `0.3`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/sum-0-3`);

await Promise.all([
setDoc(doc(colRef, 'one'), { bar: 0.4, baz: 0.1 }),
setDoc(doc(colRef, 'two'), { bar: 0.5, baz: 0.1 }),
setDoc(doc(colRef, 'three'), { bar: 0.6, baz: 0.1 }),
]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.sumBaz.should.eql(0.30000000000000004);
});

it('return JavaScript single max safe integer for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_INT = Number.MAX_SAFE_INTEGER;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-int`);

await Promise.all([setDoc(doc(colRef, 'one'), { baz: MAX_INT })]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.sumBaz.should.eql(MAX_INT);
});

it('return JavaScript nine max safe integers for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_INT = Number.MAX_SAFE_INTEGER;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-int-2`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: MAX_INT }),
setDoc(doc(colRef, 'two'), { baz: MAX_INT }),
setDoc(doc(colRef, 'three'), { baz: MAX_INT }),
setDoc(doc(colRef, 'four'), { baz: MAX_INT }),
setDoc(doc(colRef, 'five'), { baz: MAX_INT }),
setDoc(doc(colRef, 'six'), { baz: MAX_INT }),
setDoc(doc(colRef, 'seven'), { baz: MAX_INT }),
setDoc(doc(colRef, 'eight'), { baz: MAX_INT }),
setDoc(doc(colRef, 'nine'), { baz: MAX_INT }),
]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.sumBaz.should.eql(MAX_INT * 9);
});

it('return JavaScript single max safe number for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_NUMBER = Number.MAX_VALUE;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-number`);

await Promise.all([setDoc(doc(colRef, 'one'), { baz: MAX_NUMBER })]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.sumBaz.should.eql(MAX_NUMBER);
});

it('returns `Infinity` for JavaScript max safe number + 1 for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_NUMBER = Number.MAX_VALUE;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-number`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: MAX_NUMBER }),
setDoc(doc(colRef, 'two'), { baz: 1 }),
]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();
// Doesn't add 1, just returns MAX_NUMBER
data.sumBaz.should.eql(MAX_NUMBER);
});

it('returns `Infinity` for JavaScript max safe number + 100 for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_NUMBER = Number.MAX_VALUE;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-number-2`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: MAX_NUMBER }),
setDoc(doc(colRef, 'two'), { baz: 100 }),
]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();
// Doesn't add 100, just returns MAX_NUMBER
data.sumBaz.should.eql(MAX_NUMBER);
});

it('returns `Infinity` for JavaScript two max safe numbers for `sum()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, sum } =
firestoreModular;
const MAX_NUMBER = Number.MAX_VALUE;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-count/max-number-3`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: MAX_NUMBER }),
setDoc(doc(colRef, 'two'), { baz: MAX_NUMBER }),
]);

const aggregateSpec = {
sumBaz: sum('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();
// Returns Infinity
data.sumBaz.should.eql(Infinity);
});

it('returns `0` for properties with `0` for `average()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, average } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-average/0-values`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: 0 }),
setDoc(doc(colRef, 'two'), { baz: 0 }),
]);

const aggregateSpec = {
averageBaz: average('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.averageBaz.should.eql(0);
});

it('returns `-1` for properties with `-1` for `average()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, average } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-average/minus-one-values`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: -1 }),
setDoc(doc(colRef, 'two'), { baz: -1 }),
]);

const aggregateSpec = {
averageBaz: average('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.averageBaz.should.eql(-1);
});

it('returns `-3` for properties with `-3` for `average()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, average } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(firestore, `${COLLECTION}/aggregate-average/minus-three-values`);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: -3 }),
setDoc(doc(colRef, 'two'), { baz: -3 }),
setDoc(doc(colRef, 'three'), { baz: -3 }),
]);

const aggregateSpec = {
averageBaz: average('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.averageBaz.should.eql(-3);
});

it('returns `-2` for properties with `-1`, `-2`,`-3` for `average()`', async function () {
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, average } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(
firestore,
`${COLLECTION}/aggregate-average/minus-various-values`,
);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: -1 }),
setDoc(doc(colRef, 'two'), { baz: -2 }),
setDoc(doc(colRef, 'three'), { baz: -3 }),
]);

const aggregateSpec = {
averageBaz: average('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.averageBaz.should.eql(-2);
});

it.only('returns `WHAT` for properties with `-1`, `-2`,`-3` for `average()`', async function () {

Check warning on line 539 in packages/firestore/e2e/Aggregate/AggregateQuery.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected exclusive mocha test

Check warning on line 539 in packages/firestore/e2e/Aggregate/AggregateQuery.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected exclusive mocha test

Check warning on line 539 in packages/firestore/e2e/Aggregate/AggregateQuery.e2e.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected exclusive mocha test
const { getAggregateFromServer, doc, setDoc, collection, getFirestore, average } =
firestoreModular;
const firestore = getFirestore();

const colRef = collection(
firestore,
`${COLLECTION}/aggregate-average/minus-various-float-values`,
);

await Promise.all([
setDoc(doc(colRef, 'one'), { baz: -0.1 }),
setDoc(doc(colRef, 'two'), { baz: -0.2 }),
setDoc(doc(colRef, 'three'), { baz: -0.3 }),
]);

const aggregateSpec = {
averageBaz: average('baz'),
};

const result = await getAggregateFromServer(colRef, aggregateSpec);

const data = result.data();

data.averageBaz.should.eql(-0.19999999999999998);
});
});
});
});

0 comments on commit eb65fd8

Please sign in to comment.