Class: ScenarioDB

Inherits:
Object
  • Object
show all
Defined in:
lib/scenarios/scenario_db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ScenarioDB

Returns a new instance of ScenarioDB.



10
11
12
13
14
15
16
17
18
# File 'lib/scenarios/scenario_db.rb', line 10

def initialize(options={})
  if options[:db_file]
    self.db_file = options[:db_file]
  else
    self.db_file = File.dirname(File.expand_path(__FILE__)) + '/data/scenario_db.sqlite3'
  end

  handle_cli_options(options)
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



8
9
10
# File 'lib/scenarios/scenario_db.rb', line 8

def db
  @db
end

#db_fileObject

Returns the value of attribute db_file.



8
9
10
# File 'lib/scenarios/scenario_db.rb', line 8

def db_file
  @db_file
end

#routesObject

Returns the value of attribute routes.



8
9
10
# File 'lib/scenarios/scenario_db.rb', line 8

def routes
  @routes
end

#scenariosObject

Returns the value of attribute scenarios.



8
9
10
# File 'lib/scenarios/scenario_db.rb', line 8

def scenarios
  @scenarios
end

#testdataObject

Returns the value of attribute testdata.



8
9
10
# File 'lib/scenarios/scenario_db.rb', line 8

def testdata
  @testdata
end

Instance Method Details

#add_route_for_scenario(route_type, path, status_code, headers, fixture, scenario_id) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/scenarios/scenario_db.rb', line 222

def add_route_for_scenario(route_type, path, status_code, headers, fixture, scenario_id)
now = DateTime.now
  if self.routes.where(:scenario_id => scenario_id, :route_type=> route_type, :path=> path).to_a.count == 0
    self.routes.insert(:scenario_id => scenario_id, :route_type=> route_type, :path => path, :status_code => status_code, :headers => headers, :fixture => fixture, :created_at => now, :updated_at => now)
  end
#    puts "<scenario_id: #{scenario_id} #{}route_type} #Path: #{path} fixture: #{fixture}>"
end

#add_scenario(name) ⇒ Object



166
167
168
169
170
171
# File 'lib/scenarios/scenario_db.rb', line 166

def add_scenario(name)
  if self.scenarios.where(:name => name).to_a.count == 0
    now = DateTime.now
    self.scenarios.insert(:name => name, :created_at => now, :updated_at => now)
  end
end

#add_testdata_for_scenario(name, value, scenario_id) ⇒ Object



270
271
272
273
274
# File 'lib/scenarios/scenario_db.rb', line 270

def add_testdata_for_scenario(name, value, scenario_id)
  now = DateTime.now
  self.testdata.insert(:name=>name, :value=>value,  :scenario_id => scenario_id, :created_at => now, :updated_at => now)
#    puts "<#Name: #{name} value: #{value} scenario_id: #{scenario_id}>"
end

#close_databaseObject



147
148
149
# File 'lib/scenarios/scenario_db.rb', line 147

def close_database
    self.db.disconnect
end

#configure_databaseObject



124
125
126
127
128
129
130
# File 'lib/scenarios/scenario_db.rb', line 124

def configure_database
  setup_database
  open_database
  create_scenarios_table
  create_routes_table
  create_testdata_table
end

#create_routes_tableObject

routes



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/scenarios/scenario_db.rb', line 204

def create_routes_table
  # Create the routes table
  if !self.db.table_exists?(:routes)
#      puts "Creating the routes table"
    self.db.create_table :routes do
      primary_key :id
      foreign_key :scenario_id, :scenarios, :on_delete => :cascade, :on_update => :cascade
      String :route_type
      String :path
    Integer :status_code
    String :headers
      String :fixture
      DateTime :created_at
      DateTime :updated_at
    end
  end
end

#create_scenarios_tableObject

scenarios



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/scenarios/scenario_db.rb', line 153

def create_scenarios_table
  # Create Scenarios table
  if !self.db.table_exists?(:scenarios)
#      puts "Creating the scenarios table"
    self.db.create_table :scenarios do
      primary_key :id
      String :name
      DateTime :created_at
      DateTime :updated_at
    end
  end
end

#create_testdata_tableObject

testdata



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/scenarios/scenario_db.rb', line 255

def create_testdata_table
  # Create the testdata table
  if !self.db.table_exists?(:testdata)
#      puts 'Creating the testdata table'
    self.db.create_table :testdata do
      primary_key :id
      foreign_key :scenario_id, :scenarios, :on_delete => :cascade, :on_update => :cascade
      String :name
      String :value
      DateTime :created_at
      DateTime :updated_at
    end
  end
end

#delete_route(route_id) ⇒ Object



230
231
232
# File 'lib/scenarios/scenario_db.rb', line 230

def delete_route(route_id)
  self.routes.filter(:id => route_id).delete
end

#delete_scenario(scenario_id) ⇒ Object



173
174
175
# File 'lib/scenarios/scenario_db.rb', line 173

def delete_scenario(scenario_id)
  self.scenarios.filter(:id => scenario_id).delete
end

#delete_scenario_for_id(scenario_id) ⇒ Object



190
191
192
# File 'lib/scenarios/scenario_db.rb', line 190

def delete_scenario_for_id(scenario_id)
  self.scenarios.filter(:id => scenario_id).delete
end

#delete_testdata_for_scenario(testdata_id) ⇒ Object



276
277
278
# File 'lib/scenarios/scenario_db.rb', line 276

def delete_testdata_for_scenario(testdata_id)
  self.testdata.filter(:id=>testdata_id).delete
end

