# Workflow Test Factory

> Test Factory authoring guide — Factory pattern for inserting rows into the database. Used in Drizzle ORM + Vitest environments. Triggers: Factory creation, test data, test helper, EntityFactory, create, build, test fixture.

- Skill: `marzun9620/workflow-test-factory` (Agent Skill)
- Install (CLI): `npx skillmds@latest add marzun9620/workflow-test-factory`
- Raw SKILL.md: https://api.skillmd.com/api/skills/marzun9620/workflow-test-factory/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Productivity
- Author: marzun9620 (https://skillmd.com/u/marzun9620)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/marzun9620/workflow-test-factory

---


# テスト Factory 実装手順

ADR-0005 準拠。Integration test で使用するテストデータ生成用の Factory。

## ファイル配置

ファイル: `apps/datahub/tests/factories/{entity}Factory.ts`

## 既存の Factory を必ず読む

実装前に既存の Factory ファイルを読んでパターンを踏襲すること:

```bash
ls apps/datahub/tests/factories/
```

## Factory パターン

```typescript
import { randomUUID } from "node:crypto";
import type { DrizzleDb } from "~/infrastructure/db/index.js";
import { entitiesInHw } from "~/infrastructure/db/index.js";

type EntityInsert = typeof entitiesInHw.$inferInsert;

const defaults: EntityInsert = {
  publicId: randomUUID(),
  name: "Test Entity",
  status: "ACTIVE",
};

export const EntityFactory = {
  /**
   * DB に insert して返す（integration test 用）
   */
  create: async (db: DrizzleDb, overrides?: Partial<EntityInsert>) => {
    const data = { ...defaults, publicId: randomUUID(), ...overrides };
    const [row] = await db.insert(entitiesInHw).values(data).returning();
    return row;
  },

  /**
   * DB に insert せず、テストデータのみ生成（unit test 用）
   */
  build: (overrides?: Partial<EntityInsert>): EntityInsert => ({
    ...defaults,
    publicId: randomUUID(),
    ...overrides,
  }),
};
```

## 重要ルール

- `create` は毎回 `randomUUID()` で一意な `publicId` を生成（テスト間の干渉を防ぐ）
- `defaults` にデフォルト値を定義（テストケースは必要なフィールドだけ override）
- `create` は `returning()` で insert した行を返す
- `build` は DB に触らず、テストデータのみ返す（unit test 向け）
- import は barrel 経由: `~/infrastructure/db/index.js`

## テストでの使い方

```typescript
import { EntityFactory } from "../../factories/entityFactory.js";
import { getDb } from "../../helpers/db.js";

// Arrange
const db = getDb();
const entity = await EntityFactory.create(db);
const entityId = EntityId.make(entity.publicId);

// override 付き
const entity = await EntityFactory.create(db, { name: "Custom Name", status: "ARCHIVED" });

// unit test 用（DB なし）
const data = EntityFactory.build({ name: "Test" });
```

## チェックリスト

- [ ] `create` が毎回一意な publicId を生成
- [ ] `defaults` にデフォルト値を定義
- [ ] `create` が `returning()` で行を返す
- [ ] `build` が DB に触らない
- [ ] 既存の Factory パターンを踏襲
- [ ] import が barrel 経由

