Example #1
0
def test_assert_object_with_json():
    from pykern import pkunit

    pkunit.empty_work_dir()
    pkunit.assert_object_with_json('assert1', {'a': 1})
    with pytest.raises(AssertionError):
        pkunit.assert_object_with_json('assert1', {'b': 1})
Example #2
0
def test_importer():
    from sirepo.template.srw_importer import import_python
    from pykern import pkio
    from pykern import pkresource
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp
    import glob
    import py
    _TESTS = {  # Values are optional arguments:
        'amx': ('amx', None),
        'amx_bl2': ('amx', '--op_BL=2'),
        'amx_bl3': ('amx', '--op_BL=3'),
        'amx_bl4': ('amx', '--op_BL=4'),
        'chx': ('chx', None),
        'chx_fiber': ('chx_fiber', None),
        'exported_chx': ('exported_chx', None),
        'exported_gaussian_beam': ('exported_gaussian_beam', None),
        'exported_undulator_radiation': ('exported_undulator_radiation', None),
        'lcls_simplified': ('lcls_simplified', None),
        'lcls_sxr': ('lcls_sxr', None),
        'sample_from_image': ('sample_from_image', None),
        'smi_es1_bump_norm': ('smi', '--beamline ES1 --bump --BMmode Norm'),
        'smi_es1_nobump': ('smi', '--beamline ES1'),
        'smi_es2_bump_lowdiv': ('smi', '--beamline ES2 --bump --BMmode LowDiv'),
        'smi_es2_bump_norm': ('smi', '--beamline ES2 --bump --BMmode Norm'),
        'srx': ('srx', None),
        'srx_bl2': ('srx', '--op_BL=2'),
        'srx_bl3': ('srx', '--op_BL=3'),
        'srx_bl4': ('srx', '--op_BL=4'),
    }

    dat_dir = py.path.local(pkresource.filename('template/srw/',
                                                import_python))
    with pkunit.save_chdir_work():
        work_dir = py.path.local('.')
        for f in glob.glob(str(dat_dir.join('mirror_*d.dat'))):
            py.path.local(f).copy(work_dir)
        py.path.local(str(dat_dir.join('sample.tif'))).copy(work_dir)
        for b in sorted(_TESTS.keys()):
            base_py = '{}.py'.format(_TESTS[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            actual = import_python(
                code,
                tmp_dir=str(work_dir),
                lib_dir=str(work_dir),
                user_filename=r'c:\anything\{}.anysuffix'.format(_TESTS[b][0]),
                arguments=_TESTS[b][1],
            )
            actual['version'] = 'IGNORE-VALUE'
            pkunit.assert_object_with_json(b, actual)
Example #3
0
def test_sirepo_parser():
    with pkunit.save_chdir_work():
        for b in ['SRWLIB_VirtBL_LCLS_SXR_01']:
            base_py = '{}.py'.format(b)
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            error, actual = import_python(
                code,
                tmp_dir='.',
                lib_dir='.',
                user_filename=r'c:\x\{}.y'.format('SRWLIB_VirtBL_LCLS_SXR_01'),
            )
            assert not error, \
                '{}: should be valid input'.format(base_py)
            pkunit.assert_object_with_json(b, actual)
Example #4
0
def test_importer():
    from sirepo.template.srw_importer import import_python
    from pykern import pkio
    from pykern import pkresource
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp
    import glob
    import py
    _TESTS = {  # Values are optional arguments:
        'amx': ('amx', None),
        'amx_bl2': ('amx', '--op_BL=2'),
        'amx_bl3': ('amx', '--op_BL=3'),
        'amx_bl4': ('amx', '--op_BL=4'),
        'chx': ('chx', None),
        'chx_fiber': ('chx_fiber', None),
        'exported_chx': ('exported_chx', None),
        'exported_gaussian_beam': ('exported_gaussian_beam', None),
        'exported_undulator_radiation': ('exported_undulator_radiation', None),
        'lcls_simplified': ('lcls_simplified', None),
        'lcls_sxr': ('lcls_sxr', None),
        'sample_from_image': ('sample_from_image', None),
        'smi_es1_bump_norm': ('smi', '--beamline ES1 --bump --BMmode Norm'),
        'smi_es1_nobump': ('smi', '--beamline ES1'),
        'smi_es2_bump_lowdiv': ('smi', '--beamline ES2 --bump --BMmode LowDiv'),
        'smi_es2_bump_norm': ('smi', '--beamline ES2 --bump --BMmode Norm'),
        'srx': ('srx', None),
        'srx_bl2': ('srx', '--op_BL=2'),
        'srx_bl3': ('srx', '--op_BL=3'),
        'srx_bl4': ('srx', '--op_BL=4'),
    }

    dat_dir = py.path.local(pkresource.filename('template/srw/', import_python))
    with pkunit.save_chdir_work():
        work_dir = py.path.local('.')
        for f in glob.glob(str(dat_dir.join('mirror_*d.dat'))):
            py.path.local(f).copy(work_dir)
        py.path.local(str(dat_dir.join('sample.tif'))).copy(work_dir)
        for b in sorted(_TESTS.keys()):
            base_py = '{}.py'.format(_TESTS[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            actual = import_python(
                code,
                tmp_dir=str(work_dir),
                lib_dir=str(work_dir),
                user_filename=r'c:\anything\{}.anysuffix'.format(_TESTS[b][0]),
                arguments=_TESTS[b][1],
            )
            actual['version'] = 'IGNORE-VALUE'
            pkunit.assert_object_with_json(b, actual)
Example #5
0
def test_importer():
    from sirepo.importer import import_python
    with pkunit.save_chdir_work():
        for b in sorted(_TESTS.keys()):
            base_py = '{}.py'.format(_TESTS[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            tmp_dir = _create_tmp_dir()
            error, actual = import_python(
                code,
                tmp_dir='.',
                lib_dir=str(tmp_dir),
                user_filename=r'c:\anything\{}.anysuffix'.format(_TESTS[b][0]),
                arguments=_TESTS[b][1],
            )
            _remove_dir(tmp_dir)
            assert not error, \
                '{}: should import with an error: {}'.format(base_py, error)
            actual['version'] = 'IGNORE-VALUE'
            assert not error, \
                '{}: should be valid input'.format(base_py)
            pkunit.assert_object_with_json(b, actual)
Example #6
0
def _t(tests):
    from sirepo.template.srw_importer import import_python
    from pykern import pkio
    from pykern import pkresource
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp
    import glob
    import py

    with pkio.save_chdir(pkunit.work_dir()):
        for b in sorted(tests.keys()):
            base_py = '{}.py'.format(tests[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            actual = import_python(
                code,
                tmp_dir='.',
                user_filename=r'c:\anything\{}.anysuffix'.format(tests[b][0]),
                arguments=tests[b][1],
            )
            actual['version'] = 'IGNORE-VALUE'
            pkunit.assert_object_with_json(b, actual)
Example #7
0
def test_importer():
    from sirepo.importer import import_python
    dat_dir = py.path.local(pkresource.filename('static/dat/', import_python))
    with pkunit.save_chdir_work():
        work_dir = py.path.local('.')
        for f in glob.glob(str(dat_dir.join('mirror_*d.dat'))):
            py.path.local(f).copy(work_dir)
        for b in sorted(_TESTS.keys()):
            base_py = '{}.py'.format(_TESTS[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            error, actual = import_python(
                code,
                tmp_dir=str(work_dir),
                lib_dir=str(work_dir),
                user_filename=r'c:\anything\{}.anysuffix'.format(_TESTS[b][0]),
                arguments=_TESTS[b][1],
            )
            assert not error, \
                '{}: should import with an error: {}'.format(base_py, error)
            actual['version'] = 'IGNORE-VALUE'
            assert not error, \
                '{}: should be valid input'.format(base_py)
            pkunit.assert_object_with_json(b, actual)
Example #8
0
def test_importer():
    from sirepo.importer import import_python
    dat_dir = py.path.local(pkresource.filename('static/dat/', import_python))
    with pkunit.save_chdir_work():
        work_dir = py.path.local('.')
        for f in glob.glob(str(dat_dir.join('mirror_*d.dat'))):
            py.path.local(f).copy(work_dir)
        py.path.local(str(dat_dir.join('sample.tif'))).copy(work_dir)
        for b in sorted(_TESTS.keys()):
            base_py = '{}.py'.format(_TESTS[b][0])
            code = pkio.read_text(pkunit.data_dir().join(base_py))
            error, actual = import_python(
                code,
                tmp_dir=str(work_dir),
                lib_dir=str(work_dir),
                user_filename=r'c:\anything\{}.anysuffix'.format(_TESTS[b][0]),
                arguments=_TESTS[b][1],
            )
            assert not error, \
                '{}: should import with an error: {}'.format(base_py, error)
            actual['version'] = 'IGNORE-VALUE'
            assert not error, \
                '{}: should be valid input'.format(base_py)
            pkunit.assert_object_with_json(b, actual)
Example #9
0
def test_assert_object_with_json():
    pkunit.empty_work_dir()
    pkunit.assert_object_with_json('assert1', {'a': 1})
    with pytest.raises(AssertionError):
        pkunit.assert_object_with_json('assert1', {'b': 1})