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 1 commit
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
136 changes: 136 additions & 0 deletions test/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { randomBytes } from 'node:crypto'

export const fixtures = {
onlyId: {
text: 'test encoding of record with missing fields',
plan: 1,
doc: {id: randomBytes(32).toString('hex')}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe change to docId, since testing missing fields not invalid fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that field was copy/pasted from older tests, I should change that.

},
badDocName: {
text: 'test encoding of wrong record type',
plan: 1,
doc: {
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
id: randomBytes(32).toString('hex'),
schemaName: 'doesnotexist',
links: [],
createdAt: new Date().toJSON(),
refs: [],
attachments: [],
metadata: {
manual_location: true,
},
}
},
goodDocs:{
text: 'test encoding of rightfully formated records',
plan: 1,
docs: {
observation:{
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'observation',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
lat: 10,
lon: -20.3,
refs: [],
attachments: [],
metadata: {
manualLocation: true,
},
tags: {}
},
project: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'project',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
name: 'myProject',
defaultPresets: {
point: ["myPointPreset"],
vertex: ["myVertexPreset"],
area: ["myAreaPreset"],
line: ["myLinePreset"],
relation: ["myRelationPreset"],
}
},
field: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'field',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
tagKey: "myTagKey",
type: "text",
label: "myLabel"
},
preset: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'preset',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
name:"myPreset",
geometry:["point"],
tags:{},
addTags:{},
removeTags:{},
fieldIds:[],
terms:[]
},
role: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'role',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
role:"author",
projectId:randomBytes(32).toString('hex'),
action:"role_set",
signature:"signature",
authorIndex:2,
deviceIndex:4,
authorId:randomBytes(32).toString('hex'),
capabilityType:"capability"
},
device: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'device',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
action:"ban",
authorId:randomBytes(32).toString('hex'),
projectId:randomBytes(32).toString('hex'),
signature:"mySignature",
authorIndex:2,
deviceIndex:20,
capabilityType:"someCapability",
},
coreOwnership: {
docId: randomBytes(32).toString('hex'),
versionId: `${randomBytes(32).toString('hex')}/0`,
schemaName: 'coreOwnership',
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
links: [],
action:"remove",
coreId:randomBytes(32).toString('hex'),
projectId:randomBytes(32).toString('hex'),
storeType:"blob",
signature:"mySig",
authorIndex: 10,
deviceIndex: 9,
authorId:randomBytes(32).toString('hex'),
capabilityType: "someCapability"
}
}
}
}
171 changes: 93 additions & 78 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,103 @@
import test from 'tape'
import { encode, decode, validate } from '../index.js'
import { randomBytes } from 'node:crypto'
import { docs } from './docs.js'
import { fixtures } from './fixture.js'
import { encode } from '../dist/encode.js'
import { decode } from '../dist/decode.js'
import { validate } from '../dist/validate.js'
import { parseVersionId } from '../dist/lib/utils.js'
// import { randomBytes } from 'node:crypto'

test('test encoding of record with missing fields', async (t) => {
t.plan(1)
t.throws(() => {
encode(docs.onlyId)
})
})

test('test encoding of wrong record type', async (t) => {
t.plan(1)
t.throws(() => {
encode(docs.badDocType)
})
})

