changeset 171:d6652b33ce53

tests close to working, except for stderr?
author catherine@dellzilla
date Thu, 23 Oct 2008 14:28:03 -0400
parents 1e0e769ced2e
children 78ae062a1173
files sqlpython/test_sqlpyPlus.py sqlpython/test_sqlpyPlus.txt
diffstat 2 files changed, 83 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/test_sqlpyPlus.py	Thu Oct 23 13:05:03 2008 -0400
+++ b/sqlpython/test_sqlpyPlus.py	Thu Oct 23 14:28:03 2008 -0400
@@ -1,30 +1,58 @@
-import unittest, sys, tempfile, re
+import unittest, sys, tempfile, re, os.path
 from sqlpyPlus import *
+
+class Borg(object):
+    # from Python Cookbook, 2nd Ed., recipe 6.16
+    _shared_state = {}
+    def __new__(cls, *a, **k):
+        obj = object.__new__(cls, *a, **k)
+        obj.__dict__ = cls._shared_state
+        return obj
+    
+class OutputTrap(Borg):
+    def __init__(self):
+        self.old_stdout = sys.stdout
+        self.trap = tempfile.TemporaryFile()
+    def dump(self):
+        self.trap.seek(0)
+        result = self.trap.read()
+        self.trap.close()
+        self.trap = tempfile.TemporaryFile()
+        return result
+    def teardown(self):
+        sys.stdout = self.old_stdout
+
 class TestSqlPyPlus(unittest.TestCase):
-    transcriptReader = re.compile('testdata@orcl> (.*?)\n\n(.*?)(?=testdata@orcl>)', re.DOTALL)
+    transcriptReader = re.compile('testdata@eqdev> (.*?)\n\n(.*?)(?=testdata@eqdev>)', re.DOTALL)
+    transcriptFileName = 'test_sqlpyPlus.txt'
     def setUp(self):
-        transcriptFile = open('test_sqlpyPlus.txt')
+        self.outputTrap = OutputTrap()
+        transcriptFile = open(self.transcriptFileName)
         self.transcript = transcriptFile.read()
         transcriptFile.close()
         self.directives = self.transcriptReader.finditer(self.transcript)        
         self.testsession = sqlpyPlus()
-        self.testsession.onecmd('connect testdata/testdata@orcl')
-    def assertOutput(self, commandtext, expected):
+        self.testsession.onecmd('connect ' + connectString)
+        self.transcriptReader = re.compile(
+            '%s(.*?)\n\n(.*?)(?=%s)' % (self.testsession.prompt, self.testsession.prompt), re.DOTALL)
+        self.commandCleaner = '\n%s' % (self.testsession.continuationPrompt)
+    def assertOutput(self, commandtext, expected, lineNum):
         self.testsession.onecmd(commandtext)
-        falseStdOut.seek(0)
-        result = falseStdOut.read()
-        falseStdOut.close()
-        falseStdOut = tempfile.TemporaryFile()
-        self.assertEqual(expected.strip(), result.strip())
+        result = self.outputTrap.dump()
+        self.assertEqual(expected.strip(), result.strip(), 
+            '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 
+            (self.transcriptFileName, lineNum, commandtext, expected, result))
     def testall(self):
         for directive in self.directives:
             (command, result) = directive.groups()
-            self.assertOutput(command, result)
+            command = command.replace(self.commandCleaner, '')
+            self.assertOutput(command, result, lineNum=self.transcript.count('\n', 0, directive.start()))
     def tearDown(self):
-        pass
+        self.outputTrap.teardown()
 
-falseStdOut = tempfile.TemporaryFile()
-trueStdOut = sys.stdout
-sys.stdout = falseStdOut
-unittest.main()
-sys.stdout = trueStdOut
\ No newline at end of file
+try:        
+    connectString = sys.argv.pop(1)
+except IndexError:
+    print 'Usage: python %s username/password@oracleSID' % os.path.split(__file__)[-1]
+    sys.exit()
+unittest.main()
\ No newline at end of file
--- a/sqlpython/test_sqlpyPlus.txt	Thu Oct 23 13:05:03 2008 -0400
+++ b/sqlpython/test_sqlpyPlus.txt	Thu Oct 23 14:28:03 2008 -0400
@@ -1,4 +1,34 @@
-testdata@orcl> select * from species;
+testdata@eqdev> CREATE TABLE species (
+> ID     NUMBER(8,0) CONSTRAINT xnn1_species NOT NULL
+>                    CONSTRAINT xpk_species PRIMARY KEY,
+> NAME   VARCHAR2(12) CONSTRAINT xnn2_species NOT NULL
+>                     CONSTRAINT xuk1_species UNIQUE );
+
+Executed
+
+testdata@eqdev> :id = 0
+testdata@eqdev> :name = 'turtle'
+testdata@eqdev> INSERT INTO species VALUES (:id, :name);
+
+Executed (1 rows)
+
+testdata@eqdev> :id = 1
+testdata@eqdev> :name = 'python'
+testdata@eqdev> INSERT INTO species VALUES (:id, :name);
+
+Executed (1 rows)
+
+testdata@eqdev> :id = 2
+testdata@eqdev> :name = 'parrot'
+testdata@eqdev> INSERT INTO species VALUES (:id, :name);
+
+Executed (1 rows)
+
+testdata@eqdev> commit;
+
+Executed
+
+testdata@eqdev> select * from species;
 
 ID NAME  
 -- ------
@@ -8,7 +38,7 @@
 
 3 rows selected.
 
-testdata@orcl> select *
+testdata@eqdev> select *
 > from species;
 
 ID NAME  
@@ -19,7 +49,7 @@
 
 3 rows selected.
 
-testdata@orcl> select * from species\h
+testdata@eqdev> select * from species\h
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
@@ -55,4 +85,8 @@
 
 3 rows selected.
 
-testdata@orcl> 
+testdata@eqdev> DROP TABLE species;
+
+Executed
+
+testdata@eqdev>