|
| 1 | +import { Database, DataClass, KeyPath, Index } from '../index'; |
| 2 | + |
| 3 | +describe('QueryBuilder API - extensive tests', () => { |
| 4 | + @DataClass() |
| 5 | + class User { |
| 6 | + @KeyPath() |
| 7 | + id!: string; |
| 8 | + @Index() |
| 9 | + email!: string; |
| 10 | + @Index() |
| 11 | + age!: number; |
| 12 | + status!: string; |
| 13 | + createdAt!: number; |
| 14 | + constructor(id: string, email: string, age: number, status: string, createdAt: number) { |
| 15 | + this.id = id; |
| 16 | + this.email = email; |
| 17 | + this.age = age; |
| 18 | + this.status = status; |
| 19 | + this.createdAt = createdAt; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + let db: any; |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + db = await Database.build('QueryExtensiveTestDB', [User]); |
| 27 | + await db.User.clear(); |
| 28 | + await db.User.create(new User('u1', 'alice@example.com', 25, 'active', 100)); |
| 29 | + await db.User.create(new User('u2', 'bob@example.com', 17, 'inactive', 200)); |
| 30 | + await db.User.create(new User('u3', 'carol@example.com', 30, 'active', 300)); |
| 31 | + await db.User.create(new User('u4', 'dave@example.com', 22, 'active', 400)); |
| 32 | + await db.User.create(new User('u5', 'eve@example.com', 40, 'inactive', 500)); |
| 33 | + }); |
| 34 | + |
| 35 | + it('true positive: should find active users older than 18', async () => { |
| 36 | + const users = await db.query(User) |
| 37 | + .where('age').gt(18) |
| 38 | + .and('status').equals('active') |
| 39 | + .execute(); |
| 40 | + expect(users.map((u: User) => u.id).sort()).toEqual(['u1', 'u3', 'u4']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('true negative: should not find users with age < 10 and status active', async () => { |
| 44 | + const users = await db.query(User) |
| 45 | + .where('age').lt(10) |
| 46 | + .and('status').equals('active') |
| 47 | + .execute(); |
| 48 | + expect(users.length).toBe(0); |
| 49 | + }); |
| 50 | + |
| 51 | + it('false positive: should not match inactive users when filtering for active', async () => { |
| 52 | + const users = await db.query(User) |
| 53 | + .where('status').equals('active') |
| 54 | + .execute(); |
| 55 | + expect(users.map((u: User) => u.status)).not.toContain('inactive'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('false negative: should not miss any active users', async () => { |
| 59 | + const users = await db.query(User) |
| 60 | + .where('status').equals('active') |
| 61 | + .execute(); |
| 62 | + const expected = ['u1', 'u3', 'u4']; |
| 63 | + expect(users.map((u: User) => u.id).sort()).toEqual(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it('positive: should order users by createdAt desc', async () => { |
| 67 | + const users = await db.query(User) |
| 68 | + .orderBy('createdAt', 'desc') |
| 69 | + .limit(2) |
| 70 | + .execute(); |
| 71 | + expect(users[0].id).toBe('u5'); |
| 72 | + expect(users[1].id).toBe('u4'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('negative: should return empty for impossible age', async () => { |
| 76 | + const users = await db.query(User) |
| 77 | + .where('age').equals(999) |
| 78 | + .execute(); |
| 79 | + expect(users.length).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('index positive: should find users by email index', async () => { |
| 83 | + const users = await db.query(User) |
| 84 | + .useIndex('email') |
| 85 | + .range('bob@example.com', 'carol@example.com') |
| 86 | + .execute(); |
| 87 | + expect(users.map((u: User) => u.id)).toEqual(['u2', 'u3']); |
| 88 | + }); |
| 89 | + |
| 90 | + it('index negative: should throw for non-existent index', async () => { |
| 91 | + await expect(db.query(User).useIndex('nonexistent').execute()).rejects.toThrow(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('offset and limit: should paginate results', async () => { |
| 95 | + const users = await db.query(User) |
| 96 | + .orderBy('createdAt', 'asc') |
| 97 | + .offset(1) |
| 98 | + .limit(2) |
| 99 | + .execute(); |
| 100 | + expect(users.map((u: User) => u.id)).toEqual(['u2', 'u3']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('multiple conditions: should find users with age >= 22 and status active', async () => { |
| 104 | + const users = await db.query(User) |
| 105 | + .where('age').gte(22) |
| 106 | + .and('status').equals('active') |
| 107 | + .execute(); |
| 108 | + expect(users.map((u: User) => u.id).sort()).toEqual(['u1', 'u3', 'u4']); |
| 109 | + }); |
| 110 | +}); |
0 commit comments