# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def http_json_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using JSON'.format(uri))
#     return requests.post(uri, json=data)
# 
# def http_form_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using x-www-form-urlencoded'.format(uri))
#     return requests.post(uri, data=data)
# 
# def http_csv_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using CSV'.format(uri))
#     body = ''
#     body += '## {}\n'.format(','.join(map(str, list(data.keys()))))
#     body += '{}\n'.format(','.join(map(str, list(data.values()))))
#     return requests.post(uri, data=body, headers={'Content-Type': 'text/csv'})
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def http_raw(topic, headers=None, json=None, data=None):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting raw request to {}'.format(uri))
#     return requests.post(uri, headers=headers, json=json, data=data)


import json
import logging
import pytest
import pytest_twisted
import requests
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_HTTP
from test.util import http_json_sensor, http_form_sensor, http_csv_sensor, sleep, http_raw
# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.http
def test_http_json_valid(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in JSON format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 25.26,
        'humidity': 51.8,
    }

# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def http_json_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using JSON'.format(uri))
#     return requests.post(uri, json=data)
# 
# def http_form_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using x-www-form-urlencoded'.format(uri))
#     return requests.post(uri, data=data)
# 
# def http_csv_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using CSV'.format(uri))
#     body = ''
#     body += '## {}\n'.format(','.join(map(str, list(data.keys()))))
#     body += '{}\n'.format(','.join(map(str, list(data.values()))))
#     return requests.post(uri, data=body, headers={'Content-Type': 'text/csv'})
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def http_raw(topic, headers=None, json=None, data=None):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting raw request to {}'.format(uri))
#     return requests.post(uri, headers=headers, json=json, data=data)


# Path: test/test_daq_http.py
import json
import logging
import pytest
import pytest_twisted
import requests
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_HTTP
from test.util import http_json_sensor, http_form_sensor, http_csv_sensor, sleep, http_raw

# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.http
def test_http_json_valid(machinery, create_influxdb, reset_influxdb):
    """
    Submit single reading in JSON format to HTTP API
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 25.26,
        'humidity': 51.8,
    }

    deferred = threads.deferToThread(http_json_sensor, settings.channel_path_data, data)