Class: PgSqlTriggers::Testing::FunctionTester
- Inherits:
-
Object
- Object
- PgSqlTriggers::Testing::FunctionTester
- Defined in:
- lib/pg_sql_triggers/testing/function_tester.rb
Constant Summary collapse
- FUNCTION_NAME_PATTERN =
/CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(/i
Instance Method Summary collapse
-
#function_exists? ⇒ Boolean
Check if function already exists in database.
-
#initialize(trigger_registry) ⇒ FunctionTester
constructor
A new instance of FunctionTester.
-
#test_function_only(test_context: {}) ⇒ Object
Test ONLY the function, not the trigger.
Constructor Details
#initialize(trigger_registry) ⇒ FunctionTester
Returns a new instance of FunctionTester.
6 7 8 |
# File 'lib/pg_sql_triggers/testing/function_tester.rb', line 6 def initialize(trigger_registry) @trigger = trigger_registry end |
Instance Method Details
#function_exists? ⇒ Boolean
Check if function already exists in database
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/pg_sql_triggers/testing/function_tester.rb', line 130 def function_exists? definition = begin JSON.parse(@trigger.definition) rescue StandardError {} end function_name = definition["function_name"] || definition["name"] || definition[:function_name] || definition[:name] return false if function_name.blank? sanitized_name = ActiveRecord::Base.connection.quote_string(function_name) sql = <<~SQL.squish SELECT COUNT(*) as count FROM pg_proc WHERE proname = '#{sanitized_name}' SQL result = ActiveRecord::Base.connection.execute(sql) result.first["count"].to_i.positive? end |
#test_function_only(test_context: {}) ⇒ Object
Test ONLY the function, not the trigger.
test_context is accepted for API compatibility with future invocation logic. It is normalised to an empty hash when nil so callers can pass either.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pg_sql_triggers/testing/function_tester.rb', line 16 def test_function_only(test_context: {}) test_context ||= {} results = { function_created: false, function_executed: false, errors: [], output: [], context: test_context } return fail_result(results, "Function body is missing") if @trigger.function_body.blank? unless extract_function_name_from_body return fail_result(results, "Function body does not contain a valid CREATE FUNCTION statement") end run_function_test_transaction(results) results[:output] << "\n⚠ Function rolled back (test mode)" results end |