# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def idgen(size=6, chars=string.ascii_uppercase + string.digits):
#     """
#     ========
#     Synopsis
#     ========
#     ```
#     >>> idgen()
#     'G5G74W'
#     ```
# 
#     - https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits/2257449#2257449
#     """
#     return ''.join(random.choice(chars) for _ in range(size))


import logging
import pytest
import pytest_twisted
from copy import deepcopy
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep, idgen

logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.influxdb
def test_influxdb_tags(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker.
    Proof that all special fields are stored in the
    InfluxDB database as tags.
    """

    # Define field names which are tags.
    # FIXME: Synchronize with ``kotori.daq.influx.storage.format_chunk()``.
    tag_fields_main = ['geohash', 'latitude', 'longitude']
    tag_fields_more = ['location', 'location_id', 'location_name', 'sensor_id', 'sensor_type']

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
        'geohash': 'aa3434',
        'latitude': 42.50,
        'longitude': 52.40,
    }

    for field in tag_fields_more:
        data[field] = idgen()


# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def idgen(size=6, chars=string.ascii_uppercase + string.digits):
#     """
#     ========
#     Synopsis
#     ========
#     ```
#     >>> idgen()
#     'G5G74W'
#     ```
# 
#     - https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits/2257449#2257449
#     """
#     return ''.join(random.choice(chars) for _ in range(size))


# Path: test/test_daq_tags.py
import logging
import pytest
import pytest_twisted
from copy import deepcopy
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep, idgen


logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.influxdb
def test_influxdb_tags(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker.
    Proof that all special fields are stored in the
    InfluxDB database as tags.
    """

    # Define field names which are tags.
    # FIXME: Synchronize with ``kotori.daq.influx.storage.format_chunk()``.
    tag_fields_main = ['geohash', 'latitude', 'longitude']
    tag_fields_more = ['location', 'location_id', 'location_name', 'sensor_id', 'sensor_type']

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
        'geohash': 'aa3434',
        'latitude': 42.50,
        'longitude': 52.40,
    }

    for field in tag_fields_more:
        data[field] = idgen()


    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_json, data)