|
| 1 | +import assert from 'assert'; |
| 2 | +import Connection from '../lib/connection'; |
| 3 | +import Schema from '../lib/schema'; |
| 4 | +import Model from '../lib/model'; |
| 5 | + |
| 6 | +describe('Connection', () => { |
| 7 | + describe('constructor', () => { |
| 8 | + it('should succeed', () => { |
| 9 | + assert.ok(new Connection('data')); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should fail', () => { |
| 13 | + assert.throws(() => new Connection(), Error); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should create its own db schema type', () => { |
| 17 | + const db = new Connection('data'); |
| 18 | + assert.ok(Schema.isPrototypeOf(db.Schema)); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('model method', () => { |
| 23 | + let db; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + db = new Connection('data'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should create and store a model', () => { |
| 30 | + const schema = new db.Schema({ field: String }); |
| 31 | + const model = db.model('TheModel', schema); |
| 32 | + |
| 33 | + assert.ok(model); |
| 34 | + assert.equal(model, db.models.TheModel); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should create a schema if an object is passed', () => { |
| 38 | + const model = db.model('TheModel', { field: String }); |
| 39 | + |
| 40 | + assert.ok(model); |
| 41 | + assert.ok(model.schema); |
| 42 | + assert.ok(model.schema instanceof db.Schema); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return an already registered model', () => { |
| 46 | + const modelOne = db.model('TheModel', { field: String }); |
| 47 | + const modelTwo = db.model('TheModel', { field: String }); |
| 48 | + |
| 49 | + assert.equal(modelOne, modelTwo); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should fail because name is invalid', () => { |
| 53 | + assert.throws(() => { |
| 54 | + db.model('', { field: String }); |
| 55 | + }, Error, 'Invalid name for model'); |
| 56 | + |
| 57 | + assert.throws(() => { |
| 58 | + db.model(123, { field: String }); |
| 59 | + }, Error, 'Invalid name for model'); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments