Skip to content

Commit 86f5d36

Browse files
authored
Merge branch 'main' into clean-eslintcache
2 parents 7f38fb8 + 2804886 commit 86f5d36

File tree

3 files changed

+232
-257
lines changed

3 files changed

+232
-257
lines changed

src/random.ts

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ function arrayRemove<T>(arr: T[], values: T[]): T[] {
1414
return arr;
1515
}
1616

17+
/**
18+
* Generates random values of different kinds. Some methods are deprecated and have been moved to dedicated modules.
19+
*/
1720
export class Random {
1821
constructor(private readonly faker: Faker, seed?: any[] | any) {
1922
// Use a user provided seed if it is an array or number
@@ -33,12 +36,24 @@ export class Random {
3336
}
3437

3538
/**
36-
* Returns a single random number based on a max number or range.
39+
* Returns a single random number between zero and the given max value or the given range with the specified precision.
40+
* The bounds are inclusive.
41+
*
42+
* @param options Maximum value or options object.
43+
* @param options.min Lower bound for generated number. Defaults to `0`.
44+
* @param options.max Upper bound for generated number. Defaults to `99999`.
45+
* @param options.precision Precision of the generated number. Defaults to `1`.
3746
*
38-
* @method faker.random.number
39-
* @param options {min, max, precision}
47+
* @example
48+
* faker.random.number() // 55422
49+
* faker.random.number(100) // 52
50+
* faker.random.number({ min: 1000000 }) // 431433
51+
* faker.random.number({ max: 100 }) // 42
52+
* faker.random.number({ precision: 0.01 }) // 64246.18
53+
* faker.random.number({ min: 10, max: 100, precision: 0.01 }) // 36.94
4054
*
4155
* @deprecated
56+
* @see faker.datatype.number()
4257
*/
4358
number(
4459
options?: number | { min?: number; max?: number; precision?: number }
@@ -50,12 +65,23 @@ export class Random {
5065
}
5166

5267
/**
53-
* Returns a single random floating-point number based on a max number or range.
68+
* Returns a single random floating-point number for the given precision or range and precision.
5469
*
55-
* @method faker.random.float
56-
* @param options
70+
* @param options Precision or options object.
71+
* @param options.min Lower bound for generated number. Defaults to `0`.
72+
* @param options.max Upper bound for generated number. Defaults to `99999`.
73+
* @param options.precision Precision of the generated number. Defaults to `0.01`.
74+
*
75+
* @example
76+
* faker.random.float() // 51696.36
77+
* faker.random.float(0.1) // 52023.2
78+
* faker.random.float({ min: 1000000 }) // 212859.76
79+
* faker.random.float({ max: 100 }) // 28.11
80+
* faker.random.float({ precision: 0.1 }) // 84055.3
81+
* faker.random.float({ min: 10, max: 100, precision: 0.001 }) // 57.315
5782
*
5883
* @deprecated
84+
* @see faker.datatype.float()
5985
*/
6086
float(
6187
options?: number | { min?: number; max?: number; precision?: number }
@@ -67,10 +93,13 @@ export class Random {
6793
}
6894

6995
/**
70-
* Takes an array and returns a random element of the array.
96+
* Returns random element from the given array.
97+
*
98+
* @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`.
7199
*
72-
* @method faker.random.arrayElement
73-
* @param array
100+
* @example
101+
* faker.random.arrayElement() // 'b'
102+
* faker.random.arrayElement(['cat', 'dog', 'mouse']) // 'dog'
74103
*/
75104
arrayElement<T = string>(
76105
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
@@ -80,11 +109,17 @@ export class Random {
80109
}
81110

82111
/**
83-
* Takes an array and returns a subset with random elements of the array.
112+
* Returns a subset with random elements of the given array in random order.
84113
*
85-
* @method faker.random.arrayElements
86-
* @param array
87-
* @param count number of elements to pick
114+
* @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`.
115+
* @param count Number of elements to pick.
116+
* When not provided, random number of elements will be picked.
117+
* When value exceeds array boundaries, it will be limited to stay inside.
118+
*
119+
* @example
120+
* faker.random.arrayElements() // ['b', 'c']
121+
* faker.random.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat']
122+
* faker.random.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2]
88123
*/
89124
arrayElements<T>(
90125
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>,
@@ -117,7 +152,7 @@ export class Random {
117152
}
118153

119154
/**
120-
* Takes an object and returns a random key or value.
155+
* Returns a random key or value from given object.
121156
*
122157
* @method faker.random.objectElement
123158
* @param object
@@ -142,10 +177,13 @@ export class Random {
142177
}
143178

144179
/**
145-
* uuid
180+
* Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)).
181+
*
182+
* @example
183+
* faker.random.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'
146184
*
147-
* @method faker.random.uuid
148185
* @deprecated
186+
* @see faker.datatype.uuid()
149187
*/
150188
uuid(): string {
151189
console.log(
@@ -155,10 +193,13 @@ export class Random {
155193
}
156194

157195
/**
158-
* boolean
196+
* Returns the boolean value true or false.
197+
*
198+
* @example
199+
* faker.random.boolean() // false
159200
*
160-
* @method faker.random.boolean
161201
* @deprecated
202+
* @see faker.datatype.boolean()
162203
*/
163204
boolean(): boolean {
164205
console.log(
@@ -167,15 +208,14 @@ export class Random {
167208
return this.faker.datatype.boolean();
168209
}
169210

170-
// TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc
171211
/**
172-
* word
212+
* Returns random word.
173213
*
174-
* @method faker.random.word
175-
* @param type
214+
* @example
215+
* faker.random.word() // 'Seamless'
176216
*/
177-
// TODO @Shinigami92 2022-01-11: `type` is not in use
178-
word(type?: unknown): string {
217+
// TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc
218+
word(): string {
179219
const wordMethods = [
180220
'commerce.department',
181221
'commerce.productName',
@@ -216,27 +256,34 @@ export class Random {
216256
return this.faker.random.arrayElement(result.split(' '));
217257
}
218258

259+
/**
260+
* @see word()
261+
*/
219262
readonly randomWord: Random['word'] = this.word.bind(this);
220263

221264
/**
222-
* randomWords
265+
* Returns string with set of random words.
266+
*
267+
* @param count Number of words. Defaults to a random value between `1` and `3`
223268
*
224-
* @method faker.random.words
225-
* @param count defaults to a random value between 1 and 3
269+
* @example
270+
* faker.random.words() // 'neural'
271+
* faker.random.words(5) // 'copy Handcrafted bus client-server Point'
226272
*/
227273
words(count?: number): string {
228274
const words: string[] = [];
275+
229276
if (typeof count === 'undefined') {
230277
count = this.faker.datatype.number({ min: 1, max: 3 });
231278
}
279+
232280
for (let i = 0; i < count; i++) {
233281
words.push(this.faker.random.word());
234282
}
283+
235284
return words.join(' ');
236285
}
237286

238-
readonly randomWords: Random['words'] = this.words.bind(this);
239-
240287
/**
241288
* locale
242289
*

test/database.spec.ts

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,98 @@
1-
import { describe, expect, it, vi } from 'vitest';
1+
import { afterEach, describe, expect, it } from 'vitest';
22
import { faker } from '../src';
33

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'];
3137

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';
5741
});
5842

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);
7248

73-
spy_database_type.mockRestore();
49+
const actual = faker.database[functionName]();
50+
expect(actual).toEqual(expectations[functionName]);
51+
});
52+
}
7453
});
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+
}
7597
});
7698
});

0 commit comments

Comments
 (0)