Class: ActsAsQA::Generate
- Inherits:
-
Object
- Object
- ActsAsQA::Generate
- Defined in:
- lib/acts_as_qa/acts_as_qa.rb
Class Method Summary collapse
-
.datatypes ⇒ Object
Different datatypes which can be used as value for a parameter.
-
.evaluate_object(params) ⇒ Object
Evaluates the object.
- .generate_if_active_record(path_list, repeat, parameters, count, k, v, value) ⇒ Object
-
.generate_if_array(path_list, repeat, k, v, parameters) ⇒ Object
If parameter is an array then it will use one by one the value of array as parameter value.
- .generate_if_datatype(path_list, repeat, parameters, done, count, k, v) ⇒ Object
-
.generate_random(path_list, repeat, parameters, count, done, k, v) ⇒ Object
Generates more random data based on the value of ‘repeat’.
-
.multiply_all(count, path_list, parameters, repeat) ⇒ Object
Generates different values for parameters.
- .multiply_if_symbol(count, path_list, parameters, repeat, k, v, flag, done) ⇒ Object
-
.parameters_multiply(path_list, parameters, repeat) ⇒ Object
Generates a list of actions with different combinations of values for parameters.
-
.qa(params, repeat) ⇒ Object
Creates a list of parameters.
-
.random_value(data_type) ⇒ Object
Generates random values based on datatype/.
Class Method Details
.datatypes ⇒ Object
Different datatypes which can be used as value for a parameter.
101 102 103 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 101 def self.datatypes return [:boolean, :datetime, :decimal, :float, :integer, :string, :nil, :blank] end |
.evaluate_object(params) ⇒ Object
Evaluates the object. So if it’s written ‘:object => :user’ then it will find a random user and replace object with its attributes. This way final result will be :id => 1, :name => ‘Bob’ …
91 92 93 94 95 96 97 98 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 91 def self.evaluate_object(params) if params[:object] value = eval(params[:object].to_s.capitalize) params.merge!(value.all.rand.attributes) params.delete(:object) end params.each{|k, v| evaluate_object(v) if v.instance_of?(Hash) } end |
.generate_if_active_record(path_list, repeat, parameters, count, k, v, value) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 55 def self.generate_if_active_record(path_list, repeat, parameters, count, k, v, value) list_value = parameters.dup list_value[:parameters] = parameters[:parameters].dup value = eval(value) list_value[:parameters][k]=(eval("#{value}.all.map(&:#{k.to_s})").rand rescue eval("#{value}.all.map(&:id)").rand) parameters_multiply(path_list, list_value, repeat) count+1 end |
.generate_if_array(path_list, repeat, k, v, parameters) ⇒ Object
If parameter is an array then it will use one by one the value of array as parameter value.
79 80 81 82 83 84 85 86 87 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 79 def self.generate_if_array(path_list, repeat, k, v ,parameters) v.each do |vc| list_value = parameters.dup list_value[:parameters] = parameters[:parameters].dup list_value[:parameters][k] = parameters[:parameters][k].dup list_value[:parameters][k]=vc ? vc.to_s : nil parameters_multiply(path_list, list_value, repeat) end end |
.generate_if_datatype(path_list, repeat, parameters, done, count, k, v) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 47 def self.generate_if_datatype(path_list, repeat, parameters, done, count, k, v) list_value = parameters.dup list_value[:parameters] = parameters[:parameters].dup list_value[:parameters][k]=random_value(v) parameters_multiply(path_list, list_value, repeat) [done+[k], count+1] end |
.generate_random(path_list, repeat, parameters, count, done, k, v) ⇒ Object
Generates more random data based on the value of ‘repeat’.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 65 def self.generate_random(path_list, repeat, parameters,count, done, k, v) (repeat-count).times do dt = (self.datatypes-done).rand done << dt list_value = parameters.dup list_value[:parameters] = parameters[:parameters].dup list_value[:parameters][k]=random_value(dt) count+=1 parameters_multiply(path_list, list_value, repeat) end [done, count] end |
.multiply_all(count, path_list, parameters, repeat) ⇒ Object
Generates different values for parameters. If a parameter is a symbol or an array then it will generate different values for parameters otherwise it will take the value of parameter as it is.
27 28 29 30 31 32 33 34 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 27 def self.multiply_all(count, path_list, parameters, repeat) done, flag = [], 0 Hash[[parameters[:parameters].select{|k, v| v.class==Symbol or v.class==Array}.first]].each do |k, v| flag=1 v.class==Symbol ? self.multiply_if_symbol(count, path_list, parameters, repeat, k, v, flag, done) : self.generate_if_array(path_list, repeat, k,v,parameters) end flag end |
.multiply_if_symbol(count, path_list, parameters, repeat, k, v, flag, done) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 36 def self.multiply_if_symbol(count, path_list, parameters, repeat, k, v, flag, done) value = v.to_s.split('_').collect{|x| x.capitalize!}.join('') # Check if the parameter is a model name or a datatype and generates the random value. if self.datatypes.include?(v) done, count = self.generate_if_datatype(path_list, repeat, parameters, done, count, k, v) elsif (eval(value).descends_from_active_record? rescue false) count = self.generate_if_active_record(path_list, repeat, parameters, count, k, v, value) end done, count = self.generate_random(path_list, repeat, parameters,count, done, k, v) end |
.parameters_multiply(path_list, parameters, repeat) ⇒ Object
Generates a list of actions with different combinations of values for parameters.
17 18 19 20 21 22 23 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 17 def self.parameters_multiply(path_list, parameters, repeat) count=0 evaluate_object(parameters[:parameters]) # Generates 'repeat' no of values for each variable parameters. So there will be '(repeat)^(no of variable parameters)' no of combinations. flag = self.multiply_all(count, path_list, parameters, repeat) unless count >= repeat path_list << parameters if flag==0 end |
.qa(params, repeat) ⇒ Object
Creates a list of parameters
9 10 11 12 13 14 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 9 def self.qa(params, repeat) parameters, path_list = {}, [] params.each{|k, v| parameters[k.to_sym] = v} parameters_multiply(path_list, parameters, repeat||1) #By default repeat is 1. path_list end |
.random_value(data_type) ⇒ Object
Generates random values based on datatype/
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/acts_as_qa/acts_as_qa.rb', line 106 def self.random_value(data_type) case data_type when :boolean Random.boolean when :date Random.date when :decimal Random.number(100000) when :integer Random.number(100000) when :float rand when :string Random.alphanumeric when :nil nil when :blank "" end end |