Skip to content

Commit

Permalink
Refactor tests to directly check the CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
njam committed Aug 28, 2024
1 parent ea89b8f commit 75d8909
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 214 deletions.
2 changes: 1 addition & 1 deletion docs/background.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ Together these two pieces of information (models' geometry and models' propertie


[citygml]: https://www.citygml.org/
[3d-tiles]: https://github.com/AnalyticalGraphicsInc/3d-tiles
[3d-tiles]: https://github.com/CesiumGS/3d-tiles
[gltf]: https://www.khronos.org/gltf/
94 changes: 77 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^10.1.0"
"mocha": "^10.1.0",
"fs-jetpack": "^5.1.0"
},
"scripts": {
"test": "node_modules/.bin/mocha"
Expand Down
34 changes: 17 additions & 17 deletions src/3dtiles/BatchTable.mjs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/**
* @see https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/TileFormats/BatchTable/README.md
* @see https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/TileFormats/BatchTable/README.md
*/
class BatchTable {

constructor () {
this.features = {}
this.items = {}
}

/**
* @param {String|Number} id
* @param {String|Number} batchId
* @param {Object} properties
*/
addFeature (id, properties) {
id = String(id)
if (this.features[id]) {
throw new Error('A feature with this ID already exists: ' + id)
addBatchItem (batchId, properties) {
batchId = String(batchId)
if (this.items[batchId]) {
throw new Error('An item with this ID already exists: ' + batchId)
}
this.features[id] = properties
this.items[batchId] = properties
}

/**
* @returns {String[]}
*/
getIds () {
return Object.keys(this.features)
getBatchIds () {
return Object.keys(this.items)
}

/**
* @returns {string[]}
*/
getPropertyNames () {
let propertyNames = {}
for (const id in this.features) {
let properties = this.features[id]
for (const id in this.items) {
let properties = this.items[id]
for (const name in properties) {
propertyNames[name] = true
}
Expand All @@ -44,15 +44,15 @@ class BatchTable {
* @returns {Object}
*/
getBatchTableJson () {
let ids = this.getIds()
let ids = this.getBatchIds()
let propertyNames = this.getPropertyNames()

let batchTable = {
id: ids
}
propertyNames.forEach(name => {
batchTable[name] = ids.map((id, i) => {
let value = this.features[id][name]
let value = this.items[id][name]
if (typeof value === 'undefined') {
value = null
}
Expand All @@ -67,16 +67,16 @@ class BatchTable {
* @returns {Number}
*/
getLength () {
return Object.keys(this.features).length
return Object.keys(this.items).length
}

/**
* @returns {Object}
*/
getMinMax () {
let minmax = {}
for (const id in this.features) {
let properties = this.features[id]
for (const id in this.items) {
let properties = this.items[id]
for (const name in properties) {
let value = properties[name]
if (typeof value === 'number') {
Expand Down
44 changes: 22 additions & 22 deletions src/3dtiles/BatchTable.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ describe('BatchTable', function () {
describe('#addFeature()', function () {
it('should throw on duplicate ID', function () {
let table = new BatchTable()
table.addFeature('feature1', {foo: 12})
table.addBatchItem('item1', {foo: 12})

assert.throws(() => {
table.addFeature('feature1', {foo: 13})
table.addBatchItem('item1', {foo: 13})
}, /already exists/)
})
})

describe('#addFeature()', function () {
it('should throw on duplicate ID, once string, once integer', function () {
let table = new BatchTable()
table.addFeature('2', {foo: 12})
table.addBatchItem('2', {foo: 12})

assert.throws(() => {
table.addFeature(2, {foo: 13})
table.addBatchItem(2, {foo: 13})
}, /already exists/)
})
})

describe('#getIds()', function () {
it('should return all IDs', function () {
let table = new BatchTable()
table.addFeature('feature1', {foo: 12, bar: 'bar1'})
table.addFeature('feature2', {foo: 44})
table.addFeature('feature3', {foo: 99, bar: 'bar3'})
table.addBatchItem('item1', {foo: 12, bar: 'bar1'})
table.addBatchItem('item2', {foo: 44})
table.addBatchItem('item3', {foo: 99, bar: 'bar3'})

assert.deepEqual(table.getIds(), ['feature1', 'feature2', 'feature3'])
assert.deepEqual(table.getBatchIds(), ['item1', 'item2', 'item3'])
})
})

describe('#getPropertyNames()', function () {
it('should return all property names', function () {
let table = new BatchTable()
table.addFeature('feature1', {foo: 12, bar: 'bar1'})
table.addFeature('feature2', {foo: 44})
table.addFeature('feature3', {foo: 99, bar: 'bar3'})
table.addBatchItem('item1', {foo: 12, bar: 'bar1'})
table.addBatchItem('item2', {foo: 44})
table.addBatchItem('item3', {foo: 99, bar: 'bar3'})

assert.deepEqual(table.getPropertyNames(), ['foo', 'bar'])
})
Expand All @@ -49,25 +49,25 @@ describe('BatchTable', function () {
describe('#getBatchTableJson()', function () {
it('should return a valid batch table', function () {
let table = new BatchTable()
table.addFeature('feature1', {foo: 12, bar: 'bar1'})
table.addFeature('feature2', {foo: 44})
table.addFeature('feature3', {foo: 99, bar: 'bar3'})
table.addBatchItem('item1', {foo: 12, bar: 'bar1'})
table.addBatchItem('item2', {foo: 44})
table.addBatchItem('item3', {foo: 99, bar: 'bar3'})

assert.deepEqual(table.getBatchTableJson(),
{
foo: [12, 44, 99],
bar: ['bar1', null, 'bar3'],
id: ['feature1', 'feature2', 'feature3'],
id: ['item1', 'item2', 'item3'],
})
})
})

describe('#getLength()', function () {
it('should return the number of features', function () {
let table = new BatchTable()
table.addFeature('feature1', {foo: 12, bar: 'bar1'})
table.addFeature('feature2', {foo: 44})
table.addFeature('feature3', {foo: 99, bar: 'bar3'})
table.addBatchItem('item1', {foo: 12, bar: 'bar1'})
table.addBatchItem('item2', {foo: 44})
table.addBatchItem('item3', {foo: 99, bar: 'bar3'})

assert.equal(table.getLength(), 3)
})
Expand All @@ -76,10 +76,10 @@ describe('BatchTable', function () {
describe('#getMinMax()', function () {
it('should return the minimum and maximum values of numeric properties', function () {
let table = new BatchTable()
table.addFeature('feature4', {foo: 4, bar: 44, str: 'str4'})
table.addFeature('feature1', {foo: 1, bar: 'str11', str: 'str1'})
table.addFeature('feature2', {foo: 2})
table.addFeature('feature3', {foo: 3, bar: 33, str: 'str3'})
table.addBatchItem('item4', {foo: 4, bar: 44, str: 'str4'})
table.addBatchItem('item1', {foo: 1, bar: 'str11', str: 'str1'})
table.addBatchItem('item2', {foo: 2})
table.addBatchItem('item3', {foo: 3, bar: 33, str: 'str3'})

assert.deepEqual(table.getMinMax(), {
foo: {minimum: 1, maximum: 4},
Expand Down
2 changes: 1 addition & 1 deletion src/3dtiles/Batched3DModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import createB3dm from './createB3dm.mjs'
import Cesium from 'cesium'

/**
* @see https://github.com/AnalyticalGraphicsInc/3d-tiles/blob/master/TileFormats/Batched3DModel/README.md
* @see https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/TileFormats/Batched3DModel/README.md
*/
class Batched3DModel {
/**
Expand Down
Loading

0 comments on commit 75d8909

Please sign in to comment.