Skip to content

Commit

Permalink
Merge pull request #11 from pelias/sqlite_table_tests
Browse files Browse the repository at this point in the history
sqlite: table tests
  • Loading branch information
missinglink authored Mar 10, 2020
2 parents 6f572ee + c1da926 commit e73baaa
Show file tree
Hide file tree
Showing 12 changed files with 750 additions and 45 deletions.
4 changes: 2 additions & 2 deletions sqlite/mock/MockDatabase.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const MockStatement = require('./MockStatement')

class MockDatabase {
constructor() { this.pragmas = [] }
constructor() { this.stmt = [] }
prepare(sql) {
const stmt = new MockStatement(sql)
this.pragmas.push(stmt)
this.stmt.push(stmt)
return stmt
}
}
Expand Down
13 changes: 9 additions & 4 deletions sqlite/mock/MockStatement.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
class MockStatement {
constructor(sql) {
this.sql = sql
this.action = 'none'
this.sql = sql.replace(/\n/g, ' ').replace(/\s{2,}/g, ' ')
this.action = {
run: [],
get: [],
all: []
}
}
run() { this.action = 'run' }
get() { this.action = 'get' }
run(...args) { this.action.run.push(args) }
get(...args) { this.action.get.push(args) }
all(...args) { this.action.all.push(args) }
}

