A generator method intended to be used by the mock db client when involving the mocked query() method. It takes the network and service items, and generates a result from
those items each time query() is called by taking turns - starts with network result, followed by service result and then it repeats, until all items have been exhausted.
Network items and service items are expected to have the same length.
:param network_items: the network data to generate from
:param service_items: the service data to generate from
:return: a generator object
"""
assertlen(network_items)==len(service_items),"The data points generator must receive the same number of network items as the number of service items"
index=0
...
...
@@ -47,6 +60,7 @@ class TestAggregation(object):
# before yielding the service data points, check if both sets of data points are enumerated
ifindex==len(network_items)-1:
# if so, set the finished flag of the test
getattr(self,self.FINISHED).set()
yieldMockResultSet(items)
...
...
@@ -54,7 +68,14 @@ class TestAggregation(object):
index+=1
defsetup_mock_db_client(self,mock_class):
setattr(self,self.ACTUAL_RESULTS,[])
"""
Sets up a mock db client and also defines the expected aggregation results from the test.
:param mock_class: the mock class used as an influx db client instance
:return:
"""
setattr(self,self.ACTUAL_RESULTS,[])# initially, there are no actual results, these are built progressively while the aggregator is running
])# defines the expected rows from the aggregation
setattr(self,self.FINISHED,Event())
# initialises the influx data generator, which is involved each time the query() method of the mock db client is called
mock_points=self.points_generator(
# network items is a list of tuples, each tuple represents a result from a query; each time query() is called and a network measurement must be generated, then one of
# these tuples is generated, empty tuple means result with no points
# service items is a list of tuples, each tuple represents a result from a query; each time query() is called and a service measurement must be generated, then one of
# these tuples is generated, empty tuple means result with no points
# implement the query() and write_points() methods of the mock db client
mock_class.query=lambdaquery:next(mock_points)# query() returns the next element of the mock_points generator
mock_class.write_points=lambdapoints:getattr(self,self.ACTUAL_RESULTS).append(drop_timestamp(points[0]))# write_points() adds aggregated rows to actual results list
# in the end of the test, we can compare the expected results with the actual results that were generated during the aggregation process
The actual test that's executed when running pytest.
:param MockDBClient: a mock object argument passed by the mock.patch decorator. The decorator changes all occurrences of InfluxDBClient in the aggregator's code to the
return value of this MockDBClient object
"""
# set up the mock db client by providing implementations for the necessary methods (query and write_points)
# we compare sorted versions of the expected and actual results; this is because the aggregator implementation uses dictionary for efficiency purposes, hence the order of
# the collected results may vary, especially on different OS; hence, we only care about the two list of results to contain the same elements