Skip to content

Commit

Permalink
Merge pull request #4 from NCAR/develop
Browse files Browse the repository at this point in the history
Bring fork up to date 3/20/18
  • Loading branch information
malloryprow authored Mar 20, 2018
2 parents 6fcedda + 43d5149 commit ad72317
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 8 deletions.
16 changes: 16 additions & 0 deletions internal_tests/pytests/produtil/produtil_test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

# Test configuration for MET+ produtil
[config]
STRING_VALUE = someStringValue!#@$%
INT_VALUE = 2908887
RAW_VALUE = GRIB_lvl_type = 100
BOOL_VALUE = True
NEW_LINES = very long line requiring newline character to be tested 12345
67890 end of the line.
UNASSIGNED_VALUE =

[dir]
DIR_VALUE = /tmp/some_dir
METPLUS_CONF =
BASE_DIR = /tmp
SPECIFIC_DIR = {BASE_DIR}/specific_place
133 changes: 133 additions & 0 deletions internal_tests/pytests/produtil/test_produtil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env python
from __future__ import print_function
import os
import produtil.setup
import sys
import logging
import pytest
import config_metplus
import config_launcher as launcher
import met_util as util


#
# These are tests (not necessarily unit tests) for the
# MET Point-Stat Wrapper, PointStatWrapper.py
# NOTE: This test requires pytest, which is NOT part of the standard Python
# library.
# These tests require one configuration file in addition to the three
# required METplus configuration files: point_stat_test.conf. This contains
# the information necessary for running all the tests. Each test can be
# customized to replace various settings if needed.
#

#
# -----------Mandatory-----------
# configuration and fixture to support METplus configuration files beyond
# the metplus_data, metplus_system, and metplus_runtime conf files.
#


# Add a test configuration
def pytest_addoption(parser):
parser.addoption("-c", action="store", help=" -c <test config file>")


# @pytest.fixture
def cmdopt(request):
return request.config.getoption("-c")


# ------------------------

def get_config_obj():
"""! Create the configuration object that is used by all tests"""
file_list = ["/Users/cyclopath/dev_MET+/METplus/internal_tests/pytests/produtil"]
config_obj = config_metplus.setup(file_list[0])

return config_obj


def test_getstr_ok():
"""! Test that the expected string is retrieved via produtil's getstr
method
"""
conf_obj = get_config_obj()
str_value = conf_obj.getstr('config', 'STRING_VALUE')
expected_str_value = "someStringValue!#@$%"
assert str_value == expected_str_value


def test_getint_ok():
"""! Test that the expected int in the produtil_test.conf file has been
retrieved correctly.
"""
conf_obj = get_config_obj()
expected_int_value = int(2908887)
int_value = conf_obj.getint('config', 'INT_VALUE')
assert int_value == expected_int_value


def test_getraw_ok():
"""! Test that the raw value in the produtil_test.conf file has been
retrieved correctly.
"""
conf_obj = get_config_obj()
expected_raw = 'GRIB_lvl_type = 100'
raw_value = conf_obj.getraw('config', 'RAW_VALUE')
assert raw_value == expected_raw


def test_getdir_ok():
"""! Test that the directory in the produtil_test.conf file has been
correctly retrieved.
"""
conf_obj = get_config_obj()
expected_dir = "/tmp/some_dir"
dir_retrieved = conf_obj.getdir('DIR_VALUE')
assert dir_retrieved == expected_dir


def test_getdir_compound_ok():
"""! Test that directories created from other directories, ie.
BASE_DIR = /base/dir
SPECIFIC_DIR = {BASE_DIR}/specific/dir
correctly returns the directory path for SPECIFIC_DIR
"""
expected_specific_dir = "/tmp/specific_place"
conf_obj = get_config_obj()
specific_dir = conf_obj.getdir('SPECIFIC_DIR')
assert specific_dir == expected_specific_dir


def test_no_value_as_string():
"""! Tests that a key with no value returns an empty string."""

conf_obj = get_config_obj()
expected_unassigned = ''
unassigned = conf_obj.getstr('config', 'UNASSIGNED_VALUE')
print("unassigned: ", unassigned)
print("expected: ", expected_unassigned)
assert unassigned == expected_unassigned


def test_no_value_as_list():
"""! Tests that a key with no list of strings returns an empty list."""

conf_obj = get_config_obj()
expected_unassigned = []
unassigned = util.getlist(conf_obj.getstr('config', 'UNASSIGNED_VALUE'))
assert unassigned == expected_unassigned


def test_new_lines_in_conf():
"""! Test that any newlines in the configuration file are handled
properly
"""

conf_obj = get_config_obj()
expected_string = \
"very long line requiring newline character to be tested 12345\n67890 end of the line."
long_line = conf_obj.getstr('config', 'NEW_LINES')
assert long_line == expected_string
46 changes: 38 additions & 8 deletions ush/met_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,18 +1219,48 @@ def get_dirs(base_dir):


def getlist(s, logger=None):
"""! returns a list of string elements from a comma or space
separated string of values, returns and empty list
if s is ''
'4,4,2,4,2,4,2, ' or '4,4,2,4,2,4,2 ' or
'4, 4, 4, 4, ' or '4, 4, 4, 4 '
"""! Returns a list of string elements from a comma or space
separated string of values.
This function MUST also return an empty list [] if s is '' empty.
This function is meant to handle these possible or similar inputs:
AND return a clean list with no surrounding spaces or trailing
commas in the elements.
'4,4,2,4,2,4,2, ' or '4,4,2,4,2,4,2 ' or
'4, 4, 4, 4, ' or '4, 4, 4, 4 '
Note: getstr on an empty variable (EMPTY_VAR = ) in
a conf file returns '' an empty string.
@param s the string being converted to a list.
"""

# removes surrounding comma, and spaces, if present.
# Developer NOTE: we could just force this to only operate
# on comma seperated lists, not space seperated.

# FIRST remove surrounding comma, and spaces, form the string.
s = s.strip().strip(',').strip()

s = s.split(',')
s = [item.strip() for item in s]
# splitting an empty string, s with ',', creates a 1 element
# list with an empty string element, we dont want to create or
# retrun that, ie. NEVER RETURN THIS [''], If s is '', an
# empty string, then return an empty list [].
# Doing so allows for proper boolean testing of your
# list elsewhere in the code, ie. bool([]) is False.

# if s is not an empty string, split it on
# commas or spaces
if s:
if ',' in s:
s = s.split(',')
s = [item.strip() for item in s]
else:
s = s.split()
else:
# create an empty list []
s = list()

return s

Expand Down

0 comments on commit ad72317

Please sign in to comment.