Debian Codegraph @

__tests__/internal/integration/schema/schema-verification/mixed-test.js

raw
1 import "@lib/container";
2 import Db from "@lib/db";
3 import Schema from "@lib/orm/schema";
4 import { Model, hasMany, belongsTo } from "miragejs";
5
6 describe("Integration | ORM | Schema Verification | Mixed", function () {
7 test("unnamed one-to-many associations are correct", () => {
8 let schema = new Schema(
9 new Db({
10 wordSmiths: [{ id: 1, name: "Frodo" }],
11 blogPosts: [{ id: 1, title: "Lorem" }],
12 }),
13 {
14 wordSmith: Model.extend({
15 blogPosts: hasMany(),
16 }),
17 blogPost: Model.extend({
18 wordSmith: belongsTo(),
19 }),
20 }
21 );
22
23 let frodo = schema.wordSmiths.find(1);
24 let association = frodo.associationFor("blogPosts");
25
26 expect(association.name).toEqual("blogPosts");
27 expect(association.modelName).toEqual("blog-post");
28 expect(association.ownerModelName).toEqual("word-smith");
29
30 let post = schema.blogPosts.find(1);
31
32 expect(post.inverseFor(association)).toEqual(
33 post.associationFor("wordSmith")
34 );
35 });
36
37 test("a named one-to-many association is correct", () => {
38 let schema = new Schema(
39 new Db({
40 wordSmiths: [{ id: 1, name: "Frodo" }],
41 blogPosts: [{ id: 1, title: "Lorem" }],
42 }),
43 {
44 wordSmith: Model.extend({
45 posts: hasMany("blog-post"),
46 }),
47 blogPost: Model.extend({
48 author: belongsTo("word-smith"),
49 }),
50 }
51 );
52
53 let frodo = schema.wordSmiths.find(1);
54 let association = frodo.associationFor("posts");
55
56 expect(association.name).toEqual("posts");
57 expect(association.modelName).toEqual("blog-post");
58 expect(association.ownerModelName).toEqual("word-smith");
59
60 let post = schema.blogPosts.find(1);
61
62 expect(post.inverseFor(association)).toEqual(post.associationFor("author"));
63 });
64
65 test("multiple has-many associations of the same type", () => {
66 let schema = new Schema(
67 new Db({
68 users: [{ id: 1, name: "Frodo" }],
69 posts: [{ id: 1, title: "Lorem" }],
70 }),
71 {
72 user: Model.extend({
73 notes: hasMany("post", { inverse: "author" }),
74 messages: hasMany("post", { inverse: "messenger" }),
75 }),
76 post: Model.extend({
77 author: belongsTo("user", { inverse: "notes" }),
78 messenger: belongsTo("user", { inverse: "messages" }),
79 }),
80 }
81 );
82
83 let frodo = schema.users.find(1);
84 let notesAssociation = frodo.associationFor("notes");
85
86 expect(notesAssociation.name).toEqual("notes");
87 expect(notesAssociation.modelName).toEqual("post");
88 expect(notesAssociation.ownerModelName).toEqual("user");
89
90 let post = schema.posts.find(1);
91
92 expect(post.inverseFor(notesAssociation)).toEqual(
93 post.associationFor("author")
94 );
95
96 let messagesAssociation = frodo.associationFor("messages");
97
98 expect(messagesAssociation.name).toEqual("messages");
99 expect(messagesAssociation.modelName).toEqual("post");
100 expect(messagesAssociation.ownerModelName).toEqual("user");
101
102 expect(post.inverseFor(messagesAssociation)).toEqual(
103 post.associationFor("messenger")
104 );
105 });
106
107 test("one-to-many reflexive association is correct", () => {
108 let schema = new Schema(
109 new Db({
110 users: [{ id: 1, name: "Frodo" }],
111 }),
112 {
113 user: Model.extend({
114 parent: belongsTo("user", { inverse: "children" }),
115 children: hasMany("user", { inverse: "parent" }),
116 }),
117 }
118 );
119
120 let frodo = schema.users.find(1);
121 let parentAssociation = frodo.associationFor("parent");
122
123 expect(parentAssociation.name).toEqual("parent");
124 expect(parentAssociation.modelName).toEqual("user");
125 expect(parentAssociation.ownerModelName).toEqual("user");
126
127 expect(frodo.inverseFor(parentAssociation)).toEqual(
128 frodo.associationFor("children")
129 );
130 });
131
132 test("one-to-many polymorphic association is correct", () => {
133 let schema = new Schema(
134 new Db({
135 authors: [{ id: 1, name: "Peter" }],
136 posts: [{ id: 1, title: "Lorem" }],
137 articles: [{ id: 1, title: "Ipsum" }],
138 }),
139 {
140 author: Model.extend({
141 writings: hasMany({ polymorphic: true }),
142 }),
143 post: Model.extend({
144 author: belongsTo("author", { inverse: "writings" }),
145 }),
146 article: Model.extend({
147 author: belongsTo("author", { inverse: "writings" }),
148 }),
149 }
150 );
151
152 let author = schema.authors.find(1);
153 let writingsAssociation = author.associationFor("writings");
154
155 let post = schema.posts.find(1);
156 let postAuthorAssociation = post.associationFor("author");
157
158 let article = schema.articles.find(1);
159 let articleAuthorAssociation = article.associationFor("author");
160
161 expect(post.inverseFor(writingsAssociation)).toEqual(postAuthorAssociation);
162 expect(article.inverseFor(writingsAssociation)).toEqual(
163 articleAuthorAssociation
164 );
165 expect(author.inverseFor(postAuthorAssociation)).toEqual(
166 writingsAssociation
167 );
168 expect(author.inverseFor(postAuthorAssociation)).toEqual(
169 writingsAssociation
170 );
171 });
172
173 test("multiple implicit inverse associations with the same key throws an error", () => {
174 let schema = new Schema(
175 new Db({
176 users: [{ id: 1, name: "Frodo" }],
177 posts: [{ id: 1, title: "Lorem" }],
178 }),
179 {
180 user: Model.extend({
181 posts: hasMany("post"),
182 }),
183 post: Model.extend({
184 editor: belongsTo("user"),
185 authors: hasMany("user"),
186 }),
187 }
188 );
189
190 let frodo = schema.users.find(1);
191 let userPostsAssociation = frodo.associationFor("posts");
192 let post = schema.posts.find(1);
193
194 expect(function () {
195 post.inverseFor(userPostsAssociation);
196 }).toThrow();
197 });
198
199 test("multiple explicit inverse associations with the same key throws an error", () => {
200 let schema = new Schema(
201 new Db({
202 users: [{ id: 1, name: "Frodo" }],
203 posts: [{ id: 1, title: "Lorem" }],
204 }),
205 {
206 user: Model.extend({
207 posts: hasMany("post", { inverse: "authors" }),
208 }),
209 post: Model.extend({
210 editor: belongsTo("user", { inverse: "posts" }),
211 authors: hasMany("user", { inverse: "posts" }),
212 }),
213 }
214 );
215
216 let frodo = schema.users.find(1);
217 let userPostsAssociation = frodo.associationFor("posts");
218 let post = schema.posts.find(1);
219
220 expect(function () {
221 post.inverseFor(userPostsAssociation);
222 }).toThrow();
223 });
224
225 test("explicit inverse is chosen over implicit inverses", () => {
226 let schema = new Schema(
227 new Db({
228 users: [{ id: 1, name: "Frodo" }],
229 posts: [{ id: 1, title: "Lorem" }],
230 }),
231 {
232 user: Model.extend({
233 posts: hasMany("post", { inverse: "authors" }),
234 }),
235 post: Model.extend({
236 editor: belongsTo("user"),
237 authors: hasMany("user", { inverse: "posts" }),
238 }),
239 }
240 );
241
242 let frodo = schema.users.find(1);
243 let userPostsAssociation = frodo.associationFor("posts");
244
245 expect(userPostsAssociation.name).toEqual("posts");
246 expect(userPostsAssociation.modelName).toEqual("post");
247 expect(userPostsAssociation.ownerModelName).toEqual("user");
248
249 let post = schema.posts.find(1);
250
251 expect(post.inverseFor(userPostsAssociation)).toEqual(
252 post.associationFor("authors")
253 );
254 });
255
256 test("multiple explicit inverse associations with the same key but different models does not throw an error", () => {
257 let schema = new Schema(
258 new Db({
259 users: [{ id: 1, name: "Frodo" }],
260 posts: [{ id: 1, title: "Lorem" }],
261 books: [{ id: 1, title: "Ipsum" }],
262 }),
263 {
264 user: Model.extend({
265 authoredPosts: hasMany("post", { inverse: "authors" }),
266 authoredBooks: hasMany("book", { inverse: "authors" }),
267 }),
268 post: Model.extend({
269 authors: hasMany("user", { inverse: "authoredPosts" }),
270 }),
271 book: Model.extend({
272 authors: hasMany("user", { inverse: "authoredBooks" }),
273 }),
274 }
275 );
276
277 let frodo = schema.users.find(1);
278 let post = schema.posts.find(1);
279 let book = schema.books.find(1);
280
281 let userAuthoredPostsAssociation = frodo.associationFor("authoredPosts");
282 let userAuthoredBooksAssociation = frodo.associationFor("authoredBooks");
283 let postsAuthorsAssociation = post.associationFor("authors");
284 let bookAuthorsAssociation = book.associationFor("authors");
285 expect(post.inverseFor(userAuthoredPostsAssociation)).toEqual(
286 post.associationFor("authors")
287 );
288 expect(book.inverseFor(userAuthoredBooksAssociation)).toEqual(
289 book.associationFor("authors")
290 );
291 expect(frodo.inverseFor(postsAuthorsAssociation)).toEqual(
292 frodo.associationFor("authoredPosts")
293 );
294 expect(frodo.inverseFor(bookAuthorsAssociation)).toEqual(
295 frodo.associationFor("authoredBooks")
296 );
297 });
298 });