Test Data Generator

Gen

Test Data Generator

  • ํ•ด๋‹น ๋„๋ฉ”์ธ ๊ด€๋ จ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์ž.

  • ์„œ๋น„์Šค ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๋Š” ์ „์šฉ ๋žœ๋ค ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๊ฒƒ์ด ์กฐํƒ€

Generator

import { Content } from '@/domains/content/content.entity';
import { User } from '@/domains/user/user.entity';
import { faker } from '@faker-js/faker';
import { draw } from 'radashi';

const imgPath = ['/globe.svg', '/vercel.svg', '/window.svg'];

export const gen = {
  img: () => draw(imgPath) as string,

  content: {
    title: faker.book.title,
    body: () => faker.word.words({ count: { min: 5, max: 25 } }),

    instance: (partial?: Partial<Content>): Content => ({
      id: faker.string.uuid(),
      createdAt: faker.date.past(),
      title: gen.content.title(),
      body: gen.content.body(),
      thumbnail: gen.img(),
      authorId: faker.string.uuid(),
      ...partial,
    }),
  },

  user: {
    instance: (parital?: Partial<User>): User => ({
      id: faker.string.uuid(),
      nickname: faker.person.firstName(),
      imgUrl: gen.img(),
      ...parital,
    }),
  },
};

CLI

// generate-user
import { gen } from '@__tests__/generator';
import { objectToString } from '@__tests__/libs/object-to-string';
import { list } from 'radashi';

const main = (len: number) => {
  const instance = list(0, len - 1).map(() => gen.user.instance());

  const str = objectToString(instance);
  console.log(str);
};

main(1);
// generate-content
import { userFixtures } from '@__tests__/fixtures/user-fixture';
import { gen } from '@__tests__/generator';
import { objectToString } from '@__tests__/libs/object-to-string';
import { faker } from '@faker-js/faker';
import { list } from 'radashi';

const main = (len: number) => {
  const instance = list(0, len - 1).map((i) => {
    const user = userFixtures[0];
    // if (i === 1)
    //   return gen.content.instance({ authorId: user.id, createdAt: new Date() });
    // if (i === 2)
    //   return gen.content.instance({
    //     authorId: user.id,
    //     title: '00000000' + faker.string.sample(),
    //   });
    // return gen.content.instance({ authorId: user.id });
    return gen.content.instance({ authorId: user.id });
  });

  const str = objectToString(instance);
  console.log(str);
};

main(1);

libs

/* eslint-disable @typescript-eslint/no-explicit-any */
export const objectToString = (obj: any): string => {
  if (obj === null) {
    return 'null';
  }
  if (typeof obj === 'object') {
    if (Array.isArray(obj)) {
      // ๋ฐฐ์—ด ์ฒ˜๋ฆฌ
      return `[${obj.map((item) => objectToString(item)).join(', ')}]`;
    } else if (obj instanceof Date) {
      return `new Date("${obj.toISOString()}")`;
    } else {
      // ๊ฐ์ฒด ์ฒ˜๋ฆฌ
      const entries = Object.entries(obj)
        .map(([key, value]) => `${key}: ${objectToString(value)}`)
        .join(', ');
      return `{${entries}}`;
    }
  }
  if (typeof obj === 'string') {
    return `"${obj}"`; // ๋ฌธ์ž์—ด์€ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ๊ธฐ
  }
  return String(obj); // ์ˆซ์ž, ๋ถˆ๋ฆฌ์–ธ, undefined ๋“ฑ
};

Last updated