adding schedule save test

pull/48/head
TimEvWw 2014-12-29 00:24:50 +01:00
parent 28fd50075c
commit eef9dda17d
18 changed files with 467 additions and 97 deletions

2
.gitignore vendored
View File

@ -41,3 +41,5 @@ snippet.coffee
snippets.rb
tmp/**/*
write_db_settings.rb
*.rb~

View File

@ -139,12 +139,6 @@ class DbAccessParameters
def read_parameter(name)
param = Parameter.find_or_create_by(name: name)
get_value_from_param(param)
#type = param.valuetype
#value = param.valueint if type == 1
#value = param.valuefloat if type == 2
#value = param.valuestring if type == 3
#value = param.valuebool if type == 4
#value
end
# read parameter

View File

@ -34,7 +34,7 @@ class MessageHandlerLog < MessageHandlerBase
{
:message_type => 'read_logs_response',
:time_stamp => Time.now.to_f.to_s,
:confirm_id => time_stamp,
:confirm_id => message.time_stamp,
:logs => log_list
}

View File

@ -23,14 +23,14 @@ class MessageHandlerParameter < MessageHandlerBase
{
:message_type => 'read_parameters_response',
:time_stamp => Time.now.to_f.to_s,
:confirm_id => time_stamp,
:parameters => param_list
:confirm_id => message.time_stamp,
:parameters => param_list
}
@dbaccess.write_to_log(2,"reply = #{return_message}")
message.handler.send_message(sender, return_message)
message.handler.send_message(message.sender, return_message)
end
@ -60,7 +60,7 @@ class MessageHandlerParameter < MessageHandlerBase
end
end
message.handler.send_confirmation(sender, time_stamp)
message.handler.send_confirmation(message.sender, message.time_stamp)
else
message.handler.send_error(sender, time_stamp, 'no paramer list in message')
end

View File

@ -44,7 +44,7 @@ class MessageHandlerSchedule < MessageHandlerBase
def save_command_line(command)
@dbaccess.add_command_line(command.action, command.x.to_i, command.y.to_i, command.z.to_i, command.speed.to_s, command.amount.to_i,
command.pin_nr.to_i, command.pin_value1.to_i, command.pin_value2.to_i, command.pin_mode.to_i, command.pin_time.to_i)
command.pin_nr.to_i, command.pin_value1.to_i, command.pin_value2.to_i, command.pin_mode.to_i, command.pin_time.to_i, command.ext_info)
end
def crop_schedule_update(message)

View File

@ -1,64 +0,0 @@
require 'spec_helper'
require './lib/messaging/messagehandler_emergencystop.rb'
require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
describe MessageHandler do
before do
#$db_write_sync = Mutex.new
#$dbaccess = DbAccess.new('development')
#@msg = MessageHandler.new
$db_write_sync = Mutex.new
$bot_dbaccess = DbAccess.new('development')
$dbaccess = $bot_dbaccess
$dbaccess.isable_log_to_screen()
$status = Status.new
$messaging = MessagingTest.new
$messaging.reset
@handler = MessageHandlerEmergencyStop.new
@main_handler = MessageHandler.new
end
## messaging
it "message handler emergency stop" do
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
# $messaging.emergency_stop(message)
@handler.emergency_stop(message)
# emergency_stop(message)
#puts $message.message
expect(1).to eq(1)
end
it "message handler emergency stop reset" do
expect(1).to eq(1)
end
# it "create new command" do
# crop_id = rand(9999999).to_i
# scheduled_time = Time.now
#
# @db.create_new_command(scheduled_time, crop_id)
#
# cmd = Command.where("scheduled_time = ?",scheduled_time).first
#
# expect(cmd.crop_id).to eq(crop_id)
# end
end

View File

