File size: 2,631 Bytes
78f0f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

# load json file from this file's directory
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
FILE_PATH = os.path.join(CURRENT_DIR, "agents.json")

with open(FILE_PATH) as f:
    sample_agent_json = json.load(f)


class HumanResources:
    def __init__(self):
        self.agent_json = sample_agent_json

    def __repr__(self):
        return f"{self.agent_json}"

    def _create(self, new_agent):
        self.agent_json[new_agent["role"]] = new_agent
        self._save_to_file()
        return self.agent_json[new_agent["role"]]

    def _read(self, agent_name):
        return self.agent_json[agent_name]

    def _update(self, agent_name, new_agent):
        self.agent_json[agent_name] = new_agent
        self._save_to_file()
        return self.agent_json[agent_name]

    def _delete(self, agent_name):
        del self.agent_json[agent_name]
        self._save_to_file()
        return self.agent_json

    def agent_object(self, agent_name):
        return self.agent_json[agent_name]

    def select_agents(self):
        return list(self.agent_json.keys())

    def _save_to_file(self):
        with open(FILE_PATH, "w") as f:
            json.dump(self.agent_json, f, indent=4)


# # Example usage
# hr = HumanResources()
# new_agent = {"name": "Agent Smith", "role": "Analyst", "description": "Data Analysis"}
# hr._create(new_agent)

# # Print updated agents
# print(hr.select_agents())


if __name__ == "__main__":
    hr = HumanResources()
    # assert _create() returns a new agent object: {'name': 'finance', 'title': 'finance manager'}
    assert hr._create({"name": "finance", "title": "finance manager"}) == {
        "name": "finance",
        "title": "finance manager",
    }
    # assert _read() returns an agent object: {'name': 'finance', 'title': 'finance manager'}
    assert hr._read("finance") == {"name": "finance", "title": "finance manager"}
    # assert _update() returns an updated agent object: {'name': 'finance', 'title': 'senior finance manager'}
    assert hr._update(
        "finance", {"name": "finance", "title": "senior finance manager"}
    ) == {"name": "finance", "title": "senior finance manager"}
    # assert _delete() returns json all agents without the deleted agent, just check the names of the agents: ['sales', 'marketing', 'engineer']
    temp_json = hr._delete("finance")
    assert list(temp_json.keys()) == ["sales", "marketing", "engineer"]
    # assert select_agents() returns a list of all agents: ['sales', 'marketing', 'engineer']
    assert hr.select_agents() == ["sales", "marketing", "engineer"]

    print("All tests passed!")