Skip to content

Commit

Permalink
Move tests to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
cau777 committed Oct 28, 2024
1 parent c617c13 commit 71f4c51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
32 changes: 32 additions & 0 deletions src/colection.distinct.assert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ObjectId } from 'mongodb'
import { TsReadWriteCollection } from './collection'
import * as ta from 'type-assertions'

type DistinctSampleType = {
_id: ObjectId
str: string
nested: {
field: number
}
}

type TestCollection = TsReadWriteCollection<
DistinctSampleType & { writeOnly: boolean },
DistinctSampleType & { readOnly: boolean }
>

const distinctString = (c: TestCollection) => c.distinct('str', {})
const distinctId = (c: TestCollection) => c.distinct('_id', {})
const distinctNested = (c: TestCollection) => c.distinct('nested.field', {})
const distinctReadOnly = (c: TestCollection) => c.distinct('readOnly', {})

ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctString>>, string[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctId>>, ObjectId[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctNested>>, number[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctReadOnly>>, boolean[]>>()

// @ts-expect-error
const _distinctIncorrect = (c: TestCollection) => c.distinct('no', {})

// @ts-expect-error
const _distinctWriteOnly = (c: TestCollection) => c.distinct('writeOnly', {})
35 changes: 1 addition & 34 deletions src/collection.assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import type {
WithId,
} from 'mongodb'
import * as ta from 'type-assertions'
import {
TsCollection,
TsReadCollection,
TsReadWriteCollection,
} from './collection'
import { TsCollection, TsReadCollection } from './collection'
import type { TsFilter, TsFindOneAndDeleteOptions } from './types'

type TSchema = { a: string; _id: ObjectId }
Expand Down Expand Up @@ -87,32 +83,3 @@ ta.assert<
>
>
>()

type DistinctSampleType = {
_id: ObjectId
str: string
nested: {
field: number
}
}

type TestCollection = TsReadWriteCollection<
DistinctSampleType & { writeOnly: boolean },
DistinctSampleType & { readOnly: boolean }
>

const distinctString = (c: TestCollection) => c.distinct('str', {})
const distinctId = (c: TestCollection) => c.distinct('_id', {})
const distinctNested = (c: TestCollection) => c.distinct('nested.field', {})
const distinctReadOnly = (c: TestCollection) => c.distinct('readOnly', {})

ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctString>>, string[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctId>>, ObjectId[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctNested>>, number[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctReadOnly>>, boolean[]>>()

// @ts-expect-error
const _distinctIncorrect = (c: TestCollection) => c.distinct('no', {})

// @ts-expect-error
const _distinctWriteOnly = (c: TestCollection) => c.distinct('writeOnly', {})

0 comments on commit 71f4c51

Please sign in to comment.