Project

General

Profile

Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / south / tests / logger.py @ d1a4905f

History | View | Annotate | Download (2.88 KB)

1
import logging
2
import os
3
import tempfile
4
from south.tests import unittest
5
import StringIO
6
import sys
7

    
8
from django.conf import settings
9
from django.db import connection, models
10

    
11
from south.db import db
12
from south.logger import close_logger
13

    
14
class TestLogger(unittest.TestCase):
15

    
16
    """
17
    Tests if the logging is working reasonably. Some tests ignored if you don't
18
    have write permission to the disk.
19
    """
20
    
21
    def setUp(self):
22
        db.debug = False
23
        self.test_path = tempfile.mkstemp(suffix=".south.log")[1]
24
    
25
    def test_db_execute_logging_nofile(self):
26
        "Does logging degrade nicely if SOUTH_LOGGING_ON not set?"
27
        settings.SOUTH_LOGGING_ON = False     # this needs to be set to False
28
                                              # to avoid issues where other tests
29
                                              # set this to True. settings is shared
30
                                              # between these tests.
31
        db.create_table("test9", [('email_confirmed', models.BooleanField(default=False))])
32

    
33
    def test_db_execute_logging_off_with_basic_config(self):
34
        """
35
        Does the south logger avoid outputing debug information with
36
        south logging turned off and python logging configured with
37
        a basic config?"
38
        """
39
        settings.SOUTH_LOGGING_ON = False
40

    
41
        # Set root logger to capture WARNING and worse
42
        logging_stream = StringIO.StringIO()
43
        logging.basicConfig(stream=logging_stream, level=logging.WARNING)
44

    
45
        db.create_table("test12", [('email_confirmed', models.BooleanField(default=False))])
46

    
47
        # since south logging is off, and our root logger is at WARNING
48
        # we should not find DEBUG info in the log
49
        self.assertEqual(logging_stream.getvalue(), '')
50

    
51
    def test_db_execute_logging_validfile(self):
52
        "Does logging work when passing in a valid file?"
53
        settings.SOUTH_LOGGING_ON = True
54
        settings.SOUTH_LOGGING_FILE = self.test_path
55
        # Check to see if we can make the logfile
56
        try:
57
            fh = open(self.test_path, "w")
58
        except IOError:
59
            # Permission was denied, ignore the test.
60
            return
61
        else:
62
            fh.close()
63
        # Do an action which logs
64
        db.create_table("test10", [('email_confirmed', models.BooleanField(default=False))])
65
        # Close the logged file
66
        close_logger()
67
        try:
68
            os.remove(self.test_path)
69
        except:
70
            # It's a tempfile, it's not vital we remove it.
71
            pass
72

    
73
    def test_db_execute_logging_missingfilename(self):
74
        "Does logging raise an error if there is a missing filename?"
75
        settings.SOUTH_LOGGING_ON = True
76
        settings.SOUTH_LOGGING_FILE = None
77
        self.assertRaises(
78
            IOError,
79
            db.create_table,
80
            "test11",
81
            [('email_confirmed', models.BooleanField(default=False))],
82
        )