Esempio n. 1
0
 manager.writeFileAsString = function(filepath, string, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.writeFile(fullpath, string, 'utf8', callback)
   })
   .end(callback)
 }
Esempio n. 2
0
 manager.readdir = function(filepath, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.readdir(fullpath, callback)
   })
   .end(callback)
 }
Esempio n. 3
0
 it('should unsubscribe the device from all channel', function(done) {
   txain(function(callback) {
     request(app)
       .post('/push/unsubscribe-all')
       .end(function(err, res) {
         assert.ifError(err)
         assert.equal(res.status, 200, res.text)
         assert.ok(res.body)
         assert.equal(res.body.status, 'Success')
         callback()
       })
   })
   .then(function(callback) {
     request(app)
       .post('/push/subscribed-channels')
       .end(function(err, res) {
         assert.ifError(err)
         assert.equal(res.status, 200, res.text)
         assert.ok(res.body)
         assert.equal(res.body.status, 'Success')
         assert.ok(res.body.channels)
         assert.ok(_.isEqual(res.body.channels, []))
         callback()
       })
   })
   .end(done)
 })
Esempio n. 4
0
 manager.writeFileAsJSON = function(filepath, data, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.writeFile(fullpath, JSON.stringify(data, null, 2), 'utf8', callback)
   })
   .end(callback)
 }
Esempio n. 5
0
exports.runAndCompare = function(commands1, commands2, expected, callback) {
  var arr = []

  dbdiff.logger = function(msg) {
    if (msg) {
      arr.push(msg)
    }
  }

  txain(function(callback) {
    exports.runCommands(commands1, commands2, callback)
  })
  .then(function(callback) {
    dbdiff.compareDatabases(conString1, conString2, callback)
  })
  .then(function(callback) {
    // run the expected commands
    client1.query(arr.join('\n'), callback)
  })
  .then(function(callback) {
    assert.deepEqual(arr, expected)
    // compare again the dbs
    arr.splice(0)
    dbdiff.compareDatabases(conString1, conString2, callback)
  })
  .then(function(callback) {
    assert.deepEqual(arr, [])
    callback(null, arr)
  })
  .end(callback)
}
Esempio n. 6
0
 function runJoins(conn, entity, queries, normalized, count, callback) {
   if (normalized.ids.length === 0) {
     return callback(null, normalized.ids, normalized.objects, count)
   }
   txain(queries.joins)
   .each(function(join, callback) {
     txain(function(callback) {
       var sql = []
       var allParams = []
       var params = join.params
       normalized.ids.forEach(function(id) {
         sql.push('('+join.sql+')')
         for (var i = 0; i < params.length; i++) {
           allParams.push(params[i] === null ? id : params[i])
         }
       })
       conn.query(sql.join(' UNION '), allParams, callback)
     })
     .then(function(result, callback) {
       normalized = normalizeObjects(entity, result, normalized.objects, normalized.ids)
       callback()
     })
     .end(callback)
   })
   .end(function(err) {
     if (err) return callback(err)
     return callback(null, normalized.ids, normalized.objects, count)
   })
 }
Esempio n. 7
0
 db.queryAll = function(query, params, callback) {
   txain(connect)
     .then(function(conn, callback) {
       conn.queryAll(query, params, callback)
     })
     .end(callback)
 }
Esempio n. 8
0
 db.deleteDataFromTable = function(table, callback) {
   txain(connect)
     .then(function(conn, callback) {
       conn.query('DELETE FROM '+quote(table), callback)
     })
     .end(callback)
 }
Esempio n. 9
0
 manager.removeFile = function(filepath, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.unlink(fullpath, callback)
   })
   .end(callback)
 }
Esempio n. 10
0
 manager.stat = function(filepath, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.stat(fullpath, callback)
   })
   .end(callback)
 }
Esempio n. 11
0
        .then(function(expected, callback) {
          var current = this.get('current')
          var conn = this.get('conn')

          txain(expected)
            .each(compareTable, conn, current, commands)
            .end(callback)
        })
