Commits

AJ R committed 1673ec9

added test_test_SPSSIOWarning.py (skipped in Python 2, passes in 3)

Comments (0)

Files changed (2)

savReaderWriter/error.py

     pass
 
 # Warnings are usually harmless!
-env = os.environ.get("SAVRW_DISPLAY_WARNS")
-SAVRW_DISPLAY_WARNS = bool(int(env)) if str(env).isdigit() else False
-action = "default" if SAVRW_DISPLAY_WARNS else "ignore"
+action = os.environ.get("SAVRW_DISPLAY_WARNS")
+action = action.lower() if action else "ignore"
+actions = ("error", "ignore", "always", "default", "module", "once")
+if action not in actions:
+   raise ValueError("If set, environment variable SAVRW_DISPLAY_WARNS must be"
+                    " one of the following values:\n" % ", ".join(actions))
 warnings.simplefilter(action, SPSSIOWarning)
 
 def checkErrsWarns(msg, retcode):
     if retcode > 0:
         raise SPSSIOError(msg, retcode)
     elif retcode < 0:
-        warnings.warn(msg, SPSSIOWarning, stacklevel=2)
+        warnings.warn(msg, SPSSIOWarning, stacklevel=2)

savReaderWriter/unit_tests/test_SPSSIOWarning.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+##############################################################################
+## Check if the custom error class does its job
+##############################################################################
+
+import unittest
+import os
+import sys
+import warnings
+import savReaderWriter as rw
+
+warnings_are_ignored = os.getenv("PYTHONWARNINGS") == "ignore"
+
+class test_SPSSIOWarning(unittest.TestCase):
+
+    def setUp(self):
+        self.savFileName = "../savReaderWriter/test_data/Employee data.sav"
+        os.environ["SAVRW_DISPLAY_WARNS"] = "always"
+
+    # I have no idea why this works differently in Python 2
+    @unittest.skipIf(sys.version_info[0] < 3, "Python 2: Warnings different?")
+    @unittest.skipIf(warnings_are_ignored, "Warnings are ignored")
+    def test_raises_SPSSIOWarning(self):
+        module = rw if sys.version_info[0] > 2 else rw.error
+        SPSSIOWarning = module.SPSSIOWarning  
+        #with self.assertRaises(SPSSIOWarning) as e:
+        with warnings.catch_warnings(record=True) as w:
+            with rw.SavHeaderReader(self.savFileName) as header:
+                metadata = str(header)
+                self.assertTrue(issubclass(w[-1].category, UserWarning))
+                self.assertTrue("SPSS_NO_CASEWGT" in str(w[-1].message))
+  
+    def tearDown(self):
+        try:
+            del os.environ["SAVRW_DISPLAY_WARNS"]
+        except:
+            pass
+
+if __name__ == "__main__":
+    unittest.main()
+
+