11import * as nock from "nock" ;
2+ import * as utils from "../../utils" ;
23import { expect } from "chai" ;
34
45import DatabaseImporter from "../../database/import" ;
@@ -8,33 +9,37 @@ const dbUrl = new URL("https://test-db.firebaseio.com/foo");
89
910describe ( "DatabaseImporter" , ( ) => {
1011 const DATA = { a : 100 , b : [ true , "bar" , { f : { g : 0 , h : 1 } , i : "baz" } ] } ;
12+ let DATA_STREAM : NodeJS . ReadableStream ;
1113
12- it ( "parses data as single chunk" , ( ) => {
13- const importer = new DatabaseImporter ( dbUrl , JSON . stringify ( DATA ) ) ;
14- expect ( importer . chunks . length ) . to . equal ( 1 ) ;
15- expect ( importer . chunks [ 0 ] . json ) . to . deep . equal ( DATA ) ;
16- expect ( importer . chunks [ 0 ] . pathname ) . to . equal ( "/foo" ) ;
14+ beforeEach ( ( ) => {
15+ DATA_STREAM = utils . stringToStream ( JSON . stringify ( DATA ) ) ! ;
1716 } ) ;
1817
19- it ( "parses data as multiple chunks" , ( ) => {
20- const importer = new DatabaseImporter ( dbUrl , JSON . stringify ( DATA ) , /* chunkSize= */ 20 ) ;
21- expect ( importer . chunks . length ) . to . equal ( 5 ) ;
22- expect ( importer . chunks ) . to . deep . include ( { json : 100 , pathname : "/foo/a" } ) ;
23- expect ( importer . chunks ) . to . deep . include ( { json : true , pathname : "/foo/b/0" } ) ;
24- expect ( importer . chunks ) . to . deep . include ( { json : "bar" , pathname : "/foo/b/1" } ) ;
25- expect ( importer . chunks ) . to . deep . include ( { json : { g : 0 , h : 1 } , pathname : "/foo/b/2/f" } ) ;
26- expect ( importer . chunks ) . to . deep . include ( { json : "baz" , pathname : "/foo/b/2/i" } ) ;
27- } ) ;
18+ it ( "throws FirebaseError when JSON is invalid" , async ( ) => {
19+ nock ( "https://test-db.firebaseio.com" ) . get ( "/foo.json?shallow=true" ) . reply ( 200 ) ;
2820
29- it ( "throws FirebaseError when JSON is invalid" , ( ) => {
30- const INVALID_JSON = '{"a": }' ;
31- expect ( ( ) => new DatabaseImporter ( dbUrl , INVALID_JSON ) ) . to . throw (
21+ const INVALID_JSON = '{"a": {"b"}}' ;
22+ const importer = new DatabaseImporter ( dbUrl , utils . stringToStream ( INVALID_JSON ) ! ) ;
23+ await expect ( importer . execute ( ) ) . to . be . rejectedWith (
3224 FirebaseError ,
3325 "Invalid data; couldn't parse JSON object, array, or value."
3426 ) ;
3527 } ) ;
3628
37- it ( "sends multiple chunked requests" , async ( ) => {
29+ it ( "chunks data in top-level objects" , async ( ) => {
30+ nock ( "https://test-db.firebaseio.com" ) . get ( "/foo.json?shallow=true" ) . reply ( 200 ) ;
31+ nock ( "https://test-db.firebaseio.com" ) . put ( "/foo/a.json" , "100" ) . reply ( 200 ) ;
32+ nock ( "https://test-db.firebaseio.com" )
33+ . put ( "/foo/b.json" , JSON . stringify ( [ true , "bar" , { f : { g : 0 , h : 1 } , i : "baz" } ] ) )
34+ . reply ( 200 ) ;
35+
36+ const importer = new DatabaseImporter ( dbUrl , DATA_STREAM ) ;
37+ const responses = await importer . execute ( ) ;
38+ expect ( responses ) . to . have . length ( 2 ) ;
39+ expect ( nock . isDone ( ) ) . to . be . true ;
40+ } ) ;
41+
42+ it ( "chunks data according to provided chunk size" , async ( ) => {
3843 nock ( "https://test-db.firebaseio.com" ) . get ( "/foo.json?shallow=true" ) . reply ( 200 ) ;
3944 nock ( "https://test-db.firebaseio.com" ) . put ( "/foo/a.json" , "100" ) . reply ( 200 ) ;
4045 nock ( "https://test-db.firebaseio.com" ) . put ( "/foo/b/0.json" , "true" ) . reply ( 200 ) ;
@@ -44,15 +49,15 @@ describe("DatabaseImporter", () => {
4449 . reply ( 200 ) ;
4550 nock ( "https://test-db.firebaseio.com" ) . put ( "/foo/b/2/i.json" , '"baz"' ) . reply ( 200 ) ;
4651
47- const importer = new DatabaseImporter ( dbUrl , JSON . stringify ( DATA ) , /* chunkSize= */ 20 ) ;
52+ const importer = new DatabaseImporter ( dbUrl , DATA_STREAM , /* chunkSize= */ 20 ) ;
4853 const responses = await importer . execute ( ) ;
4954 expect ( responses ) . to . have . length ( 5 ) ;
5055 expect ( nock . isDone ( ) ) . to . be . true ;
5156 } ) ;
5257
5358 it ( "throws FirebaseError when data location is nonempty" , async ( ) => {
5459 nock ( "https://test-db.firebaseio.com" ) . get ( "/foo.json?shallow=true" ) . reply ( 200 , { a : "foo" } ) ;
55- const importer = new DatabaseImporter ( dbUrl , JSON . stringify ( DATA ) ) ;
60+ const importer = new DatabaseImporter ( dbUrl , DATA_STREAM ) ;
5661 await expect ( importer . execute ( ) ) . to . be . rejectedWith (
5762 FirebaseError ,
5863 / I m p o r t i n g i s o n l y a l l o w e d f o r a n e m p t y l o c a t i o n ./
0 commit comments