def test_Instrument_Initialization():
    Instr = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0), concept='ManyToMany')
    pixels = 33
    split = [12]
    Det = Detector.TubeDetector1D(position=(1.0, 1, 0),
                                  direction=(1, 0, 0),
                                  pixels=pixels,
                                  split=split)
    Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1))

    wedge.append([Det, Det, Ana, Ana, Ana])

    try:
        Instr.initialize()
        assert False
    except ValueError:
        assert True

    try:
        print(Instr.A4)
        assert False
    except RuntimeError:
        assert True

    try:
        print(Instr.Ef)
        assert False
    except RuntimeError:
        assert True

    Instr.append(wedge)
    try:
        Instr.initialize()
        assert False
    except ValueError:
        assert True
    Instr.wedges[0].detectors[0].split = [0, 12, 20, pixels]
    Instr.initialize()

    assert (len(Instr.A4) == 1)
    assert (len(Instr.A4[0]) == 2)
    assert (len(Instr.A4[0][0]) == pixels)
    assert (len(Instr.A4) == len(Instr.Ef))
    assert (len(Instr.A4[0]) == len(Instr.Ef[0]))
    assert (len(Instr.A4[0][0]) == len(Instr.Ef[0][0]))
    assert (Instr.settings['Initialized'] == True)

    try:
        Instr.A4 = []
        assert False
    except NotImplementedError:
        assert True

    try:
        Instr.Ef = []
        assert False
    except NotImplementedError:
        assert True
def test_Instrument_append():
    Instr = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Instr.append([wedge, wedge])
    Instr.append(wedge)

    assert (len(Instr.wedges) == 3)
def test_Instrument_plot():
    Instr = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0))
    Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1))

    wedge.append([Det, Ana])
    Instr.append(wedge)
    plt.ioff()
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    Instr.plot(ax)
def test_Instrument_saveload():
    import os
    Instr = Instrument(position=(0, 1, 0))
    Instr2 = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0))
    Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1))

    wedge.append([Det, Ana])
    Instr.append(wedge)

    tempFile = 'temp.bin'
    Instr.save(tempFile)
    Instr2.load(tempFile)
    os.remove(tempFile)

    assert (Instr == Instr2)
def test_Instrument_error():

    try:
        Instr = Instrument(fileName='wrongDummyFile.bin')
        assert False
    except ValueError:
        assert True

    Instr = Instrument()

    Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1))

    try:
        Instr.wedges = Ana
        assert False
    except AttributeError:
        assert True

    try:
        Instr.wedges = [Ana, Ana]
        assert False
    except AttributeError:
        assert True

    try:
        Instr.append("Wrong object type")
        assert False
    except AttributeError:
        assert True

    try:
        Instr.append(["List of", 3.0, "wrong objects"])
        assert False
    except AttributeError:
        assert True

    try:
        Instr.settings = {'Name', 'New dictionary'}
        assert False
    except NotImplementedError:
        return True
def test_parseXML():  # Improve this test!

    tempFileName = '__temp__.xml'

    Instr = Instrument()
    Instr.settings['Author'] = 'Jakob Lass'

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0))
    Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1))

    wedge.append([Det, Ana])
    Instr.append([wedge, wedge])
    Instr.append(wedge)
    Instr.saveXML(tempFileName)

    InstrLoaded = Instrument(fileName=tempFileName)
    os.remove(tempFileName)

    assert (Instr == InstrLoaded)