var config    = require("./config/config")
  , Sequelize = require("../index")
  , sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
  , Helpers   = new (require("./config/helpers"))(sequelize)
  , Migrator  = require("../lib/migrator")
  , _         = Sequelize.Utils._

describe('Migrator', function() {
  describe('getUndoneMigrations', function() {
    var migrator      = null
      , SequelizeMeta = null

    var setup = function(_options) {
      Helpers.async(function(done) {
        var options = Sequelize.Utils._.extend({
          path: __dirname + '/assets/migrations'
        }, _options || {})

        migrator = new Migrator(sequelize, options)
        migrator.findOrCreateSequelizeMetaModel({ force: true }).success(function(_SequelizeMeta) {
          SequelizeMeta = _SequelizeMeta
          done()
        })
      })
    }

    beforeEach(function() { migrator = null })

    // specs

    it("returns no files if timestamps are after the files timestamp", function() {
      setup({ from: 20120101010101 })

      Helpers.async(function(done) {
        migrator.getUndoneMigrations(function(migrations) {
          expect(migrations.length).toEqual(0)
          done()
        })
      })
    })

    it("returns only files between from and to", function() {
      setup({ from: 19700101000000, to: 20111117063700 })

      Helpers.async(function(done) {
        migrator.getUndoneMigrations(function(migrations) {
          expect(migrations.length).toEqual(1)
          expect(_.last(migrations).filename).toEqual('20111117063700-createPerson.js')
          done()
        })
      })
    })

    it("returns also the file which is exactly options.from or options.to", function() {
      setup({ from: 20111117063700, to: 20111123060700 })

      Helpers.async(function(done) {
        migrator.getUndoneMigrations(function(migrations) {
          expect(migrations.length).toEqual(2)
          expect(migrations[0].filename).toEqual('20111117063700-createPerson.js')
          expect(migrations[1].filename).toEqual('20111123060700-addBirthdateToPerson.js')
          done()
        })
      })
    })

    it("returns all files to options.to if no options.from is defined", function() {
      setup({ to: 20111123060700 })

      Helpers.async(function(done) {
        migrator.getUndoneMigrations(function(migrations) {
          expect(migrations.length).toEqual(2)
          done()
        })
      })
    })

    it("returns all files from last migration id stored in database", function() {
      setup()

      Helpers.async(function(done) {
        SequelizeMeta.create({ lastMigrationId: '20111117063700' }).success(function() {
          migrator.getUndoneMigrations(function(migrations) {
            expect(migrations.length).toEqual(1)
            expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
            done()
          })
        })
      })
    })
  })

  describe('migrate', function() {
    afterEach(function() {
      Helpers.async(function(done) {
        sequelize.getQueryInterface()
          .dropAllTables()
          .success(done)
          .error(function(err) { console.log(err) })
      })
    })

    xit("should execute the specified migration (and e.g. create a file)", function() {
      var migrator = new Migrator(sequelize, {
        path: __dirname + '/assets/migrations',
        from: 20111117063700,
        to: 20111117063700
      })

      Helpers.async(function(done) {
        migrator.migrate().success(done).error(function(err) { console.log(err) })
      })

      Helpers.async(function(done) {
        sequelize.getQueryInterface().showAllTables(function(tableName) {
          expect(tableNames.length).toEqual(1)
          done()
        })
      })
    })
  })
})

@@ -3239,17 +3239,16 @@
 %7D)%0A%0A    
-x
 it(%22shou
@@ -3703,16 +3703,26 @@
 lTables(
+).success(
 function
@@ -3731,16 +3731,17 @@
 ableName
+s
 ) %7B%0A    
@@ -3750,43 +3750,153 @@
     
-expect(tableNames.length).toEqual(1
+tableNames = tableNames.slice('SequelizeMeta', 1)%0A          expect(tableNames.length).toEqual(1)%0A          expect(tableNames%5B0%5D).toEqual('Person'
 )%0A  

correct spec for migration execution