def test_multiple_problem_reports():
    errs = StringIO.StringIO()
    with pytest.raises(AssertionError) as exc:
        vn.check_results(datafiles('missing-bdb'), errstream=errs)
    assert " bdb " in errs.getvalue()

    vn.check_results(datafiles('missing-bdb'),
        required=[(r'quickstart', [vn.assert_raises("No bdb in")]),
                  (r'import matplotlib', [vn.assert_warns(r'prop_cycle')])])

    errs = []
    vn.check_results(datafiles('missing-bdb'), errstream=errs)
    assert 2 == len(errs), errs
    assert "No bdb in" in str(errs[0])
    assert "prop_cycle" in str(errs[1])

    errs = StringIO.StringIO()
    with pytest.raises(AssertionError) as exc:
        vn.check_results(datafiles('missing-bdb'), errstream=errs,
            required=[(r'quickstart', [vn.assert_raises("No bdb in")]),
                      (r'import matplotlib', [vn.assert_warns(r'prop_cycle')]),
                      (r'not-there', [vn.assert_raises("no such error")])])
    assert "not-there" in errs.getvalue()
Example #2
0
def test_ma_schools():
  do_not_track(MASCHOOLS_DIR)
  msglimit = None if pytest.config.option.verbose else 1000
  vn.run_and_verify_notebook(
      os.path.join(MASCHOOLS_DIR, "MASchoolDistricts"),
      msglimit=msglimit,
      required=[('schools2.quick_explore_vars\(\[',
                 [vn.assert_warns('matplotlib.*backend')]),
                ('schools0.quick_describe_columns()', [r'categorical']),
                ('schools1.quick_describe_columns()', [r'categorical']),
                # Once solved, this will not contain categorical, but as
                # set up to be solved, it does:
                ('schools2.quick_describe_columns()', [r'categorical']),
                # As set up, this should have "cannot convert" messages,
                # Not once it's solved though.
                ('df.applymap', [vn.assert_stream_matches('Cannot convert')]),
                ])
def test_exhaustive():
    errs = []
    vn.check_results(datafiles('missing-bdb'), errstream=errs,
        exhaustive=[
            (r'quickstart', [vn.assert_raises("No bdb in")]),
            (r'import matplotlib', [vn.assert_warns(r'prop_cycle')])])
    assert 40 == len(errs)
    assert "Expected /quickstart/\nFound: # Exploring" in str(errs[0])
    assert "Expected /import matplotlib/\nFound: # Load" in str(errs[1])
    assert "Unexpected cell at end: Cell[2]:" in str(errs[2])
    assert "SessionOrchestrator" in str(errs[2])

    errs = []
    def recordTrue(i, c, d):
        errs.append((i, c, d))
        return True


    expected = [
        (r'# Exploring',
         [vn.assert_markdown_matches(r'Union of Concerned Scientists'),
          r'database',
          vn.assert_markdown_not("foobarbaz")]),
        (r'quickstart', [vn.assert_raises("No bdb in")]),
        (r'session-saving', [vn.assert_no_pyout()]),
        (r'Querying the data', [recordTrue, "SQL", recordTrue]),
        (r'satellites', ["Purpose", recordTrue, "Multinational",
                         vn.assert_pyout_matches("launch_vehicle", flags=re.I),
                         vn.assert_stream_matches(r"BQL \[SELECT \* FROM"),
                         # "well-known satellites" is in the input:
                         vn.assert_pyout_not("well-known satellites")]),
        # From here on out, tests fail in various ways.
        (r'do not match', []),   # 5
        ('do not match', []),    # 6
        (''),                    # 7
        ([]),                    # 8
        (),                      # 9
        ('', [], 'foo'),         # 10
        ('', [vn.assert_no_pyout(),  # One failure per cell,
              lambda i, c, d: 'c' + 5]),  # So this never runs, so no TypeError.
        ('', [vn.assert_markdown_matches('')]),  # 12
        ('', [vn.assert_pyout_matches('')]),     # 13
        ('', [vn.assert_markdown_not('')]),      # 14
        ('', [vn.assert_pyout_not('')]),         # 15
        ('', [vn.assert_markdown_not('')]),      # 16
        ('', [vn.assert_markdown_not('')]),      # 17
        ('', [vn.assert_pyout_not('')]),         # 18
        ('', []),
        ('', []),
        ('', [vn.assert_has_png()]),
        ('', []),
        ('', [vn.assert_has_png()]),
        ('', []),
        ('', []),
        ('', [vn.assert_stream_not('')]),
        ('', [lambda i, c, d: False]),
        ('', [lambda i, c, d: []]),
        ('', [lambda i, c, d: None]),
        ]
    expected += [('', [])] * (40 - len(expected) - 1)  # 40 total cells.
    vn.check_results(datafiles('missing-bdb'), errstream=errs,
                     exhaustive=expected)
    assert 3 == errs.pop(0)[0] # cell_i from the first recordTrue
    assert 3 == errs.pop(0)[0]
    assert 4 == errs.pop(0)[0]
    assert "/do not match/" in str(errs.pop(0))
    assert "/do not match/" in str(errs.pop(0))
    assert "Expected an input pattern and a list of tests" in str(errs.pop(0))
    assert "Expected an input pattern and a list of tests" in str(errs.pop(0))
    assert "Expected an input pattern and a list of tests" in str(errs.pop(0))
    assert "Expected an input pattern and a list of tests" in str(errs.pop(0))
    assert "Expected code cell" in str(errs.pop(0))
    assert "Expected markdown cell" in str(errs.pop(0))
    assert "Expected code cell" in str(errs.pop(0))
    assert "Expected markdown cell" in str(errs.pop(0))
    assert "Expected code cell" in str(errs.pop(0))
    assert "Expected markdown cell" in str(errs.pop(0))
    assert "Expected not to find" in str(errs.pop(0))
    assert "Expected not to find" in str(errs.pop(0))
    assert "Expected not to find" in str(errs.pop(0))
    assert "returned False" in str(errs.pop(0))
    assert "returned []" in str(errs.pop(0))
    assert "returned None" in str(errs.pop(0))
    assert "Undeclared png" in str(errs.pop(0))
    assert "Unexpected cell at end" in str(errs.pop(0))
    assert [] == errs

    expected = [('', [])] * 40   # 40 total cells
    expected += [('foobarbaz', [])] * 2
    vn.check_results(datafiles('missing-bdb'), errstream=errs,
                     exhaustive=expected)
    assert "Expected pattern /foobarbaz/ not found" in str(errs[-1])
    assert "Expected pattern /foobarbaz/ not found" in str(errs[-2])
    assert 8 == len(errs)  # Those two plus the six undeclared displays.