test('test encoding of record with wrong schema version', async (t) => {
t.plan(1)
t.throws(() => {
encode(docs.badSchemaVersion)
{
const {text,plan,doc} = fixtures.onlyId
test(text, async(t) => {
t.plan(plan)
t.throws(() => {
encode(doc)
})
})
})

test('test encoding of rightfully formated record', async (t) => {
const goodDocs = Object.keys(docs.good)
t.plan(goodDocs.length)
goodDocs.forEach((k) => {
const doc = docs.good[k]
t.doesNotThrow(() => {
}
{
const {text,plan, doc} = fixtures.badDocName
test(text,async(t) => {
t.plan(plan)
t.throws(() => {
encode(doc)
}, `testing ${k}`)
})
})
})

test('test validation of record', async (t) => {
const goodDocs = Object.keys(docs.good)
t.plan(goodDocs.length)
goodDocs.forEach((k) => {
const doc = docs.good[k]
const record = decode(encode(doc), {
coreKey: randomBytes(32),
index: 0,
}
{
const {text,docs} = fixtures.goodDocs
test({text},async(t) => {
t.plan(Object.keys(docs).length)
Object.keys(docs).forEach((docName) => {
const doc = docs[docName]
t.doesNotThrow(() => {
const buf = encode(doc)
console.log(decode(buf, parseVersionId(doc.versionId)))
}, `testing ${docName}`)
})
t.doesNotThrow(() => {
// field has a type which is different from the rest :|
if (k !== 'field') validate(record)
}, `testing validation of ${k}`)
})
})

test('test encoding, decoding of record and comparing the two versions', async (t) => {
const goodDocs = Object.keys(docs.good)
goodDocs.forEach((k) => {
const doc = docs.good[k]
const record = decode(encode(doc), {
coreKey: randomBytes(32),
index: 0,
})
const fields = Object.keys(doc)
// t.plan(goodDocs.length * fields.length * 2)
fields.forEach((f) => {
console.log(typeof record[f], typeof doc[f])
const msg = `checking ${f} for ${k}`
record[f] && doc[f] ? t.pass(msg) : t.fail(msg)
}

// if field is not an object, check equality
// since objects as fields usually mean the possibility of additionalFields in jsonSchemas
if (typeof record[f] !== 'object') {
t.equal(record[f], doc[f], `comparing value of ${f} for ${k}`)
}
})
})
})
// test('test encoding of rightfully formated record', async (t) => {
// const goodDocs = Object.keys(docs.good)
// t.plan(goodDocs.length)
// goodDocs.forEach((k) => {
// const doc = docs.good[k]
// t.doesNotThrow(() => {
// encode(doc)
// }, `testing ${k}`)
// })
// })

test('test decoding of record without passing core key and index', async (t) => {
const goodDocs = Object.keys(docs.good)
t.plan(goodDocs.length)
goodDocs.forEach((key) => {
const doc = docs.good[key]
const record = encode(doc)
t.throws(() => {
decode(record)
}, `testing ${key}`)
})
})
// test('test validation of record', async (t) => {
// const goodDocs = Object.keys(docs.good)
// t.plan(goodDocs.length)
// goodDocs.forEach((k) => {
// const doc = docs.good[k]
// const record = decode(encode(doc), {
// coreKey: randomBytes(32),
// index: 0,
// })
// t.doesNotThrow(() => {
// // field has a type which is different from the rest :|
// if (k !== 'field') validate(record)
// }, `testing validation of ${k}`)
// })
// })

// test('test encoding, decoding of record and comparing the two versions', async (t) => {
// const goodDocs = Object.keys(docs.good)
// goodDocs.forEach((k) => {
// const doc = docs.good[k]
// const record = decode(encode(doc), {
// coreKey: randomBytes(32),
// index: 0,
// })
// const fields = Object.keys(doc)
// // t.plan(goodDocs.length * fields.length * 2)
// fields.forEach((f) => {
// console.log(typeof record[f], typeof doc[f])
// const msg = `checking ${f} for ${k}`
// record[f] && doc[f] ? t.pass(msg) : t.fail(msg)

// // if field is not an object, check equality
// // since objects as fields usually mean the possibility of additionalFields in jsonSchemas
// if (typeof record[f] !== 'object') {
// t.equal(record[f], doc[f], `comparing value of ${f} for ${k}`)
// }
// })
// })
// })

// test('test decoding of record without passing core key and index', async (t) => {
// const goodDocs = Object.keys(docs.good)
// t.plan(goodDocs.length)
// goodDocs.forEach((key) => {
// const doc = docs.good[key]
// const record = encode(doc)
// t.throws(() => {
// decode(record)
// }, `testing ${key}`)
// })
// })