@leansdk/leanrc
Version:
LeanRC is a MVC framework for creating graceful applications
238 lines (236 loc) • 12 kB
text/coffeescript
{ expect, assert } = require 'chai'
sinon = require 'sinon'
_ = require 'lodash'
RC = require '@leansdk/rc/lib'
LeanRC = require.main.require 'lib'
{
FuncG, SubsetG
RecordInterface
Record
Utils: { co }
} = LeanRC::
describe 'RelationsMixin', ->
describe '.new', ->
facade = null
afterEach ->
facade?.remove?()
it 'should create item with record mixin', ->
expect ->
collectionName = 'TestsCollection'
KEY = 'TEST_RELATIONS_MIXIN_001'
facade = LeanRC::Facade.getInstance KEY
class Test extends LeanRC
class TestsCollection extends Test::Collection
LeanRC::MemoryCollectionMixin
LeanRC::GenerateUuidIdMixin
Test
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
collection = TestsCollection.new collectionName,
delegate: 'TestRecord'
facade.registerProxy collection
record = TestRecord.new {type: 'Test::TestRecord'}, collection
assert.instanceOf record, TestRecord, 'record is not a TestRecord instance'
.to.not.throw Error
describe '.relatedTo', ->
it 'relatedTo: should define one-to-one or one-to-many optional relation for class', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
relation: LeanRC::PromiseT,
attr: 'relation_attr'
refKey: 'id'
recordName: -> 'TestRecord'
collectionName: -> 'TestsCollection'
through: ['tomatosRels', by: 'cucumberId']
inverse: 'test'
{ relation: relationData } = TestRecord.relations
assert.equal relationData.refKey, 'id', 'Value of `refKey` is incorrect'
assert.equal relationData.attr, 'relation_attr', 'Value of `attr` is incorrect'
assert.equal relationData.inverse, 'test', 'Value of `inverse` is incorrect'
assert.equal relationData.relation, 'relatedTo', 'Value of `relation` is incorrect'
assert.deepEqual relationData.through, ['tomatosRels', by: 'cucumberId'], 'Value of `through` is incorrect'
assert.isArray relationData.through, 'Value of `through` isn`t array'
assert.isString relationData.through[0], 'Value of `through[0]` isn`t string'
assert.isObject relationData.through[1], 'Value of `through[1]` isn`t object'
assert.isString relationData.through[1].by, 'Value of `through[1].by` isn`t string'
assert.equal relationData.recordName.call(Test::TestRecord), 'TestRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'TestsCollection', 'Value of `collectionName` is incorrect'
yield return
it 'relatedTo: should define options automatically', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
cucumber: LeanRC::PromiseT
{ cucumber: relationData } = TestRecord.relations
assert.equal relationData.attr, 'cucumberId', 'Value of `attr` is incorrect'
assert.equal relationData.recordName.call(Test::TestRecord), 'CucumberRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'CucumbersCollection', 'Value of `collectionName` is incorrect'
assert.equal relationData.inverse, 'tests', 'Value of `inverse` is incorrect'
yield return
describe '.belongsTo', ->
it 'belongsTo: should define one-to-one or one-to-many parent relation for class', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
relation: LeanRC::PromiseT,
attr: 'relation_attr'
refKey: 'id'
recordName: -> 'TestRecord'
collectionName: -> 'TestsCollection'
through: ['tomatosRels', by: 'cucumberId']
inverse: 'test'
{ relation: relationData } = TestRecord.relations
assert.equal relationData.refKey, 'id', 'Value of `refKey` is incorrect'
assert.equal relationData.attr, 'relation_attr', 'Value of `attr` is incorrect'
assert.equal relationData.inverse, 'test', 'Value of `inverse` is incorrect'
assert.equal relationData.relation, 'belongsTo', 'Value of `relation` is incorrect'
assert.deepEqual relationData.through, ['tomatosRels', by: 'cucumberId'], 'Value of `through` is incorrect'
assert.isArray relationData.through, 'Value of `through` isn`t array'
assert.isString relationData.through[0], 'Value of `through[0]` isn`t string'
assert.isObject relationData.through[1], 'Value of `through[1]` isn`t object'
assert.isString relationData.through[1].by, 'Value of `through[1].by` isn`t string'
assert.equal relationData.recordName.call(Test::TestRecord), 'TestRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'TestsCollection', 'Value of `collectionName` is incorrect'
yield return
it 'belongsTo: should define options automatically', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
cucumber: LeanRC::PromiseT
{ cucumber: relationData } = TestRecord.relations
assert.equal relationData.attr, 'cucumberId', 'Value of `attr` is incorrect'
assert.equal relationData.recordName.call(Test::TestRecord), 'CucumberRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'CucumbersCollection', 'Value of `collectionName` is incorrect'
assert.equal relationData.inverse, 'tests', 'Value of `inverse` is incorrect'
yield return
describe '.hasMany', ->
it 'hasMany: should define one-to-one or one-to-many relation for class', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
manyRelation: LeanRC::PromiseT,
refKey: 'id'
recordName: -> 'TestRecord'
collectionName: -> 'TestsCollection'
through: ['tomatosRels', by: 'cucumberId']
inverse: 'test'
{ manyRelation: relationData } = TestRecord.relations
assert.equal relationData.refKey, 'id', 'Value of `refKey` is incorrect'
assert.equal relationData.inverse, 'test', 'Value of `inverse` is incorrect'
assert.equal relationData.relation, 'hasMany', 'Value of `relation` is incorrect'
assert.deepEqual relationData.through, ['tomatosRels', by: 'cucumberId'], 'Value of `through` is incorrect'
assert.isArray relationData.through, 'Value of `through` isn`t array'
assert.isString relationData.through[0], 'Value of `through[0]` isn`t string'
assert.isObject relationData.through[1], 'Value of `through[1]` isn`t object'
assert.isString relationData.through[1].by, 'Value of `through[1].by` isn`t string'
assert.equal relationData.recordName.call(Test::TestRecord), 'TestRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'TestsCollection', 'Value of `collectionName` is incorrect'
yield return
describe '.hasOne', ->
it 'hasOne: should define many-to-one or many-to-one relation for class', ->
co ->
class Test extends LeanRC
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
findRecordByName: FuncG(String, SubsetG RecordInterface),
default: (asType) -> TestRecord
oneRelation: LeanRC::PromiseT,
refKey: 'id'
recordName: -> 'TestRecord'
collectionName: -> 'TestsCollection'
through: ['tomatosRels', by: 'cucumberId']
inverse: 'test'
{ oneRelation: relationData } = TestRecord.relations
assert.equal relationData.refKey, 'id', 'Value of `refKey` is incorrect'
assert.equal relationData.inverse, 'test', 'Value of `inverse` is incorrect'
assert.equal relationData.relation, 'hasOne', 'Value of `relation` is incorrect'
assert.deepEqual relationData.through, ['tomatosRels', by: 'cucumberId'], 'Value of `through` is incorrect'
assert.isArray relationData.through, 'Value of `through` isn`t array'
assert.isString relationData.through[0], 'Value of `through[0]` isn`t string'
assert.isObject relationData.through[1], 'Value of `through[1]` isn`t object'
assert.isString relationData.through[1].by, 'Value of `through[1].by` isn`t string'
assert.equal relationData.recordName.call(Test::TestRecord), 'TestRecord', 'Value of `recordName` is incorrect'
assert.equal relationData.collectionName.call(Test::TestRecord), 'TestsCollection', 'Value of `collectionName` is incorrect'
yield return
describe '.inverseFor', ->
it 'should get inverse info', ->
co ->
class Test extends LeanRC
class RelationRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
test: LeanRC::PromiseT,
inverse: 'relation_attr'
class TestRecord extends LeanRC::Record
LeanRC::RelationsMixin
Test
relation: LeanRC::PromiseT,
attr: 'relation_attr'
refKey: 'id'
inverse: 'test'
inverseInfo = TestRecord.inverseFor 'relation'
assert.equal inverseInfo.recordClass, RelationRecord, 'Record class is incorrect'
assert.equal inverseInfo.attrName,'test', 'Record class is incorrect'
assert.equal inverseInfo.relation, 'hasOne', 'Record class is incorrect'
yield return