# Copyright 2017, Major Hayden <major@mhtx.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests the output methods."""
import json
import platform

from monitorstack.common import formatters

SAMPLE_RESULT = {
    'exit_code': 0,
    'message': 'uptime is ok',
    'measurement_name': 'system_uptime',
    'meta': {
        'platform': platform.platform(),
    },
    'variables': {
        'uptime': '29587.75'
    }
}


class TestFormatters(object):
    """Tests for the base cli module."""

    def test_current_time(self):
        """Test current_time()."""
        result = formatters._current_time()
        assert isinstance(result, int)
        assert result > 0

    def test_write_json(self, capsys):
        """Test write_json() module."""
        formatters.write_json(SAMPLE_RESULT)
        out, err = capsys.readouterr()
        result_json = json.loads(out)
        assert isinstance(result_json, dict)
        assert result_json['measurement_name'] == \
            SAMPLE_RESULT['measurement_name']

    def test_write_line(self, capsys):
        """Test write_line() module."""
        formatters.write_line(SAMPLE_RESULT)
        out, err = capsys.readouterr()
        assert out == "uptime {}\n".format(
            SAMPLE_RESULT['variables']['uptime']
        )

    def test_write_telegraf(self, capsys):
        """Test write_telegraf() module."""
        formatters.write_telegraf(SAMPLE_RESULT)
        out, err = capsys.readouterr()
        assert out.startswith(SAMPLE_RESULT['measurement_name'])

@@ -634,24 +634,8 @@
 json
-%0Aimport platform
 %0A%0Afr
@@ -824,25 +824,25 @@
 m': 
-platform.
+'example_
 platform
 (),%0A
@@ -837,18 +837,17 @@
 platform
-()
+'
 ,%0A    %7D,
@@ -904,16 +904,382 @@
    %7D%0A%7D%0A%0A
+SAMPLE_RESULT_NO_META = %7B%0A    'exit_code': 0,%0A    'message': 'uptime is ok',%0A    'measurement_name': 'system_uptime',%0A    'variables': %7B%0A        'uptime': '29587.75'%0A    %7D%0A%7D%0A%0ASAMPLE_RESULT_NO_META_WITH_FLOAT = %7B%0A    'exit_code': 0,%0A    'message': 'uptime is ok',%0A    'measurement_name': 'system_uptime',%0A    'variables': %7B%0A        'uptime': float(29587.75)%0A    %7D%0A%7D%0A%0A
 %0Aclass T
@@ -2300,32 +2300,32 @@
 ys.readouterr()%0A
-
         assert o
@@ -2373,8 +2373,724 @@
 name'%5D)%0A
+%0A    def test_write_telegraf_without_meta(self, capsys):%0A        %22%22%22Test write_telegrat() module without meta in result.%22%22%22%0A        formatters.write_telegraf(SAMPLE_RESULT_NO_META)%0A        out, err = capsys.readouterr()%0A        assert out.startswith(SAMPLE_RESULT%5B'measurement_name'%5D)%0A%0A    def test_write_telegraf_line_format_with_float(self):%0A        %22%22%22Test _telegraf_line_format() with float in meta.%22%22%22%0A        sets = %7B%0A            'platform': 'example_platform',%0A            'othervar': float(3)%0A        %7D%0A        result = formatters._telegraf_line_format(sets=sets, quote=True)%0A        assert isinstance(result, str)%0A        assert 'othervar=3' in result%0A        assert 'platform=%22example_platform%22' in result%0A

Improve tests for formatters (#8)