|
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,80 @@ 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 | + it('should store geometrySubtype for Polygon geometry column', async () => { |
| 253 | + const typmod = getGISTypeModifier(GisSubtype.Polygon, false, false, 4326); |
| 254 | + const attribute: Record<string, any> = { codec: { name: 'geometry' }, extensions: {} }; |
| 255 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 256 | + |
| 257 | + await attributeHook({}, event); |
| 258 | + expect(attribute.extensions.geometrySubtype).toBe('Polygon'); |
| 259 | + }); |
| 260 | + |
| 261 | + it('should store geometrySubtype for Point geometry column', async () => { |
| 262 | + const typmod = getGISTypeModifier(GisSubtype.Point, false, false, 4326); |
| 263 | + const attribute: Record<string, any> = { codec: { name: 'geometry' }, extensions: {} }; |
| 264 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 265 | + |
| 266 | + await attributeHook({}, event); |
| 267 | + expect(attribute.extensions.geometrySubtype).toBe('Point'); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should store geometrySubtype for MultiPolygon geography column', async () => { |
| 271 | + const typmod = getGISTypeModifier(GisSubtype.MultiPolygon, false, false, 4326); |
| 272 | + const attribute: Record<string, any> = { codec: { name: 'geography' }, extensions: {} }; |
| 273 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 274 | + |
| 275 | + await attributeHook({}, event); |
| 276 | + expect(attribute.extensions.geometrySubtype).toBe('MultiPolygon'); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should skip unconstrained geometry (atttypmod = -1)', async () => { |
| 280 | + const attribute: Record<string, any> = { codec: { name: 'geometry' }, extensions: {} }; |
| 281 | + const event = { pgAttribute: { atttypmod: -1 }, attribute }; |
| 282 | + |
| 283 | + await attributeHook({}, event); |
| 284 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 285 | + }); |
| 286 | + |
| 287 | + it('should skip when atttypmod is null', async () => { |
| 288 | + const attribute: Record<string, any> = { codec: { name: 'geometry' }, extensions: {} }; |
| 289 | + const event = { pgAttribute: { atttypmod: null as number | null }, attribute }; |
| 290 | + |
| 291 | + await attributeHook({}, event); |
| 292 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 293 | + }); |
| 294 | + |
| 295 | + it('should not store subtype for base Geometry (subtype=0)', async () => { |
| 296 | + const typmod = getGISTypeModifier(GisSubtype.Geometry, false, false, 4326); |
| 297 | + const attribute: Record<string, any> = { codec: { name: 'geometry' }, extensions: {} }; |
| 298 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 299 | + |
| 300 | + await attributeHook({}, event); |
| 301 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 302 | + }); |
| 303 | + |
| 304 | + it('should skip non-geometry codec types', async () => { |
| 305 | + const typmod = getGISTypeModifier(GisSubtype.Point, false, false, 4326); |
| 306 | + const attribute: Record<string, any> = { codec: { name: 'text' }, extensions: {} }; |
| 307 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 308 | + |
| 309 | + await attributeHook({}, event); |
| 310 | + expect(attribute.extensions.geometrySubtype).toBeUndefined(); |
| 311 | + }); |
| 312 | + |
| 313 | + it('should create extensions object if not present', async () => { |
| 314 | + const typmod = getGISTypeModifier(GisSubtype.LineString, false, false, 4326); |
| 315 | + const attribute: Record<string, any> = { codec: { name: 'geometry' } }; |
| 316 | + const event = { pgAttribute: { atttypmod: typmod }, attribute }; |
| 317 | + |
| 318 | + await attributeHook({}, event); |
| 319 | + expect(attribute.extensions).toBeDefined(); |
| 320 | + expect(attribute.extensions.geometrySubtype).toBe('LineString'); |
| 321 | + }); |
| 322 | + }); |
245 | 323 | }); |
0 commit comments