|
| 1 | +import { gql } from 'graphql-tag'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { EntitiesQueryNode } from '../core/query-tree/queries.js'; |
| 4 | +import { createSimpleModel } from '../testing/utils/create-simple-model.js'; |
| 5 | +import { getAQLQuery } from './aql-generator.js'; |
| 6 | +import { |
| 7 | + billingCollectionName, |
| 8 | + getCollectionNameForRelation, |
| 9 | + getCollectionNameForRootEntity, |
| 10 | +} from './arango-basics.js'; |
| 11 | +import { ArangoDBAdapter } from './arangodb-adapter.js'; |
| 12 | +import { |
| 13 | + getFlexSearchViewNameForRootEntity, |
| 14 | + getRequiredViewsFromModel, |
| 15 | +} from './schema-migration/arango-search-helpers.js'; |
| 16 | +import { getRequiredIndicesFromModel } from './schema-migration/index-helpers.js'; |
| 17 | + |
| 18 | +describe('collectionNamePrefix', () => { |
| 19 | + const model = createSimpleModel(gql` |
| 20 | + type Order @rootEntity(flexSearch: true) { |
| 21 | + orderNumber: String @key @flexSearch @index |
| 22 | + deliveries: [Delivery] @relation |
| 23 | + } |
| 24 | +
|
| 25 | + type Delivery @rootEntity { |
| 26 | + trackingNumber: String @index |
| 27 | + } |
| 28 | + `); |
| 29 | + const orderType = model.getRootEntityTypeOrThrow('Order'); |
| 30 | + const deliveryType = model.getRootEntityTypeOrThrow('Delivery'); |
| 31 | + const orderToDeliveryRelation = orderType.relations[0]; |
| 32 | + |
| 33 | + describe('getCollectionNameForRootEntity', () => { |
| 34 | + it('returns unprefixed name when prefix is undefined', () => { |
| 35 | + expect(getCollectionNameForRootEntity(orderType, { prefix: undefined })).toBe('orders'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('prepends the prefix to the collection name', () => { |
| 39 | + expect(getCollectionNameForRootEntity(orderType, { prefix: 'myapp_' })).toBe( |
| 40 | + 'myapp_orders', |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('works with an empty string prefix', () => { |
| 45 | + expect(getCollectionNameForRootEntity(orderType, { prefix: '' })).toBe('orders'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('getCollectionNameForRelation', () => { |
| 50 | + it('returns unprefixed name when prefix is undefined', () => { |
| 51 | + expect( |
| 52 | + getCollectionNameForRelation(orderToDeliveryRelation, { prefix: undefined }), |
| 53 | + ).toBe('orders_deliveries'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('prepends the prefix to the edge collection name', () => { |
| 57 | + expect( |
| 58 | + getCollectionNameForRelation(orderToDeliveryRelation, { prefix: 'myapp_' }), |
| 59 | + ).toBe('myapp_orders_deliveries'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('getFlexSearchViewNameForRootEntity', () => { |
| 64 | + it('returns unprefixed view name when prefix is undefined', () => { |
| 65 | + expect(getFlexSearchViewNameForRootEntity(orderType, { prefix: undefined })).toBe( |
| 66 | + 'flex_view_orders', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('prepends the prefix before flex_view_ in the view name', () => { |
| 71 | + expect(getFlexSearchViewNameForRootEntity(orderType, { prefix: 'myapp_' })).toBe( |
| 72 | + 'myapp_flex_view_orders', |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('getRequiredViewsFromModel', () => { |
| 78 | + it('returns unprefixed names when prefix is undefined', () => { |
| 79 | + const views = getRequiredViewsFromModel(model, { prefix: undefined }); |
| 80 | + expect(views).toHaveLength(1); |
| 81 | + expect(views[0].viewName).toBe('flex_view_orders'); |
| 82 | + expect(views[0].collectionName).toBe('orders'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('uses the prefix for both view name and collection name', () => { |
| 86 | + const views = getRequiredViewsFromModel(model, { prefix: 'myapp_' }); |
| 87 | + expect(views).toHaveLength(1); |
| 88 | + expect(views[0].viewName).toBe('myapp_flex_view_orders'); |
| 89 | + expect(views[0].collectionName).toBe('myapp_orders'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('getRequiredIndicesFromModel', () => { |
| 94 | + it('returns unprefixed collection names when prefix is undefined', () => { |
| 95 | + const indices = getRequiredIndicesFromModel(model, { prefix: undefined }); |
| 96 | + const orderIndices = indices.filter((i) => i.rootEntity === orderType); |
| 97 | + expect(orderIndices.every((i) => i.collectionName === 'orders')).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('prepends the prefix to collection names in index definitions', () => { |
| 101 | + const indices = getRequiredIndicesFromModel(model, { prefix: 'myapp_' }); |
| 102 | + const orderIndices = indices.filter((i) => i.rootEntity === orderType); |
| 103 | + const deliveryIndices = indices.filter((i) => i.rootEntity === deliveryType); |
| 104 | + expect(orderIndices.every((i) => i.collectionName === 'myapp_orders')).toBe(true); |
| 105 | + expect(deliveryIndices.every((i) => i.collectionName === 'myapp_deliveries')).toBe( |
| 106 | + true, |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('AQL generation', () => { |
| 112 | + it('generates unprefixed collection names when no prefix is set', () => { |
| 113 | + const aqlCompound = getAQLQuery(new EntitiesQueryNode(orderType)); |
| 114 | + expect(aqlCompound.readAccessedCollections).toContain('orders'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('generates prefixed collection names in AQL when collectionNamePrefix is set', () => { |
| 118 | + const aqlCompound = getAQLQuery(new EntitiesQueryNode(orderType), { |
| 119 | + collectionNamePrefix: 'myapp_', |
| 120 | + }); |
| 121 | + expect(aqlCompound.readAccessedCollections).toContain('myapp_orders'); |
| 122 | + expect(aqlCompound.readAccessedCollections).not.toContain('orders'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('includes prefix in AQL bind parameters', () => { |
| 126 | + const aqlCompound = getAQLQuery(new EntitiesQueryNode(orderType), { |
| 127 | + collectionNamePrefix: 'myapp_', |
| 128 | + }); |
| 129 | + const query = aqlCompound.getExecutableQueries()[0]; |
| 130 | + expect(query.boundValues).toHaveProperty('@myapp_orders', 'myapp_orders'); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('billing collection', () => { |
| 135 | + it('billingCollectionName is a plain string constant', () => { |
| 136 | + expect(billingCollectionName).toBe('billingEntities'); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('ArangoDBAdapter constructor validation', () => { |
| 141 | + const validConfig = { |
| 142 | + url: 'http://localhost:8529', |
| 143 | + databaseName: 'test', |
| 144 | + }; |
| 145 | + |
| 146 | + it('accepts a valid prefix', () => { |
| 147 | + expect( |
| 148 | + () => |
| 149 | + new ArangoDBAdapter({ |
| 150 | + ...validConfig, |
| 151 | + collectionNamePrefix: 'myapp_', |
| 152 | + }), |
| 153 | + ).not.toThrow(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('accepts undefined prefix', () => { |
| 157 | + expect(() => new ArangoDBAdapter({ ...validConfig })).not.toThrow(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('throws on a prefix with invalid characters (hyphen)', () => { |
| 161 | + expect( |
| 162 | + () => |
| 163 | + new ArangoDBAdapter({ |
| 164 | + ...validConfig, |
| 165 | + collectionNamePrefix: 'my-app_', |
| 166 | + }), |
| 167 | + ).toThrow(/collectionNamePrefix/); |
| 168 | + }); |
| 169 | + |
| 170 | + it('throws on a prefix with a space', () => { |
| 171 | + expect( |
| 172 | + () => |
| 173 | + new ArangoDBAdapter({ |
| 174 | + ...validConfig, |
| 175 | + collectionNamePrefix: 'my app_', |
| 176 | + }), |
| 177 | + ).toThrow(/collectionNamePrefix/); |
| 178 | + }); |
| 179 | + |
| 180 | + it('throws on a prefix with a dot', () => { |
| 181 | + expect( |
| 182 | + () => |
| 183 | + new ArangoDBAdapter({ |
| 184 | + ...validConfig, |
| 185 | + collectionNamePrefix: 'my.app_', |
| 186 | + }), |
| 187 | + ).toThrow(/collectionNamePrefix/); |
| 188 | + }); |
| 189 | + |
| 190 | + it('throws on an empty string prefix', () => { |
| 191 | + expect( |
| 192 | + () => |
| 193 | + new ArangoDBAdapter({ |
| 194 | + ...validConfig, |
| 195 | + collectionNamePrefix: '', |
| 196 | + }), |
| 197 | + ).toThrow(/collectionNamePrefix/); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments