Skip to content

Commit

Permalink
test: use mock data in more places
Browse files Browse the repository at this point in the history
Our tests have several spots where we create mock data, such as
observations or presets. We typically rely on `@mapeo/mock-data` for
this, but could use it in a few more places. That's what this commit
does!

I think this is useful for three reasons:

1. It's less code.
2. It doesn't need to be manually updated when we make schema updates.
3. It will make [an upcoming change][0] smaller.

[0]: #715
  • Loading branch information
EvanHahn committed Aug 26, 2024
1 parent 4b02546 commit 680dd79
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 50 deletions.
24 changes: 6 additions & 18 deletions test-e2e/core-ownership.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { KeyManager } from '@mapeo/crypto'
import { parseVersionId } from '@mapeo/schema'
import { generate } from '@mapeo/mock-data'
import { parseVersionId, valueOf } from '@mapeo/schema'
import RAM from 'random-access-memory'
import { discoveryKey } from 'hypercore-crypto'
import Fastify from 'fastify'
Expand Down Expand Up @@ -33,28 +34,15 @@ test('CoreOwnership', async () => {
const authCoreId = await coreOwnership.getCoreId(deviceId, 'auth')
assert.equal(await coreOwnership.getOwner(authCoreId), deviceId)

const preset = await project.preset.create({
schemaName: 'preset',
name: 'test preset',
geometry: ['point'],
tags: {},
addTags: {},
removeTags: {},
terms: [],
fieldRefs: [],
color: '#ff00ff',
})
const preset = await project.preset.create(valueOf(generate('preset')[0]))
assert.equal(
discoveryId(await coreOwnership.getCoreId(deviceId, 'config')),
parseVersionId(preset.versionId).coreDiscoveryKey.toString('hex')
)

const observation = await project.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
const observation = await project.observation.create(
valueOf(generate('observation')[0])
)
assert.equal(
discoveryId(await coreOwnership.getCoreId(deviceId, 'data')),
parseVersionId(observation.versionId).coreDiscoveryKey.toString('hex')
Expand Down
11 changes: 5 additions & 6 deletions test-e2e/created_by_to_device_id.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { generate } from '@mapeo/mock-data'
import { valueOf } from '@mapeo/schema'
import assert from 'node:assert/strict'
import test from 'node:test'
import {
Expand Down Expand Up @@ -32,12 +34,9 @@ test('$createdByToDeviceId', async (t) => {
creatorProject.$sync.start()
memberProject.$sync.start()

const observation = await creatorProject.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
const observation = await creatorProject.observation.create(
valueOf(generate('observation')[0])
)

await waitForSync(projects, 'full')

Expand Down
39 changes: 13 additions & 26 deletions test-e2e/project-leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import assert from 'node:assert/strict'
import * as fs from 'node:fs/promises'
import { temporaryDirectory } from 'tempy'

import { generate } from '@mapeo/mock-data'
import { valueOf } from '@mapeo/schema'
import {
BLOCKED_ROLE_ID,
COORDINATOR_ROLE_ID,
Expand Down Expand Up @@ -254,12 +256,7 @@ test('Data access after leaving project', async (t) => {

const [, coordinatorProject, memberProject] = projects

await memberProject.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
await memberProject.observation.create(valueOf(generate('observation')[0]))
assert(
(await memberProject.observation.getMany()).length >= 1,
'Test is set up correctly'
Expand All @@ -275,12 +272,7 @@ test('Data access after leaving project', async (t) => {
await waitForSync(projects, 'initial')

await assert.rejects(async () => {
await memberProject.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
await memberProject.observation.create(valueOf(generate('observation')[0]))
}, 'member cannot create new data after leaving')
await assert.rejects(
() => memberProject.observation.getMany(),
Expand Down Expand Up @@ -344,12 +336,9 @@ test('leaving a project deletes data from disk', async (t) => {
)
const [creatorProject, memberProject] = projects

const observation = await creatorProject.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
const observation = await creatorProject.observation.create(
valueOf(generate('observation')[0])
)

creatorProject.$sync.start()
memberProject.$sync.start()
Expand Down Expand Up @@ -421,15 +410,10 @@ test('partly-left projects are cleaned up on startup', async (t) => {
const custodian = new ManagerCustodian(t)

const projectId = await custodian.withManagerInSeparateProcess(
async (manager, LEFT_ROLE_ID) => {
async (manager, { observation, LEFT_ROLE_ID }) => {
const projectId = await manager.createProject({ name: 'foo' })
const project = await manager.getProject(projectId)
await project.observation.create({
schemaName: 'observation',
attachments: [],
tags: {},
metadata: {},
})
await project.observation.create(observation)
await project.$member.assignRole(
manager.getDeviceInfo().deviceId,
/**
Expand All @@ -440,7 +424,10 @@ test('partly-left projects are cleaned up on startup', async (t) => {
)
return projectId
},
LEFT_ROLE_ID
{
observation: valueOf(generate('observation')[0]),
LEFT_ROLE_ID,
}
)

const couldGetObservations = await custodian.withManagerInSeparateProcess(
Expand Down

0 comments on commit 680dd79

Please sign in to comment.