require 'active_record'
require 'jam_db'
require 'spec_db'

# recreate test database and migrate it
db_config = YAML::load(File.open('config/database.yml'))["test"]

SpecDb::recreate_database()
# initialize ActiveRecord's db connection
ActiveRecord::Base.establish_connection(db_config)

jamenv = ENV['JAMENV']
jamenv ||= 'test'

require 'jam_websockets'
require 'timeout'
require 'evented-spec'


fn = "#{File.dirname(__FILE__)}/../config/application.yml"
puts "*** spec_helper.rb:  fn=#{fn}; #{File.exists?(fn)}; #{jamenv}"

ff = File.open("#{File.dirname(__FILE__)}/../config/application.yml",'r')
config = YAML::load(ff)[jamenv]
puts "*** spec_helper.rb:  jamenv=#{jamenv}; config = #{config}"

if config["verbose"]
    Logging.logger.root.level = :debug
else
    Logging.logger.root.level = :info
end

Logging.logger.root.appenders = Logging.appenders.stdout


require 'jam_ruby'
require 'jampb'
require 'rubygems'
#require 'spork'     
require 'database_cleaner'
require 'factory_girl'
require 'factories'

include JamRuby
include JamWebsockets
include Jampb


#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'


#Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
  #ENV["RAILS_ENV"] ||= 'test'
  #require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/autorun'
  #require 'rspec/rails'
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
  RSpec.configure do |config|
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.run_all_when_everything_filtered = true
    config.filter_run :focus

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation, {:except => %w[instruments genres] }
      DatabaseCleaner.clean_with(:truncation, {:except => %w[instruments genres] })
    end

    #config.after(:each) do
	#		ActiveRecord::Base.connection.execute('select truncate_tables()')
  #   # DatabaseCleaner.clean
  #  end

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    #config.use_transactional_fixtures = true

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = 'random'
  end
#end


#Spork.each_run do
  # This code will be run each time you run your specs.

#end
