Class: Stratagem::AutoMock::Aquifer

Inherits:
Object
  • Object
show all
Includes:
Singleton, Factory, UserLoader
Defined in:
lib/stratagem/auto_mock/aquifer.rb

Constant Summary

Constants included from ValueGenerator

ValueGenerator::ADDRESSES, ValueGenerator::GENERIC_NAMES, ValueGenerator::GENERIC_URLS, ValueGenerator::NUMERIC_TYPES, ValueGenerator::USERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserLoader

#load_user_from_configuration, #matching_credentials

Methods included from Factory

#add_mocked, #mock, #mock_attributes

Methods included from ValueGenerator

#generate_boolean, #generate_datetime, #generate_float, #generate_int, #generate_string, #generate_text, #generate_value

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



9
10
11
# File 'lib/stratagem/auto_mock/aquifer.rb', line 9

def application
  @application
end

Class Method Details

.init(application) ⇒ Object



11
12
13
14
15
# File 'lib/stratagem/auto_mock/aquifer.rb', line 11

def self.init(application)
  at_exit { self.instance.destroy }
  self.instance.application = application
  self.instance
end

Instance Method Details

#clearObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stratagem/auto_mock/aquifer.rb', line 17

def clear
  if (Stratagem.configuration.use_automatic_mocking)
    objects = self.repo.values.inject([]) {|memo,obj| memo += obj.compact }
    i = 0
    while (objects.size > 0 && ((i+=1) < objects.size))
      objects = objects.select do |instance|
        next if (pre_existing_object_ids[instance.class] || []).include?(instance.id)
        puts "deleting #{instance.class.name} - #{instance.id}"
        begin
          instance.destroy
        rescue
          begin
            instance.delete
          rescue
            puts "Unable to delete object: #{instance.class.name} - #{$!.message}"
            Stratagem.logger.error($!)
          end
        end
        !instance.frozen?
      end
    end
  end
  self.repo.clear
end

#destroyObject

def export

h = {}
repo.each {|model_klass,instances|
  h[model_klass.name] = instances.map {|instance|
    instance.stratagem.
    :database_attributes => instance.
  }

}

end



53
54
55
# File 'lib/stratagem/auto_mock/aquifer.rb', line 53

def destroy
  clear
end

#fill_by_automock(model_count = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/stratagem/auto_mock/aquifer.rb', line 125

def fill_by_automock(model_count=nil)
  # load the ids of existing models for each class
  application.models.each do |meta_model|
    next unless meta_model.stratagem?
    load_existing_object_ids(meta_model.klass)
  end

  # mock instances of all models
  application.models.each do |meta_model|
    mock_model(meta_model.klass, model_count) if (meta_model.stratagem?)
  end

  # determine the new ids for each model
  application.models.each do |meta_model|
    next unless meta_model.klass
    next unless meta_model.stratagem?
    
    known_mocked_instances = mocked(meta_model.klass)
    new_ids = load_instance_ids(meta_model) - (pre_existing_object_ids[meta_model.klass] || [])
    
    # limit the number of objects that the system will interact with
    new_ids = new_ids.slice(0,200)
    repo[meta_model.klass.name] = new_ids.map {|id|
      if (id.kind_of?(Fixnum))
        puts "loading instance #{id} of #{meta_model.klass.name}"
        begin
          known_mocked_instances.find {|i| i.id == id } || meta_model.klass.find(id)
        rescue
          puts "FILL ERROR: #{$!.message}"
          nil
        end
      else
        id # id is actually a model instance
      end
    }.compact
    
  end

  print
  self
end

#fill_by_configuration(credentials, model_count = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/stratagem/auto_mock/aquifer.rb', line 99

def fill_by_configuration(credentials, model_count=nil)
  objects_by_class = {}

  log "Loading users for #{credentials.size} sets of credentials"
  credentials.each do |user_credentials|
    log "\t#{user_credentials.authentication_parameters.inspect}"
    user = load_user_from_configuration(user_credentials)
    if (user)
      log "Loaded credentials for user #{user.id}: #{user.stratagem.mock_attributes.inspect}"
      user.stratagem.related_objects.slice(0,5).each do |object|
        (objects_by_class[object.class] ||= []) << object
      end
    else
      Stratagem.logger.fatal "Could not locate user with credentials: #{user_credentials.authentication_parameters.inspect}"
    end
  end
  
  log "Loaded #{objects_by_class.keys.size} object types: #{objects_by_class.keys.inspect}"

  objects_by_class.each do |klass, objects|
    puts "filling aquifer - class #{klass.name} has #{objects.size} instances"
    repo[klass.name] = objects
  end
  self
end

#instances_of(model_klass) ⇒ Object



57
58
59
60
61
# File 'lib/stratagem/auto_mock/aquifer.rb', line 57

def instances_of(model_klass)
  objects = (repo[model_klass.name] || []).clone
  # puts "found #{objects.size} instances in well"
  objects
end

#load_existing_object_ids(klass) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stratagem/auto_mock/aquifer.rb', line 74

def load_existing_object_ids(klass)
  # handle polymorphic objects (class may actually be a subclass of the klass parameter)
  klass.existing_instance_ids.each do |existing_id|
    if (existing_id.kind_of?(Fixnum))
      begin
        instance = klass.find(existing_id)
        (pre_existing_object_ids[instance.class] ||= []) << existing_id
      rescue
        puts "ERROR: instance id #{existing_id} of #{klass.name} could not be loaded - #{$!.message}"
      end
    else
      # an instance was loaded because the model has no id field
      (pre_existing_object_ids[instance.class] ||= []) << existing_id
    end
  end
end

#load_instance_ids(meta_model) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/stratagem/auto_mock/aquifer.rb', line 91

def load_instance_ids(meta_model)
  if (meta_model.stratagem?)
    meta_model.klass.existing_instance_ids
  else
    []
  end
end

#mock_model(klass, count = nil) ⇒ Object



178
179
180
181
182
183
# File 'lib/stratagem/auto_mock/aquifer.rb', line 178

def mock_model(klass, count=nil)
  count ||= 6
  count.times do |i|
    instance,valid = mock(klass)
  end
end

#pre_existing_object_idsObject



70
71
72
# File 'lib/stratagem/auto_mock/aquifer.rb', line 70

def pre_existing_object_ids
  @pre_existing_object_ids ||= {}
end


167
168
169
170
171
172
173
174
175
176
# File 'lib/stratagem/auto_mock/aquifer.rb', line 167

def print
  puts "Mocked models:"
  application.models.each do |meta_model|
    puts "#{meta_model.klass.name}"
    (repo[meta_model.klass.name] || []).each do |instance|
      puts "\t#{instance.class} - #{instance.id}"
    end
  end
  puts "end of aquifer"
end

#random_instance(model_klass) ⇒ Object



63
64
65
66
67
68
# File 'lib/stratagem/auto_mock/aquifer.rb', line 63

def random_instance(model_klass)
  objects = repo[model_klass.name]
  # puts "found #{objects.size} instances in well"
  instance = objects[rand objects.size]
  instance
end

#repoObject



185
186
187
# File 'lib/stratagem/auto_mock/aquifer.rb', line 185

def repo
  mocked
end