mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-01-12 22:50:20 +08:00
[firestore][tests] Tests for most of the current functionality
This commit is contained in:
@@ -31,7 +31,9 @@ public class FirestoreSerialize {
|
||||
WritableMap documentMap = Arguments.createMap();
|
||||
|
||||
documentMap.putString(KEY_PATH, documentSnapshot.getReference().getPath());
|
||||
documentMap.putMap(KEY_DATA, objectMapToWritable(documentSnapshot.getData()));
|
||||
if (documentSnapshot.exists()) {
|
||||
documentMap.putMap(KEY_DATA, objectMapToWritable(documentSnapshot.getData()));
|
||||
}
|
||||
// Missing fields from web SDK
|
||||
// createTime
|
||||
// readTime
|
||||
|
||||
@@ -84,7 +84,9 @@
|
||||
+ (NSDictionary *)snapshotToDictionary:(FIRDocumentSnapshot *)documentSnapshot {
|
||||
NSMutableDictionary *snapshot = [[NSMutableDictionary alloc] init];
|
||||
[snapshot setValue:documentSnapshot.reference.path forKey:@"path"];
|
||||
[snapshot setValue:documentSnapshot.data forKey:@"data"];
|
||||
if (documentSnapshot.exists) {
|
||||
[snapshot setValue:documentSnapshot.data forKey:@"data"];
|
||||
}
|
||||
// Missing fields from web SDK
|
||||
// createTime
|
||||
// readTime
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Text, View } from 'react-native';
|
||||
import fb from './firebase';
|
||||
|
||||
global.Promise = require('bluebird');
|
||||
|
||||
const firebase = fb.native;
|
||||
|
||||
|
||||
function bootstrap() {
|
||||
// Remove logging on production
|
||||
if (!__DEV__) {
|
||||
console.log = () => {
|
||||
};
|
||||
console.warn = () => {
|
||||
};
|
||||
console.error = () => {
|
||||
};
|
||||
console.disableYellowBox = true;
|
||||
}
|
||||
|
||||
class Root extends Component {
|
||||
|
||||
async componentDidMount() {
|
||||
console.log(`Starting`);
|
||||
const db = firebase.firestore();
|
||||
const docRef = await db.collection('chris').add({ first: 'Ada', last: 'Lovelace', born: 1815 });
|
||||
console.log(`Document written with ID: ${docRef.id}`);
|
||||
const docRef2 = await db.collection('chris').add({ first: 'Alan', middle: 'Mathison', last: 'Turing', born: 1912 });
|
||||
console.log(`Document written with ID: ${docRef2.id}`);
|
||||
await db.collection('chris').doc('manual').set({ first: 'Manual', last: 'Man', born: 1234 });
|
||||
console.log('Manual document set');
|
||||
await db.collection('chris').doc().set({ first: 'Auto', last: 'Man', born: 2000 });
|
||||
console.log('Auto document set');
|
||||
|
||||
const docRefT = db.doc(docRef.path);
|
||||
const docRefS = await docRefT.get();
|
||||
console.log(`Should be the same as first written ID: ${docRefT.id}`, docRefS.data());
|
||||
|
||||
await docRefT.set({ empty: true });
|
||||
const docRefS2 = await docRefT.get();
|
||||
console.log(`Should have empty only: ${docRefT.id}`, docRefS2.data());
|
||||
|
||||
await docRefT.set({ first: 'Ada', last: 'Lovelace', born: 1815 }, { merge: true });
|
||||
const docRefS3 = await docRefT.get();
|
||||
console.log(`Should have everything plus empty: ${docRefT.id}`, docRefS3.data());
|
||||
|
||||
await docRefT.update({ first: 'AdaUpdated' });
|
||||
const docRefS4 = await docRefT.get();
|
||||
console.log(`Should have updated firstname: ${docRefT.id}`, docRefS4.data());
|
||||
|
||||
const docs = await db.collection('chris').get();
|
||||
const tasks = [];
|
||||
docs.forEach((doc) => {
|
||||
console.log(`Cleaning up ${doc.id}`, doc.data());
|
||||
tasks.push(doc.ref.delete());
|
||||
});
|
||||
Promise.all(tasks);
|
||||
console.log('Finished cleaning collection');
|
||||
|
||||
const nycRef = db.collection('chris').doc('NYC');
|
||||
const sfRef = db.collection('chris').doc('SF');
|
||||
|
||||
await db.batch()
|
||||
.set(nycRef, { name: 'New York City' })
|
||||
.set(sfRef, { name: 'San Francisco' })
|
||||
.commit();
|
||||
|
||||
const docs2 = await db.collection('chris').get();
|
||||
docs2.forEach((doc) => {
|
||||
console.log(`Got ${doc.id}`, doc.data());
|
||||
});
|
||||
|
||||
await db.batch()
|
||||
.update(nycRef, { population: 1000000 })
|
||||
.update(sfRef, { name: 'San Fran' })
|
||||
.commit();
|
||||
const docs3 = await db.collection('chris').get();
|
||||
docs3.forEach((doc) => {
|
||||
console.log(`Got ${doc.id}`, doc.data());
|
||||
});
|
||||
|
||||
await db.batch()
|
||||
.delete(nycRef)
|
||||
.delete(sfRef)
|
||||
.commit();
|
||||
const docs4 = await db.collection('chris').get();
|
||||
docs4.forEach((doc) => {
|
||||
console.log(`Got ${doc.id}`, doc.data());
|
||||
});
|
||||
console.log('Finished');
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<View>
|
||||
<Text>Check console logs</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Root;
|
||||
}
|
||||
|
||||
export default bootstrap();
|
||||
@@ -1,30 +0,0 @@
|
||||
// import docSnapTests from './docSnapTests';
|
||||
// import querySnapTests from './querySnapTests';
|
||||
// import onSnapshotTests from './onSnapshotTests';
|
||||
// import bugTests from './bugTests';
|
||||
import whereTests from './whereTests';
|
||||
|
||||
const testGroups = [
|
||||
// onSnapshotTests,
|
||||
// querySnapTests,
|
||||
// docSnapTests,
|
||||
// bugTests,
|
||||
whereTests,
|
||||
];
|
||||
|
||||
function registerTestSuite(testSuite) {
|
||||
testSuite.beforeEach(async function () {
|
||||
// todo reset test data
|
||||
});
|
||||
|
||||
testSuite.afterEach(async function () {
|
||||
// todo reset test data
|
||||
});
|
||||
|
||||
testGroups.forEach((testGroup) => {
|
||||
testGroup(testSuite);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = registerTestSuite;
|
||||
@@ -1,86 +0,0 @@
|
||||
import should from 'should';
|
||||
|
||||
/**
|
||||
|
||||
Test document structure from fb console:
|
||||
|
||||
baz: true
|
||||
daz: 123
|
||||
foo: "bar"
|
||||
gaz: 12.1234567
|
||||
naz: null
|
||||
|
||||
*/
|
||||
|
||||
function whereTests({ describe, it, context, firebase }) {
|
||||
describe('CollectionReference.where()', () => {
|
||||
context('correctly handles', () => {
|
||||
it('== boolean values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('tests')
|
||||
.where('baz', '==', true)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().baz, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('== string values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('tests')
|
||||
.where('foo', '==', 'bar')
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().foo, 'bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('== null values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('tests')
|
||||
.where('naz', '==', null)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().naz, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('>= number values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('tests')
|
||||
.where('daz', '>=', 123)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().daz, 123);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('<= float values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('tests')
|
||||
.where('gaz', '<=', 12.1234666)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().gaz, 12.1234567);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default whereTests;
|
||||
123
tests/src/tests/firestore/collectionReferenceTests.js
Normal file
123
tests/src/tests/firestore/collectionReferenceTests.js
Normal file
@@ -0,0 +1,123 @@
|
||||
import should from 'should';
|
||||
|
||||
function collectionReferenceTests({ describe, it, context, firebase }) {
|
||||
describe('CollectionReference', () => {
|
||||
context('class', () => {
|
||||
it('should return instance methods', () => {
|
||||
return new Promise((resolve) => {
|
||||
const collection = firebase.native.firestore().collection('collection-tests');
|
||||
collection.should.have.property('firestore');
|
||||
// TODO: Remaining checks
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('add()', () => {
|
||||
it('should create Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.add({ first: 'Ada', last: 'Lovelace', born: 1815 })
|
||||
.then(async (docRef) => {
|
||||
const doc = await firebase.native.firestore().doc(docRef.path).get();
|
||||
doc.data().first.should.equal('Ada');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('doc()', () => {
|
||||
it('should create DocumentReference with correct path', () => {
|
||||
return new Promise((resolve) => {
|
||||
const docRef = firebase.native.firestore().collection('collection-tests').doc('doc');
|
||||
should.equal(docRef.path, 'collection-tests/doc');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('get()', () => {
|
||||
it('should retrieve a single document', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().baz, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Where
|
||||
context('where()', () => {
|
||||
it('correctly handles == boolean values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.where('baz', '==', true)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().baz, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly handles == string values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.where('foo', '==', 'bar')
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().foo, 'bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly handles == null values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.where('naz', '==', null)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().naz, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly handles >= number values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.where('daz', '>=', 123)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().daz, 123);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly handles <= float values', () => {
|
||||
return firebase.native.firestore()
|
||||
.collection('collection-tests')
|
||||
.where('gaz', '<=', 12.1234666)
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
should.equal(querySnapshot.size, 1);
|
||||
querySnapshot.forEach((documentSnapshot) => {
|
||||
should.equal(documentSnapshot.data().gaz, 12.1234567);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default collectionReferenceTests;
|
||||
@@ -1,22 +0,0 @@
|
||||
// import whereTests from './whereTests';
|
||||
|
||||
const testGroups = [
|
||||
// whereTests,
|
||||
];
|
||||
|
||||
function registerTestSuite(testSuite) {
|
||||
testSuite.beforeEach(async function () {
|
||||
// todo reset test data
|
||||
});
|
||||
|
||||
testSuite.afterEach(async function () {
|
||||
// todo reset test data
|
||||
});
|
||||
|
||||
testGroups.forEach((testGroup) => {
|
||||
testGroup(testSuite);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = registerTestSuite;
|
||||
80
tests/src/tests/firestore/documentReferenceTests.js
Normal file
80
tests/src/tests/firestore/documentReferenceTests.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import should from 'should';
|
||||
|
||||
function collectionReferenceTests({ describe, it, context, firebase }) {
|
||||
describe('DocumentReference', () => {
|
||||
context('class', () => {
|
||||
it('should return instance methods', () => {
|
||||
return new Promise((resolve) => {
|
||||
const document = firebase.native.firestore().doc('document-tests/doc1');
|
||||
document.should.have.property('firestore');
|
||||
// TODO: Remaining checks
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('delete()', () => {
|
||||
it('should delete Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.doc('document-tests/doc1')
|
||||
.delete()
|
||||
.then(async () => {
|
||||
const doc = await firebase.native.firestore().doc('document-tests/doc1').get();
|
||||
should.equal(doc.exists, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('set()', () => {
|
||||
it('should create Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.doc('document-tests/doc2')
|
||||
.set({ name: 'doc2' })
|
||||
.then(async () => {
|
||||
const doc = await firebase.native.firestore().doc('document-tests/doc2').get();
|
||||
doc.data().name.should.equal('doc2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('set()', () => {
|
||||
it('should merge Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.doc('document-tests/doc1')
|
||||
.set({ merge: 'merge' }, { merge: true })
|
||||
.then(async () => {
|
||||
const doc = await firebase.native.firestore().doc('document-tests/doc1').get();
|
||||
doc.data().name.should.equal('doc1');
|
||||
doc.data().merge.should.equal('merge');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('set()', () => {
|
||||
it('should overwrite Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.doc('document-tests/doc1')
|
||||
.set({ name: 'overwritten' })
|
||||
.then(async () => {
|
||||
const doc = await firebase.native.firestore().doc('document-tests/doc1').get();
|
||||
doc.data().name.should.equal('overwritten');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('update()', () => {
|
||||
it('should update Document', () => {
|
||||
return firebase.native.firestore()
|
||||
.doc('document-tests/doc1')
|
||||
.set({ name: 'updated' })
|
||||
.then(async () => {
|
||||
const doc = await firebase.native.firestore().doc('document-tests/doc1').get();
|
||||
doc.data().name.should.equal('updated');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default collectionReferenceTests;
|
||||
63
tests/src/tests/firestore/firestoreTests.js
Normal file
63
tests/src/tests/firestore/firestoreTests.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import should from 'should';
|
||||
|
||||
function firestoreTests({ describe, it, context, firebase }) {
|
||||
describe('firestore()', () => {
|
||||
context('collection()', () => {
|
||||
it('should create CollectionReference with the right id', () => {
|
||||
return new Promise((resolve) => {
|
||||
const collectionRef = firebase.native.firestore().collection('collection1/doc1/collection2');
|
||||
should.equal(collectionRef.id, 'collection2');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('doc()', () => {
|
||||
it('should create DocumentReference with correct path', () => {
|
||||
return new Promise((resolve) => {
|
||||
const docRef = firebase.native.firestore().doc('collection1/doc1/collection2/doc2');
|
||||
should.equal(docRef.path, 'collection1/doc1/collection2/doc2');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('batch()', () => {
|
||||
it('should create / update / delete as expected', () => {
|
||||
const ayRef = firebase.native.firestore().collection('firestore-tests').doc('AY');
|
||||
const lRef = firebase.native.firestore().collection('firestore-tests').doc('LON');
|
||||
const nycRef = firebase.native.firestore().collection('firestore-tests').doc('NYC');
|
||||
const sfRef = firebase.native.firestore().collection('firestore-tests').doc('SF');
|
||||
|
||||
return firebase.native.firestore()
|
||||
.batch()
|
||||
.set(ayRef, { name: 'Aylesbury' })
|
||||
.set(lRef, { name: 'London' })
|
||||
.set(nycRef, { name: 'New York City' })
|
||||
.set(sfRef, { name: 'San Francisco' })
|
||||
.update(nycRef, { population: 1000000 })
|
||||
.update(sfRef, { name: 'San Fran' })
|
||||
.set(lRef, { population: 3000000 }, { merge: true })
|
||||
.delete(ayRef)
|
||||
.commit()
|
||||
.then(async () => {
|
||||
const ayDoc = await ayRef.get();
|
||||
should.equal(ayDoc.exists, false);
|
||||
|
||||
const lDoc = await lRef.get();
|
||||
lDoc.data().name.should.equal('London');
|
||||
lDoc.data().population.should.equal(3000000);
|
||||
|
||||
const nycDoc = await nycRef.get();
|
||||
nycDoc.data().name.should.equal('New York City');
|
||||
nycDoc.data().population.should.equal(1000000);
|
||||
|
||||
const sfDoc = await sfRef.get();
|
||||
sfDoc.data().name.should.equal('San Fran');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default firestoreTests;
|
||||
@@ -4,16 +4,63 @@ import TestSuite from '../../../lib/TestSuite';
|
||||
/*
|
||||
Test suite files
|
||||
*/
|
||||
import collectionTestGroups from './collection/index';
|
||||
import documentTestGroups from './document/index';
|
||||
import collectionReferenceTests from './collectionReferenceTests';
|
||||
import documentReferenceTests from './documentReferenceTests';
|
||||
import firestoreTests from './firestoreTests';
|
||||
|
||||
const suite = new TestSuite('Firestore', 'firebase.firestore()', firebase);
|
||||
|
||||
const testGroups = [
|
||||
collectionReferenceTests,
|
||||
documentReferenceTests,
|
||||
firestoreTests,
|
||||
];
|
||||
|
||||
function firestoreTestSuite(testSuite) {
|
||||
testSuite.beforeEach(async () => {
|
||||
this.collectionTestsCollection = testSuite.firebase.native.firestore().collection('collection-tests');
|
||||
this.documentTestsCollection = testSuite.firebase.native.firestore().collection('document-tests');
|
||||
this.firestoreTestsCollection = testSuite.firebase.native.firestore().collection('firestore-tests');
|
||||
// Clean the collections in case the last run failed
|
||||
await cleanCollection(this.collectionTestsCollection);
|
||||
await cleanCollection(this.documentTestsCollection);
|
||||
await cleanCollection(this.firestoreTestsCollection);
|
||||
|
||||
await this.collectionTestsCollection.add({
|
||||
baz: true,
|
||||
daz: 123,
|
||||
foo: 'bar',
|
||||
gaz: 12.1234567,
|
||||
naz: null,
|
||||
});
|
||||
|
||||
await this.documentTestsCollection.doc('doc1').set({
|
||||
name: 'doc1',
|
||||
});
|
||||
});
|
||||
|
||||
testSuite.afterEach(async () => {
|
||||
await cleanCollection(this.collectionTestsCollection);
|
||||
await cleanCollection(this.documentTestsCollection);
|
||||
await cleanCollection(this.firestoreTestsCollection);
|
||||
});
|
||||
|
||||
testGroups.forEach((testGroup) => {
|
||||
testGroup(testSuite);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
Register tests with test suite
|
||||
*/
|
||||
|
||||
suite.addTests(documentTestGroups);
|
||||
suite.addTests(collectionTestGroups);
|
||||
suite.addTests(firestoreTestSuite);
|
||||
|
||||
export default suite;
|
||||
|
||||
/* HELPER FUNCTIONS */
|
||||
async function cleanCollection(collection) {
|
||||
const collectionTestsDocs = await collection.get();
|
||||
const tasks = [];
|
||||
collectionTestsDocs.forEach(doc => tasks.push(doc.ref.delete()));
|
||||
await Promise.all(tasks);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user