From 135866c38b7d8c6155e21ada6a670aba5aceae73 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Thu, 19 Dec 2013 17:44:03 +0100
Subject: [PATCH] Update unit tests for new style configuration

---
 mutalyzer/config/__init__.py         | 23 ++++++++++-------------
 mutalyzer/config/default_settings.py |  3 +++
 tests/test_crossmap.py               |  4 ++++
 tests/test_describe.py               |  4 ++++
 tests/test_grammar.py                |  4 ++++
 tests/test_mapping.py                |  4 ++++
 tests/test_mutator.py                |  4 ++++
 tests/test_parsers_genbank.py        |  4 ++++
 tests/test_services_json.py          |  4 ++++
 tests/test_services_soap.py          |  4 ++++
 tests/test_variantchecker.py         |  4 ++++
 tests/test_website.py                |  4 ++++
 tests/utils.py                       | 14 ++++++++++++++
 13 files changed, 67 insertions(+), 13 deletions(-)
 create mode 100644 tests/utils.py

diff --git a/mutalyzer/config/__init__.py b/mutalyzer/config/__init__.py
index 69564731..5617162b 100644
--- a/mutalyzer/config/__init__.py
+++ b/mutalyzer/config/__init__.py
@@ -6,8 +6,9 @@ module and overridden by any values from the module specified by the
 `MUTALYZER_SETTINGS`.
 
 Alternatively, the default values can be overridden manually using the
-:meth:`settings.configure` method, in which case the `MUTALYZER_SETTINGS`
-environment variable will not be used.
+:meth:`settings.configure` method before the first use of a configuration
+value, in which case the `MUTALYZER_SETTINGS` environment variable will not be
+used.
 """
 
 
@@ -40,28 +41,24 @@ class LazySettings(util.LazyObject):
     .. note:: Django also does some logging config magic here, we did not copy
         that.
     """
-    def _setup(self, settings=None):
+    def _setup(self, from_environment=True):
         """
         Load the settings module pointed to by the environment variable. This
         is used the first time we need any settings at all, if the user has not
-        previously configured the settings manually.
+        previously configured the settings manually with :meth:`configure`.
         """
         self._wrapped = Settings()
         self._wrapped.from_object('mutalyzer.config.default_settings')
-        if settings is None:
+        if from_environment:
             self._wrapped.from_envvar(ENVIRONMENT_VARIABLE)
-        else:
-            self._wrapped.update(settings)
 
     def configure(self, settings):
         """
-        Called to manually configure the settings. The 'default_settings'
-        parameter sets where to retrieve any unspecified values from (its
-        argument must support attribute access (__getattr__)).
+        Called to manually configure the settings.
         """
-        if self._wrapped is not None:
-            raise RuntimeError('settings already configured')
-        self._setup(settings)
+        if self._wrapped is None:
+            self._setup(from_environment=False)
+        self._wrapped.update(settings)
 
     @property
     def configured(self):
diff --git a/mutalyzer/config/default_settings.py b/mutalyzer/config/default_settings.py
index bc4b12cf..3d2e33cd 100644
--- a/mutalyzer/config/default_settings.py
+++ b/mutalyzer/config/default_settings.py
@@ -7,6 +7,9 @@ pointed-to by the `MUTALYZER_SETTINGS` environment variable.
 # Use Mutalyzer in debug mode.
 DEBUG = True
 
+# We are running unit tests.
+TESTING = False
+
 # This address is used in contact information on the website, as sender in
 # batch job notifications, and with retrieval of records at the NCBI using
 # Entrez.
diff --git a/tests/test_crossmap.py b/tests/test_crossmap.py
index 230a7083..d0f60411 100644
--- a/tests/test_crossmap.py
+++ b/tests/test_crossmap.py
@@ -3,6 +3,10 @@ Tests for the Crossmap module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 from nose.tools import *
 
diff --git a/tests/test_describe.py b/tests/test_describe.py
index c7a5560f..d4a3a96c 100644
--- a/tests/test_describe.py
+++ b/tests/test_describe.py
@@ -3,6 +3,10 @@ Tests for the mutalyzer.describe module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 import os
 from nose.tools import *
diff --git a/tests/test_grammar.py b/tests/test_grammar.py
index a27b473d..2a20abd5 100644
--- a/tests/test_grammar.py
+++ b/tests/test_grammar.py
@@ -3,6 +3,10 @@ Tests for the mutalyzer.grammar module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 import os
 from nose.tools import *
diff --git a/tests/test_mapping.py b/tests/test_mapping.py
index 1732d9af..33674e0c 100644
--- a/tests/test_mapping.py
+++ b/tests/test_mapping.py
@@ -3,6 +3,10 @@ Tests for the mapping module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 from nose.tools import *
 
diff --git a/tests/test_mutator.py b/tests/test_mutator.py
index f6ff2dfc..8b32e8f6 100644
--- a/tests/test_mutator.py
+++ b/tests/test_mutator.py
@@ -3,6 +3,10 @@ Tests for the mutalyzer.mutator module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 import re
 import os
diff --git a/tests/test_parsers_genbank.py b/tests/test_parsers_genbank.py
index c7da5791..a04aa5fa 100644
--- a/tests/test_parsers_genbank.py
+++ b/tests/test_parsers_genbank.py
@@ -3,6 +3,10 @@ Tests for the mutalyzer.parsers.genbank module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 from nose.tools import *
 
diff --git a/tests/test_services_json.py b/tests/test_services_json.py
index 0b235781..35bbd180 100644
--- a/tests/test_services_json.py
+++ b/tests/test_services_json.py
@@ -3,6 +3,10 @@ Tests for the JSON interface to Mutalyzer.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 from nose.tools import *
 import simplejson as json
 from spyne.server.null import NullServer
diff --git a/tests/test_services_soap.py b/tests/test_services_soap.py
index 09f99615..cea1ca25 100644
--- a/tests/test_services_soap.py
+++ b/tests/test_services_soap.py
@@ -3,6 +3,10 @@ Tests for the SOAP interface to Mutalyzer.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 import datetime
 import logging
 import os
diff --git a/tests/test_variantchecker.py b/tests/test_variantchecker.py
index a9d44a86..78033e3f 100644
--- a/tests/test_variantchecker.py
+++ b/tests/test_variantchecker.py
@@ -3,6 +3,10 @@ Tests for the variantchecker module.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 from nose.tools import *
 
diff --git a/tests/test_website.py b/tests/test_website.py
index 52e50795..02b1de9f 100644
--- a/tests/test_website.py
+++ b/tests/test_website.py
@@ -11,6 +11,10 @@ I just installed webtest by 'easy_install webtest'.
 """
 
 
+from utils import TEST_SETTINGS
+from mutalyzer.config import settings
+settings.configure(TEST_SETTINGS)
+
 #import logging; logging.basicConfig()
 import os
 import re
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 00000000..c119d67b
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,14 @@
+import os
+import tempfile
+
+
+log_handle, log_filename = tempfile.mkstemp()
+os.close(log_handle)
+
+
+TEST_SETTINGS = dict(
+    DEBUG     = True,
+    TESTING   = True,
+    CACHE_DIR = tempfile.mkdtemp(),
+    LOG_FILE  = log_filename
+)
-- 
GitLab