summaryrefslogtreecommitdiff
path: root/spec/action_file_executor_spec.rb
blob: cab34b3214c7d3968a633866bd6ec80d97ced699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
# frozen_string_literal: true

require 'tempfile'
require_relative '../lib/action_file_executor'
require_relative '../lib/family_tree_manager'

RSpec.describe ActionFileExecutor do
  let(:invalid_file_path) { 'non_existent_file.txt' }
  let(:family_tree_manager) { instance_double('FamilyTreeManager') }
  let(:tempfile) { Tempfile.new('actions.txt') }

  before do
    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
    tempfile.close
    tempfile.unlink
  end

  describe '#initialize' do
    context 'when the file does not exist' do
      it 'prints an error message and exits' do
        expect do
          ActionFileExecutor.new(invalid_file_path)
        end.to output("Error: The file 'non_existent_file.txt' does not exist.\n").to_stdout.and raise_error(SystemExit)
      end
    end

    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
    end
  end

  describe '#execute_actions' 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
        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