1+ var sqlite = require ( 'sqlite3' ) ;
2+ var pg = require ( 'pg' ) ;
13var should = require ( 'should' ) ;
24var helper = require ( '../support/spec_helper' ) ;
5+ var common = require ( '../common' ) ;
36var ORM = require ( '../../' ) ;
47
58describe ( "ORM" , function ( ) {
69 var db = null ;
7- var Person = null ;
8-
9- var setup = function ( cache ) {
10- return function ( done ) {
11- Person = db . define ( "person" , {
12- name : String
13- } , {
14- cache : cache ,
15- methods : {
16- UID : function ( ) {
17- return this . id ;
18- }
19- }
20- } ) ;
21-
22- ORM . singleton . clear ( ) ; // clear cache
23-
24- return helper . dropSync ( Person , function ( ) {
25- Person . create ( [ {
26- name : "John Doe"
27- } , {
28- name : "Jane Doe"
29- } ] , done ) ;
30- } ) ;
31- } ;
32- } ;
3310
3411 before ( function ( done ) {
3512 helper . connect ( function ( connection ) {
@@ -44,8 +21,6 @@ describe("ORM", function() {
4421 } ) ;
4522
4623 describe ( "when loaded" , function ( ) {
47- before ( setup ( true ) ) ;
48-
4924 it ( "should expose .express(), .use() and .connect()" , function ( done ) {
5025 ORM . express . should . a ( "function" ) ;
5126 ORM . use . should . a ( "function" ) ;
@@ -109,6 +84,17 @@ describe("ORM.connect()", function () {
10984 } ) ;
11085 } ) ;
11186
87+ it ( "should allow protocol alias" , function ( done ) {
88+ var db = ORM . connect ( "pg://unknowndb" ) ;
89+
90+ db . on ( "connect" , function ( err ) {
91+ should . exist ( err ) ;
92+ err . message . should . not . equal ( "CONNECTION_PROTOCOL_NOT_SUPPORTED" ) ;
93+
94+ return done ( ) ;
95+ } ) ;
96+ } ) ;
97+
11298 it ( "should emit an error if empty url is passed" , function ( done ) {
11399 var db = ORM . connect ( "" ) ;
114100
@@ -149,6 +135,51 @@ describe("ORM.connect()", function () {
149135 } ) ;
150136 } ) ;
151137
138+ it ( "should emit an error if cannot connect" , function ( done ) {
139+ var db = ORM . connect ( "mysql://fakeuser:nopassword@127.0.0.1/unknowndb" ) ;
140+
141+ db . on ( "connect" , function ( err ) {
142+ should . exist ( err ) ;
143+ err . message . should . not . equal ( "CONNECTION_PROTOCOL_NOT_SUPPORTED" ) ;
144+ err . message . should . not . equal ( "CONNECTION_URL_NO_PROTOCOL" ) ;
145+ err . message . should . not . equal ( "CONNECTION_URL_EMPTY" ) ;
146+
147+ return done ( ) ;
148+ } ) ;
149+ } ) ;
150+
151+ it ( "should emit no error if ok" , function ( done ) {
152+ var db = ORM . connect ( common . getConnectionString ( ) ) ;
153+
154+ db . on ( "connect" , function ( err ) {
155+ should . not . exist ( err ) ;
156+
157+ return done ( ) ;
158+ } ) ;
159+ } ) ;
160+
161+ describe ( "if no connection error" , function ( ) {
162+ var db = null ;
163+
164+ before ( function ( done ) {
165+ helper . connect ( function ( connection ) {
166+ db = connection ;
167+
168+ return done ( ) ;
169+ } ) ;
170+ } ) ;
171+
172+ after ( function ( ) {
173+ return db . close ( ) ;
174+ } ) ;
175+
176+ it ( "should be able to ping the server" , function ( done ) {
177+ db . ping ( function ( ) {
178+ return done ( ) ;
179+ } ) ;
180+ } ) ;
181+ } ) ;
182+
152183 describe ( "if callback is passed" , function ( done ) {
153184 it ( "should return an error if empty url is passed" , function ( done ) {
154185 ORM . connect ( "" , function ( err ) {
@@ -175,3 +206,35 @@ describe("ORM.connect()", function () {
175206 } ) ;
176207 } ) ;
177208} ) ;
209+
210+ describe ( "ORM.use()" , function ( ) {
211+ it ( "should be able to use an established connection" , function ( done ) {
212+ var db = new sqlite . Database ( ':memory:' ) ;
213+
214+ ORM . use ( db , "sqlite" , function ( err ) {
215+ should . equal ( err , null ) ;
216+
217+ return done ( ) ;
218+ } ) ;
219+ } ) ;
220+
221+ it ( "should be accept protocol alias" , function ( done ) {
222+ var db = new pg . Client ( ) ;
223+
224+ ORM . use ( db , "pg" , function ( err ) {
225+ should . equal ( err , null ) ;
226+
227+ return done ( ) ;
228+ } ) ;
229+ } ) ;
230+
231+ it ( "should return an error in callback if protocol not supported" , function ( done ) {
232+ var db = new pg . Client ( ) ;
233+
234+ ORM . use ( db , "unknowndriver" , function ( err ) {
235+ should . exist ( err ) ;
236+
237+ return done ( ) ;
238+ } ) ;
239+ } ) ;
240+ } ) ;
0 commit comments