Class: Familia::Validation::CommandExpectations

Inherits:
Object
  • Object
show all
Defined in:
lib/familia/validation/expectations.rb

Overview

Fluent DSL for defining expected Redis command sequences with support for transaction and pipeline validation. Provides a readable way to specify expected database operations and verify they execute correctly.

Examples:

Basic command expectations

expectation = CommandExpectations.new
expectation.hset("user:123", "name", "John")
           .incr("user:counter")
           .expire("user:123", 3600)

# Validate against actual commands
result = expectation.validate(actual_commands)
result.valid? #=> true/false

Transaction expectations

expectation = CommandExpectations.new
expectation.transaction do |tx|
  tx.hset("user:123", "name", "John")
    .incr("counter")
end

Pattern matching

expectation = CommandExpectations.new
expectation.hset(any_string, "name", any_string)
           .match_pattern(/^INCR/)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandExpectations

Returns a new instance of CommandExpectations.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/familia/validation/expectations.rb', line 34

def initialize
  @expected_commands = []
  @transaction_blocks = []
  @pipeline_blocks = []
  @current_transaction = nil
  @current_pipeline = nil
  @options = {
    strict_order: true,
    exact_match: true,
    allow_extra_commands: false
  }
end

Instance Attribute Details

#expected_commandsObject (readonly)

Returns the value of attribute expected_commands.



32
33
34
# File 'lib/familia/validation/expectations.rb', line 32

def expected_commands
  @expected_commands
end

#pipeline_blocksObject (readonly)

Returns the value of attribute pipeline_blocks.



32
33
34
# File 'lib/familia/validation/expectations.rb', line 32

def pipeline_blocks
  @pipeline_blocks
end

#transaction_blocksObject (readonly)

Returns the value of attribute transaction_blocks.



32
33
34
# File 'lib/familia/validation/expectations.rb', line 32

def transaction_blocks
  @transaction_blocks
end

Instance Method Details

#allow_extra_commands(enabled = true) ⇒ Object



58
59
60
61
# File 'lib/familia/validation/expectations.rb', line 58

def allow_extra_commands(enabled = true)
  @options[:allow_extra_commands] = enabled
  self
end

#any_numberObject



124
125
126
# File 'lib/familia/validation/expectations.rb', line 124

def any_number
  ArgumentMatcher.new(:any_number)
end

#any_stringObject

Helper methods for common patterns



120
121
122
# File 'lib/familia/validation/expectations.rb', line 120

def any_string
  ArgumentMatcher.new(:any_string)
end

#any_valueObject



128
129
130
# File 'lib/familia/validation/expectations.rb', line 128

def any_value
  ArgumentMatcher.new(:any_value)
end

#command(cmd, *args) ⇒ Object

Generic command expectation



104
105
106
# File 'lib/familia/validation/expectations.rb', line 104

def command(cmd, *args)
  add_command_expectation(cmd.to_s.upcase, args)
end

#exact_match(enabled = true) ⇒ Object



53
54
55
56
# File 'lib/familia/validation/expectations.rb', line 53

def exact_match(enabled = true)
  @options[:exact_match] = enabled
  self
end

#match_pattern(pattern, description = nil) ⇒ Object

Pattern matching for flexible expectations



109
110
111
112
# File 'lib/familia/validation/expectations.rb', line 109

def match_pattern(pattern, description = nil)
  expectation = PatternExpectation.new(pattern, description)
  add_expectation(expectation)
end

#match_regex(pattern) ⇒ Object



132
133
134
# File 'lib/familia/validation/expectations.rb', line 132

def match_regex(pattern)
  ArgumentMatcher.new(:regex, pattern)
end

#pipeline(&block) ⇒ Object

Pipeline block expectation



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/familia/validation/expectations.rb', line 77

def pipeline(&block)
  @current_pipeline = PipelineExpectation.new(@options)
  @pipeline_blocks << @current_pipeline

  if block_given?
    block.call(@current_pipeline)
    @current_pipeline = nil
  end

  self
end

#strict_order(enabled = true) ⇒ Object

Configuration methods



48
49
50
51
# File 'lib/familia/validation/expectations.rb', line 48

def strict_order(enabled = true)
  @options[:strict_order] = enabled
  self
end

#transaction(&block) ⇒ Object

Transaction block expectation



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/familia/validation/expectations.rb', line 64

def transaction(&block)
  @current_transaction = TransactionExpectation.new(@options)
  @transaction_blocks << @current_transaction

  if block_given?
    block.call(@current_transaction)
    @current_transaction = nil
  end

  self
end

#validate(command_sequence) ⇒ Object

Validate against actual recorded commands



115
116
117
# File 'lib/familia/validation/expectations.rb', line 115

def validate(command_sequence)
  ValidationResult.new(self, command_sequence).validate
end