Commits

Marcin Kasperski committed fef1cea

Test functions improvements

Comments (0)

Files changed (4)

src/mekk/fics/test_utils/__init__.py

 Routines used in unit tests of mekk.fics and derived modules. Not
 intended for non-test code.
 """
+
+from mekk.fics.test_utils.helpers import \
+    load_file_content, load_test_file, assert_dicts_equal, assert_tables_equal

src/mekk/fics/test_utils/helpers.py

 
 ## TODO: use pkg_resources instead of hacks below
 
-def load_file(subdir, name):
+import pkg_resources
+def load_test_file(subdir, name, package="mekk.fics"):
     """Loads file of given name from given subdir of tests
-    directory. Adapts to different test runner current directory
-    conventions to find the dir (cwd=tests dir, cwd=main module dir,
-    cwd=src dir, cwd=subdir of tests dir)."""
-    candidate_dirs = [
-        x % subdir 
-        for x in [ "%s", "tests/%s", "../tests/%s", "../%s" ] ]
-    return load_file_content(
-        pick_existing_dir(*candidate_dirs), name)
+    directory."""
+    # path/tomekk.fics/src
+    package_dir = pkg_resources.get_distribution(package).location
+    tests_dir = os.path.join(os.path.dirname(package_dir), "tests")
+    return load_file_content(os.path.join(tests_dir, subdir), name)
+    #candidate_dirs = [
+    #    x % subdir
+    #    for x in [ "%s", "tests/%s", "../tests/%s", "../%s" ] ]
+    #return load_file_content(
+    #    pick_existing_dir(*candidate_dirs), name)
 
 def pick_existing_dir(*dirs):
     """

tests/test_input_processor.py

 from mekk.fics.datatypes.notifications import ChannelTell, GameNote, AttributedTell
 from mekk.fics.datatypes.player import PlayerName, PlayerRating, ResultStats
 from mekk.fics.parsing import fics_parser
-from mekk.fics.test_utils.helpers import load_file as load_testdata_file, assert_dicts_equal
+from mekk.fics.test_utils.helpers import load_test_file, assert_dicts_equal
 import logging
 import datetime
 from decimal import Decimal
         self.replies[command_id].append( (command_name, status, command_data) )
 
     def test_input_processor(self):
-        file_text = load_testdata_file('ficsparserdata', 'session-short.transcript')
+        file_text = load_test_file('ficsparserdata', 'session-short.transcript')
         file_lines = file_text.split("\n\r")
         for line in file_lines:
             if 'Starting FICS session as' in line:

tests/test_parser.py

 from mekk.fics.parsing.reply.list_operations import parse_showlist_reply
 from mekk.fics.parsing.reply.observe import parse_observe_reply, parse_unobserve_reply
 from mekk.fics.parsing.reply_parser import parse_fics_reply
-from mekk.fics.test_utils.helpers import load_file as load_testdata_file, assert_dicts_equal, assert_tables_equal
+from mekk.fics.test_utils import load_test_file, assert_dicts_equal, assert_tables_equal
 from mekk.fics import errors
 import datetime
 from decimal import Decimal
 FICS_PARSE_DATA_DIR = "ficsparserdata"
 
 def load_parse_data_file(name):
-    return load_testdata_file(FICS_PARSE_DATA_DIR, name)
+    return load_test_file(FICS_PARSE_DATA_DIR, name)
 
 def load_parse_data_file_patching_continuations(name):
     data = load_parse_data_file(name)
             parse_observe_reply,
             "Blah blah")
     def testNormal(self):
-        d = parse_observe_reply( load_testdata_file( 'ficsparserdata', 'observe-normal.lines') )
+        d = parse_observe_reply( load_test_file( 'ficsparserdata', 'observe-normal.lines') )
         self.failUnless(d)
         self.failUnlessEqual(d.game_no, 92)
         self.failUnlessEqual(d.white_name, PlayerName('Motyl'))
         self.failUnless(isinstance(is12, style12.Style12))
         self.failUnlessEqual(str(is12), "<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 92 Motyl Aretus 0 15 10 39 39 900 900 1 none (0:00) none 0 0 0")
     def testProblematic(self):
-        d = parse_observe_reply( load_testdata_file( 'ficsparserdata', 'observe-spec-norating.lines')  )
+        d = parse_observe_reply( load_test_file( 'ficsparserdata', 'observe-spec-norating.lines')  )
         self.failUnless(d)
         self.failUnlessEqual(d.game_no, 161)
         self.failUnlessEqual(d.white_name, PlayerName('ilmagicoalverman'))
         self.failUnlessEqual(str(is12), "<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 161 ilmagicoalverman bibibibi 0 15 0 39 39 900 900 1 none (0:00) none 0 0 0")
 
     def testProblematic2(self):
-        d = parse_observe_reply(load_testdata_file( 'ficsparserdata', 'observe-spec-shortrating.lines') )
+        d = parse_observe_reply(load_test_file( 'ficsparserdata', 'observe-spec-shortrating.lines') )
         self.failUnless(d)
         self.failUnlessEqual(d.game_no, 222)
         self.failUnlessEqual(d.white_name, PlayerName('Johnnyp'))
         self.failUnlessEqual(str(is12), "<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 222 Johnnyp schakeric 0 20 0 39 39 1200 1200 1 none (0:00) none 0 0 0")
 
     def testProblematic3(self):
-        d = parse_observe_reply(load_testdata_file( 'ficsparserdata', 'observe-spec-shortrating-white.lines') )
+        d = parse_observe_reply(load_test_file( 'ficsparserdata', 'observe-spec-shortrating-white.lines') )
         self.failUnless(d)
         self.failUnlessEqual(d.game_no, 221)
         self.failUnlessEqual(d.white_name, PlayerName('GuestMGVG'))
         self.failUnlessEqual(d.game_spec.clock, GameClock(5,5))
 
     def testWild4(self):
-        d = parse_observe_reply(load_testdata_file( 'ficsparserdata', 'observe-wild4.lines') )
+        d = parse_observe_reply(load_test_file( 'ficsparserdata', 'observe-wild4.lines') )
         self.failUnless(d)
         self.failUnlessEqual(d.game_no, 226)
         self.failUnlessEqual(d.white_name, PlayerName('Raph'))