|
1 |
| -import { describe, expect, it, vi } from 'vitest'; |
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
2 | 2 | import { faker } from '../dist/cjs';
|
3 | 3 |
|
4 |
| -describe('database', () => { |
5 |
| - describe('column()', () => { |
6 |
| - it('returns a column name', () => { |
7 |
| - const spy_database_column = vi |
8 |
| - .spyOn(faker.database, 'column') |
9 |
| - .mockReturnValue('title'); |
10 |
| - |
11 |
| - const column = faker.database.column(); |
12 |
| - const expected = 'title'; |
13 |
| - |
14 |
| - expect( |
15 |
| - column, |
16 |
| - `The column name should be equals ${expected}. Current is ${column}` |
17 |
| - ).toBe(expected); |
18 |
| - |
19 |
| - spy_database_column.mockRestore(); |
20 |
| - }); |
21 |
| - }); |
22 |
| - |
23 |
| - describe('collation()', () => { |
24 |
| - it('returns a collation', () => { |
25 |
| - const spy_database_collation = vi |
26 |
| - .spyOn(faker.database, 'collation') |
27 |
| - .mockReturnValue('utf8_bin'); |
28 |
| - |
29 |
| - const collation = faker.database.collation(); |
30 |
| - const expected = 'utf8_bin'; |
| 4 | +const seededRuns = [ |
| 5 | + { |
| 6 | + seed: 42, |
| 7 | + expectations: { |
| 8 | + column: 'token', |
| 9 | + type: 'smallint', |
| 10 | + collation: 'utf8_bin', |
| 11 | + engine: 'MEMORY', |
| 12 | + }, |
| 13 | + }, |
| 14 | + { |
| 15 | + seed: 1337, |
| 16 | + expectations: { |
| 17 | + column: 'email', |
| 18 | + type: 'time', |
| 19 | + collation: 'utf8_general_ci', |
| 20 | + engine: 'MyISAM', |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + seed: 1211, |
| 25 | + expectations: { |
| 26 | + column: 'createdAt', |
| 27 | + type: 'geometry', |
| 28 | + collation: 'cp1250_general_ci', |
| 29 | + engine: 'ARCHIVE', |
| 30 | + }, |
| 31 | + }, |
| 32 | +]; |
| 33 | + |
| 34 | +const NON_SEEDED_BASED_RUN = 5; |
| 35 | + |
| 36 | +const functionNames = ['column', 'type', 'collation', 'engine']; |
31 | 37 |
|
32 |
| - expect( |
33 |
| - collation, |
34 |
| - `The collation should be equals ${expected}. Current is ${collation}` |
35 |
| - ).toBe(expected); |
36 |
| - |
37 |
| - spy_database_collation.mockRestore(); |
38 |
| - }); |
39 |
| - }); |
40 |
| - |
41 |
| - describe('engine()', () => { |
42 |
| - it('returns an engine', () => { |
43 |
| - const spy_database_engine = vi |
44 |
| - .spyOn(faker.database, 'engine') |
45 |
| - .mockReturnValue('InnoDB'); |
46 |
| - |
47 |
| - const engine = faker.database.engine(); |
48 |
| - const expected = 'InnoDB'; |
49 |
| - |
50 |
| - expect( |
51 |
| - engine, |
52 |
| - `The db engine should be equals ${expected}. Current is ${engine}` |
53 |
| - ).toBe(expected); |
54 |
| - |
55 |
| - spy_database_engine.mockRestore(); |
56 |
| - }); |
| 38 | +describe('database', () => { |
| 39 | + afterEach(() => { |
| 40 | + faker.locale = 'en'; |
57 | 41 | });
|
58 | 42 |
|
59 |
| - describe('type()', () => { |
60 |
| - it('returns a column type', () => { |
61 |
| - const spy_database_type = vi |
62 |
| - .spyOn(faker.database, 'type') |
63 |
| - .mockReturnValue('int'); |
64 |
| - |
65 |
| - const type = faker.database.type(); |
66 |
| - const expected = 'int'; |
67 |
| - |
68 |
| - expect( |
69 |
| - type, |
70 |
| - `The column type should be equals ${expected}. Current is ${type}` |
71 |
| - ).toBe(expected); |
| 43 | + for (const { seed, expectations } of seededRuns) { |
| 44 | + describe(`seed: ${seed}`, () => { |
| 45 | + for (const functionName of functionNames) { |
| 46 | + it(`${functionName}()`, () => { |
| 47 | + faker.seed(seed); |
72 | 48 |
|
73 |
| - spy_database_type.mockRestore(); |
| 49 | + const actual = faker.database[functionName](); |
| 50 | + expect(actual).toEqual(expectations[functionName]); |
| 51 | + }); |
| 52 | + } |
74 | 53 | });
|
| 54 | + } |
| 55 | + |
| 56 | + // Create and log-back the seed for debug purposes |
| 57 | + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); |
| 58 | + |
| 59 | + describe(`random seeded tests for seed ${faker.seedValue}`, () => { |
| 60 | + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { |
| 61 | + describe('column()', () => { |
| 62 | + it('should return a column name from array', () => { |
| 63 | + const column = faker.database.column(); |
| 64 | + expect(column).toBeTruthy(); |
| 65 | + expect(typeof column).toBe('string'); |
| 66 | + expect(faker.definitions.database.column).toContain(column); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('collation()', () => { |
| 71 | + it('should return a collation from array', () => { |
| 72 | + const collation = faker.database.collation(); |
| 73 | + expect(collation).toBeTruthy(); |
| 74 | + expect(typeof collation).toBe('string'); |
| 75 | + expect(faker.definitions.database.collation).toContain(collation); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('engine()', () => { |
| 80 | + it('should return an engine from array', () => { |
| 81 | + const engine = faker.database.engine(); |
| 82 | + expect(engine).toBeTruthy(); |
| 83 | + expect(typeof engine).toBe('string'); |
| 84 | + expect(faker.definitions.database.engine).toContain(engine); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('type()', () => { |
| 89 | + it('should return a column type from array', () => { |
| 90 | + const type = faker.database.type(); |
| 91 | + expect(type).toBeTruthy(); |
| 92 | + expect(typeof type).toBe('string'); |
| 93 | + expect(faker.definitions.database.type).toContain(type); |
| 94 | + }); |
| 95 | + }); |
| 96 | + } |
75 | 97 | });
|
76 | 98 | });
|
0 commit comments