1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("primary-key-required", rule, {
7 valid: [
8 // id primary key.
9 {
10 code: `
11 import { model } from "@medusajs/framework/utils"
12 export const Post = model.define("post", {
13 id: model.id().primaryKey(),
14 title: model.text(),
15 })
16 `,
17 },
18 // text primary key.
19 {
20 code: `
21 import { model } from "@medusajs/framework/utils"
22 export const Post = model.define("post", {
23 title: model.text().primaryKey(),
24 })
25 `,
26 },
27 // number primary key.
28 {
29 code: `
30 import { model } from "@medusajs/framework/utils"
31 export const Post = model.define("post", {
32 views: model.number().primaryKey(),
33 })
34 `,
35 },
36 // primaryKey not at the end of the chain.
37 {
38 code: `
39 import { model } from "@medusajs/framework/utils"
40 export const Post = model.define("post", {
41 id: model.id().primaryKey().nullable(),
42 title: model.text(),
43 })
44 `,
45 },
46 // Aliased model import binding.
47 {
48 code: `
49 import { model as m } from "@medusajs/framework/utils"
50 export const Post = m.define("post", {
51 id: m.id().primaryKey(),
52 })
53 `,
54 },
55 // \`model\` from another package — not flagged.
56 {
57 code: `
58 import { model } from "some-other-lib"
59 export const Post = model.define("post", {
60 title: model.text(),
61 })
62 `,
63 },
64 // Non-define method — not flagged.
65 {
66 code: `
67 import { model } from "@medusajs/framework/utils"
68 const col = model.text()
69 `,
70 },
71 // Non-object second argument — skipped (can't analyze).
72 {
73 code: `
74 import { model } from "@medusajs/framework/utils"
75 const schema = { id: model.id().primaryKey() }
76 export const Post = model.define("post", schema)
77 `,
78 },
79 // Spread in schema — primary key could come from the spread, so skipped.
80 {
81 code: `
82 import { model } from "@medusajs/framework/utils"
83 const base = { id: model.id().primaryKey() }
84 export const Post = model.define("post", {
85 ...base,
86 title: model.text(),
87 })
88 `,
89 },
90 ],
91 invalid: [
92 // No primary key on any property.
93 {
94 code: `
95 import { model } from "@medusajs/framework/utils"
96 export const Post = model.define("post", {
97 title: model.text(),
98 views: model.number(),
99 })
100 `,
101 errors: [{ messageId: "missingPrimaryKey", data: { name: '"post"' } }],
102 },
103 // Empty schema — no primary key.
104 {
105 code: `
106 import { model } from "@medusajs/framework/utils"
107 export const Post = model.define("post", {})
108 `,
109 errors: [{ messageId: "missingPrimaryKey", data: { name: '"post"' } }],
110 },
111 // \`id\` property without \`.primaryKey()\`.
112 {
113 code: `
114 import { model } from "@medusajs/framework/utils"
115 export const Post = model.define("post", {
116 id: model.id(),
117 title: model.text(),
118 })
119 `,
120 errors: [{ messageId: "missingPrimaryKey", data: { name: '"post"' } }],
121 },
122 // Aliased model import without a primary key.
123 {
124 code: `
125 import { model as m } from "@medusajs/framework/utils"
126 export const Post = m.define("post", {
127 title: m.text(),
128 })
129 `,
130 errors: [{ messageId: "missingPrimaryKey", data: { name: '"post"' } }],
131 },
132 ],
133 })