def test_init():
    temp1 = ProblemTemplate(name="test1", template="template")
    assert temp1.name == "test1"
    assert temp1.template == "template"
    assert temp1.params == []

    temp2 = ProblemTemplate(name="test2", template="template", params="params")
    assert temp2.name == "test2"
    assert temp2.template == "template"
    assert temp2.params == "params"
    return
def test_str(mock_read_csv, mock_get_template, fullTemplate1, emptyTemplate,
             paramsDf, template):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf
    newTemplate = ProblemTemplate.from_file("templateName",
                                            "params",
                                            name="test_problem")
    assert str(newTemplate) == fullTemplate1

    mock_read_csv.return_value = []
    newnewTemplate = ProblemTemplate.from_file("templateName2",
                                               "params2",
                                               name="test_problem")
    assert str(newnewTemplate) == emptyTemplate
    return
def test_read_template(mock_get_template, template):
    mock_get_template.return_value = template

    templ = ProblemTemplate(template="template")
    templ.read_template("someDir/templ")
    assert templ.template == template

    mock_get_template.side_effect = Exception()
    templ = ProblemTemplate(template="template")
    templ.read_template("someDir/othertempl")
    assert templ.template == None
    return
def test_read_param_csv(mock_read_csv, paramsDf, paramsList):
    mock_read_csv.return_value = paramsDf
    templ = ProblemTemplate("templateFile", "paramFile", "test_problem")
    templ.read_param_csv("paramFile", overwrite=True)
    assert templ.params == paramsList

    templ.read_param_csv("paramFile", overwrite=False)
    assert templ.params == paramsList + paramsList

    # Test exception
    mock_read_csv.side_effect = Exception()
    templ.read_param_csv("paramFile", overwrite=True)
    assert templ.params == []
    return
def test_from_file(mock_read_csv, mock_get_template, paramsDf, template,
                   paramsList):
    # Set up mock return values with fixtures.
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    templ = ProblemTemplate.from_file("templateName",
                                      "params",
                                      name="test_problem")
    assert templ.name == "test_problem"
    assert templ.template == template
    assert templ.params == paramsList
    return
def test_read(mock_get_template, mock_read_csv, template, paramsDf,
              paramsList):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    templ = ProblemTemplate("template", "params", "test_problem")
    templ.read("templatefile", "paramfile", True)
    assert templ.params == paramsList
    assert templ.template == template

    templ.read("templatefile", "paramfile", False)
    assert templ.params == paramsList + paramsList
    assert templ.template == template
def test_write_to_dir(mock_read_csv, mock_get_template, template, paramsDf,
                      fullTemplate1, fullTemplate2):
    mock_get_template.return_value = template
    mock_read_csv.return_value = paramsDf

    m = mock_open()
    # with patch('cvxbenchmarks.problem_generator.open', m, create=True):
    with patch('cvxbenchmarks.database.problem_generator.open', m):
        templ = ProblemTemplate.from_file("templateName",
                                          "params",
                                          name="test_problem")
        templ.write_to_dir("problemDir")
        expected_calls = [
            call("problemDir/test_problem_p1.py", "w"),
            call().__enter__(),
            call().write(fullTemplate1),
            call().__exit__(None, None, None),
            call("problemDir/test_problem_p2.py", "w"),
            call().__enter__(),
            call().write(fullTemplate2),
            call().__exit__(None, None, None)
        ]
        assert expected_calls == m.mock_calls