-
Notifications
You must be signed in to change notification settings - Fork 6
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
RFC: pubOp for creating and updating pubs more easily #965
Changes from 24 commits
934f7cc
1969dfa
7b961d0
0feeca1
18b38c6
db62dda
f9567d0
7deacf6
7ead642
eab252d
631bc83
39dd06d
d6d638e
0c90e5d
2258024
1390713
e7aa8ac
6c42929
8cdb530
da0b8eb
b52d745
27dee15
64ebece
6af01c3
db1eef2
72fc7d2
19842b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect } from "vitest"; | ||
|
||
import type { ProcessedPub } from "contracts"; | ||
import type { PubsId } from "db/public"; | ||
|
||
import type { db } from "~/kysely/database"; | ||
|
||
const deepSortValues = (pub: ProcessedPub): ProcessedPub => { | ||
pub.values | ||
.sort((a, b) => (a.value as string).localeCompare(b.value as string)) | ||
.map((item) => ({ | ||
...item, | ||
relatedPub: item.relatedPub?.values ? deepSortValues(item.relatedPub) : item.relatedPub, | ||
})); | ||
|
||
return pub; | ||
}; | ||
|
||
expect.extend({ | ||
async toExist(received: PubsId | ProcessedPub, expected?: typeof db) { | ||
if (typeof received !== "string") { | ||
throw new Error("toExist() can only be called with a PubsId"); | ||
} | ||
const { getPlainPub } = await import("../server/pub"); | ||
|
||
const pub = await getPlainPub(received, expected).executeTakeFirst(); | ||
const pass = Boolean(pub && pub.id === received); | ||
const { isNot } = this; | ||
|
||
return { | ||
pass, | ||
message: () => | ||
isNot | ||
? `Expected pub with ID ${received} not to exist, but it ${pass ? "does" : "does not"}` | ||
: `Expected pub with ID ${received} to exist, but it ${pass ? "does" : "does not"}`, | ||
}; | ||
}, | ||
|
||
toHaveValues( | ||
received: PubsId | ProcessedPub, | ||
expected: Partial<ProcessedPub["values"][number]>[] | ||
) { | ||
if (typeof received === "string") { | ||
throw new Error("toHaveValues() can only be called with a ProcessedPub"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how come There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! it was left over bc i think i wanted to be able to pass in both in either case, and was having some trouble with the types. i later decided to just use |
||
} | ||
|
||
const pub = received; | ||
const sortedPubValues = deepSortValues(pub); | ||
|
||
const expectedLength = expected.length; | ||
const receivedLength = sortedPubValues.values.length; | ||
|
||
const isNot = this.isNot; | ||
if (!isNot && !this.equals(expectedLength, receivedLength)) { | ||
return { | ||
pass: false, | ||
message: () => | ||
`Expected pub to have ${expectedLength} values, but it has ${receivedLength}`, | ||
}; | ||
} | ||
|
||
// equiv. to .toMatchObject | ||
const pass = this.equals(sortedPubValues.values, expected, [ | ||
this.utils.iterableEquality, | ||
this.utils.subsetEquality, | ||
]); | ||
|
||
return { | ||
pass, | ||
message: () => | ||
pass | ||
? `Expected pub ${isNot ? "not" : ""} to have values ${JSON.stringify(expected)}, and it does ${isNot ? "not" : ""}` | ||
: `Expected pub ${isNot ? "not to" : "to"} match values ${this.utils.diff(sortedPubValues.values, expected)}`, | ||
}; | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ import { describe, expect, it } from "vitest"; | |
|
||
import { CoreSchemaType, MemberRole } from "db/public"; | ||
|
||
import type { Seed } from "~/prisma/seed/seedCommunity"; | ||
import { createSeed } from "~/prisma/seed/createSeed"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iirc importing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but i think that was bc they were both imported from |
||
import { mockServerCode } from "../__tests__/utils"; | ||
|
||
await mockServerCode(); | ||
|
||
const seed = { | ||
const seed = createSeed({ | ||
community: { | ||
name: "test-pub-capabilities", | ||
slug: "test-pub-capabilities", | ||
|
@@ -97,7 +97,7 @@ const seed = { | |
}, | ||
}, | ||
], | ||
} as Seed; | ||
}); | ||
|
||
describe("getPubsWithRelatedValuesAndChildren capabilities", () => { | ||
it("should restrict pubs by visibility", async () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are helper methods to make matching pubvalues easier.
eg instead of doing
you just do
Similarly for
just easier than manually looking up the pub yourself