From 3824dda1f184158c8946f02a4f2d533a17e95cd4 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Thu, 24 Oct 2024 13:04:16 +1100 Subject: Execute actions via the FamilyTreeManager - Added logic for executing different FamilyTreeManager methods based on actions - More test coverage --- lib/action_file_executor.rb | 11 ++++++++- lib/family_tree_manager.rb | 9 ++++++-- spec/action_file_executor_spec.rb | 47 +++++++++++++++++++++++++++++++++------ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/action_file_executor.rb b/lib/action_file_executor.rb index 15d4f63..1cd3a94 100644 --- a/lib/action_file_executor.rb +++ b/lib/action_file_executor.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative 'family_tree_manager' + class ActionFileExecutor def initialize(file_path) @file_path = file_path @@ -10,7 +12,14 @@ class ActionFileExecutor File.open(@file_path, 'r') do |file| file.each_line do |line| action, *params = line.split(' ') - puts "Executing action: #{action} with params: #{params.join(', ')}" + case action + when 'ADD_CHILD' + FamilyTreeManager.instance.add_child(*params) + when 'GET_RELATIONSHIP' + FamilyTreeManager.instance.query_hierarchy(*params) + else + puts "Ignoring unsupported action: [#{action}]" + end end end end diff --git a/lib/family_tree_manager.rb b/lib/family_tree_manager.rb index 75e1b05..6b0bc12 100644 --- a/lib/family_tree_manager.rb +++ b/lib/family_tree_manager.rb @@ -9,6 +9,11 @@ class FamilyTreeManager @family_members = {} end - def add_child(*params); end - def query_hierarchy(*params); end + def add_child(*params) + puts "Adding Child with params: #{params.join(', ')}" + end + + def query_hierarchy(*params) + puts "Querying Hierarcy with params: #{params.join(', ')}" + end end diff --git a/spec/action_file_executor_spec.rb b/spec/action_file_executor_spec.rb index c909bf5..cab34b3 100644 --- a/spec/action_file_executor_spec.rb +++ b/spec/action_file_executor_spec.rb @@ -2,14 +2,17 @@ require 'tempfile' require_relative '../lib/action_file_executor' +require_relative '../lib/family_tree_manager' RSpec.describe ActionFileExecutor do - let(:tempfile) { Tempfile.new('actions.txt') } let(:invalid_file_path) { 'non_existent_file.txt' } + let(:family_tree_manager) { instance_double('FamilyTreeManager') } + let(:tempfile) { Tempfile.new('actions.txt') } before do - tempfile.write('ADD_CHILD Mother Child Male') - tempfile.rewind + allow(FamilyTreeManager).to receive(:instance).and_return(family_tree_manager) + allow(family_tree_manager).to receive(:add_child) + allow(family_tree_manager).to receive(:query_hierarchy) end after do @@ -28,6 +31,9 @@ RSpec.describe ActionFileExecutor do context 'when the file exists' do it 'initializes successfully' do + tempfile.puts('ADD_CHILD Mother Child Male') + tempfile.rewind + action_file_executor = ActionFileExecutor.new(tempfile.path) expect(action_file_executor).to be_an_instance_of(ActionFileExecutor) end @@ -35,11 +41,38 @@ RSpec.describe ActionFileExecutor do end describe '#execute_actions' do - it 'prints message indicating action being executed and the parameters which were passed' do - action_file_executor = ActionFileExecutor.new(tempfile.path) - expect do + context 'with valid actions' do + before do + tempfile.puts('ADD_CHILD Mother Child Male') + tempfile.puts('GET_RELATIONSHIP Child Maternal-Uncle') + tempfile.rewind + end + + it 'calls the add_child method on FamilyTreeManager when it encounters the ADD_CHILD action' do + action_file_executor = ActionFileExecutor.new(tempfile.path) action_file_executor.execute_actions - end.to output("Executing action: ADD_CHILD with params: Mother, Child, Male\n").to_stdout + expect(family_tree_manager).to have_received(:add_child).with('Mother', 'Child', 'Male') + end + + it 'calls the query_hierarchy method on FamilyTreeManager when it encounters the GET_RELATIONSHIP action' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + action_file_executor.execute_actions + expect(family_tree_manager).to have_received(:query_hierarchy).with('Child', 'Maternal-Uncle') + end + end + + context 'with unsupported actions' do + before do + tempfile.puts('ADD_MOTHER Child Mother') + tempfile.rewind + end + + it 'prints an error message when it encounters an unsupported action' do + expect do + action_file_executor = ActionFileExecutor.new(tempfile.path) + action_file_executor.execute_actions + end.to output("Ignoring unsupported action: [ADD_MOTHER]\n").to_stdout + end end end end -- cgit v1.2.3