UNPKG

als-mongo-list

Version:

A flexible, lightweight MongoDB query utility for Node.js applications. Simplifies database operations with intuitive filtering, pagination, sorting, and field selection. Ideal for REST API endpoints, providing a primary List class that abstracts complex

71 lines (61 loc) 2.61 kB
const { isValidDate } = require('../lib/filter/date'); const { isValidNumber } = require('../lib/filter/number'); const assert = require('node:assert'); const { describe, it } = require('node:test'); describe('Filter Utils', () => { describe('isValidDate', () => { it('should add a valid UTC date to the search object', () => { const search = {}; isValidDate('2022-01-01', search, '$gte', 'createdAt'); assert.deepStrictEqual(search, { createdAt: { $gte: new Date(Date.UTC(2022, 0, 1)) }, }); }); it('should ignore invalid date strings', () => { const search = {}; isValidDate('invalid-date', search, '$gte', 'createdAt'); assert.deepStrictEqual(search, {}); }); it('should handle empty date values gracefully', () => { const search = {}; isValidDate('', search, '$gte', 'createdAt'); assert.deepStrictEqual(search, {}); }); }); describe('isValidNumber', () => { it('should add a valid number to the search object', () => { const search = {}; isValidNumber('25', search, '$gte', 'age', { min: 18, max: 99 }); assert.deepStrictEqual(search, { age: { $gte: 25 } }); }); it('should ignore values below the minimum', () => { const search = {}; isValidNumber('10', search, '$gte', 'age', { min: 18, max: 99 }); assert.deepStrictEqual(search, {}); }); it('should ignore values above the maximum', () => { const search = {}; isValidNumber('120', search, '$gte', 'age', { min: 18, max: 99 }); assert.deepStrictEqual(search, {}); }); it('should ignore invalid number strings', () => { const search = {}; isValidNumber('abc', search, '$gte', 'age', { min: 18, max: 99 }); assert.deepStrictEqual(search, {}); }); }); }); describe('isValidNumber with step', () => { it('should allow number matching the step', () => { const search = {}; isValidNumber('20', search, '$gte', 'price', { min: 10, max: 30, step: 10 }); // 20 делится на 10 без остатка assert.deepStrictEqual(search, { price: { $gte: 20 } }); }); it('should ignore number not matching the step', () => { const search = {}; isValidNumber('25', search, '$gte', 'price', { min: 10, max: 30, step: 10 }); // 25 не делится на 10 без остатка assert.deepStrictEqual(search, {}); }); });