require 'rubygems'
gem 'rspec', '>=1.1.3'
require 'spec'
require 'pathname'

SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
require SPEC_ROOT.parent + 'lib/dm-core'

# Load the various helpers for the spec suite
Dir[DataMapper.root / 'spec' / 'lib' / '*.rb'].each do |file|
  require file
end

# setup mock adapters
[ :default, :mock, :legacy, :west_coast, :east_coast ].each do |repository_name|
  DataMapper.setup(repository_name, "mock://localhost/#{repository_name}")
end

# These environment variables will override the default connection string:
#   MYSQL_SPEC_URI
#   POSTGRES_SPEC_URI
#   SQLITE3_SPEC_URI
#
# For example, in the bash shell, you might use:
#   export MYSQL_SPEC_URI="mysql://localhost/dm_core_test?socket=/opt/local/var/run/mysql5/mysqld.sock"
#
def setup_adapter(name, default_uri)
  begin
    DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
    Object.const_set('ADAPTER', ENV['ADAPTER'].to_sym) if name.to_s == ENV['ADAPTER']
    true
  rescue Exception => e
    if name.to_s == ENV['ADAPTER']
      Object.const_set('ADAPTER', nil)
      warn "Could not load #{name} adapter: #{e}"
    end
    false
  end
end

ENV['ADAPTER'] ||= 'sqlite3'

HAS_SQLITE3  = setup_adapter(:sqlite3,  'sqlite3::memory:')
HAS_MYSQL    = setup_adapter(:mysql,    'mysql://localhost/dm_core_test')
HAS_POSTGRES = setup_adapter(:postgres, 'postgres://postgres@localhost/dm_core_test')

DataMapper::Logger.new(nil, :debug)

# ----------------------------------------------------------------------
# --- Do not declare new models unless absolutely necessary. Instead ---
# --- pick a metaphor and use those models. If you do need new       ---
# --- models, define them according to the metaphor being used.      ---
# ----------------------------------------------------------------------

Spec::Runner.configure do |config|
  config.before(:each) do
    # load_models_for_metaphor :vehicles
  end
end

# ----------------------------------------------------------------------
# --- All these models are going to be removed. Don't use them!!!    ---
# ----------------------------------------------------------------------

class Article
  include DataMapper::Resource

  property :id,         Serial
  property :blog_id,    Integer
  property :created_at, DateTime
  property :author,     String
  property :title,      String
end

class Comment
  include DataMapper::Resource

  property :id,         Serial # blah
end

class NormalClass
  # should not include DataMapper::Resource
end
