~ubuntu-branches/ubuntu/trusty/txtorcon/trusty-proposed

« back to all changes in this revision

Viewing changes to test/test_util_imports.py

  • Committer: Package Import Robot
  • Author(s): Jérémy Bobbio
  • Date: 2014-01-21 15:10:52 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140121151052-r1dw06if9912ihbp
Tags: 0.9.1-1
* New upstream release.
* Update debian/watch and verify upstream signature
* Improve debian/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.trial import unittest
 
2
 
 
3
import sys
 
4
import functools
 
5
 
 
6
 
 
7
def fake_import(orig, name, *args, **kw):
 
8
    ##print "IMPORTING", name
 
9
    if 'GeoIP' in name:
 
10
        raise ImportError('testing!')
 
11
    return orig(*((name,) + args), **kw)
 
12
 
 
13
 
 
14
class TestImports(unittest.TestCase):
 
15
 
 
16
    def test_no_GeoIP(self):
 
17
        """
 
18
        make sure the code we run if there's no GeoIP installed
 
19
        doesn't do anything horrific
 
20
        """
 
21
 
 
22
        global __import__
 
23
        orig = __import__
 
24
        try:
 
25
            # attempt to ensure we've unimportted txtorcon.util
 
26
            del sys.modules['txtorcon.util']
 
27
            import gc
 
28
            gc.collect()
 
29
 
 
30
            # replace global import with our test import, which will
 
31
            # throw on GeoIP import no matter what
 
32
            global __builtins__
 
33
            __builtins__['__import__'] = functools.partial(fake_import, orig)
 
34
 
 
35
            # now ensure we set up all the databases as "None" when we
 
36
            # import w/o the GeoIP thing available.
 
37
            import txtorcon.util
 
38
            self.assertEqual(None, txtorcon.util.city)
 
39
            self.assertEqual(None, txtorcon.util.asn)
 
40
            self.assertEqual(None, txtorcon.util.country)
 
41
 
 
42
        finally:
 
43
            __import__ = orig