spec/spec_helper.rb
changeset 0 3cc90e88c6ab
equal deleted inserted replaced
-1:000000000000 0:3cc90e88c6ab
       
     1 #!/usr/bin/ruby
       
     2 # coding: utf-8
       
     3 
       
     4 BEGIN {
       
     5 	require 'pathname'
       
     6 	basedir = Pathname( __FILE__ ).dirname.parent
       
     7 
       
     8 	thingfishdir = basedir.parent + 'Thingfish'
       
     9 	thingfishlib = thingfishdir + 'lib'
       
    10 
       
    11 	$LOAD_PATH.unshift( thingfishlib.to_s ) if thingfishlib.exist?
       
    12 }
       
    13 
       
    14 
       
    15 # SimpleCov test coverage reporting; enable this using the :coverage rake task
       
    16 require 'simplecov' if ENV['COVERAGE']
       
    17 
       
    18 require 'loggability'
       
    19 require 'loggability/spechelpers'
       
    20 require 'configurability'
       
    21 require 'configurability/behavior'
       
    22 
       
    23 require 'rspec'
       
    24 require 'thingfish'
       
    25 require 'thingfish/spechelpers'
       
    26 require 'thingfish/behaviors'
       
    27 require 'thingfish/metastore'
       
    28 
       
    29 Loggability.format_with( :color ) if $stdout.tty?
       
    30 
       
    31 # Some helper functions for testing. Usage:
       
    32 #
       
    33 #    # in spec/spec_helper.rb
       
    34 #    RSpec.configure do |c|
       
    35 #      c.include( Thingfish::Metastore::PgGraph::SpecHelpers )
       
    36 #    end
       
    37 #
       
    38 #    # in my_class_spec.rb; mark an example as needing database setup
       
    39 #    describe MyClass, db: true do
       
    40 #    end
       
    41 #
       
    42 module Thingfish::MetastorePGSpecHelpers
       
    43 
       
    44 	TESTDB_ENV_VAR = 'THINGFISH_DB_URI'
       
    45 
       
    46 	### Inclusion callback -- install some hooks
       
    47 	def self::included( context )
       
    48 
       
    49 		context.before( :all ) do
       
    50 			if ((db_uri = ENV[ TESTDB_ENV_VAR ]))
       
    51 				Thingfish::Metastore::PgGraph.configure( uri: db_uri )
       
    52 			end
       
    53 		end
       
    54 
       
    55 		context.after( :all ) do
       
    56 			Thingfish::Metastore::PgGraph.teardown_database if Thingfish::Metastore::PgGraph.db
       
    57 		end
       
    58 
       
    59 		context.around( :each ) do |example|
       
    60 			if (( setting = example.metadata[:db] ))
       
    61 				Loggability[ Thingfish::Metastore::PgGraph ].debug "DB setting: %p" % [ setting ]
       
    62 
       
    63 				if ((db = Thingfish::Metastore::PgGraph.db))
       
    64 					if setting == :no_transaction || setting == :without_transaction
       
    65 						Loggability[ Thingfish::Metastore::PgGraph ].debug "  running without a transaction"
       
    66 						example.run
       
    67 					else
       
    68 						Loggability[ Thingfish::Metastore::PgGraph ].debug "  running with a transaction"
       
    69 						db.transaction( rollback: :always ) do
       
    70 							example.run
       
    71 						end
       
    72 					end
       
    73 				elsif setting.to_s == 'pending'
       
    74 					example.metadata[:pending] ||=
       
    75 						"a configured database URI in #{TESTDB_ENV_VAR}"
       
    76 				else
       
    77 					fail "No database connection! " +
       
    78 					     "Ensure you have the #{TESTDB_ENV_VAR} ENV variable set to " +
       
    79 						 "the URI of an (empty) test database you have write permissions to."
       
    80 				end
       
    81 			else
       
    82 				example.run
       
    83 			end
       
    84 		end
       
    85 
       
    86 		super
       
    87 	end
       
    88 
       
    89 end # module Thingfish::Metastore::PgGraph::SpecHelpers
       
    90 
       
    91 
       
    92 ### Mock with RSpec
       
    93 RSpec.configure do |c|
       
    94 	include Thingfish::SpecHelpers
       
    95 	include Thingfish::SpecHelpers::Constants
       
    96 
       
    97 	c.run_all_when_everything_filtered = true
       
    98 	c.filter_run :focus
       
    99 	# c.order = 'random'
       
   100 	c.mock_with( :rspec ) do |mock|
       
   101 		mock.syntax = :expect
       
   102 	end
       
   103 
       
   104 	c.include( Loggability::SpecHelpers )
       
   105 	c.include( Thingfish::SpecHelpers )
       
   106 	c.include( Thingfish::MetastorePGSpecHelpers )
       
   107 end
       
   108 
       
   109 # vim: set nosta noet ts=4 sw=4:
       
   110