@ -0,0 +1,151 @@
require 'spec_helper'
require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
#require './lib/messagehandler_schedule'
#require './lib/messagehandler_schedule_cmd_line'
#require './lib/messaging/messagehandler_logs.rb'
describe MessageHandlerSchedule do
before do
$db_write_sync = Mutex.new
$bot_dbaccess = DbAccess.new('development')
$dbaccess = $bot_dbaccess
$dbaccess.disable_log_to_screen()
$status = Status.new
$messaging = MessagingTest.new
$messaging.reset
@handler = MessageHandlerSchedule.new
@main_handler = MessageHandler.new
end
## commands / scheduling
#def single_command(message)
#def save_single_command(command, delay)
#def save_command_line(command)
#def crop_schedule_update(message)
#def save_command_with_lines(command)
it "save command line" do
# create new command data
sched_time = Time.now
crop_id = rand(9999999).to_i
action = rand(9999999).to_s
x = rand(9999999).to_i
y = rand(9999999).to_i
z = rand(9999999).to_i
speed = rand(9999999).to_s
amount = rand(9999999).to_i
pin_nr = rand(9999999).to_i
pin_value1 = rand(9999999).to_i
pin_value2 = rand(9999999).to_i
pin_mode = rand(9999999).to_i
pin_time = rand(9999999).to_i
ext_info = rand(9999999).to_s
delay = rand( 99).to_i
# save the new command in the database
command =
{
'action' => action ,
'delay' => delay ,
'x' => x ,
'y' => y ,
'z' => z ,
'speed' => speed ,
'amount' => amount ,
'pin' => pin_nr ,
'value1' => pin_value1 ,
'value2' => pin_value2 ,
'mode' => pin_mode ,
'time' => pin_time ,
'info' => ext_info
}
command_obj = MessageHandlerScheduleCmdLine.new
command_obj.split_command_line( command )
$dbaccess.create_new_command(sched_time,crop_id)
@handler.save_command_line(command_obj)
$dbaccess.save_new_command
# get the data back from the database
cmd = Command.where("scheduled_time = ?",sched_time).first
line = CommandLine.where("command_id = ?",cmd.id).first
# line = CommandLine.where("command_id = ? and external_info = ?",cmd.id, ext_info).first
expect(cmd.crop_id ).to eq(crop_id )
expect(line.action ).to eq(action )
expect(line.external_info).to eq(ext_info )
expect(line.coord_x ).to eq(x )
expect(line.coord_y ).to eq(y )
expect(line.coord_z ).to eq(z )
expect(line.speed ).to eq(speed )
expect(line.amount ).to eq(amount )
expect(line.pin_nr ).to eq(pin_nr )
expect(line.pin_value_1 ).to eq(pin_value1 )
expect(line.pin_value_2 ).to eq(pin_value2 )
expect(line.pin_mode ).to eq(pin_mode )
expect(line.pin_time ).to eq(pin_time )
end
=begin
it "single command" do
# write a few lines in the log
log_text_1 = rand(9999999).to_s
log_text_2 = rand(9999999).to_s
log_module_1 = 99
log_module_2 = 98
$dbaccess.write_to_log( log_module_1, log_text_1 )
$dbaccess.write_to_log( log_module_2, log_text_2 )
# get the logs in a message
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
@handler.read_logs(message)
return_list = $messaging.message
# check if the logged lines are present in the message
found_in_list_1 = false
found_in_list_2 = false
return_list[:logs].each do |item|
if item['text'] == log_text_1 and item['module'] == log_module_1
found_in_list_1 = true
end
if item['text'] == log_text_2 and item['module'] == log_module_2
found_in_list_2 = true
end
end
# check expectations
expect(found_in_list_1).to eq(true)
expect(found_in_list_2).to eq(true)
expect($messaging.message[:message_type]).to eq('read_logs_response')
end
=end
end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
require './lib/messaging/messaging.rb'
require './lib/messaging/messagehandler_message.rb'
describe MessageHandler do
describe MessageHandlerBase do
before do

View File

