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 Tests #106

Merged
merged 24 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3b4609b
feat: add tests for valid docs and badDocName
Aug 7, 2023
247ae91
feat: added test for deep comparison of fields of doc with decodedDoc
Aug 9, 2023
5aa150a
feat: fix tests for nested objects (one-level) and floats
Aug 9, 2023
1358a69
chore: refactor ifelse to switch
Aug 9, 2023
a0db8e2
feat: fix bug in `encode-convertions/convertProject`, refactor tests
Aug 9, 2023
e459b99
feat: observation.proto/{lat,lon}: use double instead of float
Aug 9, 2023
c075ec0
add fields with default value to `field` to pass deepEqual test
Aug 9, 2023
e98e928
feat: add tests for optional values, proto and schema changes
Aug 10, 2023
fc139a4
feat: add configStore tests with additional fields
Aug 10, 2023
ef595e1
Merge branch 'master' of github.com:digidem/mapeo-schema into feat/fi…
Aug 10, 2023
5f12072
feat: fix test after master merge
Aug 10, 2023
be09f67
feat: add tests for extra non required values, and cleanup
Aug 10, 2023
e9097d8
feat: fix buf in encoding field tags
Aug 14, 2023
9e3aca1
chore: format with prettier
gmaclennan Aug 15, 2023
f2dff2e
remove unused import
gmaclennan Aug 15, 2023
fa016ac
Make badDocs test more DRY
gmaclennan Aug 15, 2023
0ded478
remove unused file
gmaclennan Aug 15, 2023
d8b508d
move fixtures into separate files
gmaclennan Aug 15, 2023
756d345
fix typos and make goodDocs correct types
gmaclennan Aug 15, 2023
a1793dc
fix typo acurracy -> accuracy
gmaclennan Aug 15, 2023
2838ad7
remove extra fields fixtures - not necessary
gmaclennan Aug 15, 2023
8a6113d
remove doesNotThrow check
gmaclennan Aug 15, 2023
d7fd299
only define fields that are expected to change
gmaclennan Aug 15, 2023
17706d2
let -> const
gmaclennan Aug 15, 2023
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
16 changes: 8 additions & 8 deletions proto/observation/v5.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ message Observation_5 {

Common_1 common = 1;

optional float lat = 5;
optional float lon = 6;
optional double lat = 5;
optional double lon = 6;

message Ref {
bytes id = 1;
Expand Down Expand Up @@ -47,12 +47,12 @@ message Observation_5 {
bool mocked = 2;

message Coords {
float latitude = 1;
float longitude = 2;
float altitude = 3;
float heading = 4;
float speed = 5;
float acurracy = 6;
double latitude = 1;
double longitude = 2;
double altitude = 3;
double heading = 4;
double speed = 5;
double accuracy = 6;
}
optional Coords coords = 3;
}
Expand Down
2 changes: 1 addition & 1 deletion schema/preset/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"type": "string"
}
},
"icon": {
"iconId": {
"description": "hex-encoded string. ID of preset icon which represents this preset",
"type": "string"
},
Expand Down
37 changes: 17 additions & 20 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export const convertProject: ConvertFunction<'project'> = (
...jsonSchemaCommon,
...rest,
defaultPresets: {
point: message.defaultPresets?.point.map(p => p.toString('hex')),
area: message.defaultPresets?.area.map(a => a.toString('hex')),
vertex: message.defaultPresets?.vertex.map(v => v.toString('hex')),
line: message.defaultPresets?.line.map(l => l.toString('hex')),
relation: message.defaultPresets?.relation.map(r => r.toString('hex')),
}
point: message.defaultPresets?.point.map((p) => p.toString('hex')),
area: message.defaultPresets?.area.map((a) => a.toString('hex')),
vertex: message.defaultPresets?.vertex.map((v) => v.toString('hex')),
line: message.defaultPresets?.line.map((l) => l.toString('hex')),
relation: message.defaultPresets?.relation.map((r) => r.toString('hex')),
},
}
}

Expand Down Expand Up @@ -113,26 +113,24 @@ export const convertPreset: ConvertFunction<'preset'> = (
...jsonSchemaCommon,
...rest,
geometry,
iconId: rest.iconId ? rest.iconId.toString('hex') : undefined,
tags: convertTags(rest.tags),
addTags: convertTags(rest.addTags),
removeTags: convertTags(rest.removeTags),
fieldIds: rest.fieldIds.map((id) => id.toString('hex')),
}
}

export const convertRole: ConvertFunction<'role'> = (
message,
versionObj
) => {
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
export const convertRole: ConvertFunction<'role'> = (message, versionObj) => {
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
role: rest.role,
projectId: message.projectId.toString('hex'),
authorId: message.authorId.toString('hex')
}
authorId: message.authorId.toString('hex'),
}
}

export const convertDevice: ConvertFunction<'device'> = (
Expand All @@ -145,7 +143,7 @@ export const convertDevice: ConvertFunction<'device'> = (
...jsonSchemaCommon,
...rest,
authorId: message.authorId.toString('hex'),
projectId: message.projectId.toString('hex')
projectId: message.projectId.toString('hex'),
}
}

Expand All @@ -160,9 +158,8 @@ export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = (
...rest,
coreId: message.coreId.toString('hex'),
projectId: message.projectId.toString('hex'),
authorId: message.authorId.toString('hex')
authorId: message.authorId.toString('hex'),
}

}

