Class: Og::MockDatabase

Inherits:
FlexMock
  • Object
show all
Includes:
Enchant
Defined in:
lib/og/mock.rb

Overview

A utility object to Mock a Database in test units. Extends the standard FlexMock object. – TODO: Factor out common functionality with Database to avoid code duplication.

Defined Under Namespace

Classes: ManagedClassMeta

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enchant

#enchant

Constructor Details

#initializeMockDatabase

Initialize the database interface.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/og/mock.rb', line 50

def initialize
	# Initialize FlexMock
	super
	
	@managed_classes = N::SafeHash.new
	
	Logger.info "Using mock database."
	
	if Og.auto_manage_classes
		# automatically manage classes with properties and metadata.
		# gmosx: Any idea how to optimize this?

		classes_to_manage = []
		ObjectSpace.each_object(Class) do |c|
			if c.respond_to?(:__props) and c.__props
				classes_to_manage << c
			end
		end
		Logger.info "Og auto manages the following classes:"
		Logger.info "#{classes_to_manage.inspect}"
		manage_classes(*classes_to_manage)
	end

	# use the newly created database.

	Og.use(self)
end

Instance Attribute Details

#configObject

hash of configuration options.



38
39
40
# File 'lib/og/mock.rb', line 38

def config
  @config
end

#connection_poolObject

Pool of connections to the backend.



42
43
44
# File 'lib/og/mock.rb', line 42

def connection_pool
  @connection_pool
end

#managed_classesObject

Managed classes.



46
47
48
# File 'lib/og/mock.rb', line 46

def managed_classes
  @managed_classes
end

Class Method Details

.create_db!(config) ⇒ Object



156
157
158
# File 'lib/og/mock.rb', line 156

def self.create_db!(config)
	# nop
end

.drop_db!(config) ⇒ Object



160
161
162
# File 'lib/og/mock.rb', line 160

def self.drop_db!(config)
	# nop
end

.wrap_method(method, args) ⇒ Object

Automatically wrap connection methods.



147
148
149
150
151
152
153
154
# File 'lib/og/mock.rb', line 147

def self.wrap_method(method, args)
	args = args.split(/,/)
	class_eval %{
		def #{method}(#{args.join(", ")})
			# nop
		end
	}
end

Instance Method Details

#convert(klass) ⇒ Object

Add standard og functionality to the class



134
135
136
137
138
139
140
141
142
143
# File 'lib/og/mock.rb', line 134

def convert(klass)
	klass.class_eval %{
		DBTABLE = "#{Og::Backend.table(klass)}"
		DBSEQ = "#{Og::Backend.table(klass)}_oids_seq" 
		
		def to_i()
			@oid
		end			
	}
end

#get_connectionObject Also known as: connection

Get a connection from the pool to access the database. Stores the connection in a thread-local variable.



87
88
89
# File 'lib/og/mock.rb', line 87

def get_connection
	# nop
end

#manage(klass) ⇒ Object

Register a standard Ruby class as managed.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/og/mock.rb', line 100

def manage(klass)
	return if managed?(klass) or klass.ancestors.include?(Og::Unmanageable)
	
	@managed_classes[klass] = ManagedClassMeta.new(klass)
	
	# Add standard og methods to the class.
	convert(klass)
	
	# Add helper methods to the class.
	enchant(klass) if Og.enchant_managed_classes 
end

#manage_classes(*klasses) ⇒ Object

Helper method to set multiple managed classes.



114
115
116
117
118
# File 'lib/og/mock.rb', line 114

def manage_classes(*klasses)
	for klass in klasses
		manage(klass)
	end
end

#managed?(klass) ⇒ Boolean

Is this class managed?

Returns:

  • (Boolean)


128
129
130
# File 'lib/og/mock.rb', line 128

def managed?(klass)
	return @managed_classes.include?(klass)
end

#put_connectionObject

Restore an unused connection to the pool.



94
95
96
# File 'lib/og/mock.rb', line 94

def put_connection
	# nop
end

#shutdownObject Also known as: close

Shutdown the database interface.



80
81
# File 'lib/og/mock.rb', line 80

def shutdown
end

#unmanage(klass) ⇒ Object

Stop managing a Ruby class



122
123
124
# File 'lib/og/mock.rb', line 122

def unmanage(klass)
	@managed_classes.delete(klass)
end