from disco.test import DiscoJobTestFixture, DiscoTestCase
from disco.util import external

from subprocess import check_call, STDOUT
from os import uname, path

class ExternalTestCase(DiscoJobTestFixture, DiscoTestCase):
    inputs     = ["ape", "cat", "dog"]
    ext_params = {"test1": "1,2,3", "one two three": "dim\ndam\n", "dummy": "value"}
    sort       = False
    ext_map    = True

    def setUp(self):
        if uname()[0] == 'Darwin':
            self.skipTest('Cannot build static test_external on OS X')
        else:
            home = self.settings['DISCO_HOME']
            check_call(['gcc', '-g', '-O3', '-static', '-Wall',
                        '-I', path.join(home, 'ext'),
                        '-o', path.join(home, 'tests', 'test_external'),
                        path.join(home, 'ext', 'disco.c'),
                        path.join(home, 'tests', 'test_external.c'),
                        '-l', 'Judy'],
                       stderr=STDOUT)
            self.map = external([path.join(home, 'tests', 'test_external')])
            super(ExternalTestCase, self).setUp()

    @staticmethod
    def reduce(iter, out, params):
        for k, v in iter:
            out.add("red_" + k, "red_" + v)

    def getdata(self, path):
        return 'test_%s\n' % path

    @property
    def answers(self):
        yield 'discoapi', ''

    def runTest(self):
        results = sorted((v, k) for k, v in self.results)
        for n, (v, k) in enumerate(results):
            self.assertEquals(k, 'red_dkey')
            self.assertEquals(v, 'red_test_%s\n' % self.inputs[n / 3])
        self.assertEquals(len(results), 9)

@@ -20,42 +20,25 @@
 ort 
-DiscoJobTestFixture, DiscoTestCase
+TestCase, TestJob
 %0Afro
@@ -155,92 +155,21 @@
 rnal
-TestCase(Disco
 Job
+(
 Test
-Fixture, DiscoTestCase):%0A    inputs     = %5B%22ape%22, %22cat%22, %22dog%22%5D
+Job):
 %0A   
@@ -263,43 +263,263 @@
 ort 
-      = False%0A    ext_map    = True
+= False%0A%0A    @staticmethod%0A    def reduce(iter, params):%0A        for k, v in iter:%0A            yield %22red_%22 + k, %22red_%22 + v%0A%0Aclass ExternalTestCase(TestCase):%0A    inputs = %5B'ape', 'cat', 'dog'%5D%0A%0A    def serve(self, path):%0A        return 'test_%25s%5Cn' %25 path
 %0A%0A  
@@ -524,21 +524,23 @@
     def 
-setUp
+runTest
 (self):%0A
@@ -706,16 +706,77 @@
 _HOME'%5D%0A
+            test = path.join(home, 'tests', 'test_external')%0A
         
@@ -919,49 +919,12 @@
 o', 
-path.join(home, 'tests', 'test_external')
+test
 ,%0A  
@@ -1147,171 +1147,73 @@
 elf.
-map = e
+job = E
 xternal
-(%5Bpath.join(home, 'tests', 'test_external')%5D)%0A            super(ExternalTestCase, self).setUp()%0A%0A    @staticmethod%0A    def reduce(iter, out, params):
+Job().run(input=self.test_server.urls(self.inputs),
 %0A   
@@ -1221,225 +1221,67 @@
     
-for k, v in iter:%0A            out.add(%22red_%22 + k, %22red_%22 + v)%0A%0A    def getdata(self, path):%0A        return 'test_%25s%5Cn' %25 path%0A%0A    @property%0A    def answers(self):%0A        yield 'discoapi', ''%0A%0A    def runTest(self):%0A
+                                 map=external(%5Btest%5D))%0A    
     
@@ -1332,18 +1332,32 @@
 .results
-)%0A
+(self.job))%0A    
         
@@ -1397,32 +1397,36 @@
 s):%0A            
+    
 self.assertEqual
@@ -1442,16 +1442,20 @@
 _dkey')%0A
+    
         
@@ -1517,16 +1517,20 @@
 n / 3%5D)%0A
+    
         

convert test_external; (not tested)