@@ -6,59 +6,26 @@ const assert = require('../helpers/assert');
66const rewire = require ( 'rewire' ) ;
77const subject = rewire ( '../../lib/deploy-client' ) ;
88
9- describe ( 'DeployClient private "constructor" method' , function ( ) {
9+ describe ( 'DeployClient private methods' , function ( ) {
10+ const rewire = require ( 'rewire' ) ;
11+ const subject = rewire ( '../../lib/deploy-client' ) ;
12+
1013 const knex = require ( 'knex' ) ( {
1114 client : 'sqlite3' ,
1215 connection : { filename : ':memory:' } ,
1316 useNullAsDefault : true ,
1417 //debug: true
1518 } ) ;
1619
20+ beforeEach ( subject . __get__ ( 'createTable' ) . bind ( { knex } , 'test' ) ) ;
21+ afterEach ( ( ) => knex . schema . dropTable ( 'test' ) ) ;
22+
1723 // Tear down the connection pool after all the tests have completed.
1824 after ( ( ) => knex . destroy ( ) ) ;
1925
20- describe ( '#conditionallyCreateTable()' , function ( ) {
21- const conditionallyCreateTable = subject . __get__ ( 'conditionallyCreateTable' ) ;
22-
23- it ( 'creates a table if none exists' , function ( ) {
24- return conditionallyCreateTable . call ( { knex } , 'foo' )
25- . then ( ( ) => knex ( 'foo' ) . columnInfo ( ) )
26- . then ( info => {
27- assert . isObject ( info ) ;
28-
29- assert . deepPropertyVal ( info , 'id.type' , 'integer' ) ;
30- assert . deepPropertyVal ( info , 'key.type' , 'varchar' ) ;
31- assert . deepPropertyVal ( info , 'value.type' , 'text' ) ;
32- assert . deepPropertyVal ( info , 'is_active.type' , 'boolean' ) ;
33- assert . deepPropertyVal ( info , 'created_at.type' , 'datetime' ) ;
34- } )
35- . finally ( ( ) => knex . schema . dropTable ( 'foo' ) ) ;
36- } ) ;
37-
38- it ( 'does not create a table if one exists' , function ( ) {
39- return knex . schema . createTable ( 'bar' , tbl => {
40- tbl . increments ( ) ;
41- tbl . string ( 'key' ) . notNullable ( ) . unique ( ) ;
42- tbl . timestamps ( ) ;
43- } ) . then ( ( ) => conditionallyCreateTable . call ( { knex } , 'bar' ) )
44- . then ( ( ) => knex ( 'bar' ) . columnInfo ( ) )
45- . then ( info => {
46- assert . isObject ( info ) ;
47-
48- assert . deepPropertyVal ( info , 'id.type' , 'integer' ) ;
49- assert . deepPropertyVal ( info , 'key.type' , 'varchar' ) ;
50- assert . deepPropertyVal ( info , 'created_at.type' , 'datetime' ) ;
51- assert . deepPropertyVal ( info , 'updated_at.type' , 'datetime' ) ;
52-
53- assert . notProperty ( info , 'value' ) ;
54- assert . notProperty ( info , 'is_active' ) ;
55- } )
56- . finally ( ( ) => knex . schema . dropTable ( 'bar' ) ) ;
57- } ) ;
58-
26+ describe ( '#createTable()' , function ( ) {
5927 it ( 'sets appropriate column defaults' , function ( ) {
60- return conditionallyCreateTable . call ( { knex } , 'test' )
61- . then ( ( ) => knex ( 'test' ) . insert ( { key : 'foo' , value : 'bar' } ) )
28+ return knex ( 'test' ) . insert ( { key : 'foo' , value : 'bar' } )
6229 . then ( ( ) => knex ( 'test' ) . first ( ) )
6330 . then ( row => {
6431 assert . isObject ( row ) ;
@@ -76,24 +43,6 @@ describe('DeployClient private "constructor" method', function() {
7643 } ) ;
7744 } ) ;
7845 } ) ;
79- } ) ;
80-
81- describe ( 'DeployClient private methods' , function ( ) {
82- const rewire = require ( 'rewire' ) ;
83- const subject = rewire ( '../../lib/deploy-client' ) ;
84-
85- const knex = require ( 'knex' ) ( {
86- client : 'sqlite3' ,
87- connection : { filename : ':memory:' } ,
88- useNullAsDefault : true ,
89- //debug: true
90- } ) ;
91-
92- beforeEach ( subject . __get__ ( 'createTable' ) . bind ( { knex } , 'test' ) ) ;
93- afterEach ( ( ) => knex . schema . dropTable ( 'test' ) ) ;
94-
95- // Tear down the connection pool after all the tests have completed.
96- after ( ( ) => knex . destroy ( ) ) ;
9746
9847 describe ( '#conditionallyCreateRevision()' , function ( ) {
9948 const createRevision = subject . __get__ ( 'conditionallyCreateRevision' ) ;
@@ -384,6 +333,46 @@ describe('DeployClient public API', function() {
384333 }
385334 } ) ;
386335
336+ describe ( '#sanityCheck()' , function ( ) {
337+ it ( 'creates a table if none exists' , function ( ) {
338+ deployClient = new subject ( baseOptions ) ;
339+
340+ return deployClient . sanityCheck ( { tableName : 'foo' } )
341+ . then ( ( ) => deployClient . knex ( 'foo' ) . columnInfo ( ) )
342+ . then ( info => {
343+ assert . isObject ( info ) ;
344+
345+ assert . deepPropertyVal ( info , 'id.type' , 'integer' ) ;
346+ assert . deepPropertyVal ( info , 'key.type' , 'varchar' ) ;
347+ assert . deepPropertyVal ( info , 'value.type' , 'text' ) ;
348+ assert . deepPropertyVal ( info , 'is_active.type' , 'boolean' ) ;
349+ assert . deepPropertyVal ( info , 'created_at.type' , 'datetime' ) ;
350+ } ) ;
351+ } ) ;
352+
353+ it ( 'does not create a table if one exists' , function ( ) {
354+ deployClient = new subject ( baseOptions ) ;
355+
356+ return deployClient . knex . schema . createTable ( 'bar' , tbl => {
357+ tbl . increments ( ) ;
358+ tbl . string ( 'key' ) . notNullable ( ) . unique ( ) ;
359+ tbl . timestamps ( ) ;
360+ } ) . then ( ( ) => deployClient . sanityCheck ( { tableName : 'bar' } ) )
361+ . then ( ( ) => deployClient . knex ( 'bar' ) . columnInfo ( ) )
362+ . then ( info => {
363+ assert . isObject ( info ) ;
364+
365+ assert . deepPropertyVal ( info , 'id.type' , 'integer' ) ;
366+ assert . deepPropertyVal ( info , 'key.type' , 'varchar' ) ;
367+ assert . deepPropertyVal ( info , 'created_at.type' , 'datetime' ) ;
368+ assert . deepPropertyVal ( info , 'updated_at.type' , 'datetime' ) ;
369+
370+ assert . notProperty ( info , 'value' ) ;
371+ assert . notProperty ( info , 'is_active' ) ;
372+ } ) ;
373+ } ) ;
374+ } ) ;
375+
387376 describe ( '#fetchRevisions()' , function ( ) {
388377 it ( 'returns successfully' , function ( ) {
389378 deployClient = new subject ( baseOptions ) ;
@@ -456,7 +445,8 @@ describe('DeployClient public API', function() {
456445 it ( 'uploads a new revision with a payload' , function ( ) {
457446 deployClient = new subject ( baseOptions ) ;
458447
459- return deployClient . upload ( { tableName : 'foo' , value : 'bar' } )
448+ return deployClient . sanityCheck ( { tableName : 'foo' } )
449+ . then ( ( ) => deployClient . upload ( { tableName : 'foo' , value : 'bar' } ) )
460450 . then ( ( ) => deployClient . knex ( 'foo' ) . select ( 'key' , 'value' ) )
461451 . then ( revisions => {
462452 assert . isArray ( revisions ) ;
@@ -473,7 +463,8 @@ describe('DeployClient public API', function() {
473463 it ( 'returns the table name and revision key' , function ( ) {
474464 deployClient = new subject ( baseOptions ) ;
475465
476- return deployClient . upload ( { tableName : 'foo' , value : 'bar' } )
466+ return deployClient . sanityCheck ( { tableName : 'foo' } )
467+ . then ( ( ) => deployClient . upload ( { tableName : 'foo' , value : 'bar' } ) )
477468 . then ( result => {
478469 assert . propertyVal ( result , 'tableName' , 'foo' ) ;
479470 assert . propertyVal ( result , 'revisionKey' , 'default' ) ;
0 commit comments