-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(manyToMany): attach method now updates pivot rows when attributes…
… are defined
- Loading branch information
1 parent
ee94a39
commit 43c129e
Showing
4 changed files
with
263 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../adonis-typings/index.ts" /> | ||
|
||
import test from 'japa' | ||
import { syncDiff } from '../src/utils' | ||
|
||
test.group('Utils | attachDiff', () => { | ||
test('return ids to be inserted', (assert) => { | ||
const dbRows = [{ | ||
id: '1', | ||
user_id: '1', | ||
skill_id: '1', | ||
score: 1, | ||
}] | ||
|
||
const idsToSync = ['1', '2', '3'] | ||
|
||
const diff = syncDiff(dbRows, idsToSync, (rows, id) => rows.find(({ skill_id }) => skill_id === id)) | ||
assert.deepEqual(diff, { insert: ['2', '3'], update: [] }) | ||
}) | ||
|
||
test('return ids when ids to sync are represented as an object', (assert) => { | ||
const dbRows = [{ | ||
id: '1', | ||
user_id: '1', | ||
skill_id: '1', | ||
score: 1, | ||
}] | ||
|
||
const idsToSync = { | ||
'1': {}, | ||
'2': {}, | ||
'3': {}, | ||
} | ||
|
||
const diff = syncDiff(dbRows, idsToSync, (rows, id) => rows.find(({ skill_id }) => skill_id === id)) | ||
assert.deepEqual(diff, { insert: ['2', '3'], update: [] }) | ||
}) | ||
|
||
test('return ids to be updated when attributes are different', (assert) => { | ||
const dbRows = [{ | ||
id: '1', | ||
user_id: '1', | ||
skill_id: '1', | ||
score: 1, | ||
}] | ||
|
||
const idsToSync = { | ||
'1': { | ||
score: 4, | ||
}, | ||
'2': { | ||
score: 2, | ||
}, | ||
'3': { | ||
score: 1, | ||
}, | ||
} | ||
|
||
const diff = syncDiff(dbRows, idsToSync, (rows, id) => rows.find(({ skill_id }) => skill_id === id)) | ||
assert.deepEqual(diff, { insert: ['2', '3'], update: ['1'] }) | ||
}) | ||
|
||
test('ignore rows whose attributes value is same', (assert) => { | ||
const dbRows = [{ | ||
id: '1', | ||
user_id: '1', | ||
skill_id: '1', | ||
score: 1, | ||
}] | ||
|
||
const idsToSync = { | ||
'1': { | ||
score: 1, | ||
}, | ||
'2': { | ||
score: 2, | ||
}, | ||
'3': { | ||
score: 1, | ||
}, | ||
} | ||
|
||
const diff = syncDiff(dbRows, idsToSync, (rows, id) => rows.find(({ skill_id }) => skill_id === id)) | ||
assert.deepEqual(diff, { insert: ['2', '3'], update: [] }) | ||
}) | ||
}) |