count.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Database } from 'arangojs'
  2. import { DocumentMetadata } from 'arangojs/documents'
  3. import { expect } from 'chai'
  4. import lib from '../lib'
  5. const testData: Record<string, (Pick<DocumentMetadata, '_key'> & Record<string, unknown>)[]> = {
  6. pets: [
  7. { _key: 'greedo', name: 'Greedo', age: 5, species: 'cat' },
  8. { _key: 'haribo', name: 'Haribo', age: 1.5, species: 'dog' },
  9. { _key: 'iguana', name: 'Iguana', age: 3, species: 'dog' },
  10. { _key: 'jerkins', name: 'Jerkins', age: 15, species: 'cat' },
  11. { _key: 'kahlua', name: 'Kahlua', age: 0.5, species: 'hamster' },
  12. { _key: 'lemonade', name: 'Lemonade', age: 9, species: 'dog' }
  13. ],
  14. staff: [
  15. { _key: 'aaron', name: 'Aaron', age: 38 },
  16. { _key: 'benedict', name: 'Benedict', age: 25 },
  17. { _key: 'cheryl', name: 'Cheryl', age: 34 },
  18. { _key: 'davide', name: 'Davide', age: 52 },
  19. { _key: 'emma', name: 'Emma', age: 23 },
  20. { _key: 'frank', name: 'Frank', age: 47 }
  21. ]
  22. }
  23. const getDatabase = async () => {
  24. let db = new Database({
  25. url: 'arangodb://127.0.0.1:8529'
  26. })
  27. if (!await db.database('arangosearch').exists()) {
  28. await db.createDatabase('arangosearch')
  29. }
  30. db = db.database('arangosearch')
  31. await installTestData(db)
  32. return db
  33. }
  34. const installTestData = async (db: Database) => {
  35. for (const c in testData) {
  36. const collection = db.collection(c)
  37. if (!await collection.exists()) {
  38. await db.createCollection(c)
  39. await collection.saveAll(testData[c], { overwriteMode: 'ignore' })
  40. }
  41. }
  42. }
  43. describe('count()', () => {
  44. for (const c in testData) {
  45. describe(c, () => {
  46. const expected = testData[c].length
  47. it(`should match ${expected} documents`, async () => {
  48. const db = await getDatabase()
  49. const count = await lib.count(db, db.collection(c))()
  50. expect(count).to.equal(expected)
  51. })
  52. })
  53. }
  54. })
  55. describe('count({ age: { gt: 5 }})', () => {
  56. for (const c in testData) {
  57. describe(c, () => {
  58. const expected = testData[c].filter(i => i.age && i.age > 5).length
  59. it(`should match ${expected} documents`, async () => {
  60. const db = await getDatabase()
  61. const count = await lib.count(db, db.collection(c))({
  62. age: { gt: 5 }
  63. })
  64. expect(count).to.equal(expected)
  65. })
  66. })
  67. }
  68. })
  69. describe('count({ name: { eq: "Iguana" }})', () => {
  70. for (const c in testData) {
  71. describe(c, () => {
  72. const expected = testData[c].filter(i => i.name === 'Iguana').length
  73. it(`should match ${expected} documents`, async () => {
  74. const db = await getDatabase()
  75. const count = await lib.count(db, db.collection(c))({
  76. name: { eq: 'Iguana' }
  77. })
  78. expect(count).to.equal(expected)
  79. })
  80. })
  81. }
  82. })