|
1 | 1 | import type { PgCodec } from '@dataplan/pg'; |
2 | 2 | import { PostgisCodecPlugin } from '../src/plugins/codec'; |
3 | 3 | import type { GisFieldValue } from '../src/types'; |
| 4 | +import { GisSubtype } from '../src/constants'; |
| 5 | +import { getGISTypeModifier } from '../src/utils'; |
4 | 6 |
|
5 | 7 | // Test event shape matching the GatherHooks.pgCodecs_findPgCodec event |
6 | 8 | interface MockEvent { |
@@ -242,4 +244,83 @@ describe('PostgisCodecPlugin', () => { |
242 | 244 | }); |
243 | 245 | }); |
244 | 246 | }); |
| 247 | + |
| 248 | + describe('pgCodecs_attribute hook', () => { |
| 249 | + const attributeHook = (PostgisCodecPlugin as { gather: { hooks: { pgCodecs_attribute: Function } } }) |
| 250 | + .gather.hooks.pgCodecs_attribute; |
| 251 | + |
| 252 | + async function runAttributeHook({ |
| 253 | + codecName, |
| 254 | + typmod, |
| 255 | + withExtensions = true, |
| 256 | + }: { |
| 257 | + codecName: string; |
| 258 | + typmod: number | null; |
| 259 | + withExtensions?: boolean; |
| 260 | + }) { |
| 261 | + const attribute: Record<string, any> = { codec: { name: codecName } }; |
| 262 | + if (withExtensions) { |
| 263 | + attribute.extensions = {}; |
| 264 | + } |
| 265 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 266 | + |
| 267 | + await attributeHook({}, event); |
| 268 | + return attribute; |
| 269 | + } |
| 270 | + |
| 271 | + it.each([ |
| 272 | + ['geometry', GisSubtype.Polygon, 'Polygon'], |
| 273 | + ['geometry', GisSubtype.Point, 'Point'], |
| 274 | + ['geography', GisSubtype.MultiPolygon, 'MultiPolygon'], |
| 275 | + ])('stores geometrySubtype for %s subtype %s', async (codecName, subtype, expected) => { |
| 276 | + const attribute = await runAttributeHook({ |
| 277 | + codecName, |
| 278 | + typmod: getGISTypeModifier(subtype, false, false, 4326), |
| 279 | + }); |
| 280 | + |
| 281 | + expect(attribute.extensions.geometrySubtype).toBe(expected); |
| 282 | + }); |
| 283 | + |
| 284 | + it('should skip unconstrained geometry (atttypmod = -1)', async () => { |
| 285 | + const attribute = await runAttributeHook({ |
| 286 | + codecName: 'geometry', |
| 287 | + typmod: -1, |
| 288 | + }); |
| 289 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 290 | + }); |
| 291 | + |
| 292 | + it('should skip when atttypmod is null', async () => { |
| 293 | + const attribute = await runAttributeHook({ |
| 294 | + codecName: 'geometry', |
| 295 | + typmod: null, |
| 296 | + }); |
| 297 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 298 | + }); |
| 299 | + |
| 300 | + it('should not store subtype for base Geometry (subtype=0)', async () => { |
| 301 | + const attribute = await runAttributeHook({ |
| 302 | + codecName: 'geometry', |
| 303 | + typmod: getGISTypeModifier(GisSubtype.Geometry, false, false, 4326), |
| 304 | + }); |
| 305 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 306 | + }); |
| 307 | + |
| 308 | + it('should skip non-geometry codec types', async () => { |
| 309 | + const attribute = await runAttributeHook({ |
| 310 | + codecName: 'text', |
| 311 | + typmod: getGISTypeModifier(GisSubtype.Point, false, false, 4326), |
| 312 | + }); |
| 313 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 314 | + }); |
| 315 | + |
| 316 | + it('should create extensions object if not present', async () => { |
| 317 | + const attribute = await runAttributeHook({ |
| 318 | + codecName: 'geometry', |
| 319 | + typmod: getGISTypeModifier(GisSubtype.LineString, false, false, 4326), |
| 320 | + withExtensions: false, |
| 321 | + }); |
| 322 | + expect(attribute.extensions).toBeDefined(); |
| 323 | + expect(attribute.extensions.geometrySubtype).toBe('LineString'); |
| 324 | + }); |
| 325 | + }); |
245 | 326 | }); |
0 commit comments