summaryrefslogtreecommitdiff
path: root/tests/utils.py
blob: 92d7a4b33ba336291f370f209bc160505197eeda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-

"""Helpers for testing."""

import codecs
import difflib
import os
import unittest

from sqlparse import compat
import sqlparse.utils

NL = '\n'
DIR_PATH = os.path.abspath(os.path.dirname(__file__))
PARENT_DIR = os.path.dirname(DIR_PATH)
FILES_DIR = os.path.join(DIR_PATH, 'files')


def load_file(filename, encoding='utf-8'):
    """Opens filename with encoding and return it's contents."""
    f = codecs.open(os.path.join(FILES_DIR, filename), 'r', encoding)
    data = f.read()
    f.close()
    return data


class TestCaseBase(unittest.TestCase):
    """Base class for test cases with some additional checks."""

    # Adopted from Python's tests.
    def ndiffAssertEqual(self, first, second):
        """Like failUnlessEqual except use ndiff for readable output."""
        if first != second:
            sfirst = compat.text_type(first)
            ssecond = compat.text_type(second)
            # Using the built-in .splitlines() method here will cause incorrect
            # results when splitting statements that have quoted CR/CR+LF
            # characters.
            sfirst = sqlparse.utils.split_unquoted_newlines(sfirst)
            ssecond = sqlparse.utils.split_unquoted_newlines(ssecond)
            diff = difflib.ndiff(sfirst, ssecond)
            fp = compat.StringIO()
            fp.write(NL)
            fp.write(NL.join(diff))
            print(fp.getvalue())
            raise self.failureException(fp.getvalue())