|
| 1 | +import { beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { resetDependencyInjectionsForTesting } from "../../config/TestingDI"; |
| 3 | +import { globalServiceRegistry } from "@workglow/util"; |
| 4 | +import { ENTITY_REPOSITORY_TOKEN } from "../../storage/entity/EntitySchema"; |
| 5 | +import { queryEntities } from "./EntityQuery"; |
| 6 | + |
| 7 | +describe("queryEntities", () => { |
| 8 | + beforeEach(() => { |
| 9 | + resetDependencyInjectionsForTesting(); |
| 10 | + }); |
| 11 | + |
| 12 | + it("returns empty array and total 0 for empty DB", async () => { |
| 13 | + const result = await queryEntities({}); |
| 14 | + expect(result.rows).toEqual([]); |
| 15 | + expect(result.total).toBe(0); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns entities after insertion", async () => { |
| 19 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 20 | + await repo.put({ |
| 21 | + cik: 1318605, |
| 22 | + name: "Tesla, Inc.", |
| 23 | + type: null, |
| 24 | + sic: 3711, |
| 25 | + ein: null, |
| 26 | + description: null, |
| 27 | + website: null, |
| 28 | + investor_website: null, |
| 29 | + category: null, |
| 30 | + fiscal_year: null, |
| 31 | + state_incorporation: "TX", |
| 32 | + state_incorporation_desc: null, |
| 33 | + }); |
| 34 | + |
| 35 | + const result = await queryEntities({}); |
| 36 | + expect(result.rows.length).toBe(1); |
| 37 | + expect(result.total).toBe(1); |
| 38 | + expect(result.rows[0].cik).toBe(1318605); |
| 39 | + expect(result.rows[0].name).toBe("Tesla, Inc."); |
| 40 | + }); |
| 41 | + |
| 42 | + it("filters by CIK (exact match)", async () => { |
| 43 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 44 | + await repo.put({ |
| 45 | + cik: 1318605, |
| 46 | + name: "Tesla, Inc.", |
| 47 | + type: null, |
| 48 | + sic: 3711, |
| 49 | + ein: null, |
| 50 | + description: null, |
| 51 | + website: null, |
| 52 | + investor_website: null, |
| 53 | + category: null, |
| 54 | + fiscal_year: null, |
| 55 | + state_incorporation: "TX", |
| 56 | + state_incorporation_desc: null, |
| 57 | + }); |
| 58 | + await repo.put({ |
| 59 | + cik: 320193, |
| 60 | + name: "Apple Inc.", |
| 61 | + type: null, |
| 62 | + sic: 3571, |
| 63 | + ein: null, |
| 64 | + description: null, |
| 65 | + website: null, |
| 66 | + investor_website: null, |
| 67 | + category: null, |
| 68 | + fiscal_year: null, |
| 69 | + state_incorporation: "CA", |
| 70 | + state_incorporation_desc: null, |
| 71 | + }); |
| 72 | + |
| 73 | + const result = await queryEntities({ cik: 1318605 }); |
| 74 | + expect(result.rows.length).toBe(1); |
| 75 | + expect(result.total).toBe(1); |
| 76 | + expect(result.rows[0].name).toBe("Tesla, Inc."); |
| 77 | + }); |
| 78 | + |
| 79 | + it("filters by name search (partial, case-insensitive)", async () => { |
| 80 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 81 | + await repo.put({ |
| 82 | + cik: 1318605, |
| 83 | + name: "Tesla, Inc.", |
| 84 | + type: null, |
| 85 | + sic: 3711, |
| 86 | + ein: null, |
| 87 | + description: null, |
| 88 | + website: null, |
| 89 | + investor_website: null, |
| 90 | + category: null, |
| 91 | + fiscal_year: null, |
| 92 | + state_incorporation: "TX", |
| 93 | + state_incorporation_desc: null, |
| 94 | + }); |
| 95 | + await repo.put({ |
| 96 | + cik: 320193, |
| 97 | + name: "Apple Inc.", |
| 98 | + type: null, |
| 99 | + sic: 3571, |
| 100 | + ein: null, |
| 101 | + description: null, |
| 102 | + website: null, |
| 103 | + investor_website: null, |
| 104 | + category: null, |
| 105 | + fiscal_year: null, |
| 106 | + state_incorporation: "CA", |
| 107 | + state_incorporation_desc: null, |
| 108 | + }); |
| 109 | + |
| 110 | + const result = await queryEntities({ search: "tesla" }); |
| 111 | + expect(result.rows.length).toBe(1); |
| 112 | + expect(result.total).toBe(1); |
| 113 | + expect(result.rows[0].name).toBe("Tesla, Inc."); |
| 114 | + }); |
| 115 | + |
| 116 | + it("respects limit and offset", async () => { |
| 117 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 118 | + for (let i = 1; i <= 5; i++) { |
| 119 | + await repo.put({ |
| 120 | + cik: i, |
| 121 | + name: `Entity ${i}`, |
| 122 | + type: null, |
| 123 | + sic: null, |
| 124 | + ein: null, |
| 125 | + description: null, |
| 126 | + website: null, |
| 127 | + investor_website: null, |
| 128 | + category: null, |
| 129 | + fiscal_year: null, |
| 130 | + state_incorporation: null, |
| 131 | + state_incorporation_desc: null, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const result = await queryEntities({ limit: 2, offset: 1 }); |
| 136 | + expect(result.rows.length).toBe(2); |
| 137 | + expect(result.total).toBe(5); |
| 138 | + }); |
| 139 | + |
| 140 | + it("total count reflects unsliced results", async () => { |
| 141 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 142 | + for (let i = 1; i <= 10; i++) { |
| 143 | + await repo.put({ |
| 144 | + cik: i, |
| 145 | + name: `Entity ${i}`, |
| 146 | + type: null, |
| 147 | + sic: null, |
| 148 | + ein: null, |
| 149 | + description: null, |
| 150 | + website: null, |
| 151 | + investor_website: null, |
| 152 | + category: null, |
| 153 | + fiscal_year: null, |
| 154 | + state_incorporation: null, |
| 155 | + state_incorporation_desc: null, |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + const result = await queryEntities({ limit: 3 }); |
| 160 | + expect(result.rows.length).toBe(3); |
| 161 | + expect(result.total).toBe(10); |
| 162 | + }); |
| 163 | + |
| 164 | + it("filters by SIC code", async () => { |
| 165 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 166 | + await repo.put({ |
| 167 | + cik: 1318605, |
| 168 | + name: "Tesla, Inc.", |
| 169 | + type: null, |
| 170 | + sic: 3711, |
| 171 | + ein: null, |
| 172 | + description: null, |
| 173 | + website: null, |
| 174 | + investor_website: null, |
| 175 | + category: null, |
| 176 | + fiscal_year: null, |
| 177 | + state_incorporation: "TX", |
| 178 | + state_incorporation_desc: null, |
| 179 | + }); |
| 180 | + await repo.put({ |
| 181 | + cik: 320193, |
| 182 | + name: "Apple Inc.", |
| 183 | + type: null, |
| 184 | + sic: 3571, |
| 185 | + ein: null, |
| 186 | + description: null, |
| 187 | + website: null, |
| 188 | + investor_website: null, |
| 189 | + category: null, |
| 190 | + fiscal_year: null, |
| 191 | + state_incorporation: "CA", |
| 192 | + state_incorporation_desc: null, |
| 193 | + }); |
| 194 | + |
| 195 | + const result = await queryEntities({ sic: 3711 }); |
| 196 | + expect(result.rows.length).toBe(1); |
| 197 | + expect(result.rows[0].name).toBe("Tesla, Inc."); |
| 198 | + }); |
| 199 | + |
| 200 | + it("filters by state", async () => { |
| 201 | + const repo = globalServiceRegistry.get(ENTITY_REPOSITORY_TOKEN); |
| 202 | + await repo.put({ |
| 203 | + cik: 1318605, |
| 204 | + name: "Tesla, Inc.", |
| 205 | + type: null, |
| 206 | + sic: 3711, |
| 207 | + ein: null, |
| 208 | + description: null, |
| 209 | + website: null, |
| 210 | + investor_website: null, |
| 211 | + category: null, |
| 212 | + fiscal_year: null, |
| 213 | + state_incorporation: "TX", |
| 214 | + state_incorporation_desc: null, |
| 215 | + }); |
| 216 | + await repo.put({ |
| 217 | + cik: 320193, |
| 218 | + name: "Apple Inc.", |
| 219 | + type: null, |
| 220 | + sic: 3571, |
| 221 | + ein: null, |
| 222 | + description: null, |
| 223 | + website: null, |
| 224 | + investor_website: null, |
| 225 | + category: null, |
| 226 | + fiscal_year: null, |
| 227 | + state_incorporation: "CA", |
| 228 | + state_incorporation_desc: null, |
| 229 | + }); |
| 230 | + |
| 231 | + const result = await queryEntities({ state: "CA" }); |
| 232 | + expect(result.rows.length).toBe(1); |
| 233 | + expect(result.rows[0].name).toBe("Apple Inc."); |
| 234 | + }); |
| 235 | +}); |
0 commit comments