-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement type casting methods (#97)
* implement type casting methods * no-unused-vars * update type declaration * no-unused-vars * 2.14.2
- Loading branch information
1 parent
d3768c7
commit 898b00d
Showing
8 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,78 @@ | ||
import { SqlFormatter, QueryEntity, QueryExpression, QueryField } from '../src/index'; | ||
import { MemoryAdapter } from './test/TestMemoryAdapter'; | ||
import { MemoryFormatter } from './test/TestMemoryFormatter'; | ||
|
||
describe('Type Casting', () => { | ||
|
||
/** | ||
* @type {MemoryAdapter} | ||
*/ | ||
let db; | ||
beforeAll(() => { | ||
db = new MemoryAdapter({ | ||
name: 'local', | ||
database: './spec/db/local.db' | ||
}); | ||
}); | ||
afterAll((done) => { | ||
if (db) { | ||
db.close(); | ||
return done(); | ||
} | ||
}) | ||
it('should use $toString function', async () => { | ||
const Product = new QueryEntity('ProductData'); | ||
const Order = new QueryEntity('OrderData'); | ||
const id = new QueryField('id').from(Product); | ||
let orderedItem = new QueryField('orderedItem').from(Order); | ||
let expr = new QueryExpression().where(id).equal(orderedItem); | ||
const formatter = new MemoryFormatter(); | ||
let sql = formatter.formatWhere(expr.$where); | ||
expect(sql).toEqual('(`ProductData`.`id`=`OrderData`.`orderedItem`)'); | ||
orderedItem =new QueryField({ | ||
'$toString': [ | ||
new QueryField('orderedItem').from(Order) | ||
] | ||
}); | ||
expr = new QueryExpression().where(id).equal(orderedItem); | ||
sql = formatter.formatWhere(expr.$where); | ||
expect(sql).toEqual('(`ProductData`.`id`=CAST(`OrderData`.`orderedItem` AS TEXT))'); | ||
}); | ||
|
||
it('should use $toString inside closure', async () => { | ||
const Products = new QueryEntity('ProductData'); | ||
const q = new QueryExpression().select(({id, name, price}) => { | ||
return { | ||
id, | ||
price, | ||
priceString: price.toString(), | ||
name | ||
} | ||
}).from(Products); | ||
const formatter = new MemoryFormatter(); | ||
const items = await db.executeAsync(q); | ||
items.forEach(({price, priceString}) => { | ||
expect(typeof priceString).toEqual('string'); | ||
const fromString = parseFloat(priceString); | ||
expect(price).toEqual(fromString); | ||
}); | ||
}); | ||
|
||
it('should use $toInt inside closure', async () => { | ||
const Products = new QueryEntity('ProductData'); | ||
const q = new QueryExpression().select(({id, name, price}) => { | ||
return { | ||
id, | ||
price, | ||
priceInt: parseInt(price), | ||
name | ||
} | ||
}).from(Products); | ||
const items = await db.executeAsync(q); | ||
items.forEach(({price, priceInt}) => { | ||
expect(typeof priceInt).toEqual('number'); | ||
const fromInt = parseInt(price); | ||
expect(priceInt).toEqual(fromInt); | ||
}); | ||
}); | ||
}); |
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
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