Skip to content
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

fix: DataType.prototype.getByVersionId could return wrong type #963

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/datatype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class DataType extends TypedEmitter {
*/
async getByVersionId(versionId, { lang } = {}) {
const result = await this.#dataStore.read(versionId)
if (result.schemaName !== this.#schemaName) throw new NotFoundError()
return this.#translate(deNullify(result), { lang })
}

Expand Down
48 changes: 44 additions & 4 deletions test/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from './helpers/core-manager.js'
import RAM from 'random-access-memory'
import crypto from 'hypercore-crypto'
import { observationTable, translationTable } from '../src/schema/project.js'
import {
observationTable,
trackTable,
translationTable,
} from '../src/schema/project.js'
import { DataType, kCreateWithDocId } from '../src/datatype/index.js'
import { IndexWriter } from '../src/index-writer/index.js'
import { NotFoundError } from '../src/errors.js'
Expand All @@ -19,7 +23,13 @@ import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import { randomBytes } from 'crypto'
import TranslationApi from '../src/translation-api.js'
import { getProperty } from 'dot-prop'
import { decode, decodeBlockPrefix, parseVersionId } from '@comapeo/schema'
import {
decode,
decodeBlockPrefix,
parseVersionId,
valueOf,
} from '@comapeo/schema'
import { generate } from '@mapeo/mock-data'

/** @type {import('@comapeo/schema').ObservationValue} */
const obsFixture = {
Expand All @@ -41,6 +51,9 @@ const newObsFixture = {
metadata: { manualLocation: false },
}

/** @type {import('@comapeo/schema').TrackValue} */
const trackFixture = valueOf(generate('track')[0])

test('private createWithDocId() method', async () => {
const sqlite = new Database(':memory:')
const db = drizzle(sqlite)
Expand Down Expand Up @@ -123,6 +136,33 @@ test('getByVersionId fetches docs by their version ID', async () => {
assert.equal(created.docId, fetched.docId)
})

test('getByVersionId rejects if fetching a version ID in the same store, but with a different type', async () => {
const {
dataType: observationDataType,
dataStore,
db,
translationApi,
} = await testenv()
const trackDataType = new DataType({
dataStore,
table: trackTable,
db,
getTranslations: translationApi.get.bind(translationApi),
})

const observation = await observationDataType.create(obsFixture)
const track = await trackDataType.create(trackFixture)

await assert.rejects(
() => observationDataType.getByVersionId(track.versionId),
NotFoundError
)
await assert.rejects(
() => trackDataType.getByVersionId(observation.versionId),
NotFoundError
)
})

test('`originalVersionId` field', async () => {
const { dataType, dataStore } = await testenv()

Expand Down Expand Up @@ -318,7 +358,7 @@ async function testenv(opts = {}) {
const coreManager = createCoreManager({ ...opts, db })

const indexWriter = new IndexWriter({
tables: [observationTable, translationTable],
tables: [observationTable, trackTable, translationTable],
sqlite,
})

Expand Down Expand Up @@ -380,5 +420,5 @@ async function testenv(opts = {}) {
getTranslations: translationApi.get.bind(translationApi),
})

return { coreManager, dataType, dataStore, translationApi }
return { coreManager, dataType, dataStore, db, translationApi }
}
Loading