Ejemplo n.º 1
0
def test_import_usda(usda_data_dir):
    '''
    Sample one food is correctly imported from USDA
    
    Meaning there should be: description, protein factor, fat factor,
    carbohydrate factor and a {internal_nutrient_name} column for each nutrient
    hardcoded in _mapping.
    '''
    foods = foods_.import_usda(usda_data_dir)
    expected = dedent('''\
        description                             Butter, salted
        Conversion factor: protein                        4270
        Conversion factor: fat                            8790
        Conversion factor: carbohydrate                   3870
        alpha linolenic acid                           0.00315
        caffeine                                             0
        calcium                                        0.00024
        carbohydrate                                    0.0006
        carotenoids                                   1.58e-06
        cholesterol                                    0.00215
        choline                                       0.000188
        copper                                               0
        energy                                            7170
        fat                                             0.8111
        fatty acids                                    0.54646
        fiber                                                0
        fluoride                                       2.8e-08
        folate                                           3e-08
        folate, added                                        0
        iron                                             2e-07
        linoleic acid                                  0.02728
        linoleic acid + alpha linolenic acid           0.03043
        magnesium                                        2e-05
        manganese                                            0
        mass                                                 1
        niacin                                         4.2e-07
        pantothenic acid                               1.1e-06
        phosphorus                                     0.00024
        potassium                                      0.00024
        protein                                         0.0085
        riboflavin                                     3.4e-07
        selenium                                         1e-08
        sodium                                         0.00643
        sugars, added                                   0.0006
        thiamin                                          5e-08
        vitamin a                                     6.84e-06
        vitamin a, preformed                          6.71e-06
        vitamin b12                                    1.7e-09
        vitamin b6                                       3e-08
        vitamin c                                            0
        vitamin d                                            0
        vitamin e                                     2.32e-05
        vitamin e, added                                     0
        vitamin k                                        7e-08
        water                                           0.1587
        zinc                                             9e-07'''
    )
    assert_text_equals(foods.loc[1001].to_string(), expected)
Ejemplo n.º 2
0
def test_configure(temp_dir_cwd, capsys, caplog):
    log_file = Path('file.log')
    stderr_handler, file_handler = logging_.configure(log_file)
    assert isinstance(stderr_handler, logging.StreamHandler)
    assert isinstance(file_handler, logging.FileHandler)
    
    logger = logging.getLogger(__name__)
    logger.info('Fake info')
    logger.debug('Ignored debug')
    logger.setLevel(logging.DEBUG)
    logger.debug('Fake debug')
    
    # stderr
    #
    # - level is INFO
    # - terse format
    expected = 'I: Fake info\n'
    actual = capsys.readouterr()[1]
    assert_text_equals(actual, expected)
    
    # log file
    #
    # - level is DEBUG
    # - long format with fairly unambiguous source
    log_file_content = path_.read(log_file)
    def prefix(log_type, package, module_name):
        return r'{} [0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}} [0-9]{{2}}:[0-9]{{2}}:[0-9]{{2}},[0-9]{{3}} {} \({}:[0-9]+\):'.format(
            log_type, package, module_name
        )
    pattern = dedent('''\
        {}
        Fake info
         
        {}
        Fake debug
        '''
        .format(
            prefix('I', __name__, 'test_logging'),
            prefix('D', __name__, 'test_logging'),
        )
    )
    assert re.match(pattern, log_file_content, re.MULTILINE), 'Actual:{}\n\nExpected to match:\n{}'.format(log_file_content, pattern)
    
Ejemplo n.º 3
0
def assert_equals(file1, file2, contents=True, name=True, mode=True):
    '''
    Assert 2 files are equal
    
    Parameters
    -----------
    file1 : Path
    file2 : Path
    contents : bool
        Assert file contents are equal
    name : bool
        Assert file names are equal
    mode : bool
        Assert the last 3 octal digits of file modes are equal 
    '''
    if name:
        assert file1.name == file2.name
    if contents:
        assert_text_equals(read(file1), read(file2))
    if mode:
        assert file1.stat().st_mode == file2.stat().st_mode