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

feat: add e2e tests for config-import. #700

Merged
merged 21 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
42 changes: 40 additions & 2 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ export class MapeoProject extends TypedEmitter {
return this.#dataTypes.field
}

get translation() {
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
return this.#dataTypes.translation
}

get $member() {
return this.#memberApi
}
Expand Down Expand Up @@ -720,6 +724,12 @@ export class MapeoProject extends TypedEmitter {
// check for already present fields and presets and delete them if exist
await deleteAll(this.preset)
await deleteAll(this.field)
// delete only translations that refer to deleted fields and presets
await deleteTranslations({
translation: this.translation,
presets: this.preset,
fields: this.field,
})

const config = await readConfig(configPath)
/** @type {Map<string, string>} */
Expand Down Expand Up @@ -830,8 +840,7 @@ function extractEditableProjectSettings(projectDoc) {
return result
}

// TODO: maybe a better signature than a bunch of any?
/** @param {DataType<any,any,any,any,any>} dataType */
/** @param {MapeoProject['field'] | MapeoProject['preset']} dataType */
async function deleteAll(dataType) {
const deletions = []
for (const { docId } of await dataType.getMany()) {
Expand All @@ -840,6 +849,35 @@ async function deleteAll(dataType) {
return Promise.all(deletions)
}

/**
@param {Object} dataTypes
@param {MapeoProject['translation']} dataTypes.translation
@param {MapeoProject['preset']} dataTypes.presets
@param {MapeoProject['field']} dataTypes.fields
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
*/
async function deleteTranslations(dataTypes) {
const deletions = []
for (const {
docId,
docIdRef,
schemaNameRef,
} of await dataTypes.translation.getMany()) {
try {
if (schemaNameRef === 'presets' || schemaNameRef === 'fields') {
const toDelete = await dataTypes[schemaNameRef].getByDocId(docIdRef)
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
if (toDelete.deleted)
deletions.push(dataTypes.translation.delete(docId))
}
} catch (e) {
console.log(
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
`referred ${schemaNameRef} is not found, deleting translation`
)
deletions.push(dataTypes.translation.delete(docId))
}
}
await Promise.all(deletions)
}

/**
* Return a map of namespace -> core keypair
*
Expand Down
80 changes: 80 additions & 0 deletions test-e2e/config-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { createManager } from './utils.js'
import { defaultConfigPath } from '../tests/helpers/default-config.js'

test('config import - load default config when passed a path to `createProject`', async (t) => {
const manager = createManager('device0', t)
const project = await manager.getProject(
await manager.createProject({ configPath: defaultConfigPath })
)
const presets = await project.preset.getMany()
const fields = await project.field.getMany()
const translations = await project.translation.getMany()
assert.equal(presets.length, 28, 'correct number of loaded presets')
assert.equal(fields.length, 11, 'correct number of loaded fields')
assert.equal(
translations.length,
870,
'correct number of loaded translations'
)
})

test('config import - load and re-load config manually', async (t) => {
const manager = createManager('device0', t)
const project = await manager.getProject(await manager.createProject())

const warnings = await project.importConfig({ configPath: defaultConfigPath })
let presets = await project.preset.getMany()
let fields = await project.field.getMany()
let translations = await project.translation.getMany()

assert.equal(
warnings.length,
0,
'no warnings when manually loading default config'
)
assert.equal(presets.length, 28, 'correct number of manually loaded presets')
assert.equal(fields.length, 11, 'correct number of manually loaded fields')
assert.equal(
translations.length,
870,
'correct number of manually loaded translations'
)

// re load the config
await project.importConfig({ configPath: defaultConfigPath })
presets = await project.preset.getMany()
fields = await project.field.getMany()
translations = await project.translation.getMany()
assert.equal(
presets.length,
28,
're-loading the same config leads to the same number of presets (since they are deleted)'
)
assert.equal(
fields.length,
11,
're-loading the same config leads to the same number of fields (since they are deleted)'
)
assert.equal(
translations.length,
870,
're-loading the same config leads to the same number of translations (since they are deleted)'
)
})

test('manually load config in parallel', async (t) => {
const manager = createManager('device0', t)
const project = await manager.getProject(await manager.createProject())
await Promise.all([
project.importConfig({ configPath: defaultConfigPath }),
project.importConfig({ configPath: defaultConfigPath }),
])
const presets = await project.preset.getMany()
const fields = await project.field.getMany()
const translations = await project.translation.getMany()
console.log('presets', presets.length)
console.log('fields', fields.length)
console.log('translations', translations.length)
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
})
Loading