def test_metric_properties(self): metric = Metric(self.metric_name, self.metric_fields, self.metric_metadata, **self.metric_values) assert metric.name == self.metric_name assert metric.fields == self.metric_fields assert metric.metadata == self.metric_metadata assert metric.values == self.metric_values
def test_metric_add_time(self): alternative_values = { 'value': 0, 'hostname': 'test' } # Note: No explicit time metric = Metric(self.metric_name, self.metric_fields, self.metric_metadata, **alternative_values) assert 'time' in metric.values
def test_metric_name_exception(self): alternative_values = { 'value': 0, 'host': 'test', 'time': time() } # Note: "host" not "hostname" with pytest.raises(NameError) as exc: metric = Metric(self.metric_name, self.metric_fields, self.metric_metadata, **alternative_values) assert "Expected ['hostname', 'value'] but got ['host', 'value']" in str( exc.value)
def test_metric_repr(self): import re from ast import literal_eval metric = Metric(self.metric_name, self.metric_fields, self.metric_metadata, **self.metric_values) regex = '<Metric:"([^"]+)" Fields:(\[[^\]]+\]) Metadata:(\[[^\]]+\]) Values:({[^}]+})>' match = re.match(regex, repr(metric)) assert match is not None assert match.lastindex == 4 assert match.group(1) == self.metric_name assert literal_eval(match.group(2)) == self.metric_fields assert literal_eval(match.group(3)) == self.metric_metadata assert literal_eval(match.group(4)) == self.metric_values
def test_to_string(self): timestamp = time() metric_name = 'metric' metric_fields = ['value', 'string'] metric_metadata = ['hostname'] metric_values = { 'value': 0, 'string': 'test', 'hostname': 'test', 'time': timestamp } metric = Metric(metric_name, metric_fields, metric_metadata, **metric_values) dialect = InfluxDBDialect() string_repr = dialect.to_string(metric) assert string_repr == 'metric,hostname=test string="test",value=0 {}'.format( int(timestamp * 1e6))
def from_string(self, string): try: data = json.loads(string) name = data['name'] fields = list(six.iterkeys(data['fields'])) metadata = list(six.iterkeys(data['metadata'])) timestamp = data['timestamp'] kwargs = {} kwargs.update(data['fields']) kwargs.update(data['metadata']) return Metric(name=name, fields=fields, metadata=metadata, time=timestamp, **kwargs) except ValueError as e: raise DialectError('Could not decode JSON', e) except KeyError as e: raise DialectError('Metric didn\'t contain all necessary fields', e)
def test_metric_handle_time_field(self): metric = Metric(self.metric_name, self.metric_fields + ['time'], self.metric_metadata, **self.metric_values) assert 'time' not in metric.fields