コード例 #1
0
def test_publish_from_path():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root",
                 "some_path").AndReturn("test_web_root/some_path")
    fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)

    f = mox.CreateMockAnything()
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR
        | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    fs_mock.rename('some_temp_file', 'test_web_root/some_path')

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator(http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    instance.publish_from_path("some_path", content="some_content")

    mox.VerifyAll()
コード例 #2
0
def test_publish_raises_when_unable_to_create_folder():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    FAKE_WEB_ROOT = 'test_web_root'

    mox.StubOutWithMock(os, 'makedirs')
    mox.StubOutWithMock(os.path, 'exists')
    os.makedirs(FAKE_WEB_ROOT).AndRaise(ValueError())
    os.path.exists(FAKE_WEB_ROOT).AndReturn(False)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    mox.ReplayAll()

    with remove_web_root_from_settings():
        instance = StaticGenerator(
            http_request=http_request,
            model_base=model_base,
            manager=manager,
            model=model,
            queryset=queryset,
            settings=settings,
        )

        try:
            instance.publish_from_path("some_path", content="some_content")
        except StaticGeneratorException, e:
            assert str(e) == 'Could not create the directory: ' + FAKE_WEB_ROOT
            mox.VerifyAll()
            return
        finally:
コード例 #3
0
def test_publish_raises_when_unable_to_create_temp_file():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root",
                 "some_path").AndReturn("test_web_root/some_path")
    fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)

    fs_mock.tempfile(directory="test_web_root").AndRaise(ValueError())

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator(http_request=http_request,
                               model_base=model_base,
                               manager=manager,
                               model=model,
                               queryset=queryset,
                               settings=settings,
                               fs=fs_mock)

    try:
        instance.publish_from_path("some_path", content="some_content")
    except StaticGeneratorException, e:
        assert str(e) == 'Could not create the file: test_web_root/some_path'
        mox.VerifyAll()
        return
コード例 #4
0
def test_publish_raises_when_unable_to_create_temp_file():
    mox = Mox()
    _, model_base, manager, model, queryset = get_mocks(mox)

    FAKE_WEB_ROOT = 'test_web_root'

    mox.StubOutWithMock(tempfile, 'mkstemp')
    tempfile.mkstemp(dir="test_web_root").AndRaise(ValueError())

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    mox.ReplayAll()

    with remove_web_root_from_settings():
        instance = StaticGenerator(
            model_base=model_base,
            manager=manager,
            model=model,
            queryset=queryset,
            settings=settings,
        )

        try:
            instance.publish_from_path("some_path", content="some_content")
        except StaticGeneratorException, e:
            assert str(
                e) == 'Could not create the file: test_web_root/some_path'
            mox.VerifyAll()
            return
        finally:
コード例 #5
0
def test_publish_raises_when_unable_to_create_temp_file():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path")
    fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)

    fs_mock.tempfile(directory="test_web_root").AndRaise(ValueError())

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator(
        http_request=http_request,
        model_base=model_base,
        manager=manager,
        model=model,
        queryset=queryset,
        settings=settings,
        fs=fs_mock,
    )

    try:
        instance.publish_from_path("some_path", content="some_content")
    except StaticGeneratorException, e:
        assert str(e) == "Could not create the file: test_web_root/some_path"
        mox.VerifyAll()
        return
コード例 #6
0
def test_publish_raises_when_unable_to_create_temp_file():
    mox = Mox()
    _, model_base, manager, model, queryset = get_mocks(mox)

    FAKE_WEB_ROOT = 'test_web_root'

    mox.StubOutWithMock(tempfile, 'mkstemp')
    tempfile.mkstemp(dir="test_web_root").AndRaise(ValueError())

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    mox.ReplayAll()

    with remove_web_root_from_settings():
        instance = StaticGenerator(
            model_base=model_base,
            manager=manager,
            model=model,
            queryset=queryset,
            settings=settings,
        )

        try:
            instance.publish_from_path("some_path", content="some_content")
        except StaticGeneratorException, e:
            assert str(e) == 'Could not create the file: test_web_root/some_path'
            mox.VerifyAll()
            return
        finally:
コード例 #7
0
def test_publish_raises_when_unable_to_create_folder():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)
    FAKE_WEB_ROOT = 'test_web_root'

    mox.StubOutWithMock(os, 'makedirs')
    mox.StubOutWithMock(os.path, 'exists')
    os.makedirs(FAKE_WEB_ROOT).AndRaise(ValueError())
    os.path.exists(FAKE_WEB_ROOT).AndReturn(False)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    mox.ReplayAll()

    with remove_web_root_from_settings():
        instance = StaticGenerator(
            http_request=http_request,
            model_base=model_base,
            manager=manager,
            model=model,
            queryset=queryset,
            settings=settings,
        )

        try:
            instance.publish_from_path("some_path", content="some_content")
        except StaticGeneratorException, e:
            assert str(e) == 'Could not create the directory: ' + FAKE_WEB_ROOT
            mox.VerifyAll()
            return
        finally:
コード例 #8
0
def test_publish_from_path():
    FAKE_WEB_ROOT = 'test_web_root'
    FILE_PATH = 'some_path'
    FILE_CONTENT = 'some_content'

    FILE_RELATIVE_PATH = os.path.join(FAKE_WEB_ROOT, FILE_PATH)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    with remove_web_root_from_settings():
        instance = StaticGenerator(
            settings=settings
        )
        instance.publish_from_path(FILE_PATH, content=FILE_CONTENT)

    assert os.path.exists(FILE_RELATIVE_PATH), 'File {file_path} not found'.format(file_path=FILE_RELATIVE_PATH)

    with open(FILE_RELATIVE_PATH, 'r') as fd:
        assert fd.readline() == FILE_CONTENT, 'File {file_path} content differs'.format(file_path=FILE_RELATIVE_PATH)
コード例 #9
0
def test_publish_from_path():
    FAKE_WEB_ROOT = 'test_web_root'
    FILE_PATH = 'some_path'
    FILE_CONTENT = 'some_content'

    FILE_RELATIVE_PATH = os.path.join(FAKE_WEB_ROOT, FILE_PATH)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    with remove_web_root_from_settings():
        instance = StaticGenerator(settings=settings)
        instance.publish_from_path(FILE_PATH, content=FILE_CONTENT)

    assert os.path.exists(
        FILE_RELATIVE_PATH), 'File {file_path} not found'.format(
            file_path=FILE_RELATIVE_PATH)

    with open(FILE_RELATIVE_PATH, 'r') as fd:
        assert fd.readline(
        ) == FILE_CONTENT, 'File {file_path} content differs'.format(
            file_path=FILE_RELATIVE_PATH)
コード例 #10
0
def test_publish_from_path():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    fs_mock.join("test_web_root", "some_path").AndReturn("test_web_root/some_path")
    fs_mock.dirname("test_web_root/some_path").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)

    f = mox.CreateMockAnything()
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename,
        stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH,
    )
    fs_mock.rename("some_temp_file", "test_web_root/some_path")

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    instance = StaticGenerator(
        http_request=http_request,
        model_base=model_base,
        manager=manager,
        model=model,
        queryset=queryset,
        settings=settings,
        fs=fs_mock,
    )

    instance.publish_from_path("some_path", content="some_content")

    mox.VerifyAll()