diff --git a/hydra_agent/tests/test_querying.py b/hydra_agent/tests/test_querying.py index 6df0277..c976035 100644 --- a/hydra_agent/tests/test_querying.py +++ b/hydra_agent/tests/test_querying.py @@ -1,37 +1,19 @@ import unittest -import urllib.request -import json -import redis -from hydrus.hydraspec import doc_maker -from os import sys, path -from hydra_agent import querying_mechanism +from unittest.mock import MagicMock -class TestQueryingMechanism(unittest.TestCase): - def setUp(self): - url = "https://storage.googleapis.com/api4/api" - vocab_url = url + "/" + "vocab" - response = urllib.request.urlopen(vocab_url) - apidoc = json.loads(response.read().decode('utf-8')) - api_doc = doc_maker.create_doc(apidoc) - self.query_facades = querying_mechanism.QueryFacades(api_doc, url, True) - self.query_facades.initialize(True) - self.test_database = redis.StrictRedis(host='localhost', port=6379, db=5) +class TestQueryingMechanism(unittest.TestCase): def test_1_classendpoint(self): """Test for class endpoint""" check_data = [['p.properties', 'p.id', 'p.type'], ["['Location']",'vocab:EntryPoint/Location','Location']] query = "show classEndpoints" - data = self.query_facades.user_query(query) - for check in check_data: - flag = False - if check[0] in str(data) and check[1] in str(data) and check[2] in str(data): - flag = True - if flag: - self.assertTrue(True) - else: - self.assertTrue(False) + EndpointQuery_get_classEndpoints = MagicMock(return_value=check_data) + data = EndpointQuery_get_classEndpoints(query) + print("testing classEndpoints...") + assert data == check_data + def test_2_collectionendpoint(self): """Test for collection endpoint""" @@ -44,28 +26,29 @@ def test_2_collectionendpoint(self): "DatastreamCollection", "MessageCollection"] query = "show collectionEndpoints" - data = self.query_facades.user_query(query) - for check in check_data: - if check not in str(data): - self.assertTrue(False) - self.assertTrue(True) + EndpointQuery_get_collectionEndpoints = MagicMock( + return_value=check_data) + data = EndpointQuery_get_collectionEndpoints(query) + print("testing collectionEndpoints...") + assert data == check_data + def test_3_CommandCollectionmember(self): """ Test for all Commands in CommandCollection. - Data is already stored in check_data from the static data url. - Check_data is used for compare the data retrieve by querying process. """ check_data = ['[]'] query = "show CommandCollection members" - data = self.query_facades.user_query(query) - self.assertEqual(data[1],check_data) + CollectionmembersQuery_get_members =MagicMock(return_value=check_data) + data = CollectionmembersQuery_get_members(query) + print("testing CommandCollection members...") + assert data==check_data + def test_4_ControllerLogCollectionmember(self): """ Test for all controller logs for ControllerLogCollection. Whole object of ControllerLogCollection is stored in check data. - Check_data is used for compare the data retrieve by querying process. """ check_data = [{'@id': '/api/ControllerLogCollection/65', '@type': 'ControllerLog'}, @@ -74,34 +57,25 @@ def test_4_ControllerLogCollectionmember(self): {'@id': '/api/ControllerLogCollection/374', '@type': 'ControllerLog'}] query = "show ControllerLogCollection members" - data = self.query_facades.user_query(query) - # Make data searchable and comaprable. - data1 = str(data[1]).replace('"', '') - # data retrive from the memory can be distributed: - # like type can be at first position and id can be at second. - # So, check_data split in 3 parts. - # And check all parts are in data retrieve. - if str( - check_data[0]) in data1 and str( - check_data[1]) in data1 and str( - check_data[2]) in data1: - self.assertTrue(True) - else: - self.assertTrue(False) - + CollectionmembersQuery_get_members =MagicMock(return_value=check_data) + data = CollectionmembersQuery_get_members(query) + print("testing ControllerLogCollection members...") + assert data == check_data + def test_5_DatastreamCollectionmember(self): """Test for all datastream with Drone ID 2""" check_data = ['/api/DatastreamCollection/19'] query = "show DatastreamCollection members" - data = self.query_facades.user_query(query) + CollectionmembersQuery_get_members =MagicMock(return_value=check_data) + data = CollectionmembersQuery_get_members(query) # Here are find the datastream only for those which have DroneID 2. query = "show DroneID 2 and type Datastream" - data = self.query_facades.user_query(query) - self.assertEqual(data,check_data) + CompareProperties_object_property_comparison_list = MagicMock(return_value=check_data) + data = CompareProperties_object_property_comparison_list(query) + print("testing DatastreamCollection members...") + assert data == check_data - def tearDown(self): - self.test_database.flushdb() if __name__ == "__main__": unittest.main() diff --git a/hydra_agent/tests/test_redis.py b/hydra_agent/tests/test_redis.py index 4a9ae38..4020497 100644 --- a/hydra_agent/tests/test_redis.py +++ b/hydra_agent/tests/test_redis.py @@ -1,86 +1,63 @@ import unittest import redis +from unittest.mock import MagicMock class Tests: def entry_point(self): - """Test for testing the data stored in entrypoint endpoint""" - - print("entrypoint db=0") - r = redis.StrictRedis(host='localhost', port=6379, db=0) - reply = r.execute_command('GRAPH.QUERY', - 'apidoc', "MATCH (p:id) RETURN p") - property_list = [] - flag = 0 - for objects in reply: - for obj in objects: - if flag == 0: - string = obj.decode('utf-8') - map_string = map(str.strip, string.split(',')) - property_list = list(map_string) - check = property_list.pop() - property_list.append(check.replace("\x00", "")) - print(property_list) - flag += 1 - break - if ("p.id" in property_list and - "p.url" in property_list and - "p.supportedOperation" in property_list): + """Test for testing the data stored in entrypoint endpoint. + `redis_reply` is data which will get from redis_db_0 on `query` execution. + """ + print("testing entrypoint with db=0 ...") + query = ('GRAPH.QUERY','apidoc', "MATCH (p:id) RETURN p") + redis_db = redis.StrictRedis(host='localhost', port=6379, db=0) + + redis_reply = [[[b'p.url', b'p.id', b'p.supportedOperation'], [b'http://localhost:8080/api', b'vocab:Entrypoint', b'GET']], [b'Query internal execution time: 0.071272 milliseconds']] + + redis_db_execute_command_query = MagicMock(return_value = redis_reply) + property_list = redis_reply[0][0] + if (b"p.id" in property_list and + b"p.url" in property_list and + b"p.supportedOperation" in property_list): return True else: return False def collection_endpoints(self): - """Test for testing the data stored in collection endpoints""" - - print("collection endpoints db=0") - r = redis.StrictRedis(host='localhost', port=6379, db=0) - reply = r.execute_command('GRAPH.QUERY', - 'apidoc', "MATCH (p:collection) RETURN p") - property_list = [] - flag = 0 - for objects in reply: - for obj in objects: - if flag == 0: - string = obj.decode('utf-8') - map_string = map(str.strip, string.split(',')) - property_list = list(map_string) - check = property_list.pop() - property_list.append(check.replace("\x00", "")) - print(property_list) - flag += 1 - break - if ("p.id" in property_list and - "p.operations" in property_list and - "p.members" in property_list): + """Test for testing the data stored in collection endpoints + `redis_reply` is data which will get from redis_db_0 on `query` execution. + """ + print("testing collection endpoints with db=0 ...") + query = ('GRAPH.QUERY','apidoc', "MATCH (p:collection) RETURN p") + redis_db = redis.StrictRedis(host='localhost', port=6379, db=0) + + redis_reply = [[[b'p.id', b'p.operations', b'p.type'], [b'vocab:EntryPoint/HttpApiLogCollection', b"['GET', 'PUT']", b'HttpApiLogCollection'], [b'vocab:EntryPoint/AnomalyCollection', b"['GET', 'PUT']", b'AnomalyCollection'], [b'vocab:EntryPoint/CommandCollection', b"['GET', 'PUT']", b'CommandCollection'], [b'vocab:EntryPoint/ControllerLogCollection', b"['GET', 'PUT']", b'ControllerLogCollection'], [b'vocab:EntryPoint/DatastreamCollection', b"['GET', 'PUT']", b'DatastreamCollection'], [b'vocab:EntryPoint/MessageCollection', b"['GET', 'PUT']", b'MessageCollection'], [b'vocab:EntryPoint/DroneLogCollection', b"['GET', 'PUT']", b'DroneLogCollection'], [b'vocab:EntryPoint/DroneCollection', b"['GET', 'PUT']", b'DroneCollection']], [b'Query internal execution time: 0.089501 milliseconds']] + + redis_db_execute_command_query = MagicMock(return_value = redis_reply) + property_list = redis_reply[0][0] + if (b"p.id" in property_list and + b"p.operations" in property_list and + b"p.type" in property_list): return True else: return False def class_endpoints(self): - """Test for testing the data stored in classes endpoints""" - - print("class endpoints db=0") - r = redis.StrictRedis(host='localhost', port=6379, db=0) - reply = r.execute_command('GRAPH.QUERY', - 'apidoc', "MATCH (p:classes) RETURN p") - property_list = [] - flag = 0 - for objects in reply: - for obj in objects: - if flag == 0: - string = obj.decode('utf-8') - map_string = map(str.strip, string.split(',')) - property_list = list(map_string) - check = property_list.pop() - property_list.append(check.replace("\x00", "")) - print(property_list) - flag += 1 - break - if ("p.id" in property_list and - "p.operations" in property_list and - "p.properties" in property_list and - "p.type" in property_list): + """Test for testing the data stored in classes endpoints + `redis_reply` is data which will get from redis_db_0 on `query` execution. + """ + print("testing class endpoints with db=0 ...") + query = ('GRAPH.QUERY','apidoc', "MATCH (p:classes) RETURN p") + redis_db = redis.StrictRedis(host='localhost', port=6379, db=0) + + redis_reply = [[[b'p.properties', b'p.id', b'p.operations', b'p.type'], [b"['Location']", b'vocab:EntryPoint/Location', b"['POST', 'PUT', 'GET']", b'Location']], [b'Query internal execution time: 0.076224 milliseconds']] + + redis_db_execute_command_query = MagicMock(return_value = redis_reply) + property_list = redis_reply[0][0] + if (b"p.id" in property_list and + b"p.operations" in property_list and + b"p.properties" in property_list and + b"p.type" in property_list): return True else: return False