Пример #1
0
def test_calculate_page_grid_empty():
    sane_json = SaneJson(get_mock('grid_checks/onecellgrid.json'))
    page = sane_json.get_sane_page(0)
    if SHOULD_HAVE_12_GRID:
        assert page.calculate_page_grid() == (12, 1)
    else:
        assert page.calculate_page_grid() == (1, 1)
Пример #2
0
def test_normalize_row_positions():
    sane_json = SaneJson(get_mock('grid_checks/fullgridpaged.json'))

    for sane_page in sane_json.get_sane_pages():
        sections = sane_page.get_sections()

        # Let's test that the page starts with 0
        assert sorted([row_pos(s) for s in sections])[0] == 0
Пример #3
0
    def __init__(self, sane_json_path: str):
        with open(sane_json_path, 'r') as f:
            self.json_data = json.load(f)

        # Transform the json if it is an old json's json
        if self.is_old_json_format():
            self.json_data = transform_old_json_format(self.json_data)

        self.json_data = general_json_fixes(self.json_data)
        self.sane_json = SaneJson(self.json_data)
Пример #4
0
def test_sane_json_constructor():
    sane_json = SaneJson(get_mock('basic.json'))

    assert sane_json
    assert len(sane_json.json_data) == 2
    assert sane_json.json_data[0]['type'] == 'text'
    assert sane_json.json_data[1]['type'] == 'text'
Пример #5
0
def test_pages_grid_constructor():
    sane_json = SaneJson(get_mock('basic.json'))
    assert len(sane_json.sane_pages) == 1

    first_page = sane_json.sane_pages[0]
    assert isinstance(first_page, SaneJsonPage)
    assert len(first_page) == 2
Пример #6
0
class Transform:
    """ Transforming the sane json into sections per page """
    def __init__(self, sane_json_path: str):
        with open(sane_json_path, 'r') as f:
            self.json_data = json.load(f)

        # Remove the logos
        self.json_data = remove_first_logos(self.json_data)

        # Transform the json if it is an old json's json
        if self.is_old_json_format():
            self.json_data = transform_old_json_format(self.json_data)

        self.json_data = general_json_fixes(self.json_data)
        self.sane_json = SaneJson(self.json_data)

    def get_pages(self) -> List[Page]:
        """
        Get pages and their corresponding section/wrapper objects.
        """
        pages = []

        for _, sane_page in enumerate(self.sane_json.get_sane_pages()):
            page = Page(sane_page)
            page.transform()
            pages.append(page)

        return pages

    def get_sane_json(self):
        """ Return the transformed sane json """
        return self.sane_json

    def is_old_json_format(self):
        json_data = self.json_data

        # Pass through to the json validation in SaneJson
        if len(json_data) == 0 or not isinstance(json_data, list):
            return False

        # Check basic validity of json, will validate in SaneJson
        if any([LAYOUT_KEY not in i for i in json_data]):
            return False

        has_w = WIDTH_POSITION_KEY in json_data[0][LAYOUT_KEY]
        has_h = HEIGHT_POSITION_KEY in json_data[0][LAYOUT_KEY]
        return not has_w or not has_h
Пример #7
0
def test_calculate_page_grid():
    sane_json = SaneJson(get_mock('basic.json'))
    page = sane_json.get_sane_page(0)
    assert page.calculate_page_grid() == (12, 2)
Пример #8
0
def test_calculate_page_grid_merge():
    sane_json = SaneJson(get_mock('grid_checks/mergegrid.json'))
    page = sane_json.get_sane_page(0)
    assert page.calculate_page_grid() == (12, 9)
Пример #9
0
def test_no_second_page_exception():
    sane_json = SaneJson(get_mock('basic.json'))
    with pytest.raises(IndexError):
        sane_json.get_sane_page(1)
Пример #10
0
def test_sane_json_invalid_layout_keys():
    with pytest.raises(JsonSchemaException) as e:
        SaneJson(get_mock('invalid/invalid_layout_keys.json'))
    assert 'data[0].layout.h must be bigger than or equal to 1' in str(e.value)
Пример #11
0
def test_sane_json_invalid_2ndpage_no_height_key():
    with pytest.raises(JsonSchemaException) as e:
        SaneJson(get_mock('invalid/bad_sane_json_7.json'))
    assert "data[1].layout must contain ['rowPos', 'columnPos', 'h', 'w']" + \
           " properties" in str(e.value)
Пример #12
0
def test_sane_json_invalid_no_layout():
    with pytest.raises(JsonSchemaException) as e:
        SaneJson(get_mock('invalid/bad_sane_json_2.json'))

    assert "data[0] must contain ['type', 'data', 'layout'] properties" in str(
        e.value)
Пример #13
0
def test_sane_json_invalid_not_list():
    with pytest.raises(JsonSchemaException) as e:
        SaneJson(get_mock('invalid/bad_sane_json_1.json'))
    assert 'data must be array' in str(e.value)
Пример #14
0
def test_sane_json_invalid_json():
    with pytest.raises(json.JSONDecodeError):
        SaneJson(get_mock('invalid/invalid_json.json'))

    with pytest.raises(json.JSONDecodeError):
        SaneJson(get_mock('invalid/empty.json'))