test: conditional geojson
This commit is contained in:
@@ -1,20 +1,13 @@
|
||||
import { randomBytes } from 'crypto'
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
import type { Relation } from './config'
|
||||
import type { ErrorOnHook, Post } from './payload-types'
|
||||
import type { Relation } from './config';
|
||||
import type { ErrorOnHook, Post } from './payload-types';
|
||||
|
||||
import payload from '../../packages/payload/src'
|
||||
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
|
||||
import { initPayloadTest } from '../helpers/configHelpers'
|
||||
import { RESTClient } from '../helpers/rest'
|
||||
import config, {
|
||||
customIdNumberSlug,
|
||||
customIdSlug,
|
||||
errorOnHookSlug,
|
||||
pointSlug,
|
||||
relationSlug,
|
||||
slug,
|
||||
} from './config'
|
||||
import payload from '../../packages/payload/src';
|
||||
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync';
|
||||
import { initPayloadTest } from '../helpers/configHelpers';
|
||||
import { RESTClient } from '../helpers/rest';
|
||||
import config, { customIdNumberSlug, customIdSlug, errorOnHookSlug, pointSlug, relationSlug, slug, } from './config';
|
||||
|
||||
let client: RESTClient
|
||||
|
||||
@@ -25,12 +18,14 @@ describe('collections-rest', () => {
|
||||
|
||||
// Wait for indexes to be created,
|
||||
// as we need them to query by point
|
||||
await new Promise((resolve, reject) => {
|
||||
payload.db.collections[pointSlug].ensureIndexes(function (err) {
|
||||
if (err) reject(err)
|
||||
resolve(true)
|
||||
if (payload.db.name === 'mongoose') {
|
||||
await new Promise((resolve, reject) => {
|
||||
payload.db.collections[pointSlug].ensureIndexes(function (err) {
|
||||
if (err) reject(err)
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -116,11 +111,12 @@ describe('collections-rest', () => {
|
||||
})
|
||||
|
||||
const description = 'updated'
|
||||
const { status, docs } = await client.updateMany<Post>({
|
||||
const { status, docs, errors } = await client.updateMany({
|
||||
where: { title: { equals: 'title' } },
|
||||
data: { description },
|
||||
})
|
||||
|
||||
expect(errors).toHaveLength(0);
|
||||
expect(status).toEqual(200)
|
||||
expect(docs[0].title).toEqual('title') // Check was not modified
|
||||
expect(docs[0].description).toEqual(description)
|
||||
@@ -870,162 +866,164 @@ describe('collections-rest', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('near', () => {
|
||||
const point = [10, 20]
|
||||
const [lat, lng] = point
|
||||
it('should return a document near a point', async () => {
|
||||
const near = `${lat + 0.01}, ${lng + 0.01}, 10000`
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
near,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should not return a point far away', async () => {
|
||||
const near = `${lng + 1}, ${lat - 1}, 5000`
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
near,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should sort find results by nearest distance', async () => {
|
||||
// creating twice as many records as we are querying to get a random sample
|
||||
await mapAsync([...Array(10)], async () => {
|
||||
// setTimeout used to randomize the creation timestamp
|
||||
setTimeout(async () => {
|
||||
await payload.create({
|
||||
collection: pointSlug,
|
||||
data: {
|
||||
// only randomize longitude to make distance comparison easy
|
||||
point: [Math.random(), 0],
|
||||
},
|
||||
})
|
||||
}, Math.random())
|
||||
})
|
||||
|
||||
const { result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
// querying large enough range to include all docs
|
||||
point: { near: '0, 0, 100000, 0' },
|
||||
},
|
||||
limit: 5,
|
||||
})
|
||||
const { docs } = result
|
||||
let previous = 0
|
||||
docs.forEach(({ point: coordinates }) => {
|
||||
// the next document point should always be greater than the one before
|
||||
expect(previous).toBeLessThanOrEqual(coordinates[0])
|
||||
;[previous] = coordinates
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('within', () => {
|
||||
type Point = [number, number]
|
||||
const polygon: Point[] = [
|
||||
[9.0, 19.0], // bottom-left
|
||||
[9.0, 21.0], // top-left
|
||||
[11.0, 21.0], // top-right
|
||||
[11.0, 19.0], // bottom-right
|
||||
[9.0, 19.0], // back to starting point to close the polygon
|
||||
]
|
||||
it('should return a document with the point inside the polygon', async () => {
|
||||
// There should be 1 total points document populated by default with the point [10, 20]
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
within: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon],
|
||||
if (['mongoose'].includes(process.env.PAYLOAD_DATABASE)) {
|
||||
describe('near', () => {
|
||||
const point = [10, 20]
|
||||
const [lat, lng] = point
|
||||
it('should return a document near a point', async () => {
|
||||
const near = `${lat + 0.01}, ${lng + 0.01}, 10000`
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
near,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should not return a document with the point outside a smaller polygon', async () => {
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
within: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon.map((vertex) => vertex.map((coord) => coord * 0.1))], // Reduce polygon to 10% of its size
|
||||
it('should not return a point far away', async () => {
|
||||
const near = `${lng + 1}, ${lat - 1}, 5000`
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
near,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
it('should sort find results by nearest distance', async () => {
|
||||
// creating twice as many records as we are querying to get a random sample
|
||||
await mapAsync([...Array(10)], async () => {
|
||||
// setTimeout used to randomize the creation timestamp
|
||||
setTimeout(async () => {
|
||||
await payload.create({
|
||||
collection: pointSlug,
|
||||
data: {
|
||||
// only randomize longitude to make distance comparison easy
|
||||
point: [Math.random(), 0],
|
||||
},
|
||||
})
|
||||
}, Math.random())
|
||||
})
|
||||
|
||||
const { result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
// querying large enough range to include all docs
|
||||
point: { near: '0, 0, 100000, 0' },
|
||||
},
|
||||
limit: 5,
|
||||
})
|
||||
const { docs } = result
|
||||
let previous = 0
|
||||
docs.forEach(({ point: coordinates }) => {
|
||||
// the next document point should always be greater than the one before
|
||||
expect(previous).toBeLessThanOrEqual(coordinates[0])
|
||||
;[previous] = coordinates
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('intersects', () => {
|
||||
type Point = [number, number]
|
||||
const polygon: Point[] = [
|
||||
[9.0, 19.0], // bottom-left
|
||||
[9.0, 21.0], // top-left
|
||||
[11.0, 21.0], // top-right
|
||||
[11.0, 19.0], // bottom-right
|
||||
[9.0, 19.0], // back to starting point to close the polygon
|
||||
]
|
||||
|
||||
it('should return a document with the point intersecting the polygon', async () => {
|
||||
// There should be 1 total points document populated by default with the point [10, 20]
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
intersects: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon],
|
||||
describe('within', () => {
|
||||
type Point = [number, number]
|
||||
const polygon: Point[] = [
|
||||
[9.0, 19.0], // bottom-left
|
||||
[9.0, 21.0], // top-left
|
||||
[11.0, 21.0], // top-right
|
||||
[11.0, 19.0], // bottom-right
|
||||
[9.0, 19.0], // back to starting point to close the polygon
|
||||
]
|
||||
it('should return a document with the point inside the polygon', async () => {
|
||||
// There should be 1 total points document populated by default with the point [10, 20]
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
within: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should not return a document with the point not intersecting a smaller polygon', async () => {
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
intersects: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon.map((vertex) => vertex.map((coord) => coord * 0.1))], // Reduce polygon to 10% of its size
|
||||
it('should not return a document with the point outside a smaller polygon', async () => {
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
within: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon.map((vertex) => vertex.map((coord) => coord * 0.1))], // Reduce polygon to 10% of its size
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('intersects', () => {
|
||||
type Point = [number, number]
|
||||
const polygon: Point[] = [
|
||||
[9.0, 19.0], // bottom-left
|
||||
[9.0, 21.0], // top-left
|
||||
[11.0, 21.0], // top-right
|
||||
[11.0, 19.0], // bottom-right
|
||||
[9.0, 19.0], // back to starting point to close the polygon
|
||||
]
|
||||
|
||||
it('should return a document with the point intersecting the polygon', async () => {
|
||||
// There should be 1 total points document populated by default with the point [10, 20]
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
intersects: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(1)
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
it('should not return a document with the point not intersecting a smaller polygon', async () => {
|
||||
const { status, result } = await client.find({
|
||||
slug: pointSlug,
|
||||
query: {
|
||||
point: {
|
||||
intersects: {
|
||||
type: 'Polygon',
|
||||
coordinates: [polygon.map((vertex) => vertex.map((coord) => coord * 0.1))], // Reduce polygon to 10% of its size
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(status).toEqual(200)
|
||||
expect(result.docs).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
it('or', async () => {
|
||||
const post1 = await createPost({ title: 'post1' })
|
||||
|
||||
Reference in New Issue
Block a user