-
Notifications
You must be signed in to change notification settings - Fork 359
[MRG + 1] Create a new figure and test each plot type #127 #179
Changes from 3 commits
dbf68a4
336fee8
5924d95
9dad125
0af6517
837a657
d0edd43
c83ebc2
655c4f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import matplotlib | ||
|
||
matplotlib.use('Agg') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import camelot | ||
import os | ||
import pytest | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from camelot.plotting import * | ||
|
||
|
||
testdir = os.path.dirname(os.path.abspath(__file__)) | ||
testdir = os.path.join(testdir, "files") | ||
|
||
|
||
@pytest.mark.mpl_image_compare(baseline_dir="files/baseline_plots") | ||
def test_text_plot(): | ||
filename = os.path.join(testdir, "foo.pdf") | ||
tables = camelot.read_pdf(filename) | ||
text = tables[0]._text | ||
fig = plot_text(text) | ||
ax = fig.axes[0] | ||
xs, ys = [], [] | ||
for t in text: | ||
xs.extend([t[0], t[2]]) | ||
ys.extend([t[1], t[3]]) | ||
assert ax.get_xlim() == (min(xs) - 10, max(xs) + 10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @suyash458 Why do we need additional asserts here? Doesn't pytest compare the whole image which is a sure shot way of knowing whether the images are the same or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case image comparison tests are skipped if the Should the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and in the Makefile too. After that we shouldn't need to worry about additional asserts. |
||
assert ax.get_ylim() == (min(ys) - 10, max(ys) + 10) | ||
assert len(ax.patches) == len(text) | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(baseline_dir="files/baseline_plots") | ||
def test_contour_plot(): | ||
filename = os.path.join(testdir, "foo.pdf") | ||
tables = camelot.read_pdf(filename) | ||
_, table_bbox = tables[0]._image | ||
fig = plot_contour(tables[0]._image) | ||
ax = fig.axes[0] | ||
assert len(ax.patches) == len(table_bbox.keys()) | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(baseline_dir="files/baseline_plots") | ||
def test_table_plot(): | ||
filename = os.path.join(testdir, "foo.pdf") | ||
tables = camelot.read_pdf(filename) | ||
cells = [cell for row in tables[0].cells for cell in row] | ||
num_lines = sum([sum([cell.left, cell.right, cell.top, cell.bottom]) for cell in cells]) | ||
fig = plot_table(tables[0]) | ||
ax = fig.axes[0] | ||
assert len(ax.lines) == num_lines | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(baseline_dir="files/baseline_plots") | ||
def test_line_plot(): | ||
filename = os.path.join(testdir, "foo.pdf") | ||
tables = camelot.read_pdf(filename) | ||
vertical, horizontal = tables[0]._segments | ||
fig = plot_line(tables[0]._segments) | ||
ax = fig.axes[0] | ||
assert len(ax.lines) == len(vertical + horizontal) | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(baseline_dir="files/baseline_plots") | ||
def test_joint_plot(): | ||
filename = os.path.join(testdir, "foo.pdf") | ||
tables = camelot.read_pdf(filename) | ||
_, table_bbox = tables[0]._image | ||
fig = plot_joint(tables[0]._image) | ||
ax = fig.axes[0] | ||
assert len(ax.lines) == len(table_bbox.keys()) | ||
return fig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@suyash458 Are you returning fig from the plot_* functions only for those additional asserts in the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytest-mpl's image comparison decorator requires the plot functions to return a matplotlib figure