-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwb-edit-entity.js
79 lines (70 loc) · 3.56 KB
/
wb-edit-entity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import 'should'
import { resolve } from 'node:path'
import { getDirname } from '#lib/fs'
import { readJsonFile } from '#lib/json'
import { wdTest } from '#test/lib/utils'
import editDataJs from './assets/edit_data.js'
const dirname = getDirname(import.meta.url)
const editDataJson = readJsonFile(resolve(dirname, './assets/edit_data.json'))
describe('wb edit-entity', () => {
it('should accept a path to a JSON file', async () => {
const { stdout } = await wdTest('edit-entity --dry ./test/assets/edit_data.json')
const data = JSON.parse(stdout).args[0]
data.should.deepEqual(editDataJson)
})
it('should accept a path to a JS object file', async () => {
const { stdout } = await wdTest('edit-entity --dry ./test/assets/edit_data.js')
const data = JSON.parse(stdout).args[0]
data.should.deepEqual(editDataJs)
})
it('should accept a path to a JS function file', async () => {
const { stdout } = await wdTest('edit-entity --dry ./test/assets/edit_data_function.cjs Q1 123')
const data = JSON.parse(stdout).args[0]
data.id.should.equal('Q1')
data.claims.P95228.value.should.equal(123)
})
it('should show the command help menu when called without argument', async () => {
const { stdout } = await wdTest('edit-entity')
stdout.should.containEql('Usage:')
stdout.should.containEql('Edit an existing entity')
})
it('should accept an array of edit objects', async () => {
const { stdout } = await wdTest('edit-entity ./test/assets/multi_edit_data.js --maxlag 100')
const [ line1, line2 ] = stdout.split('\n')
JSON.parse(line1).entity.id.should.equal('Q1112')
JSON.parse(line1).success.should.equal(1)
JSON.parse(line2).entity.id.should.equal('Q1113')
JSON.parse(line2).success.should.equal(1)
})
it('should not log anything when the template returns undefined', async () => {
const { stdout, stderr } = await wdTest('edit-entity --dry ./test/assets/template_returns_undefined.js Q1')
stdout.should.equal('')
stderr.should.equal('')
})
describe('meta data', () => {
it('should support exporting an object with a template function and metadata', async () => {
const { default: templateModule } = await import('./assets/edit_data_function.cjs')
templateModule.template.should.be.a.Function()
templateModule.args.should.be.an.Array()
templateModule.description.should.be.a.String()
templateModule.examples.should.be.an.Array()
const { stdout } = await wdTest('edit-entity --help ./test/assets/edit_data_function.cjs')
stdout.should.containEql('Usage:')
stdout.should.containEql('Add a P95228 statement')
const { stdout: dryStdout } = await wdTest('edit-entity --dry ./test/assets/edit_data_function.cjs Q1 123')
JSON.parse(dryStdout).args[0].id.should.equal('Q1')
})
it('should support adding metadata to the template function when main module export', async () => {
const { default: templateModule } = await import('./assets/edit_data_function_deprecated.cjs')
templateModule.should.be.a.Function()
templateModule.args.should.be.an.Array()
templateModule.description.should.be.a.String()
templateModule.examples.should.be.an.Array()
const { stdout } = await wdTest('edit-entity --help ./test/assets/edit_data_function_deprecated.cjs')
stdout.should.containEql('Usage:')
stdout.should.containEql('Add a P95228 statement')
const { stdout: dryStdout } = await wdTest('edit-entity --dry ./test/assets/edit_data_function_deprecated.cjs Q1 123')
JSON.parse(dryStdout).args[0].id.should.equal('Q1')
})
})
})