module.exports = MockStatement
76 changes: 38 additions & 38 deletions sqlite/pragma.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ module.exports.write = (test) => {
const db = new MockDatabase()
pragma.write(db)

t.equals(db.pragmas.length, 9)
t.equals(db.pragmas[0].sql, 'PRAGMA journal_mode=MEMORY')
t.equals(db.pragmas[0].action, 'run')
t.equals(db.pragmas[1].sql, 'PRAGMA locking_mode=NORMAL')
t.equals(db.pragmas[1].action, 'get')
t.equals(db.pragmas[2].sql, 'PRAGMA default_cache_size=2000')
t.equals(db.pragmas[2].action, 'run')
t.equals(db.pragmas[3].sql, 'PRAGMA foreign_keys=OFF')
t.equals(db.pragmas[3].action, 'run')
t.equals(db.pragmas[4].sql, 'PRAGMA temp_store=MEMORY')
t.equals(db.pragmas[4].action, 'run')
t.equals(db.pragmas[5].sql, 'PRAGMA page_size=4096')
t.equals(db.pragmas[5].action, 'run')
t.equals(db.pragmas[6].sql, 'PRAGMA cache_size=2000')
t.equals(db.pragmas[6].action, 'run')
t.equals(db.pragmas[7].sql, 'PRAGMA recursive_triggers=OFF')
t.equals(db.pragmas[7].action, 'run')

t.equals(db.pragmas[8].sql, 'PRAGMA synchronous=OFF')
t.equals(db.pragmas[8].action, 'run')
t.equals(db.stmt.length, 9)
t.equals(db.stmt[0].sql, 'PRAGMA journal_mode=MEMORY')
t.deepEquals(db.stmt[0].action.run[0], [])
t.equals(db.stmt[1].sql, 'PRAGMA locking_mode=NORMAL')
t.deepEquals(db.stmt[1].action.get[0], [])
t.equals(db.stmt[2].sql, 'PRAGMA default_cache_size=2000')
t.deepEquals(db.stmt[2].action.run[0], [])
t.equals(db.stmt[3].sql, 'PRAGMA foreign_keys=OFF')
t.deepEquals(db.stmt[3].action.run[0], [])
t.equals(db.stmt[4].sql, 'PRAGMA temp_store=MEMORY')
t.deepEquals(db.stmt[4].action.run[0], [])
t.equals(db.stmt[5].sql, 'PRAGMA page_size=4096')
t.deepEquals(db.stmt[5].action.run[0], [])
t.equals(db.stmt[6].sql, 'PRAGMA cache_size=2000')
t.deepEquals(db.stmt[6].action.run[0], [])
t.equals(db.stmt[7].sql, 'PRAGMA recursive_triggers=OFF')
t.deepEquals(db.stmt[7].action.run[0], [])

t.equals(db.stmt[8].sql, 'PRAGMA synchronous=OFF')
t.deepEquals(db.stmt[8].action.run[0], [])

t.end()
})
Expand All @@ -49,16 +49,16 @@ module.exports.read = (test) => {
const db = new MockDatabase()
pragma.read(db)

t.equals(db.pragmas.length, 4)
t.equals(db.pragmas[0].sql, 'PRAGMA journal_mode=OFF')
t.equals(db.pragmas[0].action, 'run')
t.equals(db.pragmas[1].sql, 'PRAGMA locking_mode=NORMAL')
t.equals(db.pragmas[1].action, 'get')
t.equals(db.pragmas[2].sql, 'PRAGMA recursive_triggers=OFF')
t.equals(db.pragmas[2].action, 'run')
t.equals(db.stmt.length, 4)
t.equals(db.stmt[0].sql, 'PRAGMA journal_mode=OFF')
t.deepEquals(db.stmt[0].action.run[0], [])
t.equals(db.stmt[1].sql, 'PRAGMA locking_mode=NORMAL')
t.deepEquals(db.stmt[1].action.get[0], [])
t.equals(db.stmt[2].sql, 'PRAGMA recursive_triggers=OFF')
t.deepEquals(db.stmt[2].action.run[0], [])

t.equals(db.pragmas[3].sql, 'PRAGMA synchronous=OFF')
t.equals(db.pragmas[3].action, 'run')
t.equals(db.stmt[3].sql, 'PRAGMA synchronous=OFF')
t.deepEquals(db.stmt[3].action.run[0], [])

t.end()
})
Expand All @@ -69,19 +69,19 @@ module.exports.synchronous = (test) => {
const db = new MockDatabase()
pragma.synchronous(db)

t.equals(db.pragmas.length, 1)
t.equals(db.pragmas[0].sql, 'PRAGMA synchronous=OFF')
t.equals(db.pragmas[0].action, 'run')
t.equals(db.stmt.length, 1)
t.equals(db.stmt[0].sql, 'PRAGMA synchronous=OFF')
t.deepEquals(db.stmt[0].action.run[0], [])

t.end()
})
test('synchronous - custom', (t) => {
const db = new MockDatabase()
pragma.synchronous(db, 'NORMAL')

t.equals(db.pragmas.length, 1)
t.equals(db.pragmas[0].sql, 'PRAGMA synchronous=NORMAL')
t.equals(db.pragmas[0].action, 'run')
t.equals(db.stmt.length, 1)
t.equals(db.stmt[0].sql, 'PRAGMA synchronous=NORMAL')
t.deepEquals(db.stmt[0].action.run[0], [])

t.end()
})
Expand All @@ -92,9 +92,9 @@ module.exports.mmap = (test) => {
const db = new MockDatabase()
pragma.mmap(db)

t.equals(db.pragmas.length, 1)
t.equals(db.pragmas[0].sql, 'PRAGMA mmap_size=268435456')
t.equals(db.pragmas[0].action, 'get')
t.equals(db.stmt.length, 1)
t.equals(db.stmt[0].sql, 'PRAGMA mmap_size=268435456')
t.deepEquals(db.stmt[0].action.get[0], [])

t.end()
})
Expand Down
3 changes: 3 additions & 0 deletions sqlite/table/ancestors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports.insert = (db) => {
`)

return (feat) => {
// table does not support alt geometries
if (feature.isAltGeometry(feat)) { return }

const hierarchies = _.get(feat, 'properties.wof:hierarchy', [])

const common = {
Expand Down
145 changes: 145 additions & 0 deletions sqlite/table/ancestors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const ancestors = require('./ancestors')
const MockDatabase = require('../mock/MockDatabase')

module.exports.interface = (test) => {
test('create', (t) => {
t.true(typeof ancestors.create === 'function')
t.equals(ancestors.create.length, 1)
t.end()
})
test('insert', (t) => {
t.true(typeof ancestors.insert === 'function')
t.equals(ancestors.insert.length, 1)
t.end()
})
test('drop', (t) => {
t.true(typeof ancestors.drop === 'function')
t.equals(ancestors.drop.length, 1)
t.end()
})
test('createIndices', (t) => {
t.true(typeof ancestors.createIndices === 'function')
t.equals(ancestors.createIndices.length, 1)
t.end()
})
}

module.exports.create = (test) => {
test('create', (t) => {
const db = new MockDatabase()
ancestors.create(db)

t.equals(db.stmt.length, 1)
t.true(db.stmt[0].sql.includes('CREATE TABLE IF NOT EXISTS ancestors'))
t.deepEquals(db.stmt[0].action.run[0], [])

t.end()
})
}

module.exports.insert = (test) => {
test('insert', (t) => {
const db = new MockDatabase()
const insert = ancestors.insert(db)

t.equals(typeof insert, 'function')
t.equals(db.stmt.length, 1)
t.true(db.stmt[0].sql.includes('INSERT OR IGNORE INTO ancestors'))

t.end()
})
test('insert - feature', (t) => {
const db = new MockDatabase()
const insert = ancestors.insert(db)

insert({
type: 'Feature',
properties: {
'wof:hierarchy': [
{
'continent_id': 102191581,
'country_id': 85632343,
'locality_id': 101851343,
'region_id': 85667945
}
],
},
geometry: {
type: 'Point',
coordinates: [0, 0]
}
})

t.deepEquals(db.stmt[0].action.run[0], [{
ancestor_id: 102191581,
ancestor_placetype: 'continent',
id: -1,
lastmodified: -1
}])

t.deepEquals(db.stmt[0].action.run[1], [{
ancestor_id: 85632343,
ancestor_placetype: 'country',
id: -1,
lastmodified: -1
}])

t.deepEquals(db.stmt[0].action.run[2], [{
ancestor_id: 101851343,
ancestor_placetype: 'locality',
id: -1,
lastmodified: -1
}])

t.deepEquals(db.stmt[0].action.run[3], [{
ancestor_id: 85667945,
ancestor_placetype: 'region',
id: -1,
lastmodified: -1
}])

t.end()
})
test('insert - do not insert alt geometries', (t) => {
const db = new MockDatabase()
const insert = ancestors.insert(db)

insert({
properties: {
'src:alt_label': 'test'
}
})

t.false(db.stmt[0].action.run.length)

t.end()
})
}

module.exports.drop = (test) => {
test('drop', (t) => {
const db = new MockDatabase()
ancestors.drop(db)

t.equals(db.stmt.length, 1)
t.true(db.stmt[0].sql.includes('DROP TABLE IF EXISTS ancestors'))
t.deepEquals(db.stmt[0].action.run[0], [])

t.end()
})
}

module.exports.createIndices = (test) => {
test('createIndices', (t) => {
const db = new MockDatabase()
ancestors.createIndices(db)

t.equals(db.stmt.length, 3)
db.stmt.forEach(stmt => {
t.true(stmt.sql.includes('CREATE INDEX IF NOT EXISTS ancestors_'))
t.deepEquals(stmt.action.run[0], [])
})

t.end()
})
}
3 changes: 3 additions & 0 deletions sqlite/table/concordances.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports.insert = (db) => {
`)

return (feat) => {
// table does not support alt geometries
if (feature.isAltGeometry(feat)) { return }

const concordances = _.get(feat, 'properties.wof:concordances', [])

const common = {
Expand Down
Loading

0 comments on commit e73baaa

Please sign in to comment.