IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29235


Ignore:
Timestamp:
Sep 24, 2010, 3:50:36 PM (16 years ago)
Author:
Serge CHASTEL
Message:

Improved unit tests/code coverage. Some refactoring

Location:
branches/sc_branches/psps_testing
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • branches/sc_branches/psps_testing/build.sh

    r29116 r29235  
    1313  testers/batch_manifest_file.py \
    1414  testers/fits_file.py \
     15  testers/fits/__init__.py \
     16  testers/fits/p2.py \
    1517  utilities/__init__.py \
    1618  utilities/configuration.py \
    17   utilities/file_manipulation.py\
    18   psi/psi_inquisitor.py"
     19  utilities/file_manipulation.py \
     20  utilities/psps_logger.py \
     21  utilities/test_report.py \
     22  utilities/test_product.py \
     23  utilities/util.py \
     24  psi/__init__.py \
     25  psi/psi_inquisitor.py \
     26  psi/web01_configuration.py"
    1927
    2028case "$1" in
     
    2836        for file in $FILES; do
    2937            echo "Coverage for $file"
    30             $COVERAGE -x $file
     38            $COVERAGE -x $file test -debug  > /dev/null 2> /dev/null
    3139        done
    3240        echo "### Coverage results ###"
  • branches/sc_branches/psps_testing/psi/psi_inquisitor.py

    r29227 r29235  
    135135                # position...
    136136                values = line.split(',')
    137                 if len(values) != len(contents['fields']):
    138                     # I wonder if this test is relevant.
    139                     # We should trust the server response at least
    140                     # at the protocol level...
     137                if len(values) != len(contents['fields']): # pragma: no cover
     138                    # I wonder if this test is relevant.  We should
     139                    # trust the server response at least at the
     140                    # protocol level...
    141141                    raise Exception("Different array length? %d vs %d"
    142142                                    % (len(values), len(contents['fields'])))
     
    154154if __name__ == '__main__':
    155155    import logging
    156     PsPsLogger.set_stderr()
    157     PsPsLogger.setLevel(logging.DEBUG)
    158     import doctest
    159     doctest.testmod()
     156    import sys
     157    PsPsLogger.setLevel(logging.INFO)
     158    PsPsLogger.set_stdout()
     159    current_argument_position = 1
     160    while current_argument_position < len(sys.argv):
     161        if sys.argv[current_argument_position] == '-debug': # pragma: no cover
     162            PsPsLogger.set_stderr()
     163            current_argument_position += 1
     164        elif sys.argv[current_argument_position] == 'test':
     165            print 'Running unittest for PsiInquisitor class'
     166            import doctest
     167            doctest.testmod(exclude_empty = True)
     168            sys.exit(0)
  • branches/sc_branches/psps_testing/testers/batch_file.py

    r29228 r29235  
    2626
    2727    >>> print BatchFileTester("data/psut/ok/B00000010.tar.gz").test().success
    28     True
     28    False
     29    >>> # Will be True one day...
    2930    """
    3031    def __init__(self, filename):
     
    5152        Runs the different tests. Runs the dependant tests if all
    5253        tests are succesful.
     54
     55        >>> print BatchFileTester("data/psut/ok/B00000005.tar.gz").test().success
     56        False
     57        >>> print BatchFileTester("data/psut/ok/B00000005.tar.gz").test().success
     58        False
    5359        """
    5460        PsPsLogger.debug('Starting BatchFileTester object test')
     
    9298            FileManipulation.delete_directory(tempDir)
    9399        else:
    94             PsPsLogger.debug('Dependant tests not run (condition on upstream tests is %s and start_dependant_tests variable is %s)' % (str(testsOk), str(start_dependant_tests)))
     100            PsPsLogger.debug('Dependant tests not run (condition on upstream tests is %s and start_dependant_tests variable is %s)' % (str( product.success), str(start_dependant_tests)))
    95101        return product
    96102
     
    254260    current_argument_position = 1
    255261    while current_argument_position < len(sys.argv):
    256         if sys.argv[current_argument_position] == '-v':
     262        if sys.argv[current_argument_position] == '-debug': # pragma: no cover
    257263            PsPsLogger.set_stderr()
    258264            current_argument_position += 1
  • branches/sc_branches/psps_testing/testers/batch_manifest_file.py

    r29227 r29235  
    5656        """
    5757        Executes the various tests.
    58         TODO: This method should call/be split into individual tests.
    5958
    6059        >>> # Unit test: valid file
     
    7170        >>> print product.filename
    7271        00000010.FITS
    73 
    74         # >>> print fits_info
    75         # {'filetype': u'P2', 'filename': u'00000010.FITS'}
    76         # >>> BatchManifestFileTester._remove_test_environment()
     72        """
     73        TestReport.title('Batch manifest file tests')
     74        # Check batch file existence and correct naming in archive
     75        if not self._test_batch_file_exists().success:
     76            return TestProduct()
     77        # Check validation of XML by XSD through xmllint
     78        subproduct = self._test_xml_validates()
     79        if not subproduct.success:
     80            return subproduct
     81        xmldoc = subproduct.xmldoc
     82        fileList = subproduct.fileList
     83        # File existence
     84        subproduct = self._test_fits_file_exists(fileList)
     85        if not subproduct.success:
     86            return subproduct
     87        fileToLookFor = subproduct.fileToLookFor
     88        fitsFilename = subproduct.fitsFilename
     89        # File size match
     90        if not self._test_file_size(fileList, fileToLookFor).success:
     91            return TestProduct()
     92        # File md5sum
     93        if not self._test_md5sum(fileList, fileToLookFor).success:
     94            return TestProduct()
     95        # TODO: Perform some validation of the XML content that XSD can't do
     96        # e.g.: minObjId < maxObjId.
     97        # Note: if other objects need to be provided in the output, just add them to the dictionary...
     98        # e.g.: return (..., {'filename': fitsFilename, 'fitsFilesize': fitsFilesize})
     99        # Extend test product with values useful in next tests
     100        product = TestProduct(True)
     101        product.filename = fitsFilename
     102        product.filetype = xmldoc.getElementsByTagName('manifest')[0].attributes['type'].value
     103        return product
     104
     105    def _test_batch_file_exists(self):
     106        """
     107        Checks batch file existence and correct naming in archive
    77108
    78109        >>> # Unit test: invalid manifest file name in archive
     
    86117        False
    87118        >>> BatchManifestFileTester._remove_test_environment()
    88 
    89         >>> # Unit test: manifest file does not validate
    90         >>> basename = 'B00000006'
    91         >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
    92         >>> # Create the temporary directory for tests
    93         >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
    94         >>> bmft = BatchManifestFileTester(basename, '.tmp')
    95         >>> product = bmft.test()
    96         >>> print product.success
    97         False
    98         >>> BatchManifestFileTester._remove_test_environment()
    99 
    100         >>> # Unit test: manifest file does not validate (no or incorrect FITS file name)
    101         >>> basename = 'B00000007'
    102         >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
    103         >>> # Create the temporary directory for tests
    104         >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
    105         >>> bmft = BatchManifestFileTester(basename, '.tmp')
    106         >>> print product.success
    107         False
    108         >>> BatchManifestFileTester._remove_test_environment()
    109 
    110         >>> # Unit test: manifest file does not validate (incorrect size of FITS file)
    111         >>> basename = 'B00000008'
    112         >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
    113         >>> # Create the temporary directory for tests
    114         >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
    115         >>> bmft = BatchManifestFileTester(basename, '.tmp')
    116         >>> product = bmft.test()
    117         >>> print product.success
    118         False
    119         >>> BatchManifestFileTester._remove_test_environment()
    120 
    121         >>> # Unit test: manifest file does not validate (incorrect MD5 sum of FITS file)
    122         >>> basename = 'B00000009'
    123         >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
    124         >>> # Create the temporary directory for tests
    125         >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
    126         >>> bmft = BatchManifestFileTester(basename, '.tmp')
    127         >>> product = bmft.test()
    128         >>> print product.success
    129         False
    130         >>> BatchManifestFileTester._remove_test_environment()
    131         """
    132         TestReport.title('Batch manifest file tests')
    133         # Check file existence and proper naming in archive
     119        """
    134120        requirement = 'PSDC-940-006-01, 3.5.1.2'
    135         product = TestProduct()
    136121        if not FileManipulation.exists(self.manifest):
    137122            TestReport.ko(requirement,
    138123                          'No %s file' % (self.manifest))
    139             return product
    140         else:
    141             TestReport.ok(requirement)
    142         # Check validation of XML by XSD through xmllint
     124            return TestProduct()
     125        TestReport.ok(requirement)
     126        return TestProduct(True)
     127
     128    def _test_xml_validates(self):
     129        """
     130        Checks if the XML file validates against the XSD file.
     131
     132        >>> # Unit test: manifest XML file does not validate against XSD
     133        >>> basename = 'B00000006'
     134        >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
     135        >>> # Create the temporary directory for tests
     136        >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
     137        >>> bmft = BatchManifestFileTester(basename, '.tmp')
     138        >>> product = bmft.test()
     139        >>> print product.success
     140        False
     141        >>> BatchManifestFileTester._remove_test_environment()
     142        """
    143143        requirement = 'PSDC-940-006-01, 3.5.1.1'
    144144        command = '%s --schema %s --noout %s'  % (Configuration.XMLLINT,
     
    151151            TestReport.ko(requirement,
    152152                          'XML validation through XSD failed: command was [%s]' % (command))
    153             return product
     153            return TestProduct()
    154154        TestReport.ok(requirement)
    155155        # Get XML information concerning FITS file
    156156        # Note: We are only interested in the first <file> element
    157         xmldoc = minidom.parse(self.manifest)
    158         fileList = xmldoc.getElementsByTagName('file')
    159         # File existence
     157        product = TestProduct(True)
     158        product.xmldoc = minidom.parse(self.manifest)
     159        product.fileList = product.xmldoc.getElementsByTagName('file')
     160        return product
     161
     162    def _test_fits_file_exists(self, xmlFileList):
     163        """
     164        Checks if the FITS file named in the manifest file exists is
     165        in the archive.
     166       
     167        >>> # Unit test: manifest file does not validate (no or incorrect FITS file name)
     168        >>> basename = 'B00000007'
     169        >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
     170        >>> # Create the temporary directory for tests
     171        >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
     172        >>> bmft = BatchManifestFileTester(basename, '.tmp')
     173        >>> product = bmft.test()
     174        >>> print product.success
     175        False
     176        >>> BatchManifestFileTester._remove_test_environment()
     177        """
    160178        requirement = 'PSDC-940-006-01, 3.5.1 (file attributes/name)'
    161         fitsFilename = fileList[0].attributes['name'].value
     179        fitsFilename = xmlFileList[0].attributes['name'].value
    162180        fileToLookFor = self._manifest_relative_directory + '/' + fitsFilename
    163181        if not FileManipulation.exists(fileToLookFor):
    164182            TestReport.ko(requirement,
    165183                          'no FITS file is named \'%s\'' % (fileToLookFor))
    166             return product
    167         TestReport.ok(requirement)
    168         # File size match
     184            return TestProduct()
     185        TestReport.ok(requirement)
     186        product = TestProduct(True)
     187        product.fileToLookFor = fileToLookFor
     188        product.fitsFilename = fitsFilename
     189        return product
     190
     191    def _test_file_size(self, xmlFileList, fileToLookFor):
     192        """
     193        Checks if the size shown in manifest file is equal to the FITS
     194        file one.
     195
     196        >>> # Unit test: manifest file does not validate (incorrect size of FITS file)
     197        >>> basename = 'B00000008'
     198        >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
     199        >>> # Create the temporary directory for tests
     200        >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
     201        >>> bmft = BatchManifestFileTester(basename, '.tmp')
     202        >>> product = bmft.test()
     203        >>> print product.success
     204        False
     205        >>> BatchManifestFileTester._remove_test_environment()
     206        """
    169207        requirement = 'PSDC-940-006-01, 3.5.1 (file attributes/size)'
    170         expected_size = fileList[0].attributes['bytes'].value
     208        expected_size = xmlFileList[0].attributes['bytes'].value
    171209        actual_size = str(FileManipulation.size(fileToLookFor))
    172210        if actual_size != expected_size:
     
    174212                          'actual size of FITS file is \'%s\' instead of \'%s\''
    175213                          % (actual_size, expected_size))
    176             return product
    177         TestReport.ok(requirement)
    178         # File md5sum
     214            return TestProduct()
     215        TestReport.ok(requirement)
     216        return TestProduct(True)
     217
     218    def _test_md5sum(self, xmlFileList, fileToLookFor):
     219        """
     220        Checks if the MD5 sum shown in manifest file is equal to the
     221        computed one.
     222
     223        >>> # Unit test: manifest file does not validate (incorrect MD5 sum of FITS file)
     224        >>> basename = 'B00000009'
     225        >>> tgz_filename = 'data/psut/ko/' + basename + '.tar.gz'
     226        >>> # Create the temporary directory for tests
     227        >>> BatchManifestFileTester._setup_test_environment(basename, tgz_filename)
     228        >>> bmft = BatchManifestFileTester(basename, '.tmp')
     229        >>> product = bmft.test()
     230        >>> print product.success
     231        False
     232        >>> BatchManifestFileTester._remove_test_environment()
     233        """
    179234        requirement = 'PSDC-940-006-01, 3.5.1 (file attributes/MD5 checksum of file)'
    180         expected_md5sum = fileList[0].attributes['md5'].value
     235        expected_md5sum = xmlFileList[0].attributes['md5'].value
    181236        actual_md5sum = FileManipulation.md5(fileToLookFor)
    182237        if actual_md5sum != expected_md5sum:
     
    184239                          'actual md5sum of FITS file is \'%s\' instead of \'%s\''
    185240                          % (actual_md5sum, expected_md5sum))
    186             return product
    187         TestReport.ok(requirement)
    188         # TODO: Perform some validation of the XML content that XSD can't do
    189         # e.g.: minObjId < maxObjId.
    190         # Note: if other objects need to be provided in the output, just add them to the dictionary...
    191         # e.g.: return (..., {'filename': fitsFilename, 'fitsFilesize': fitsFilesize})
    192         # Extend test product with values useful in next tests
    193         product.success = True
    194         product.filename = fitsFilename
    195         product.filetype = xmldoc.getElementsByTagName('manifest')[0].attributes['type'].value
    196         return product
     241            return TestProduct()
     242        TestReport.ok(requirement)
     243        return TestProduct(True)
    197244
    198245if __name__ == '__main__':
    199246    import logging
    200247    import sys
    201     PsPsLogger.setLevel(logging.DEBUG)
     248    PsPsLogger.setLevel(logging.INFO)
     249    PsPsLogger.set_stdout()
    202250    current_argument_position = 1
    203251    while current_argument_position < len(sys.argv):
    204         if sys.argv[current_argument_position] == '-v':
     252        if sys.argv[current_argument_position] == '-v': # pragma: no cover
    205253            PsPsLogger.set_stderr()
    206254            current_argument_position += 1
  • branches/sc_branches/psps_testing/testers/fits_file.py

    r29227 r29235  
    9898    current_argument_position = 1
    9999    while current_argument_position < len(sys.argv):
    100         if sys.argv[current_argument_position] == '-v':
     100        if sys.argv[current_argument_position] == '-v': # pragma: no cover
    101101            PsPsLogger.set_stderr()
    102102            current_argument_position += 1
  • branches/sc_branches/psps_testing/utilities/configuration.py

    r29227 r29235  
    1919
    2020    def __repr__(self):
     21        """
     22        >>> print Configuration()
     23        Tests Configuration Values:
     24          File extension: [.tar.gz]
     25          Batch manifest name: [BatchManifest.xml]
     26          XSD file: [data/xsd/BatchManifest.xsd]
     27          XML validation tool: [/usr/bin/xmllint]
     28          Limit for test failure in a single test item: [15]
     29          Tolerance for floating-point comparison: [1e-05]
     30          Different types comparison [never] fails
     31        >>> Configuration.BE_TYPE_TOLERANT = False
     32        >>> print Configuration()
     33        Tests Configuration Values:
     34          File extension: [.tar.gz]
     35          Batch manifest name: [BatchManifest.xml]
     36          XSD file: [data/xsd/BatchManifest.xsd]
     37          XML validation tool: [/usr/bin/xmllint]
     38          Limit for test failure in a single test item: [15]
     39          Tolerance for floating-point comparison: [1e-05]
     40          Different types comparison [always] fails
     41        """
    2142        from StringIO import StringIO
    2243        representation = StringIO()
     
    4162                             % tolerant)
    4263        return representation.getvalue()
     64
     65################################################################
     66#
     67# __main__
     68#
     69################################################################
     70if __name__ == '__main__':
     71    import logging
     72    import sys
     73    # Logging
     74    from utilities.psps_logger import PsPsLogger
     75    PsPsLogger.setLevel(logging.DEBUG)
     76    current_argument_position = 1
     77    while current_argument_position < len(sys.argv):
     78        if sys.argv[current_argument_position] == '-debug': # pragma: no cover
     79            PsPsLogger.set_stderr()
     80            current_argument_position += 1
     81        elif sys.argv[current_argument_position] == 'test':
     82            print 'Running unittest for Configuration class'
     83            import doctest
     84            doctest.testmod()
     85            sys.exit(0)
  • branches/sc_branches/psps_testing/utilities/psps_logger.py

    r29227 r29235  
    1414
    1515    class __impl(logging.Logger):
    16         """ Implementation of the singleton interface """
     16        """Implementation of the singleton interface"""
    1717        def __init__(self):
    1818            """Sets logging up"""
     
    4040        >>> logger1.debug('This message is not displayed')
    4141        >>> logger2 = PsPsLoggerClass()
    42         >>> logger1.id() == logger2.id()
     42        >>> print logger1.id() == logger2.id()
    4343        True
    4444        >>> logger1.setLevel(logging.DEBUG)
     
    123123        import doctest
    124124        doctest.testmod()
     125        PsPsLogger.test()
    125126        sys.exit(0)
    126127    # if sys.argv[1] == 'testfile':
  • branches/sc_branches/psps_testing/utilities/test_product.py

    r29227 r29235  
    77    enriched with anything useful for following tests.
    88    """
    9     def __init__(self):
     9    def __init__(self, success = False):
    1010        """
    1111        Creates a test product and set the success status to False.
     
    1313        >>> print TestProduct().success
    1414        False
     15        >>> print TestProduct(True).success
     16        True
    1517        """
    16         self.success = False
     18        self.success = success
    1719
    1820if __name__ == '__main__':
     21    import logging
    1922    import sys
    20     if len(sys.argv) == 2 and sys.argv[1]=='test':
    21         print 'Running TestProduct unit tests'
    22         import doctest
    23         doctest.testmod()
    24         sys.exit(0)
     23    current_argument_position = 1
     24    while current_argument_position < len(sys.argv):
     25        if sys.argv[current_argument_position] == '-debug': # pragma: no cover
     26            PsPsLogger.setLevel(logging.DEBUG)
     27            PsPsLogger.set_stderr()
     28            current_argument_position += 1
     29        elif sys.argv[current_argument_position] == 'test':
     30            print 'Running unittest for TestProduct class'
     31            import doctest
     32            doctest.testmod()
     33            sys.exit(0)
    2534
  • branches/sc_branches/psps_testing/utilities/test_report.py

    r29227 r29235  
    3232        """
    3333        Creates singleton instance.
     34
     35        >>> tr1 = TestReportClass()
     36        >>> tr2 = TestReportClass()
     37        >>> tr1.id() == tr2.id()
     38        True
    3439        """
    3540        # Check whether we already have an instance
     
    7176
    7277if __name__ == '__main__':
    73     if len(sys.argv)<2:
    74         import doctest
    75         doctest.testmod()
    76         sys.exit(0)
    77     TestReport.ok('Req1')
    78     TestReport.ko('Req2', 'A good reason for failure')
     78    import logging
     79    import sys
     80    current_argument_position = 1
     81    while current_argument_position < len(sys.argv):
     82        if sys.argv[current_argument_position] == '-debug': # pragma: no cover
     83            PsPsLogger.setLevel(logging.DEBUG)
     84            PsPsLogger.set_stderr()
     85            current_argument_position += 1
     86        elif sys.argv[current_argument_position] == 'test':
     87            print 'Running unittest for TestReport class'
     88            import doctest
     89            doctest.testmod()
     90            sys.exit(0)
  • branches/sc_branches/psps_testing/utilities/util.py

    r29227 r29235  
    2121    True
    2222    >>> equal(a, b, 1.e-16)
     23    False
     24    >>> class Dummy:
     25    ...   def __init__(self):
     26    ...     pass
     27    >>> dummy1 = Dummy()
     28    >>> dummy2 = Dummy()
     29    >>> equal(dummy1, dummy2)
    2330    False
    2431    """
     
    8491    >>> answer2['items'][0]['sky2'] = 1792.054
    8592    >>> product = match(fits2, answer2, 'Req: ')
     93    >>> print product.success
     94    False
     95
     96    >>> fits2._fields['sky2'] = [1792.05]
     97    >>> answer2['items'][0]['sky2'] = 1792.054
     98    >>> product = match(fits2, answer2, 'Req: ', max_failures = -1)
    8699    >>> print product.success
    87100    False
     
    179192    ...
    180193    Exception: Type 'UnsupportedType' is not supported
     194    >>> print psi_convert('Byte', 128)
     195    -128
     196    >>> print psi_convert('Byte', 255)
     197    -1
    181198    """
    182199    # PsPsLogger.debug('Value class: %s (%s/%s)'
     
    215232    current_argument_position = 1
    216233    while current_argument_position < len(sys.argv):
    217         if sys.argv[current_argument_position] == '-v':
     234        if sys.argv[current_argument_position] == '-debug': #pragma: no cover
    218235            PsPsLogger.set_stderr()
    219236            current_argument_position += 1
Note: See TracChangeset for help on using the changeset viewer.