示例#1
0
def test_days_integer():
    """Test that we can insert stuff a certain number of days
    after startup"""
    os.chdir(DATADIR)
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 1, 11, 0, 0, 0) in sch.dates

    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10.0
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 1, 11, 0, 0, 0) in sch.dates
示例#2
0
def test_emptyfiles(tmpdir):
    """Test that we don't crash when we try to include files
    which are empty (or only contains comments)"""
    tmpdir.chdir()
    with open("empty.sch", "w") as file_h:
        file_h.write("")
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["empty.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""

    with open("commentonly.sch", "w") as file_h:
        file_h.write("-- an Eclipse comment")
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["commentonly.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "insert": [{
            "filename": "commentonly.sch",
            "days": 1
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""
示例#3
0
def test_days_float(readonly_datadir):
    """Test that we can insert stuff a certain number of
    floating point days after startup"""
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10.1
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    # The TimeVector object has the "correct" date including time,
    # being 0.1 days after 2020-1-11
    assert datetime.datetime(2020, 1, 11, 2, 24, 0) in sch.dates
    # Now also the clocktime is stringified (using updated TimeVector)
    assert "11 'JAN' 2020 02:24:00" in str(sch)

    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10.9
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 1, 11, 21, 36, 0) in sch.dates
    # TimeVector now support clocktimes
    assert "11 'JAN' 2020 21:36:00" in str(sch)
    # Check that microsecs do not leak into string unless required
    assert "21:36:00 " in str(sch) or "21:36:00/" in str(sch)
示例#4
0
def test_weltarg_uda(tmp_path):
    """WELTARG supports UDA from opm-common 2020.10"""
    os.chdir(tmp_path)
    weltargkeyword = """WELTARG
  'OP-1' ORAT SOMEUDA /
/
"""
    Path("weltarg.sch").write_text(
        """DATES
  1 'NOV' 2022 /
/
""" + weltargkeyword,
        encoding="utf8",
    )
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "files": ["weltarg.sch"],
    }
    # This raises a ValueError in opm-common 2020.04
    sch = sunsch.process_sch_config(sunschconf)
    assert "ORAT" in str(sch)

    # Can anyways be injected using an insert statement:
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "insert": [{
            "date": datetime.date(2022, 11, 1),
            "string": weltargkeyword
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "ORAT" in str(sch)
    assert "SOMEUDA" in str(sch)
示例#5
0
def test_comments():
    """Comments in files that are parsed by opm-common
    prior to piecing together will be lost, mentioned as
    a caveat in the documentation.

    But can we inject comments through insert statements

    (we can actually inject anything using this, it will not
    be attempted validated through opm-common)
    """
    mycomment = "-- A comment at a specific date"
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "insert": [{
            "days": 1,
            "string": mycomment
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert mycomment in str(sch)

    # Redo the same test through a yaml string, the empty
    # identificator "" used here probably illustrates a bad
    # layout for the configuration file.
    conf_str = ("""
startdate: 2020-01-01
insert:
  - "":
      days: 1
      string: """ + mycomment)
    conf = yaml.safe_load(conf_str)
    assert mycomment in str(sunsch.process_sch_config(conf))
示例#6
0
def test_merge_paths_in_use(tmp_path, caplog):
    """If the PATHS keyword is in use for getting includes,
    there will be "variables" in use in INCLUDE-statements.

    These variables are defined in the DATA file and outside
    sunsch's scope, but we should ensure a proper error message"""
    os.chdir(tmp_path)
    Path("pathsinclude.sch").write_text(
        """
DATES
  1 'JAN' 2030 /
/

INCLUDE
  '$MYSCHFILES/something.sch' /
""",
        encoding="utf8",
    )

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["pathsinclude.sch"],
    }
    with pytest.raises(SystemExit):
        sunsch.process_sch_config(sunschconf)
    assert "PATHS variables in INCLUDE" in caplog.text
示例#7
0
def test_emptyfiles(tmp_path):
    """Test that we don't crash when we try to include files
    which are empty (or only contains comments)"""
    os.chdir(tmp_path)
    Path("empty.sch").write_text("", encoding="utf8")
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["empty.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""

    Path("commentonly.sch").write_text("-- an Eclipse comment",
                                       encoding="utf8")
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["commentonly.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "insert": [{
            "filename": "commentonly.sch",
            "days": 1
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""
示例#8
0
def test_weltarg_uda(tmpdir):
    """WELTARG supports UDA from opm-common 2020.10"""
    tmpdir.chdir()
    weltargkeyword = """WELTARG
  'OP-1' ORAT SOMEUDA /
/
"""
    with open("weltarg.sch", "w") as file_h:
        file_h.write("""DATES
  1 'NOV' 2022 /
/
""" + weltargkeyword)
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "files": ["weltarg.sch"],
    }
    # This raises a ValueError in opm-common 2020.04
    sch = sunsch.process_sch_config(sunschconf)
    assert "ORAT" in str(sch)

    # Can anyways be injected using an insert statement:
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "insert": [{
            "date": datetime.date(2022, 11, 1),
            "string": weltargkeyword
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "ORAT" in str(sch)
    assert "SOMEUDA" in str(sch)
示例#9
0
def test_days_float():
    """Test that we can insert stuff a certain number of
    floating point days after startup"""
    os.chdir(DATADIR)
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10.1
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    # The TimeVector object has the "correct" date including time,
    # being 0.1 days after 2020-1-11
    assert datetime.datetime(2020, 1, 11, 2, 24, 0) in sch.dates
    # However, the clocktime is not included when the TimeVector
    # object is stringified:
    assert "11 'JAN' 2020/" in str(sch)

    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10.9
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 1, 11, 21, 36, 0) in sch.dates
    # Rounding is downwards:
    assert "11 'JAN' 2020/" in str(sch)
示例#10
0
def test_merge():
    """Test that merge can be both a list and a string, that
    allows both syntaxes in yaml:

    merge: filename.sch

    and

    merge:
      - filename1.sch
      - filename2.sch
    """
    os.chdir(DATADIR)

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["mergeme.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "WRFTPLT" in str(sch)
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["mergeme.sch"]
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "WRFTPLT" in str(sch)
示例#11
0
def test_merge_include_nonexist(tmpdir):
    """If a user merges in a sch file which contains INCLUDE
    statements, these files may not exist yet (or only for a
    different path and so on.

    The way to get around this, is to do string insertions
    in the insert section.
    """
    tmpdir.chdir()
    open("mergewithexistinginclude.sch", "w").write("""
DATES
  1 'JAN' 2030 /
/

INCLUDE
  'something.sch' /
""")
    open("something.sch", "w").write("""
WRFTPLT
  2 /
/
""")

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "files": ["mergewithexistinginclude.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "WRFTPLT" in str(sch)

    # Now if it does not exist:
    open("mergewithnonexistinginclude.sch", "w").write("""
DATES
  1 'JAN' 2030 /
/

INCLUDE
  'somethingnotexistingyet.sch' /
""")
    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "merge": "mergewithnonexistinginclude.sch",
    }
    # This crashes in C-code and exits, can't catch it using pytest:
    # "A fatal error has occured and the application will stop"
    # "Could not open file: ....somethingnotexistingyet.sch"

    # sch = sunsch.process_sch_config(sunschconf)

    sunschconf = {
        "startdate": datetime.date(2000, 1, 1),
        "insert": [{
            "days": 2,
            "string": "INCLUDE\n  'something.sch'/\n"
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "something.sch" in str(sch)
示例#12
0
def test_main_configv1(tmpdir, caplog):
    """Test command line sunsch, loading a yaml file.

    This is run on a v1 config file, which will be autoconverted to v2.

    This format is to be deprecated, and should be removed in the future
    """

    tmpdir.chdir()
    shutil.copytree(DATADIR, "testdata_sunsch")
    tmpdir.join("testdata_sunsch").chdir()

    outfile = "schedule.sch"  # also in config_v1.yml

    if os.path.exists(outfile):
        os.unlink(outfile)
    sys.argv = ["sunsch", "config_v1.yml"]
    sunsch.main()
    assert "DEPRECATED" in caplog.text
    assert os.path.exists(outfile)

    schlines = open(outfile).readlines()
    assert len(schlines) > 70

    # Check footemplate.sch was included:
    assert any(["A-90" in x for x in schlines])

    # Sample check for mergeme.sch:
    assert any(["WRFTPLT" in x for x in schlines])

    # Check for foo1.sch, A-1 should occur twice
    assert sum(["A-1" in x for x in schlines]) == 2

    # Check for substitutetest:
    assert any(["400000" in x for x in schlines])

    # Check for randomid:
    assert any(["A-4" in x for x in schlines])

    # Test that we can have statements in the init file
    # before the first DATES that are kept:

    sch_conf = yaml.safe_load(open("config_v1.yml"))
    print(sch_conf)
    sch_conf["init"] = "initwithdates.sch"
    sunsch.process_sch_config(sch_conf)

    # BAR-FOO is a magic string that occurs before any DATES in initwithdates.sch
    assert "BAR-FOO" in "".join(open(outfile).readlines())
示例#13
0
def main():
    """Entry point from command line"""
    parser = get_parser()
    args = parser.parse_args()

    sunsch_config = {
        "startdate": datetime.date(1900, 1, 1),
        "files": args.inputfiles
    }

    if args.verbose:
        # Set the root logger to INFO, will be inherited by sunsch
        logging.getLogger().setLevel(logging.INFO)

    logger.info("# Sending the following YAML configuration to sunsch:")
    logger.info(yaml.dump(sunsch_config))

    if args.end_date:
        sunsch_config["enddate"] = dateutil.parser.parse(args.end_date).date()

    sch = sunsch.process_sch_config(sunsch_config)

    if os.path.exists(args.outputfile) and not args.force:
        logger.error("Not overwriting existing file %s", args.outputfile)
    else:
        with open(args.outputfile, "w") as outfile:
            outfile.write(str(sch))
        logger.info("Wrote %s dates to %s", str(len(sch)), args.outputfile)
示例#14
0
def main():
    """Entry point from command line"""
    parser = get_parser()
    args = parser.parse_args()

    warnings.warn("merge_schedule is deprecated, use sunsch", FutureWarning)

    sunsch_config = {"files": args.inputfiles}

    if args.verbose:
        # Set the root logger to INFO, will be inherited by sunsch
        logger.setLevel(logging.INFO)

    print("Add the following lines to a yaml file and use it as input for sunsch:")
    print("----")
    print(yaml.dump({"files": args.inputfiles, "output": args.outputfile}).strip())
    print("----")

    if args.end_date:
        sunsch_config["enddate"] = dateutil.parser.parse(args.end_date).date()

    sch = sunsch.process_sch_config(sunsch_config)

    if os.path.exists(args.outputfile) and not args.force:
        logger.error("Not overwriting existing file %s", args.outputfile)
    else:
        with open(args.outputfile, "w") as outfile:
            outfile.write(str(sch))
        logger.info("Wrote %s dates to %s", str(len(sch)), args.outputfile)
示例#15
0
def test_e300_keywords(readonly_datadir):
    """Test a keyword newly added to opm-common"""
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2020, 1, 1),
        "files": ["options3.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "OPTIONS3" in str(sch)
示例#16
0
def test_e300_keywords():
    """Test a keyword newly added to opm-common"""
    os.chdir(os.path.join(os.path.dirname(__file__), "testdata_sunsch"))
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2020, 1, 1),
        "files": ["options3.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "OPTIONS3" in str(sch)
示例#17
0
def test_long_udq_lines(tmp_path):
    """UDQ statements must not be line-wrapped, there is special code in OPM to
    avoid that."""
    inputstr = """UDQ

DEFINE FU_PWRI1 WWIR 'A_01' i WWIR 'A_02' + WWIR 'A_03' + WWIR 'A_04' + WWIR 'A_05' + WWIR 'A_06' + WWIR 'A_07' + WWIR 'A_08' + WWIR 'A_09' + WWIR 'A_10'  + WWIR 'A_11' + WWIR 'A_12' + WWIR 'A_13' + WWIR 'A_14' + WWIR 'A_15'/

/"""  # noqa
    assert len(inputstr.split("\n")) == 5  # Avoid editor-spoiled test.

    Path("longudq.sch").write_text(inputstr, encoding="utf8")
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "insert": [{
            "date": datetime.date(2020, 2, 1),
            "filename": "longudq.sch"
        }],
    }

    sch = sunsch.process_sch_config(sunschconf)
    schstr = str(sch)

    # the DEFINE line must be on its own line, so line count should be 9:
    assert len(schstr.split("\n")) == 9

    # Ensure unwanted space in front of well-names does not occur:
    assert "' A" not in schstr

    # Redo test without quotes in the input string:
    Path("longudq-noquotes.sch").write_text(inputstr.replace("'", ""),
                                            encoding="utf8")
    sunschconf = {
        "startdate":
        datetime.date(2020, 1, 1),
        "insert": [{
            "date": datetime.date(2020, 2, 1),
            "filename": "longudq-noquotes.sch"
        }],
    }
    schstr_noquotes = str(sunsch.process_sch_config(sunschconf))
    assert len(schstr_noquotes.split("\n")) == 9
    assert "' A" not in schstr_noquotes
示例#18
0
def test_microsecs(readonly_datadir):
    """Test that microsecs are formatted correctly"""
    sunschconf = {
        "starttime": datetime.datetime(2020, 1, 1, 1, 2, 3),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "01:02:03 " in str(sch) or "01:02:03/" in str(sch)
    sunschconf = {
        "starttime": datetime.datetime(2020, 1, 1, 1, 2, 3, 123400),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "01:02:03.1234" in str(sch)
示例#19
0
def test_main(testdata, caplog, mocker):
    """Test command line sunsch, loading a yaml file"""
    outfile = "schedule.inc"  # also in config_v2.yml

    mocker.patch("sys.argv", ["sunsch", "config_v2.yml"])
    sunsch.main()
    assert "DEPRECATED" not in caplog.text
    assert Path(outfile).exists()

    schlines = Path(outfile).read_text(encoding="utf8").splitlines()
    assert len(schlines) > 70

    # Check footemplate.sch was included:
    assert any("A-90" in x for x in schlines)

    # Sample check for mergeme.sch:
    assert any("WRFTPLT" in x for x in schlines)

    # Check for foo1.sch, A-1 should occur twice
    assert sum("A-1" in x for x in schlines) == 2

    # Check for substitutetest:
    assert any("400000" in x for x in schlines)

    # Check for randomid:
    assert any("A-4" in x for x in schlines)

    # Test that we can have statements in the init file
    # before the first DATES that are kept:

    sch_conf = yaml.safe_load(Path("config_v2.yml").read_text(encoding="utf8"))
    print(sch_conf)
    sch_conf["init"] = "initwithdates.sch"
    sunsch.process_sch_config(sch_conf)

    # BAR-FOO is a magic string that occurs before any DATES in initwithdates.sch
    assert "BAR-FOO" in "".join(
        Path(outfile).read_text(encoding="utf8").splitlines())
示例#20
0
def test_starttime(readonly_datadir):
    sunschconf = {
        "starttime": datetime.datetime(2020, 2, 1, 0, 0, 0),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "11 'FEB' 2020" in str(sch)

    sunschconf = {
        "starttime": datetime.datetime(2020, 2, 1, 23, 59, 59),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "days": 10
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    # Dates are rounded down, clock-times are not supported
    assert "11 'FEB' 2020" in str(sch)
示例#21
0
def test_nonisodate():
    """Test behaviour when users use non-ISO-dates"""
    sunschconf = {
        "startdate": "01-01-2020",  # Look, this is not how to write dates!
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    with pytest.raises(ValueError):
        sunsch.process_sch_config(sunschconf)

    # Check also for refdate:
    sunschconf = {
        "refdate": "01-01-2020",
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    with pytest.raises(ValueError):
        sunsch.process_sch_config(sunschconf)

    sunschconf = {
        # Beware that using a ISO-string for a date in this config
        # will give a wrong error message, since the code assumes
        # all string dates are already parsed into datimes by the
        # yaml loader.
        # "startdate": "2020-01-01",
        "startdate": datetime.date(2020, 1, 1),
        "enddate": "01-01-2020",
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    with pytest.raises(TypeError):
        sunsch.process_sch_config(sunschconf)
示例#22
0
def test_templating(tmpdir):
    """Test templating"""
    tmpdir.chdir()
    with open("template.tmpl", "w") as handle:
        handle.write("WCONHIST\n<WELLNAME> OPEN ORAT <ORAT> <GRAT> /\n/")

    sunschconf = {
        "startdate":
        datetime.date(2020, 1, 1),
        "enddate":
        datetime.date(2021, 1, 1),
        "insert": [{
            "template":
            "template.tmpl",
            "days":
            10,
            "substitute":
            dict(WELLNAME="A-007", ORAT=200.3, GRAT=1.4e6),
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "A-007" in str(sch)
    assert "200.3" in str(sch)
    assert "1400000" in str(sch)
    cfg_suite = configsuite.ConfigSuite(sunschconf,
                                        sunsch.CONFIG_SCHEMA_V2,
                                        deduce_required=True)
    assert cfg_suite.valid

    # Let some of the valued be undefined:
    sunschconf = {
        "startdate":
        datetime.date(2020, 1, 1),
        "enddate":
        datetime.date(2021, 1, 1),
        "insert": [{
            "template": "template.tmpl",
            "days": 10,
            "substitute": dict(WELLNAME="A-007"),
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert "A-007" in str(sch)
    assert "<ORAT>" in str(sch)
    # (this error is let through sunsch)

    # Let the date be undefined.
    sunschconf = {
        "startdate":
        datetime.date(2020, 1, 1),
        "enddate":
        datetime.date(2021, 1, 1),
        "insert": [{
            "template": "template.tmpl",
            "substitute": dict(WELLNAME="A-007")
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    # sunsch logs this as an error that there is no date defined for the template.
    assert "A-007" not in str(sch)

    # Skip defining substitute:
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "template": "template.tmpl",
            "days": 100
        }],
    }

    sch = sunsch.process_sch_config(sunschconf)
    assert "A-007" not in str(sch)
    # Sunsch lets this though, but logs an error.

    # Let the template file be empty:
    with open("empty.tmpl", "w") as handle:
        handle.write("")
    sunschconf = {
        "startdate":
        datetime.date(2020, 1, 1),
        "insert": [{
            "template":
            "empty.tmpl",
            "days":
            10,
            "substitute":
            dict(WELLNAME="A-007", ORAT=200.3, GRAT=1.4e6),
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert str(sch) == ""
示例#23
0
def test_dateclip(readonly_datadir):
    """Test dateclipping"""
    # Clip dates after enddate:
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2020, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2019, 2, 9, 0, 0) in sch.dates
    assert datetime.datetime(2020, 10, 1, 0, 0) not in sch.dates

    # Nothing to clip here:
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 10, 1, 0, 0) in sch.dates
    assert datetime.datetime(2019, 2, 9, 0, 0) in sch.dates

    # Clip events before startdate:
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2019, 2, 9, 0, 0) not in sch.dates
    assert datetime.datetime(2020, 10, 1, 0, 0) in sch.dates

    # Inserting before end-date should not be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2041, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2030, 1, 1, 0, 0) in sch.dates

    # Inserting after end-date should be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2030, 1, 1, 0, 0) not in sch.dates

    # Inserting before start-date should be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2000, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2000, 1, 1, 0, 0) not in sch.dates
示例#24
0
def test_dateclip():
    """Test dateclipping"""

    # This test function do not write to this directory:
    os.chdir(DATADIR)

    # Clip dates after enddate:
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2020, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2019, 2, 9, 0, 0) in sch.dates
    assert datetime.datetime(2020, 10, 1, 0, 0) not in sch.dates

    # Nothing to clip here:
    sunschconf = {
        "startdate": datetime.date(1900, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2020, 10, 1, 0, 0) in sch.dates
    assert datetime.datetime(2019, 2, 9, 0, 0) in sch.dates

    # Clip events before startdate:
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "files": ["mergeme.sch"],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2019, 2, 9, 0, 0) not in sch.dates
    assert datetime.datetime(2020, 10, 1, 0, 0) in sch.dates

    # Inserting before end-date should not be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2041, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2030, 1, 1, 0, 0) in sch.dates

    # Inserting after end-date should be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2030, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2030, 1, 1, 0, 0) not in sch.dates

    # Inserting before start-date should be clipped.
    sunschconf = {
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "insert": [{
            "filename": "foo1.sch",
            "date": datetime.date(2000, 1, 1)
        }],
    }
    sch = sunsch.process_sch_config(sunschconf)
    assert datetime.datetime(2000, 1, 1, 0, 0) not in sch.dates
示例#25
0
def test_dategrid():
    """Test dategrid generation support in sunsch"""
    # Daily
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2021, 1, 1),
        "enddate": datetime.date(2022, 1, 1),
        "dategrid": "daily",
    })
    assert len(sch) == 366
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2021, 6, 6, 0, 0) in sch.dates
    assert datetime.datetime(2022, 1, 1, 0, 0) in sch.dates

    # Yearly
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "dategrid": "yearly",
    })
    assert len(sch) == 2
    assert datetime.datetime(2020, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates

    # Monthly
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "dategrid": "monthly",
    })
    assert datetime.datetime(2020, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 2, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 12, 1, 0, 0) in sch.dates
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates
    assert max(sch.dates) == datetime.datetime(2021, 1, 1, 0, 0)
    assert min(sch.dates) == datetime.datetime(2020, 1, 1, 0, 0)

    # Bi-Monthly
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "dategrid": "bimonthly",
    })
    assert datetime.datetime(2020, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 2, 1, 0, 0) not in sch.dates
    assert datetime.datetime(2020, 3, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 11, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 12, 1, 0, 0) not in sch.dates
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates
    assert max(sch.dates) == datetime.datetime(2021, 1, 1, 0, 0)
    assert min(sch.dates) == datetime.datetime(2020, 1, 1, 0, 0)

    # Weekly
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "dategrid": "weekly",
    })
    assert len(sch) == 54
    assert datetime.datetime(2020, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 1, 8, 0, 0) in sch.dates
    assert datetime.datetime(2020, 12, 30, 0, 0) in sch.dates
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates
    assert max(sch.dates) == datetime.datetime(2021, 1, 1, 0, 0)
    assert min(sch.dates) == datetime.datetime(2020, 1, 1, 0, 0)

    # Bi-Weekly
    sch = sunsch.process_sch_config({
        "startdate": datetime.date(2020, 1, 1),
        "enddate": datetime.date(2021, 1, 1),
        "dategrid": "biweekly",
    })
    assert len(sch) == 28
    assert datetime.datetime(2020, 1, 1, 0, 0) in sch.dates
    assert datetime.datetime(2020, 1, 15, 0, 0) in sch.dates
    assert datetime.datetime(2020, 12, 16, 0, 0) in sch.dates
    assert datetime.datetime(2021, 1, 1, 0, 0) in sch.dates
    assert max(sch.dates) == datetime.datetime(2021, 1, 1, 0, 0)
    assert min(sch.dates) == datetime.datetime(2020, 1, 1, 0, 0)