Commits

Anonymous committed 79dd72e

UnitTest:
- test calling concatenate() without the basePath and resultingFileName arguments
- rearrange comments

Fix:
- add missing default value in resultingFileName argument

Comments (0)

Files changed (4)

concatenate_files.py

     return text
 
 
+#HERE poder chamar a funcao tambem com uma variavel (no formato json), a substituir o ficheiro de config 
 
-
-def concatenate( pathToConfig, resultingFileName, basePath="" ):
+def concatenate( pathToConfig, resultingFileName="result.txt", basePath="" ):
 
     '''
 

unit_test/result.txt

-bbcc
+aadd

unit_test/test.py

-import unittest
+import unittest
 import os.path
 import sys
 
     # where we have the concatenate module
 CONCATENATE_PATH = os.path.abspath( os.path.join(TEST_PATH, '..') )
 
+    # so we can refer to the concatenate_files.py module
 sys.path.append( CONCATENATE_PATH )
 
 
 
 
 UNIT_TEST_PATH = os.path.dirname( os.path.abspath(__file__) )
- 
+
 TEST_DATA_PATH = os.path.abspath( os.path.join(UNIT_TEST_PATH, 'test_data') )
 
 
+
+def manuallyConcatenateFiles( listOfPaths ):
+
+    '''
+        Arguments:
+
+            listOfPaths (list) : list of strings with the path to the files
+    '''
+
+    concatenatedText = ""
+
+    for path in listOfPaths:
+
+        with open( path, 'r', encoding = 'utf-8' ) as f:
+            concatenatedText += f.read()
+
+    return concatenatedText
+
+
+
+
 class ConcatenateTest( unittest.TestCase ):
 
     def test_invalidBasePath( self ):
-        
-        self.assertRaises( ValueError, concatenate, "", "result.txt", "aaa" )
 
-        # failed to found the configuration file
+        '''
+            We're passing an invalid path to the basePath argument.
+        '''
+
+        self.assertRaises( ValueError, concatenate, "config.txt", "result.txt", "aaa" )
+
+
     def test_fileNotFound( self ):
-    
+
+        '''
+            Failed to found the configuration file.
+        '''
+
         self.assertRaises( IOError, concatenate, os.path.join( TEST_DATA_PATH, "no_config.txt" ), "result.txt" )
-    
-        # found the configuration file, but is invalid JSON
+
+
     def test_wrongJson( self ):
-    
+
+        '''
+            Found the configuration file, but is invalid JSON.
+        '''
+
         self.assertRaises( CouldntConvertFromJsonError, concatenate, os.path.join( TEST_DATA_PATH, "invalid_json_config.txt" ), "result.txt" )
-        
-        # the JSON is valid, but not on the format required
+
+
     def test_notADict( self ):
-    
+
+        '''
+            The JSON is valid, but not on the format required.
+        '''
+
         self.assertRaises( NotADictError, concatenate, os.path.join( TEST_DATA_PATH, "not_dict_config.txt" ), "result.txt" )
-    
+
     def test_config( self ):
-        
+
+        '''
+            Tests a well-written configuration file (see "config.txt").
+        '''
+
             # manually concatenate the files, for comparison later
-        concatenatedText = ""
-        
-        with open( os.path.join( TEST_DATA_PATH, "aa.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
-            
-        with open( os.path.join( TEST_DATA_PATH, "test", "bb.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
-            
-        with open( os.path.join( TEST_DATA_PATH, "test", "cc.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
+        files = [ os.path.join( TEST_DATA_PATH, "aa.txt" ),
+                    os.path.join( TEST_DATA_PATH, "test", "bb.txt" ),
+                    os.path.join( TEST_DATA_PATH, "test", "cc.txt" ),
+                    os.path.join( TEST_DATA_PATH, "test", "test2", "dd.txt" ) ]
 
-        with open( os.path.join( TEST_DATA_PATH, "test", "test2", "dd.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
-    
-    
+
+        compareText = manuallyConcatenateFiles( files )
+
+
         concatenate( os.path.join( TEST_DATA_PATH, "config.txt" ), "result.txt", TEST_DATA_PATH )
-        
+
         with open( "result.txt", 'r' ) as f:
-            result = f.read() 
-            
-        self.assertEqual( concatenatedText, result )
-    
-    
-    
+            result = f.read()
+
+        self.assertEqual( compareText, result )
+
+
+
     def test_config2( self ):
-        
+
+        '''
+            Tests another well-written configuration file ("config2.txt").
+        '''
+
             # manually concatenate the files, for comparison later
-        concatenatedText = ""
-        
-        with open( os.path.join( TEST_DATA_PATH, "test", "bb.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
-            
-        with open( os.path.join( TEST_DATA_PATH, "test", "cc.txt" ), 'r' ) as f:
-            concatenatedText += f.read()
-            
+        files = [ os.path.join( TEST_DATA_PATH, "test", "bb.txt" ),
+                    os.path.join( TEST_DATA_PATH, "test", "cc.txt" ) ]
+
+        compareText = manuallyConcatenateFiles( files )
+
         concatenate( os.path.join( TEST_DATA_PATH, "config2.txt" ), "result.txt", TEST_DATA_PATH )
-        
+
         with open( "result.txt", 'r' ) as f:
-            result = f.read() 
-            
-            
-        self.assertEqual( concatenatedText, result )
-    
-    
-    
-    
+            result = f.read()
+
+
+        self.assertEqual( compareText, result )
+
+
+    def test_defaultBasePath( self ):
+
+        '''
+            Calls concatenate() without providing the basePath argument (defaults to the current path where the concatenate_files.py  script is executed)
+
+            Calls without the resultingFileName argument too (should be "result.txt")
+
+            ( configuration file: config3_defaultBasePath.txt )
+        '''
+
+        files = [ os.path.join( TEST_DATA_PATH, "aa.txt" ),
+                   os.path.join( TEST_DATA_PATH, "test", "test2", "dd.txt" ) ]
+
+        compareText = manuallyConcatenateFiles( files )
+
+
+        concatenate( os.path.join( TEST_DATA_PATH, "config3_defaultBasePath.txt" ) )
+
+
+        with open( "result.txt", 'r', encoding="utf-8" ) as f:
+            result = f.read()
+
+        self.assertEqual( compareText, result )
+
+
+
+
 if __name__ == '__main__':
 
-    unittest.main()
+    unittest.main()

unit_test/test_data/config3_defaultBasePath.txt

+{
+    "unit_test/test_data":
+        [
+        "aa.txt",
+        "test/test2/dd.txt"
+        ]
+}