#get_fixture_from_routes(route_type, path, scenario_name) ⇒ Object



238
239
240
241
242
# File 'lib/scenarios/scenario_db.rb', line 238

def get_fixture_from_routes(route_type, path, scenario_name)
  self.routes.left_outer_join(:scenarios, 
                            :id=>:scenario_id).where{Sequel.&({:route_type => route_type, :name=>scenario_name}, Sequel.ilike(:path,path))}.map([:status_code,:headers,:fixture]).first

end

#get_last_routes_idObject



248
249
250
# File 'lib/scenarios/scenario_db.rb', line 248

def get_last_routes_id
  return self.routes.reverse_order(:id).first[:id]
end

#get_last_scenario_idObject



198
199
200
# File 'lib/scenarios/scenario_db.rb', line 198

def get_last_scenario_id
  return self.scenarios.reverse_order(:id).first[:id]
end

#get_last_testdata_idObject



280
281
282
# File 'lib/scenarios/scenario_db.rb', line 280

def get_last_testdata_id
  return self.testdata.reverse_order(:id).first[:id]
end

#get_ordered_scenariosObject



182
183
184
# File 'lib/scenarios/scenario_db.rb', line 182

def get_ordered_scenarios
  return self.scenarios.order(:name).select(:id, :name).to_a
end

#get_route(route_id) ⇒ Object



244
245
246
# File 'lib/scenarios/scenario_db.rb', line 244

def get_route(route_id)
  return self.routes.where(:id => route_id).first
end

#get_routes_for_scenario(scenario_id) ⇒ Object



234
235
236
# File 'lib/scenarios/scenario_db.rb', line 234

def get_routes_for_scenario(scenario_id)
  return self.routes.filter(:scenario_id=>scenario_id).select(:id, :scenario_id, :route_type, :path, :status_code, :headers, :fixture).to_a
end

#get_scenario_for_id(scenario_id) ⇒ Object



194
195
196
# File 'lib/scenarios/scenario_db.rb', line 194

def get_scenario_for_id(scenario_id)
  return self.scenarios.where(:id => scenario_id).first
end

#get_scenario_namesObject



186
187
188
# File 'lib/scenarios/scenario_db.rb', line 186

def get_scenario_names
  return self.scenarios.map(:name)
end

#handle_cli_options(options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/scenarios/scenario_db.rb', line 20

def handle_cli_options(options)
  if options[:setup]

    configure_database
    puts 'database setup complete.'
  end

  if !options[:add_scenario].nil? && options[:add_scenario].length == 1
    configure_database

    add_scenario(options[:add_scenario][0])
    if self.scenarios.count > 0
      Sequel::PrettyTable.print(self.scenarios)
    else
      puts 'No records to display.'
    end
  end

  if !options[:delete_scenario].nil?
    configure_database

    delete_scenario(options[:delete_scenario])
    if self.db[:scenarios].count > 0
      Sequel::PrettyTable.print(self.db[:scenarios])
    else
      puts 'No records to display.'
    end
  end

  if !options[:add_route].nil? && options[:add_route].length == 6
    configure_database

    add_route_for_scenario(options[:add_route][0], options[:add_route][1], options[:add_route][2], options[:add_route][3], options[:add_route][4], options[:add_route][5])
    if self.routes.count > 0
      Sequel::PrettyTable.print(self.routes)
    else
      puts 'No records to display.'
    end
  end

  if !options[:delete_route].nil?
    configure_database

    delete_route(options[:delete_route])
    if self.db[:routes].count > 0
      Sequel::PrettyTable.print(self.routes)
    else
      puts 'No records to display.'
    end
  end

  if !options[:add_testdata].nil? && options[:add_testdata].length == 3
    configure_database

    add_testdata_for_scenario(options[:add_testdata][0], options[:add_testdata][1], options[:add_testdata][2])
    if self.testdata.count > 0
      Sequel::PrettyTable.print(self.testdata)
    else
      puts 'No records to display.'
    end
  end

  if !options[:delete_testdata].nil?
    configure_database

    delete_route(options[:delete_testdata])
    if self.testdata.count > 0
      Sequel::PrettyTable.print(self.testdata)
    else
      puts 'No records to display.'
    end
  end

  if options[:scenarios]
    configure_database

    if self.scenarios.count > 0
      Sequel::PrettyTable.print(self.scenarios)
    else
      puts 'No records to display.'
    end
  end

  if options[:routes]
    configure_database

    if self.routes.count > 0
      Sequel::PrettyTable.print(self.routes)
    else
      puts 'No records to display.'
    end
  end

  if options[:testdata]
    configure_database

    if self.testdata.count > 0
      Sequel::PrettyTable.print(self.testdata)
    else
      puts 'No records to display.'
    end
  end
end

#open_databaseObject



139
140
141
142
143
144
145
# File 'lib/scenarios/scenario_db.rb', line 139

def open_database
  # Open the database
  self.db ||= Sequel.sqlite(self.db_file)
  self.scenarios ||= self.db[:scenarios]
  self.routes ||= self.db[:routes]
  self.testdata ||= self.db[:testdata]
end

#reset_scenariosObject



177
178
179
180
# File 'lib/scenarios/scenario_db.rb', line 177

def reset_scenarios
  self.scenarios.delete
  create_scenarios_table
end

#setup_databaseObject



132
133
134
135
136
137
# File 'lib/scenarios/scenario_db.rb', line 132

def setup_database
  # Create an empty database file
  if !File.exists?(self.db_file)
    File.open(self.db_file, 'w'){}
  end
end