Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: rewrite database tests #393

Merged
merged 5 commits into from
Feb 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 89 additions & 67 deletions test/database.spec.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,98 @@
import { describe, expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';

describe('database', () => {
describe('column()', () => {
it('returns a column name', () => {
const spy_database_column = vi
.spyOn(faker.database, 'column')
.mockReturnValue('title');

const column = faker.database.column();
const expected = 'title';

expect(
column,
`The column name should be equals ${expected}. Current is ${column}`
).toBe(expected);

spy_database_column.mockRestore();
});
});

describe('collation()', () => {
it('returns a collation', () => {
const spy_database_collation = vi
.spyOn(faker.database, 'collation')
.mockReturnValue('utf8_bin');

const collation = faker.database.collation();
const expected = 'utf8_bin';
const seededRuns = [
{
seed: 42,
expectations: {
column: 'token',
type: 'smallint',
collation: 'utf8_bin',
engine: 'MEMORY',
},
},
{
seed: 1337,
expectations: {
column: 'email',
type: 'time',
collation: 'utf8_general_ci',
engine: 'MyISAM',
},
},
{
seed: 1211,
expectations: {
column: 'createdAt',
type: 'geometry',
collation: 'cp1250_general_ci',
engine: 'ARCHIVE',
},
},
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = ['column', 'type', 'collation', 'engine'];

expect(
collation,
`The collation should be equals ${expected}. Current is ${collation}`
).toBe(expected);

spy_database_collation.mockRestore();
});
});

describe('engine()', () => {
it('returns an engine', () => {
const spy_database_engine = vi
.spyOn(faker.database, 'engine')
.mockReturnValue('InnoDB');

const engine = faker.database.engine();
const expected = 'InnoDB';

expect(
engine,
`The db engine should be equals ${expected}. Current is ${engine}`
).toBe(expected);

spy_database_engine.mockRestore();
});
describe('database', () => {
afterEach(() => {
faker.locale = 'en';
});

describe('type()', () => {
it('returns a column type', () => {
const spy_database_type = vi
.spyOn(faker.database, 'type')
.mockReturnValue('int');

const type = faker.database.type();
const expected = 'int';

expect(
type,
`The column type should be equals ${expected}. Current is ${type}`
).toBe(expected);
for (const { seed, expectations } of seededRuns) {
describe(`seed: ${seed}`, () => {
for (const functionName of functionNames) {
it(`${functionName}()`, () => {
faker.seed(seed);

spy_database_type.mockRestore();
const actual = faker.database[functionName]();
expect(actual).toEqual(expectations[functionName]);
});
}
});
}

// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${faker.seedValue}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('column()', () => {
it('should return a column name from array', () => {
const column = faker.database.column();
expect(column).toBeTruthy();
expect(typeof column).toBe('string');
expect(faker.definitions.database.column).toContain(column);
});
});

describe('collation()', () => {
it('should return a collation from array', () => {
const collation = faker.database.collation();
expect(collation).toBeTruthy();
expect(typeof collation).toBe('string');
expect(faker.definitions.database.collation).toContain(collation);
});
});

describe('engine()', () => {
it('should return an engine from array', () => {
const engine = faker.database.engine();
expect(engine).toBeTruthy();
expect(typeof engine).toBe('string');
expect(faker.definitions.database.engine).toContain(engine);
});
});

describe('type()', () => {
it('should return a column type from array', () => {
const type = faker.database.type();
expect(type).toBeTruthy();
expect(typeof type).toBe('string');
expect(faker.definitions.database.type).toContain(type);
});
});
}
});
});