Esempio n. 12
0
 db.deleteData = function(callback) {
   var entities = core.model.entities
   var tables = entities.map(function(entity) {
     return entity.id
   })
   txain(tables)
   .each(db.deleteDataFromTable)
   .end(callback)
 }
Esempio n. 13
0
 before(function(done) {
   txain(function(callback) {
     utils.migrate(app, callback)
   })
   .then(function(callback) {
     utils.deleteData(app, callback)
   })
   .end(done)
 })
Esempio n. 14
0
 manager.renameFile = function(oldPath, newPath, callback) {
   txain([oldPath, newPath])
   .map(manager.resolve)
   .then(function(paths, callback) {
     console.log('paths', paths)
     fs.rename(paths[0], paths[1], callback)
   })
   .end(callback)
 }
Esempio n. 15
0
 manager.exists = function(filepath, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.exists(fullpath, function(exists) {
       return callback(null, exists)
     })
   })
   .end(callback)
 }
Esempio n. 16
0
    function abstractList(args, preProcessQuery, postProcessQuery, callback) {
      var q = args.query || ''
      var params = args.params || []
      var query
      var queries

      var entity = core.model.findEntity(args.entity)

      txain(connect)
        .then(function(conn, callback) {
          this.set('conn', conn)
          errors
            .with(callback)
            .when(!entity)
              .notFound('EntityNotFound: Entity not found `%s`', args.entity)
              .success(callback)
        })
        .then(function(callback) {
          try {
            query = bql.parse(q)
          } catch (err) {
            return callback(err)
          }
          return callback(null, query)
        })
        .then(function(query, callback) {
          var conn = this.get('conn')
          preProcessQuery(params, nook(callback,
            function(where) {
              var params2 = _.clone(params)
              queries = buildSQL(entity, query, params, where)
              var sql = queries.main
              sql = postProcessQuery ? postProcessQuery(sql) : sql
              if (args.limit) {
                sql += '\nLIMIT ? OFFSET ?'
                params.push(args.limit)
                params.push(args.offset || 0)
              }
              query.type = 'count'
              var count = buildSQL(entity, query, params2, where)
              conn.queryAll([sql, count], [params, params2], callback)
            }
          ))
        })
        .then(function(results, callback) {
          var conn = this.get('conn')
          var objectsResult = results[0]
          var countResult = results[1]
          var count = countResult[0]['COUNT(*)']
          var normalized = normalizeObjects(entity, objectsResult)

          runJoins(conn, entity, queries, normalized, count, callback)
        })
        .end(callback)
    }
Esempio n. 17
0
exports.resetDatabases = function(callback) {
  txain(function(callback) {
    exports.connect(callback)
  })
  .then(function(callback) {
    callback(null, [client1, client2])
  })
  .each(function(client, callback) {
    client.query('drop schema public cascade; create schema public;', callback)
  })
  .end(callback)
}
Esempio n. 18
0
exports.connect = function(callback) {
  if (client1) return callback()

  client1 = new pg.Client(conString1)
  client2 = new pg.Client(conString2)

  var arr = [client1, client2]
  txain(arr)
  .each(function(client, callback) {
    client.connect(callback)
  })
  .end(callback)
}
Esempio n. 19
0
 db.showTables = function(callback) {
   txain(connect)
     .then(function(conn, callback) {
       conn.showTables(callback)
     })
     .then(function(result, callback) {
       var tables = result.map(function(table) {
         return table[_.keys(table)[0]]
       })
       return callback(null, tables)
     })
     .end(callback)
 }