function convertTags(tags: { [key: string]: TagValue_1 } | undefined): {
Expand Down
53 changes: 27 additions & 26 deletions src/lib/encode-converstions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ export const convertProject: ConvertFunction<'project'> = (mapeoDoc) => {
common: convertCommon(mapeoDoc),
...mapeoDoc,
defaultPresets: {
point: (mapeoDoc.defaultPresets.point || []).map(p => Buffer.from(p,'hex')),
area: (mapeoDoc.defaultPresets.area || []).map(a => Buffer.from(a,'hex')),
vertex: (mapeoDoc.defaultPresets.area || []).map(v => Buffer.from(v,'hex')),
line: (mapeoDoc.defaultPresets.area || []).map(l => Buffer.from(l, 'hex')),
relation: (mapeoDoc.defaultPresets.area || []).map(r => Buffer.from(r, 'hex'))
}
point: (mapeoDoc.defaultPresets.point || []).map((p) =>
Buffer.from(p, 'hex')
),
area: (mapeoDoc.defaultPresets.area || []).map((a) =>
Buffer.from(a, 'hex')
),
vertex: (mapeoDoc.defaultPresets.vertex || []).map((v) =>
Buffer.from(v, 'hex')
),
line: (mapeoDoc.defaultPresets.line || []).map((l) =>
Buffer.from(l, 'hex')
),
relation: (mapeoDoc.defaultPresets.relation || []).map((r) =>
Buffer.from(r, 'hex')
),
},
}
}

Expand Down Expand Up @@ -64,7 +74,7 @@ export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
addTags: convertTags(mapeoDoc.addTags),
removeTags: convertTags(mapeoDoc.removeTags),
fieldIds: mapeoDoc.fieldIds.map((field) => Buffer.from(field, 'hex')),
iconId: mapeoDoc.icon ? Buffer.from(mapeoDoc.icon, 'hex') : undefined,
iconId: mapeoDoc.iconId ? Buffer.from(mapeoDoc.iconId, 'hex') : undefined,
}
}

Expand Down Expand Up @@ -94,26 +104,21 @@ export const convertObservation: ConvertFunction<'observation'> = (
}
}

export const convertRole: ConvertFunction<'role'> = (
mapeoDoc
) => {
export const convertRole: ConvertFunction<'role'> = (mapeoDoc) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
projectId: Buffer.from(mapeoDoc.projectId,'hex'),
authorId: Buffer.from(mapeoDoc.authorId,'hex')

projectId: Buffer.from(mapeoDoc.projectId, 'hex'),
authorId: Buffer.from(mapeoDoc.authorId, 'hex'),
}
}

export const convertDevice: ConvertFunction<'device'> = (
mapeoDoc
) => {
export const convertDevice: ConvertFunction<'device'> = (mapeoDoc) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
authorId: Buffer.from(mapeoDoc.authorId, 'hex'),
projectId: Buffer.from(mapeoDoc.projectId, 'hex')
projectId: Buffer.from(mapeoDoc.projectId, 'hex'),
}
}

Expand All @@ -125,7 +130,7 @@ export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = (
...mapeoDoc,
coreId: Buffer.from(mapeoDoc.coreId, 'hex'),
projectId: Buffer.from(mapeoDoc.projectId, 'hex'),
authorId: Buffer.from(mapeoDoc.authorId,'hex')
authorId: Buffer.from(mapeoDoc.authorId, 'hex'),
}
}

Expand All @@ -145,14 +150,10 @@ function convertTags(tags: {
}): {
[key: string]: TagValue_1
} {
return Object.keys(tags).reduce(
(acc: { [key: string]: TagValue_1 }, k: string) => {
return {
[k]: convertTagValue(tags[k]),
}
},
{}
)
return Object.keys(tags).reduce<{ [key: string]: TagValue_1 }>((acc, k) => {
acc[k] = convertTagValue(tags[k])
return acc
}, {})
}

function convertTagValue(tagValue: JsonTagValue): TagValue_1 {
Expand Down
104 changes: 0 additions & 104 deletions test/docs.js

This file was deleted.

26 changes: 26 additions & 0 deletions test/fixtures/bad-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check
import { randomBytes } from 'node:crypto'
import { cachedValues } from './cached.js'

export const badDocs = [
{
text: 'test encoding of record with missing fields',
doc: { docId: randomBytes(32).toString('hex') },
},
{
text: 'test encoding of wrong schema name',
/** @type Omit<import('../../dist/index.js').Observation, 'schemaName'> & { schemaName: string }} */
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
schemaName: 'observOtion',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
refs: [],
attachments: [],
tags: {},
metadata: {},
},
},
]
28 changes: 28 additions & 0 deletions test/fixtures/cached.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { randomBytes } from 'node:crypto'

// For stuff like dates or generated random Ids, so I can compare with an expected doc
const date = new Date().toJSON()
export const cachedValues = {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
projectId: randomBytes(32).toString('hex'),
authorId: randomBytes(32).toString('hex'),
coreId: randomBytes(32).toString('hex'),
createdAt: date,
updatedAt: date,
attachments: { driveId: randomBytes(32).toString('hex') },
metadata: {
position: {
timestamp: date,
},
},
defaultPresets: {
point: [randomBytes(32).toString('hex')],
area: [randomBytes(32).toString('hex')],
vertex: [randomBytes(32).toString('hex')],
line: [randomBytes(32).toString('hex')],
relation: [randomBytes(32).toString('hex')],
},
fieldIds: [randomBytes(32).toString('hex')],
iconId: randomBytes(32).toString('hex'),
}
Loading