MCPcopy Create free account
hub / github.com/Layout-Parser/layout-parser / test_dict

Function test_dict

tests/test_elements.py:406–460  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

404
405
406def test_dict():
407
408 i = Interval(1, 2, "y", canvas_height=5)
409 i_dict = {
410 "block_type": "interval",
411 "start": 1,
412 "end": 2,
413 "axis": "y",
414 "canvas_height": 5,
415 "canvas_width": 0,
416 }
417 assert i.to_dict() == i_dict
418 assert i == Interval.from_dict(i_dict)
419
420 r = Rectangle(1, 2, 3, 4)
421 r_dict = {"block_type": "rectangle", "x_1": 1, "y_1": 2, "x_2": 3, "y_2": 4}
422 assert r.to_dict() == r_dict
423 assert r == Rectangle.from_dict(r_dict)
424
425 q = Quadrilateral(np.arange(8).reshape(4, 2), 200, 400)
426 q_dict = {
427 "block_type": "quadrilateral",
428 "points": [0, 1, 2, 3, 4, 5, 6, 7],
429 "height": 200,
430 "width": 400,
431 }
432 assert q.to_dict() == q_dict
433 assert q == Quadrilateral.from_dict(q_dict)
434
435 l = Layout([i, r, q], page_data={"width": 200, "height": 200})
436 l_dict = {
437 "page_data": {"width": 200, "height": 200},
438 "blocks": [i_dict, r_dict, q_dict],
439 }
440 assert l.to_dict() == l_dict
441
442 i2 = TextBlock(i, "")
443 i_dict["text"] = ""
444 assert i2.to_dict() == i_dict
445 assert i2 == TextBlock.from_dict(i_dict)
446
447 r2 = TextBlock(r, id=24)
448 r_dict["id"] = 24
449 assert r2.to_dict() == r_dict
450 assert r2 == TextBlock.from_dict(r_dict)
451
452 q2 = TextBlock(q, text="test", parent=45)
453 q_dict["text"] = "test"
454 q_dict["parent"] = 45
455 assert q2.to_dict() == q_dict
456 assert q2 == TextBlock.from_dict(q_dict)
457
458 l2 = Layout([i2, r2, q2])
459 l2_dict = {"page_data": {}, "blocks": [i_dict, r_dict, q_dict]}
460 assert l2.to_dict() == l2_dict

Callers

nothing calls this directly

Calls 9

to_dictMethod · 0.95
to_dictMethod · 0.95
to_dictMethod · 0.95
IntervalClass · 0.90
RectangleClass · 0.90
QuadrilateralClass · 0.90
LayoutClass · 0.90
TextBlockClass · 0.90
from_dictMethod · 0.45

Tested by

no test coverage detected