def test_query_attribute__bad_type(self): var_reader = VariableReader() mock_object = ["name", "object name"] value = var_reader.query_attribute(mock_object, "name") assert value is None
def test_no_attr_2__success(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": {"%start": 1}} ] attributes = None mock_entry_1 = { "timestamp": 100, "cpu": "10.1" } mock_entry_2 = { "timestamp": 200, "cpu": "20.2" } mock_entry_3 = { "timestamp": 300, "cpu": "30.3" } var_reader.set_data([mock_entry_1, mock_entry_2, mock_entry_3]) result = var_reader.query(objects, attributes) assert result == [mock_entry_2, mock_entry_3]
def variable(self, t): var_name = t[0].value if var_name not in self.variables: raise Exception("Unassigned variable " + var_name) if len(t) > 1: var_reader = VariableReader() var_reader.set_data(self.variables[var_name]) last_object = t[-1] objects = t[1:-1] if type(last_object) == dict: if "filter" in last_object and last_object["filter"] is None: attributes = last_object["name"] else: attributes = None objects = t[1:] else: attributes = last_object if (len(objects) > 0 and type(objects[0]) is dict and "filter" not in objects[0]): objects[0] = {"name": var_name, "filter": objects[0]} else: objects.insert(0, {"name": var_name, "filter": None}) result = var_reader.query(objects, attributes) return result return self.variables[var_name]
def test_query_attribute__success(self): var_reader = VariableReader() mock_object = {"name": "object name"} value = var_reader.query_attribute(mock_object, "name") assert value == "object name"
def test_nested_range_sort__success(self, filter, expected): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "name", "filter": filter} ] attributes = "name" mock_object_1 = {"name": "a1"} mock_object_3 = {"name": "a3"} mock_object_5 = {"name": "a5"} mock_object_2 = {"name": "a2"} mock_object_4 = {"name": "a4"} mock_entry = [mock_object_1, mock_object_3, mock_object_5, mock_object_2, mock_object_4] var_reader.set_data([mock_entry]) results = var_reader.query(objects, attributes) assert results == expected
def test_query_attribute__not_found(self): var_reader = VariableReader() mock_object = {"name": "object name"} value = var_reader.query_attribute(mock_object, "foobar") assert value is None
def test_nested_object_multiple__no_child(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "disks", "filter": None}, ] attributes = "name" mock_entry_1 = { "timestamp": 100, "cpu": "10.1" } mock_entry_2 = { "timestamp": 200, "cpu": "20.2", "disks": [{"name": "cf3:"}, {"name": "cf4:"}] } var_reader.set_data([mock_entry_1, mock_entry_2]) result = var_reader.query(objects, attributes) assert result == ["cf3:", "cf4:"]
def test_single__none_found(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None} ] attributes = "timestamp" result = var_reader.query(objects, attributes) assert result == []
def test_nested_no_attr_1__success(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "disks", "filter": None}, ] attributes = None mock_entry_1 = { "timestamp": 100, "cpu": "10.1", "disks": [{"name": "cf1:"}, {"name": "cf2:"}, {"name": "cf3:"}] } mock_entry_2 = { "timestamp": 200, "cpu": "20.2", "disks": [{"name": "cf4:"}, {"name": "cf5:"}, {"name": "cf6:"}] } mock_entry_3 = { "timestamp": 200, "cpu": "20.2", "disks": [{"name": "cf7:"}, {"name": "cf8:"}, {"name": "cf9:"}] } var_reader.set_data([mock_entry_1, mock_entry_2, mock_entry_3]) result = var_reader.query(objects, attributes) assert result == [ {"name": "cf1:"}, {"name": "cf2:"}, {"name": "cf3:"}, {"name": "cf4:"}, {"name": "cf5:"}, {"name": "cf6:"}, {"name": "cf7:"}, {"name": "cf8:"}, {"name": "cf9:"}, ]
def test_attrs_single__success(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None} ] attributes = "timestamp" mock_entry_1 = { "timestamp": 100, "cpu": "10.1" } var_reader.set_data([mock_entry_1]) result = var_reader.query(objects, attributes) assert result == [100]
def test_nested__not_dict(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "not_a_dict", "filter": None} ] attributes = "name" mock_object_1 = "not_a_dict" mock_entry = {"objects": [mock_object_1]} var_reader.set_data([mock_entry]) results = var_reader.query(objects, attributes) assert results == []
def test_attrs_all__success(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None} ] attributes = ["*"] mock_entry_1 = { "timestamp": 100, "cpu": 10.1, "memory": 500.2 } var_reader.set_data([mock_entry_1]) result = var_reader.query(objects, attributes) assert result == [mock_entry_1]
def test_nested__not_found(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "foobar", "filter": None} ] attributes = "name" mock_object_1 = {"name": "a1", "dhcpleaseinterval": 300, "avatartype": "URL"} mock_entry = {"objects": [mock_object_1]} var_reader.set_data([mock_entry]) results = var_reader.query(objects, attributes) assert results == []
def test_nested_attr_filter__success(self, filter, expected): var_reader = VariableReader() objects = [ {"name": "v", "filter": None}, {"name": "objects", "filter": filter} ] attributes = "name" mock_object_1 = {"name": "a1", "dhcpleaseinterval": 300, "avatartype": "URL"} mock_object_3 = {"name": "a3", "dhcpleaseinterval": 400, "avatartype": "URL"} mock_object_5 = {"name": "a5", "dhcpleaseinterval": 500, "avatartype": "URL"} mock_object_2 = {"name": "a2", "dhcpleaseinterval": 100, "avatartype": "BASE64"} mock_object_4 = {"name": "a4", "dhcpleaseinterval": 200, "avatartype": "BASE64"} mock_entry = {"objects": [mock_object_1, mock_object_3, mock_object_5, mock_object_2, mock_object_4]} var_reader.set_data([mock_entry]) results = var_reader.query(objects, attributes) assert results == expected
def test_group_both_filtered__success(self): var_reader = VariableReader() objects = [ {"name": "v", "filter": {"%group": "dhcpleaseinterval"}}, {"name": "domains", "filter": {"%group": "bgpenabled", "bgpenabled": True}} ] attributes = "name" mock_entry_1 = { "name": "enterprise_1", "dhcpleaseinterval": 10, "domains": [ {"name": "domain_1", "bgpenabled": True}, {"name": "domain_2", "bgpenabled": False} ] } mock_entry_2 = { "name": "enterprise_2", "dhcpleaseinterval": 20, "domains": [] } mock_entry_3 = { "name": "enterprise_3", "dhcpleaseinterval": 10, "domains": [ {"name": "domain_3", "bgpenabled": True}, {"name": "domain_4", "bgpenabled": True} ] } mock_entry_4 = { "name": "enterprise_4", "dhcpleaseinterval": 30, "domains": [ {"name": "domain_5", "bgpenabled": True}, {"name": "domain_6", "bgpenabled": True} ] } var_reader.set_data([mock_entry_1, mock_entry_2, mock_entry_3, mock_entry_4]) results = var_reader.query(objects, attributes) assert results == [ [10, [[True, ["domain_1", "domain_3", "domain_4"]]]], [20, [[True, []]]], [30, [[True, ["domain_5", "domain_6"]]]] ]
def test_connect__not_supported(self): var_reader = VariableReader() with pytest.raises(NotImplementedError): var_reader.connect("host")
def test_stop__success(self): var_reader = VariableReader() var_reader.stop_session()