Esempio n. 20
0
    db.describeTable = function(table, callback) {
      txain(connect)
        .then(function(conn, callback) {
          this.set('conn', conn)
          conn.describeTable(table, callback)
        })
        .then(function(result, callback) {
          var columns = result.map(function(column) {
            var type = column.Type
            var length = column.length
            if (!length) {
              var n = type.indexOf('(')
              var m = type.indexOf(')')
              if (n > 0 && m > n) {
                length = +type.substring(n+1, m)
                type = type.substring(0, n)
              }
            }

            return {
              'name': column.Field,
              'type': type,
              'default': column.Default,
              'key': column.Key,
              'table': table,
              'length': length,
            }
          })
          this.set('columns', columns)
          callback(null, this.get('conn'))
        })
        .then(function(conn, callback) {
          conn.showIndexes(table, callback)
        })
        .then(function(result, callback) {
          var indexes = result.map(function(index) {
            return {
              'name': index.Key_name,
              'type': index.Index_type,
              'column': index.Column_name || index.Column_names[0], // TODO: multiple columns!
            }
          })

          callback(null, {
            name: table,
            columns: this.get('columns'),
            indexes: indexes,
          })
        })
        .end(callback)
    }
Esempio n. 21
0
exports.runCommands = function(commands1, commands2, callback) {
  txain(function(callback) {
    callback(null, commands1)
  })
  .each(function(command, callback) {
    client1.query(command, callback)
  })
  .then(function(callback) {
    callback(null, commands2)
  }).each(function(command, callback) {
    client2.query(command, callback)
  })
  .end(callback)
}
Esempio n. 22
0
 conn.queryAll = function(sql, params, callback) {
   var queries = []
   for (var i = 0; i < sql.length; i++) {
     queries.push({
       sql: sql[i],
       params: params[i],
     })
   }
   txain(queries)
   .map(function(query, callback) {
     conn.query(query.sql, query.params, callback)
   })
   .end(callback)
 }
Esempio n. 23
0
 it('should unsubscribe the device from one channel', function(done) {
   txain(function(callback) {
     // unsubscribe
     request(app)
       .api({
         path: '/api/push/unsubscribe',
         method: 'post',
         shared: shared,
         secret: secret,
         form: {
           'token': token,
           'gateway': gateway,
           'channels': ['foo']
         },
       })
       .end(function(err, res) {
         assert.ifError(err)
         assert.equal(res.statusCode, 200, res.text)
         assert.ok(res.body)
         assert.equal(res.body.status, 'Success')
         callback()
       })
   })
   .then(function(callback) {
     // get channels again
     request(app)
       .api({
         path: '/api/push/subscribed-channels',
         method: 'get',
         shared: shared,
         secret: secret,
         qs: {
           'token': token,
           'gateway': gateway,
         },
       })
       .end(function(err, res) {
         assert.ifError(err)
         assert.equal(res.statusCode, 200, res.text)
         assert.ok(res.body)
         assert.equal(res.body.status, 'Success')
         assert.ok(res.body.channels)
         assert.ok(_.isEqual(res.body.channels, ['bar']))
         callback()
       })
   })
   .end(done)
 })
Esempio n. 24
0
 .then(function(callback) {
   if (extra['__login_email_pending']) {
     txain(function(callback) {
       security.randomKey(callback)
     })
     .then(function(code, callback) {
       extra['__login_email_verification'] = code
       module.exports.lastEmailVerificationCode = code
       // TODO: send mail
       callback()
     })
     .end(callback)
   } else {
     return callback()
   }
 })
Esempio n. 25
0
dbdiff.compareDatabases = function(conn1, conn2, callback) {
  var db1, db2
  txain(function(callback) {
    dbdiff.describeDatabase(conn1, callback)
  })
  .then(function(db, callback) {
    db1 = db
    dbdiff.describeDatabase(conn2, callback)
  })
  .then(function(db, callback) {
    db2 = db
    dbdiff.compareSchemas(db1, db2)
    callback()
  })
  .end(callback)
}
Esempio n. 26
0
 manager.readFileAsJSON = function(filepath, callback) {
   txain(manager.resolve, filepath)
   .then(function(fullpath, callback) {
     fs.readFile(fullpath, 'utf8', callback)
   })
   .end(nook(callback,
     function(data) {
       try {
         var json = JSON.parse(data)
         return callback(null, json)
       } catch(e) {
         return callback(e)
       }
     })
   )
 }
