|
| 1 | +import { EntityConverter } from '../../generators/EntityConverter'; |
| 2 | +import { TypeParser } from '../../generators/TypeParser'; |
| 3 | +import { UtilityHelpers } from '../../generators/UtilityHelpers'; |
| 4 | +import { EntityAttribute } from '../../interfaces/EntityAttribute'; |
| 5 | + |
| 6 | +describe('EntityConverter - ISO 639 format detection', () => { |
| 7 | + let entityConverter: EntityConverter; |
| 8 | + let typeParser: TypeParser; |
| 9 | + let utilityHelpers: UtilityHelpers; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + utilityHelpers = new UtilityHelpers(); |
| 13 | + typeParser = new TypeParser(utilityHelpers); |
| 14 | + entityConverter = new EntityConverter(typeParser, utilityHelpers); |
| 15 | + }); |
| 16 | + |
| 17 | + test('should apply iso-639-1 format when attribute type contains "ISO 639-1"', () => { |
| 18 | + const attribute: EntityAttribute = { |
| 19 | + name: 'language', |
| 20 | + type: 'String (ISO 639-1 language two-letter code)', |
| 21 | + description: 'The default posting language', |
| 22 | + optional: false, |
| 23 | + nullable: false, |
| 24 | + deprecated: false, |
| 25 | + enumValues: [], |
| 26 | + versions: ['2.4.2'], |
| 27 | + }; |
| 28 | + |
| 29 | + const result = entityConverter.convertAttribute(attribute); |
| 30 | + expect(result.type).toBe('string'); |
| 31 | + expect(result.format).toBe('iso-639-1'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should apply iso-639-1 format when attribute type contains "ISO 639 Part 1"', () => { |
| 35 | + const attribute: EntityAttribute = { |
| 36 | + name: 'language', |
| 37 | + type: 'String (ISO 639 Part 1 two-letter language code)', |
| 38 | + description: 'The default posting language', |
| 39 | + optional: false, |
| 40 | + nullable: false, |
| 41 | + deprecated: false, |
| 42 | + enumValues: [], |
| 43 | + versions: ['2.4.2'], |
| 44 | + }; |
| 45 | + |
| 46 | + const result = entityConverter.convertAttribute(attribute); |
| 47 | + expect(result.type).toBe('string'); |
| 48 | + expect(result.format).toBe('iso-639-1'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should apply iso-639-1 format to array items when type indicates ISO 639-1', () => { |
| 52 | + const attribute: EntityAttribute = { |
| 53 | + name: 'languages', |
| 54 | + type: 'Array of String (ISO 639-1 language two-letter code)', |
| 55 | + description: 'Which languages are you following from this user?', |
| 56 | + optional: false, |
| 57 | + nullable: false, |
| 58 | + deprecated: false, |
| 59 | + enumValues: [], |
| 60 | + versions: ['4.0.0'], |
| 61 | + }; |
| 62 | + |
| 63 | + const result = entityConverter.convertAttribute(attribute); |
| 64 | + expect(result.type).toBe('array'); |
| 65 | + expect(result.items).toEqual({ |
| 66 | + type: 'string', |
| 67 | + format: 'iso-639-1', |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should apply iso-639-1 format with nullable attribute', () => { |
| 72 | + const attribute: EntityAttribute = { |
| 73 | + name: 'language', |
| 74 | + type: 'String (ISO 639-1 language two-letter code)', |
| 75 | + description: 'The default posting language', |
| 76 | + optional: true, |
| 77 | + nullable: true, |
| 78 | + deprecated: false, |
| 79 | + enumValues: [], |
| 80 | + versions: ['2.4.2'], |
| 81 | + }; |
| 82 | + |
| 83 | + const result = entityConverter.convertAttribute(attribute); |
| 84 | + expect(result.type).toEqual(['string', 'null']); |
| 85 | + expect(result.format).toBe('iso-639-1'); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should apply iso-639-1 format with enum values', () => { |
| 89 | + const attribute: EntityAttribute = { |
| 90 | + name: 'language', |
| 91 | + type: 'String (ISO 639-1 language two-letter code)', |
| 92 | + description: 'The default posting language', |
| 93 | + optional: false, |
| 94 | + nullable: false, |
| 95 | + deprecated: false, |
| 96 | + enumValues: ['en', 'es', 'fr'], |
| 97 | + versions: ['2.4.2'], |
| 98 | + }; |
| 99 | + |
| 100 | + const result = entityConverter.convertAttribute(attribute); |
| 101 | + expect(result.type).toBe('string'); |
| 102 | + expect(result.format).toBe('iso-639-1'); |
| 103 | + expect(result.enum).toEqual(['en', 'es', 'fr']); |
| 104 | + }); |
| 105 | + |
| 106 | + test('should apply iso-639-1 format with case-insensitive detection', () => { |
| 107 | + const attribute: EntityAttribute = { |
| 108 | + name: 'language', |
| 109 | + type: 'String (iso 639-1 language code)', |
| 110 | + description: 'The default posting language', |
| 111 | + optional: false, |
| 112 | + nullable: false, |
| 113 | + deprecated: false, |
| 114 | + enumValues: [], |
| 115 | + versions: ['2.4.2'], |
| 116 | + }; |
| 117 | + |
| 118 | + const result = entityConverter.convertAttribute(attribute); |
| 119 | + expect(result.type).toBe('string'); |
| 120 | + expect(result.format).toBe('iso-639-1'); |
| 121 | + }); |
| 122 | + |
| 123 | + test('should not apply iso-639-1 format to regular string attributes', () => { |
| 124 | + const attribute: EntityAttribute = { |
| 125 | + name: 'content', |
| 126 | + type: 'String', |
| 127 | + description: 'The content of the status', |
| 128 | + optional: false, |
| 129 | + nullable: false, |
| 130 | + deprecated: false, |
| 131 | + enumValues: [], |
| 132 | + versions: ['1.0.0'], |
| 133 | + }; |
| 134 | + |
| 135 | + const result = entityConverter.convertAttribute(attribute); |
| 136 | + expect(result.type).toBe('string'); |
| 137 | + expect(result.format).toBeUndefined(); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should not apply iso-639-1 format when type does not contain ISO 639', () => { |
| 141 | + const attribute: EntityAttribute = { |
| 142 | + name: 'language', |
| 143 | + type: 'String (language code)', |
| 144 | + description: 'The default posting language', |
| 145 | + optional: false, |
| 146 | + nullable: false, |
| 147 | + deprecated: false, |
| 148 | + enumValues: [], |
| 149 | + versions: ['2.4.2'], |
| 150 | + }; |
| 151 | + |
| 152 | + const result = entityConverter.convertAttribute(attribute); |
| 153 | + expect(result.type).toBe('string'); |
| 154 | + expect(result.format).toBeUndefined(); |
| 155 | + }); |
| 156 | + |
| 157 | + test('should preserve other formats and not apply iso-639-1 when datetime format is detected', () => { |
| 158 | + const attribute: EntityAttribute = { |
| 159 | + name: 'created_at', |
| 160 | + type: 'String (ISO8601 datetime)', |
| 161 | + description: 'When the account was created', |
| 162 | + optional: false, |
| 163 | + nullable: false, |
| 164 | + deprecated: false, |
| 165 | + enumValues: [], |
| 166 | + versions: ['1.0.0'], |
| 167 | + }; |
| 168 | + |
| 169 | + const result = entityConverter.convertAttribute(attribute); |
| 170 | + expect(result.type).toBe('string'); |
| 171 | + expect(result.format).toBe('date-time'); |
| 172 | + }); |
| 173 | + |
| 174 | + test('should apply iso-639-1 format with "or empty string" variation', () => { |
| 175 | + const attribute: EntityAttribute = { |
| 176 | + name: 'language', |
| 177 | + type: 'String (ISO 639-1 language two-letter code) or empty string', |
| 178 | + description: 'The default posting language', |
| 179 | + optional: true, |
| 180 | + nullable: true, |
| 181 | + deprecated: false, |
| 182 | + enumValues: [], |
| 183 | + versions: ['2.4.2'], |
| 184 | + }; |
| 185 | + |
| 186 | + const result = entityConverter.convertAttribute(attribute); |
| 187 | + expect(result.type).toEqual(['string', 'null']); |
| 188 | + expect(result.format).toBe('iso-639-1'); |
| 189 | + }); |
| 190 | +}); |
0 commit comments