|
| 1 | +const Joi = require('joi') |
| 2 | +const { test, requelize, dropDb } = require('./utils') |
| 3 | + |
| 4 | +test('unknown relation - hasOne', (t) => { |
| 5 | + t.plan(1) |
| 6 | + |
| 7 | + let Foo |
| 8 | + let Bar |
| 9 | + let foo |
| 10 | + |
| 11 | + dropDb() |
| 12 | + .then(() => { |
| 13 | + Foo = requelize.model('foo', { |
| 14 | + name: Joi.string() |
| 15 | + }) |
| 16 | + |
| 17 | + Bar = requelize.model('bar') |
| 18 | + |
| 19 | + Foo.hasOne('bar', 'bar') |
| 20 | + Bar.belongsTo('foo', 'foo') |
| 21 | + |
| 22 | + return requelize.sync() |
| 23 | + }) |
| 24 | + .then(() => { |
| 25 | + foo = new Foo({ name: 'foo' }) |
| 26 | + |
| 27 | + return foo.save() |
| 28 | + }) |
| 29 | + .then((res) => { |
| 30 | + return Foo.embed({ bar: true }) |
| 31 | + }) |
| 32 | + .then((res) => { |
| 33 | + t.equal(null, res[0].bar, 'hasOne relation is null when not found') |
| 34 | + }) |
| 35 | + .catch((err) => { |
| 36 | + t.fail(err) |
| 37 | + }) |
| 38 | + .then(() => { |
| 39 | + t.end() |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +test('unknown relation - belongsTo', (t) => { |
| 44 | + t.plan(1) |
| 45 | + |
| 46 | + let Foo |
| 47 | + let Bar |
| 48 | + let bar |
| 49 | + |
| 50 | + dropDb() |
| 51 | + .then(() => { |
| 52 | + Foo = requelize.model('foo', { |
| 53 | + name: Joi.string() |
| 54 | + }) |
| 55 | + |
| 56 | + Bar = requelize.model('bar') |
| 57 | + |
| 58 | + Foo.hasOne('bar', 'bar') |
| 59 | + Bar.belongsTo('foo', 'foo') |
| 60 | + |
| 61 | + return requelize.sync() |
| 62 | + }) |
| 63 | + .then(() => { |
| 64 | + bar = new Bar({ name: 'bar' }) |
| 65 | + |
| 66 | + return bar.save() |
| 67 | + }) |
| 68 | + .then((res) => { |
| 69 | + return Bar.embed({ foo: true }) |
| 70 | + }) |
| 71 | + .then((res) => { |
| 72 | + t.equal(null, res[0].foo, 'belongsTo relation is null when not found') |
| 73 | + }) |
| 74 | + .catch((err) => { |
| 75 | + t.fail(err) |
| 76 | + }) |
| 77 | + .then(() => { |
| 78 | + t.end() |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +test('unknown relation - hasMany', (t) => { |
| 83 | + t.plan(1) |
| 84 | + |
| 85 | + let Foo |
| 86 | + let Bar |
| 87 | + let foo |
| 88 | + |
| 89 | + dropDb() |
| 90 | + .then(() => { |
| 91 | + Foo = requelize.model('foo', { |
| 92 | + name: Joi.string() |
| 93 | + }) |
| 94 | + |
| 95 | + Bar = requelize.model('bar') |
| 96 | + |
| 97 | + Foo.hasMany('bar', 'bars') |
| 98 | + Bar.belongsTo('foo', 'foo') |
| 99 | + |
| 100 | + return requelize.sync() |
| 101 | + }) |
| 102 | + .then(() => { |
| 103 | + foo = new Foo({ name: 'foo' }) |
| 104 | + |
| 105 | + return foo.save() |
| 106 | + }) |
| 107 | + .then((res) => { |
| 108 | + return Foo.embed({ bars: true }) |
| 109 | + }) |
| 110 | + .then((res) => { |
| 111 | + t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found') |
| 112 | + }) |
| 113 | + .catch((err) => { |
| 114 | + t.fail(err) |
| 115 | + }) |
| 116 | + .then(() => { |
| 117 | + t.end() |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +test('unknown relation - belongsToMany', (t) => { |
| 122 | + t.plan(1) |
| 123 | + |
| 124 | + let Foo |
| 125 | + let Bar |
| 126 | + let foo |
| 127 | + |
| 128 | + dropDb() |
| 129 | + .then(() => { |
| 130 | + Foo = requelize.model('foo', { |
| 131 | + name: Joi.string() |
| 132 | + }) |
| 133 | + |
| 134 | + Bar = requelize.model('bar') |
| 135 | + |
| 136 | + Foo.belongsToMany('bar', 'bars') |
| 137 | + Bar.belongsToMany('foo', 'foo') |
| 138 | + |
| 139 | + return requelize.sync() |
| 140 | + }) |
| 141 | + .then(() => { |
| 142 | + foo = new Foo({ name: 'foo' }) |
| 143 | + |
| 144 | + return foo.save() |
| 145 | + }) |
| 146 | + .then((res) => { |
| 147 | + return Foo.embed({ bars: true }) |
| 148 | + }) |
| 149 | + .then((res) => { |
| 150 | + t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found') |
| 151 | + }) |
| 152 | + .catch((err) => { |
| 153 | + t.fail(err) |
| 154 | + }) |
| 155 | + .then(() => { |
| 156 | + t.end() |
| 157 | + }) |
| 158 | +}) |
0 commit comments