@ -4,7 +4,7 @@ require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
describe MessageHandler do
describe MessageHandlerEmergencyStop do
before do
#$db_write_sync = Mutex.new
@ -33,32 +33,24 @@ describe MessageHandler do
message.handled = false
message.handler = @main_handler
# $messaging.emergency_stop(message)
@handler.emergency_stop(message)
# emergency_stop(message)
#puts $message.message
expect(1).to eq(1)
expect($status.emergency_stop).to eq(true)
expect($messaging.message[:message_type]).to eq('confirmation')
end
it "message handler emergency stop reset" do
expect(1).to eq(1)
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
@handler.emergency_stop_reset(message)
expect($status.emergency_stop).to eq(false)
expect($messaging.message[:message_type]).to eq('confirmation')
end
# it "create new command" do
# crop_id = rand(9999999).to_i
# scheduled_time = Time.now
#
# @db.create_new_command(scheduled_time, crop_id)
#
# cmd = Command.where("scheduled_time = ?",scheduled_time).first
#
# expect(cmd.crop_id).to eq(crop_id)
# end
end

View File

@ -0,0 +1,69 @@
require 'spec_helper'
require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
require './lib/messaging/messagehandler_logs.rb'
describe MessageHandlerLog do
before do
$db_write_sync = Mutex.new
$bot_dbaccess = DbAccess.new('development')
$dbaccess = $bot_dbaccess
$dbaccess.disable_log_to_screen()
$status = Status.new
$messaging = MessagingTest.new
$messaging.reset
@handler = MessageHandlerLog.new
@main_handler = MessageHandler.new
end
## logs
it "read logs" do
# write a few lines in the log
log_text_1 = rand(9999999).to_s
log_text_2 = rand(9999999).to_s
log_module_1 = 99
log_module_2 = 98
$dbaccess.write_to_log( log_module_1, log_text_1 )
$dbaccess.write_to_log( log_module_2, log_text_2 )
# get the logs in a message
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
@handler.read_logs(message)
return_list = $messaging.message
# check if the logged lines are present in the message
found_in_list_1 = false
found_in_list_2 = false
return_list[:logs].each do |item|
if item['text'] == log_text_1 and item['module'] == log_module_1
found_in_list_1 = true
end
if item['text'] == log_text_2 and item['module'] == log_module_2
found_in_list_2 = true
end
end
# check expectations
expect(found_in_list_1).to eq(true)
expect(found_in_list_2).to eq(true)
expect($messaging.message[:message_type]).to eq('read_logs_response')
end
end

View File

@ -0,0 +1,116 @@
require 'spec_helper'
require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
require './lib/messaging/messagehandler_measurements.rb'
describe MessageHandlerMeasuremnt do
before do
$db_write_sync = Mutex.new
$bot_dbaccess = DbAccess.new('development')
$dbaccess = $bot_dbaccess
$dbaccess.disable_log_to_screen()
$status = Status.new
$messaging = MessagingTest.new
$messaging.reset
@handler = MessageHandlerMeasurement.new
@main_handler = MessageHandler.new
end
## measurements
it "read measurements" do
# write a measurement
measurement_value = rand(9999999).to_i
measurement_text = rand(9999999).to_s
$dbaccess.write_measurements(measurement_value, measurement_text)
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
# read the list of measurements
@handler.read_measurements(message)
# check if the created item is into the list to send
found_in_list = false
$messaging.message[:measurements].each do |item|
if item['value'] == measurement_value and item['ext_info'] == measurement_text
found_in_list = true
end
end
expect(found_in_list).to eq(true)
expect($messaging.message[:message_type]).to eq('read_measurements_response')
end
it "delete measurement" do
# write two measurements
measurement_value_1 = rand(9999999).to_i
measurement_text_1 = rand(9999999).to_s
$dbaccess.write_measurements(measurement_value_1, measurement_text_1)
measurement_value_2 = rand(9999999).to_i
measurement_text_2 = rand(9999999).to_s
$dbaccess.write_measurements(measurement_value_2, measurement_text_2)
# check if the measurements are in the database and get the id
found_in_list_1 = false
found_in_list_2 = false
found_in_list_1_after = false
found_in_list_2_after = false
id_1 = 0
id_2 = 0
return_list = $dbaccess.read_measurement_list()
return_list.each do |item|
if item['value'] == measurement_value_1 and item['ext_info'] == measurement_text_1
found_in_list_1 = true
id_1 = item['id']
end
if item['value'] == measurement_value_2 and item['ext_info'] == measurement_text_2
found_in_list_2 = true
id_2 = item['id']
end
end
# try to delete the measurements
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
message.payload = {'ids' => [id_1,id_2]}
@handler.delete_measurements(message)
# check if the measurements are still in the database and get the id
found_in_list_1_after = false
found_in_list_2_after = false
return_list = $dbaccess.read_measurement_list()
return_list.each do |item|
if item['value'] == measurement_value_1 and item['ext_info'] == measurement_text_1
found_in_list_1_after = true
end
if item['value'] == measurement_value_2 and item['ext_info'] == measurement_text_2
found_in_list_2_after = true
end
end
expect(found_in_list_1).to eq(true)
expect(found_in_list_2).to eq(true)
expect(found_in_list_1_after).to eq(false)
expect(found_in_list_2_after).to eq(false)
expect($messaging.message[:message_type]).to eq('confirmation')
end
end

