<commit_before>"""
Unit tests to verify proper conversion of JSON to HTML.
"""
from __future__ import division, print_function, absolute_import
import unittest
from json_to_table.json_to_table import convert, JsonConverter


class TestConvert(unittest.TestCase):
    def setUp(self):
        self.simple_json = {'key' : 'value'}
        self.custom_table_attributes = {'border' : 1}
        self.nested_json = {
        "menu": {
        "id": "file",
        "value": "File",
        "menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},
                     {"value": "Open", "onclick": "OpenDoc()"},
                     {"value": "Close", "onclick": "CloseDoc()"}]
    }}

    def test_invalid_build_direction(self):
        with self.assertRaises(ValueError) as context:
            convert(None, build_direction=None)
            self.assertTrue('Invalid build direction' in context.exception)

    def test_invalid_table_attributes(self):
        with self.assertRaises(TypeError) as context:
            convert(None, table_attributes=0)
            self.assertTrue('Table attributes must be either' in context.exception)

    def test_invalid_json(self):
        with self.assertRaises(AttributeError) as context:
            convert(None)

    def test_simple(self):
        result = convert(self.simple_json)
        simple_table = '<table><tr><th>key</th><td>value</td></tr></table>'
        self.assertEqual(result, simple_table)

    def test_custom_table_attributes(self):
        result = convert({}, table_attributes=self.custom_table_attributes)
        self.assertTrue('border=\"1\"' in result)

    def test_build_direction_top_to_bottom(self):
        result = convert(self.simple_json, build_direction='TOP_TO_BOTTOM')
        simple_table = '<table><tr><th>key</th></tr><tr><td>value</td></tr></table>'
        self.assertEqual(result, simple_table)

    def test_clubbed_json(self):
        clubbed_json = {'sample': [ {'a':1, 'b':2, 'c':3}, {'a':5, 'b':6, 'c':7} ] }
        result = convert(clubbed_json)
        clubbed_table = '<table><tr><th>sample</th><td><table><tr><th>a</th><th>c</th><th>b</th></tr>'\
                        '<tr><td>1</td><td>3</td><td>2</td></tr><tr><td>5</td><td>7</td><td>6</td></tr>'\
                        '</table></td></tr></table>'
        self.assertEqual(result, clubbed_table)

    def test_nested_left_to_right(self):
        result = convert(self.nested_json, build_direction="LEFT_TO_RIGHT")
        nested_table = '<table><tr><th>menu</th><td><table><tr><th>menuitem</th><td><table><tr><th>onclick</th>'\
                       '<th>value</th></tr><tr><td>CreateNewDoc()</td><td>New</td></tr><tr><td>OpenDoc()</td>'\
                       '<td>Open</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td></tr><tr><th>id</th>'\
                       '<td>file</td></tr><tr><th>value</th><td>File</td></tr></table></td></tr></table>'
        self.assertEqual(result, nested_table)

    def test_nested_top_to_bottom(self):
        result = convert(self.nested_json, build_direction="TOP_TO_BOTTOM")
        nested_table = '<table><tr><th>menu</th></tr><tr><td><table><tr><th>menuitem</th><th>id</th><th>value</th></tr>'\
                       '<tr><td><table><tr><th>onclick</th><th>value</th></tr><tr><td>CreateNewDoc()</td><td>New</td></tr>'\
                       '<tr><td>OpenDoc()</td><td>Open</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td>'\
                       '<td>file</td><td>File</td></tr></table></td></tr></table>'<commit_msg>Switched single line items using double quotes to single quotes.<commit_after>"""
Unit tests to verify proper conversion of JSON to HTML.
"""
from __future__ import division, print_function, absolute_import
import unittest
from json_to_table.json_to_table import convert, JsonConverter


class TestConvert(unittest.TestCase):
    def setUp(self):
        self.simple_json = {'key' : 'value'}
        self.custom_table_attributes = {'border' : 1}
        self.nested_json = {
        'menu': {
        'id': 'file',
        'value': 'File',
        'menuitem': [{'value': 'New', 'onclick': 'CreateNewDoc()'},
                     {'value': 'Open', 'onclick': 'OpenDoc()'},
                     {'value': 'Close', 'onclick': 'CloseDoc()'}]
    }}

    def test_invalid_build_direction(self):
        with self.assertRaises(ValueError) as context:
            convert(None, build_direction=None)
            self.assertTrue('Invalid build direction' in context.exception)

    def test_invalid_table_attributes(self):
        with self.assertRaises(TypeError) as context:
            convert(None, table_attributes=0)
            self.assertTrue('Table attributes must be either' in context.exception)

    def test_invalid_json(self):
        with self.assertRaises(AttributeError) as context:
            convert(None)

    def test_simple(self):
        result = convert(self.simple_json)
        simple_table = '<table><tr><th>key</th><td>value</td></tr></table>'
        self.assertEqual(result, simple_table)

    def test_custom_table_attributes(self):
        result = convert({}, table_attributes=self.custom_table_attributes)
        self.assertTrue('border=\"1\"' in result)

    def test_build_direction_top_to_bottom(self):
        result = convert(self.simple_json, build_direction='TOP_TO_BOTTOM')
        simple_table = '<table><tr><th>key</th></tr><tr><td>value</td></tr></table>'
        self.assertEqual(result, simple_table)

    def test_clubbed_json(self):
        clubbed_json = {'sample': [ {'a':1, 'b':2, 'c':3}, {'a':5, 'b':6, 'c':7} ] }
        result = convert(clubbed_json)
        clubbed_table = '<table><tr><th>sample</th><td><table><tr><th>a</th><th>c</th><th>b</th></tr>'\
                        '<tr><td>1</td><td>3</td><td>2</td></tr><tr><td>5</td><td>7</td><td>6</td></tr>'\
                        '</table></td></tr></table>'
        self.assertEqual(result, clubbed_table)

    def test_nested_left_to_right(self):
        result = convert(self.nested_json, build_direction='LEFT_TO_RIGHT')
        nested_table = '<table><tr><th>menu</th><td><table><tr><th>menuitem</th><td><table><tr><th>onclick</th>'\
                       '<th>value</th></tr><tr><td>CreateNewDoc()</td><td>New</td></tr><tr><td>OpenDoc()</td>'\
                       '<td>Open</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td></tr><tr><th>id</th>'\
                       '<td>file</td></tr><tr><th>value</th><td>File</td></tr></table></td></tr></table>'
        self.assertEqual(result, nested_table)

    def test_nested_top_to_bottom(self):
        result = convert(self.nested_json, build_direction='TOP_TO_BOTTOM')
        nested_table = '<table><tr><th>menu</th></tr><tr><td><table><tr><th>menuitem</th><th>id</th><th>value</th></tr>'\
                       '<tr><td><table><tr><th>onclick</th><th>value</th></tr><tr><td>CreateNewDoc()</td><td>New</td></tr>'\
                       '<tr><td>OpenDoc()</td><td>Open</td></tr><tr><td>CloseDoc()</td><td>Close</td></tr></table></td>'\
                       '<td>file</td><td>File</td></tr></table></td></tr></table>'
{'fraction_non_alphanumeric': 0.20229595408091838, 'fraction_numerical': 0.004199916001679967, 'mean_word_length': 3.185120093731693, 'pattern_counts': {'":': 10, '<': 323, '<?xml version=': 0, '>': 323, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1655316', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2296, 'n_words': 314}