Esempio n. 27
0
  it('should run as a cli application', function(done) {
    var conString1 = 'postgres://postgres:postgres@localhost/db1'
    var conString2 = 'postgres://postgres:postgres@localhost/db2'

    txain(function(callback) {
      utils.runCommands(['CREATE SEQUENCE seq_name'], [], callback)
    })
    .then(function(arg, callback) {
      exec('node index.js '+conString1+' '+conString2, function(err, stdout, stderr) {
        assert.ifError(err)
        assert.equal(stdout, 'DROP SEQUENCE "public"."seq_name";\n\n')
        done()
      })
    })
    .end(done)
  })
Esempio n. 28
0
    db.readObjects = function(args, callback) {
      var ids = args.ids || [args.id]
      var entity = core.model.findEntity(args.entity)
      var q = args.joins || ''
      var params = args.params || []
      var queries

      txain(connect)
        .then(function(conn, callback) {
          this.set('conn', conn)
          errors
            .with(callback)
            .when(!entity)
              .notFound('EntityNotFound: Entity not found `%s`', args.entity)
              .success(callback)
        })
        .then(function(callback) {
          var conn = this.get('conn')
          try {
            var query = bql.parse(q)
          } catch (err) {
            return callback(err)
          }
          callback(null, conn, query)
        })
        .then(function(conn, query, callback) {
          var placeholders = ids.map(function() { return '?' }).join(', ')
          query.type = 'read'
          queries = buildSQL(entity, query, params)
          var sql = queries.main
          sql += ' WHERE t._id IN('+placeholders+')'
          params = params.concat(ids) // TODO: test read + join + having
          conn.query(sql, params, callback)
        })
        .then(function(result, callback) {
          if (result.length === 0 && args.id) {
            return callback(errors.notFound('ObjectNotFound: Object with id `%s` not found', args.id))
          }
          var conn = this.get('conn')
          var normalized = normalizeObjects(entity, result)
          runJoins(conn, entity, queries, normalized, 1, callback)
        })
        .then(function(ids, objects, count, callback) {
          callback(null, objects)
        })
        .end(callback)
    }
Esempio n. 29
0
 .each(function(join, callback) {
   txain(function(callback) {
     var sql = []
     var allParams = []
     var params = join.params
     normalized.ids.forEach(function(id) {
       sql.push('('+join.sql+')')
       for (var i = 0; i < params.length; i++) {
         allParams.push(params[i] === null ? id : params[i])
       }
     })
     conn.query(sql.join(' UNION '), allParams, callback)
   })
   .then(function(result, callback) {
     normalized = normalizeObjects(entity, result, normalized.objects, normalized.ids)
     callback()
   })
   .end(callback)
 })
Esempio n. 30
0
    utils.writeTestFile(function(filename, filepath) {
      var id = null
      txain(function(callback) {
        request(app)
          .post('/query/save-file')
          .attach('picture', filepath)
          .end(function(err, res) {
            assert.ifError(err)
            assert.equal(res.status, 200, res.text)
            assert.ok(res.body)
            assert.ok(res.body.id)
            id = res.body.id
            callback()
          })
      })
      .then(function(callback) {
        request(app)
          .api({
            path: '/api/data/file/download/'+id, // no version especified
            method: 'get',
            shared: shared,
            secret: secret,
          })
          .end(function(err, res) {
            assert.ifError(err)
            assert.equal(res.statusCode, 200, res.text)

            assert.equal(res.headers['content-type'], 'image/jpeg')
            assert.equal(res.headers['cache-control'], 'public, max-age=604800')

            // TODO: test content
            callback()
          })
      })
      .end(function(err) {
        assert.ifError(err)
        done()
      })
    })