Skip to content

Commit

Permalink
Add test that screengrabs examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matsjoyce-refeyn committed Oct 28, 2024
1 parent 2423593 commit 4493e92
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ jobs:
.\activate_venv.ps1
pre-commit run --all-files --show-diff-on-failure
- name: Run tests
run: |
.\activate_venv.ps1
pytest
- name: Build docs
run: |
.\activate_venv.ps1
Expand Down
8 changes: 8 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"configurations": [
{
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
15 changes: 11 additions & 4 deletions examples/Scatter.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import QuickGraphLib.PreFabs as QGLPreFabs
QGLPreFabs.XYAxes {
id: xyaxes

property var butterflyCurve: QuickGraphLib.Helpers.linspace(0, Math.PI * 24, 10000).map(butterflyPoint)

function butterflyPoint(t: real): point {
let middle = Math.exp(Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5) + Math.sin(t * 1000);
return Qt.point(Math.sin(t) * middle, Math.cos(t) * middle);
}

title: "10,000 points"
viewRect: Qt.rect(-2, -2, 4, 4)
viewRect: Qt.rect(-4.5, -4, 9, 9)

Repeater {
model: QuickGraphLib.Helpers.range(0, 10000).map(_ => [Math.sqrt(Math.random()) * 4 - 2, Math.sqrt(Math.random()) * 4 - 2])
model: xyaxes.butterflyCurve

QGLGraphItems.Marker {
required property var modelData
required property point modelData

color: "#22ff0000"
dataTransform: xyaxes.dataTransform
position: Qt.point(modelData[0], modelData[1])
position: modelData
width: 10
}
}
Expand Down
Binary file added tests/reference_images/AllTheAxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/BasicHistogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/BasicSinGraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/BasicSinGraphNoPrefabs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/BuiltinColormaps.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ColorMappedImages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ColorMesh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/Complex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/Contours.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/Conway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/DarkSinGraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/DraggableRegion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ImageAlignment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/InvertedAxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ReadoutLine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ScalingGraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/Scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/SpansAndLines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/Subgraphs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/TwinX.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ZoomableAsImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference_images/ZoomableAxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions tests/test_exports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Refeyn Ltd and other QuickGraphLib contributors
# SPDX-License-Identifier: MIT

import pathlib

import numpy as np
import pytest
from PySide6 import QtCore, QtGui, QtQml

import QuickGraphLib
from examples import conway

EXAMPLE_PATHS = [
path
for path in (pathlib.Path(__file__).parent.parent / "examples").glob("*.qml")
if path.name != "ExampleGallery.qml"
]
REFERENCE_PATH = pathlib.Path(__file__).parent / "reference_images"
GENERATE_REFERENCE_IMAGES = False


@pytest.fixture(scope="session")
def qapp() -> QtGui.QGuiApplication:
return QtGui.QGuiApplication([])


@pytest.mark.parametrize("example_path", EXAMPLE_PATHS, ids=lambda p: str(p.stem))
def test_export(
example_path: pathlib.Path, qapp: QtGui.QGuiApplication, tmp_path: pathlib.Path
) -> None:
image_path = tmp_path / f"{example_path.stem}.png"
engine = QtQml.QQmlApplicationEngine()
engine.setInitialProperties(
{
"exampleUrl": QtCore.QUrl.fromLocalFile(example_path),
"outputUrl": QtCore.QUrl.fromLocalFile(image_path),
}
)
engine.addImportPath(QuickGraphLib.QML_IMPORT_PATH)
engine.loadData(
rb"""
import QtQuick
Window {
id: root
required property url exampleUrl
required property url outputUrl
width: 800
height: 600
visible: true
Loader {
id: loader
source: exampleUrl
anchors.fill: parent
}
Component.onCompleted: {
let res = loader.item.grabToImage(result => {
result.saveToFile(root.outputUrl);
Qt.exit(0);
});
if (!res) {
Qt.exit(1);
}
}
}
"""
)
assert len(engine.rootObjects()) != 0
engine.quit.connect(qapp.quit)
assert qapp.exec() == 0

reference_path = REFERENCE_PATH / image_path.name
if GENERATE_REFERENCE_IMAGES:
if reference_path.exists():
reference_path.unlink()
image_path.rename(reference_path)

else:
reference_img = QtGui.QImage(str(reference_path))
output_img = QtGui.QImage(str(image_path))
assert reference_img.width() == output_img.width() == 800
assert reference_img.height() == output_img.height() == 600
shape = (reference_img.height(), reference_img.width(), 4)
reference_arr = np.asarray(reference_img.bits()).reshape(shape).astype(int)
output_arr = np.asarray(output_img.bits()).reshape(shape).astype(int)
difference = np.abs(reference_arr - output_arr).sum() / shape[0] / shape[1]
print(difference)
assert difference < 0.01

0 comments on commit 4493e92

Please sign in to comment.