View File

@ -0,0 +1,110 @@
require 'spec_helper'
require './lib/status.rb'
require './lib/messaging/messaging.rb'
require './lib/messaging/messaging_test.rb'
require './lib/messaging/messagehandler_parameters.rb'
describe MessageHandlerParameter do
before do
$db_write_sync = Mutex.new
$bot_dbaccess = DbAccess.new('development')
$dbaccess = $bot_dbaccess
$dbaccess.disable_log_to_screen()
$status = Status.new
$messaging = MessagingTest.new
$messaging.reset
@handler = MessageHandlerParameter.new
@main_handler = MessageHandler.new
end
## measurements
it "read parameters" do
# write a few parameters
parameter_value_1 = rand(9999999).to_i
parameter_name_1 = rand(9999999).to_s
$dbaccess.write_parameter(parameter_name_1, parameter_value_1)
parameter_value_2 = rand(9999999).to_i
parameter_name_2 = rand(9999999).to_s
$dbaccess.write_parameter(parameter_name_2, parameter_value_2)
# get the list of parameters that the system will send
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
@handler.read_parameters(message)
return_list = $messaging.message
# check if the parameters are present in the message
found_in_list_1 = false
found_in_list_2 = false
return_list[:parameters].each do |item|
if item['value'] == parameter_value_1 and item['name'] == parameter_name_1
found_in_list_1 = true
end
if item['value'] == parameter_value_2 and item['name'] == parameter_name_2
found_in_list_2 = true
end
end
# check expectations
expect(found_in_list_1).to eq(true)
expect(found_in_list_2).to eq(true)
expect($messaging.message[:message_type]).to eq('read_parameters_response')
end
it "write parameters" do
# write a few parameters
parameter_name_1 = rand(9999999).to_s
parameter_name_2 = rand(9999999).to_s
parameter_value_1 = rand(9999999).to_i
parameter_value_2 = rand(9999999).to_i
# write a value using a message
message = MessageHandlerMessage.new
message.handled = false
message.handler = @main_handler
#message.payload = {'ids' => [id_1,id_2]}
message.payload =
{
'parameters' =>
[
{'name' => parameter_name_1, 'type' => 1, 'value' => parameter_value_1},
{'name' => parameter_name_2, 'type' => 1, 'value' => parameter_value_2}
]
}
@handler.write_parameters(message)
# check if the parameters in the database have the right values
value_read_1 = $dbaccess.read_parameter(parameter_name_1)
value_read_2 = $dbaccess.read_parameter(parameter_name_2)
# check expectations
expect(value_read_1).to eq(parameter_value_1)
expect(value_read_2).to eq(parameter_value_2)
expect($messaging.message[:message_type]).to eq('confirmation')
end
end