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