Coverage for gws-app/gws/lib/htmlx/_test.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1import pytest 

2import gws 

3import gws.test.util as u 

4import gws.lib.htmlx as htmlx 

5from pathlib import Path 

6 

7 

8def test_render_to_pdf(tmp_path): 

9 html_content = "<html><body><h1>Test PDF</h1></body></html>" 

10 out_path = str(tmp_path / "output.pdf") 

11 result = htmlx.render_to_pdf(html_content, out_path) 

12 

13 assert Path(out_path).exists(), "PDF file was not created" 

14 assert result == out_path 

15 

16 

17def test_render_to_pdf_with_page_size(tmp_path): 

18 html_content = "<html><body><h1>Test PDF</h1></body></html>" 

19 out_path = str(tmp_path / "output.pdf") 

20 page_size = (100, 200, gws.Uom.mm) 

21 result = htmlx.render_to_pdf(html_content, out_path, page_size=page_size) 

22 

23 assert Path(out_path).exists(), "PDF file was not created with custom size" 

24 assert result == out_path 

25 

26 

27def test_render_to_pdf_with_margin(tmp_path): 

28 html_content = "<html><body><h1>Test PDF</h1></body></html>" 

29 out_path = str(tmp_path / "output.pdf") 

30 page_margin = (10, 10, 10, 10, gws.Uom.mm) 

31 result = htmlx.render_to_pdf(html_content, out_path, page_margin=page_margin) 

32 

33 assert Path(out_path).exists(), "PDF file was not created with margins" 

34 assert result == out_path 

35 

36 

37def test_render_to_png(tmp_path): 

38 html_content = "<html><body><h1>Test PNG</h1></body></html>" 

39 out_path = str(tmp_path / "output.png") 

40 result = htmlx.render_to_png(html_content, out_path) 

41 

42 assert Path(out_path).exists(), "PNG file was not created" 

43 assert result == out_path 

44 

45 

46def test_render_to_png_with_page_size(tmp_path): 

47 html_content = "<html><body><h1>Test PNG</h1></body></html>" 

48 out_path = str(tmp_path / "output.png") 

49 page_size = (500, 500, gws.Uom.px) 

50 result = htmlx.render_to_png(html_content, out_path, page_size=page_size) 

51 

52 assert Path(out_path).exists(), "PNG file was not created with custom size" 

53 assert result == out_path 

54 

55 

56def test_render_to_png_with_margin(tmp_path): 

57 html_content = "<html><body><h1>Test PNG</h1></body></html>" 

58 out_path = str(tmp_path / "output.png") 

59 page_margin = [10, 10, 10, 10] 

60 result = htmlx.render_to_png(html_content, out_path, page_margin=page_margin) 

61 

62 assert Path(out_path).exists(), "PNG file was not created with margins" 

63 assert result == out_path