fix(db-postgres): query unset relation (#4862)

This commit is contained in:
Jesse Sivonen
2024-01-19 20:35:58 +02:00
committed by GitHub
parent 9f5efef78f
commit 8ce15c8b07
5 changed files with 105 additions and 11 deletions

View File

@@ -43,6 +43,7 @@ export const chainedRelSlug = 'chained'
export const customIdSlug = 'custom-id'
export const customIdNumberSlug = 'custom-id-number'
export const polymorphicRelationshipsSlug = 'polymorphic-relationships'
export const treeSlug = 'tree'
export default buildConfigWithDefaults({
collections: [
@@ -244,6 +245,20 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: treeSlug,
fields: [
{
name: 'text',
type: 'text',
},
{
name: 'parent',
type: 'relationship',
relationTo: 'tree',
},
],
},
],
onInit: async (payload) => {
await payload.create({
@@ -337,5 +352,20 @@ export default buildConfigWithDefaults({
filteredRelation: filteredRelation.id,
},
})
const root = await payload.create({
collection: 'tree',
data: {
text: 'root',
},
})
await payload.create({
collection: 'tree',
data: {
text: 'sub',
parent: root.id,
},
})
},
})

View File

@@ -22,6 +22,7 @@ import config, {
defaultAccessRelSlug,
relationSlug,
slug,
treeSlug,
} from './config'
let apiUrl
@@ -554,6 +555,64 @@ describe('Relationships', () => {
expect(stanleyNeverMadeMovies.movies).toHaveLength(0)
})
})
describe('Hierarchy', () => {
it('finds 1 root item with equals', async () => {
const {
docs: [item],
totalDocs: count,
} = await payload.find({
collection: treeSlug,
where: {
parent: { equals: null },
},
})
expect(count).toBe(1)
expect(item.text).toBe('root')
})
it('finds 1 root item with exists', async () => {
const {
docs: [item],
totalDocs: count,
} = await payload.find({
collection: treeSlug,
where: {
parent: { exists: false },
},
})
expect(count).toBe(1)
expect(item.text).toBe('root')
})
it('finds 1 sub item with equals', async () => {
const {
docs: [item],
totalDocs: count,
} = await payload.find({
collection: treeSlug,
where: {
parent: { not_equals: null },
},
})
expect(count).toBe(1)
expect(item.text).toBe('sub')
})
it('finds 1 sub item with exists', async () => {
const {
docs: [item],
totalDocs: count,
} = await payload.find({
collection: treeSlug,
where: {
parent: { exists: true },
},
})
expect(count).toBe(1)
expect(item.text).toBe('sub')
})
})
})
describe('Creating', () => {