-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
6 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,63 @@ | ||
// @flow | ||
|
||
/* eslint-disable flowtype/no-weak-types */ | ||
|
||
import test from 'ava'; | ||
import sinon from 'sinon'; | ||
import { | ||
maybeOne, | ||
DataIntegrityError | ||
} from '../../src'; | ||
|
||
test('returns the first row', async (t) => { | ||
const stub = sinon.stub().returns({ | ||
rows: [ | ||
{ | ||
foo: 1 | ||
} | ||
] | ||
}); | ||
|
||
const connection: any = { | ||
query: stub | ||
}; | ||
|
||
const result = await maybeOne(connection, 'SELECT foo FROM bar'); | ||
|
||
t.deepEqual(result, { | ||
foo: 1 | ||
}); | ||
}); | ||
|
||
test('returns null if no results', async (t) => { | ||
const stub = sinon.stub().returns({ | ||
rows: [] | ||
}); | ||
|
||
const connection: any = { | ||
query: stub | ||
}; | ||
|
||
const result = await maybeOne(connection, 'SELECT foo FROM bar'); | ||
|
||
t.true(result === null); | ||
}); | ||
|
||
test('throws an error if more than one row is returned', async (t) => { | ||
const stub = sinon.stub().returns({ | ||
rows: [ | ||
{ | ||
foo: 1 | ||
}, | ||
{ | ||
foo: 2 | ||
} | ||
] | ||
}); | ||
|
||
const connection: any = { | ||
query: stub | ||
}; | ||
|
||
await t.throws(maybeOne(connection, 'SELECT foo FROM bar'), DataIntegrityError); | ||
}); |