Skip to content

Commit

Permalink
extended unittest coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfus committed May 24, 2016
1 parent c4f8567 commit bf14a55
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 12 deletions.
82 changes: 81 additions & 1 deletion muesr/tests/core/test_magmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ def setUp(self):
self._latt = np.random.rand(3,3)
self._mm = MM(self.NUMFCS, self._latt)
self._mmnolat = MM(self.NUMFCS)

def test_wrong_init(self):
with self.assertRaises(TypeError):
mm = MM(None)
with self.assertRaises(ValueError):
mm = MM('a')
with self.assertRaises(ValueError):
mm = MM(-1)


def test_cannot_set_anything(self):
with self.assertRaises(TypeError):
self._mm.ciao = 2

def test_size_property(self):

self.assertEqual(self._mm.size,self.NUMFCS)


def test_lattice_params_property(self):

np.testing.assert_array_equal(self._mm.lattice_params, self._latt)
Expand All @@ -34,6 +48,16 @@ def test_k_property(self):
self._mm.k = np.array([0.1,0.2,0.3])

np.testing.assert_array_equal(self._mm.k, np.array([0.1,0.2,0.3]))

with self.assertRaises(TypeError):
self._mm.k = 1
with self.assertRaises(ValueError):
self._mm.k = np.zeros(4)
with self.assertRaises(ValueError):
self._mm.k = np.zeros([4,4])
with self.assertRaises(ValueError):
self._mm.k = np.array(['a','a','a'])


def test_fc_property(self):

Expand Down Expand Up @@ -65,7 +89,41 @@ def test_fc_property(self):
randomfcs = self._mm.fc_get(int(conv_list[i+1]))

np.testing.assert_array_almost_equal(origfcs, randomfcs)



self._mm.fc = randomfcs
np.testing.assert_array_almost_equal(randomfcs, self._mm.fc)
np.testing.assert_array_almost_equal(randomfcs, self._mm.fcCart)

self._mm.fcCart = randomfcs
np.testing.assert_array_almost_equal(randomfcs, self._mm.fc)
np.testing.assert_array_almost_equal(randomfcs, self._mm.fcCart)

self._mm.fcLattBMA = randomfcs
np.testing.assert_array_almost_equal(randomfcs, self._mm.fcLattBMA)

self._mm.fcLattBM = randomfcs
np.testing.assert_array_almost_equal(randomfcs, self._mm.fcLattBM)

# test wrong type
with self.assertRaises(ValueError):
self._mm.fcLattBM = np.zeros(3)
with self.assertRaises(ValueError):
self._mm.fcLattBM = np.zeros(3,dtype=np.complex)
with self.assertRaises(ValueError):
self._mm.fcLattBM = np.zeros(3,dtype=np.complex)

with self.assertRaises(TypeError):
self._mm.fc_set(np.zeros([2,3],dtype=np.complex),'cart') #must be int


with self.assertRaises(ValueError):
self._mm.fc_set(np.zeros([2,3],dtype=np.complex),4) #must 0,1,2

with self.assertRaises(TypeError):
self._mm.fc_set('a',1) #must 0,1,2



def test_phi_property(self):
np.testing.assert_array_equal(self._mm.phi, np.zeros(self.NUMFCS))
Expand All @@ -75,6 +133,24 @@ def test_phi_property(self):

np.testing.assert_array_equal(self._mm.phi, randomphis)

randomphis = np.random.random(self.NUMFCS).tolist()
self._mm.phi = randomphis
np.testing.assert_array_equal(self._mm.phi, randomphis)


with self.assertRaises(TypeError):
self._mm.phi = 1
with self.assertRaises(ValueError):
self._mm.phi = np.zeros([2,3])

with self.assertRaises(ValueError):
self._mm.phi = ['a']

with self.assertRaises(ValueError):
self._mm.phi = np.array(['a','a'])



def test_desc_property(self):
self._mm.desc = "ciao"

Expand Down Expand Up @@ -193,3 +269,7 @@ def test_desc_property(self):
def test_isSymbolic(self):

self.assertEqual(self._smm.isSymbolic,True)


if __name__ == '__main__':
unittest.main()
91 changes: 80 additions & 11 deletions muesr/tests/core/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def _set_a_cell(self):
cell=[[3.,0,0],
[0,3.,0],
[0,0,3.]])
def test_set_attribute(self):
with self.assertRaises(TypeError):
self._sample.blabla = 1


def test_name_property(self):

Expand Down Expand Up @@ -70,6 +74,14 @@ def test_add_muon_property(self):

self._set_a_cell()

with self.assertRaises(TypeError):
self._sample.add_muon('0 0 0')

with self.assertRaises(ValueError):
self._sample.add_muon([0,0,0,0])
with self.assertRaises(ValueError):
self._sample.add_muon(np.array([0,0,0,0]))

self._sample.add_muon([0,0,0])
np.testing.assert_array_equal(self._sample.muons[0], np.zeros(3))

Expand Down Expand Up @@ -167,20 +179,26 @@ def test_current_mm_idx_property(self):
self._sample.current_mm_idx = 0
self.assertEqual(self._sample.current_mm_idx,0)
np.testing.assert_array_equal(self._sample.mm.k, np.array([0,0,1.]))


with self.assertRaises(IndexError):
self._sample.current_mm_idx = 3


def test_sym_property(self):

self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

with self.assertRaises(SymmetryError):
self._sample.sym

with self.assertRaises(TypeError):
self._sample.sym = 1

with self.assertRaises(TypeError):
self._sample.sym = 1

self._sample.sym = Spacegroup(113)

self.assertEqual(self._sample.sym.no,113)
self.assertEqual(self._sample.sym.setting,1)


def test_cell_property(self):
#needs better testing
Expand All @@ -191,9 +209,10 @@ def test_cell_property(self):
self._sample.cell = 1

self._set_a_cell()



current_cell = self._sample.cell
self.assertEqual(current_cell.get_chemical_symbols(),['Co'])
current_cell.set_chemical_symbols(['Co'])
self.assertEqual(current_cell.get_chemical_symbols(),['Co'])

def test_reset(self):

Expand Down Expand Up @@ -238,13 +257,63 @@ def test_reset(self):

# TODO
def test_check_sym(self):
pass
self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

with self.assertRaises(SymmetryError):
self._sample._check_sym()

# vary bad from user side
self._sample._sym = 1
with self.assertRaises(SymmetryError):
self._sample._check_sym()

self._sample.sym = Spacegroup(113)
self.assertTrue(self._sample._check_sym())

def test_check_lattice(self):
pass
self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

with self.assertRaises(CellError):
self._sample._check_lattice()

self._set_a_cell()
self.assertTrue(self._sample._check_lattice())
self._sample._cell = 1
with self.assertRaises(CellError):
self._sample._check_lattice()



def test_check_magdefs(self):
pass
self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

with self.assertRaises(MagDefError):
self._sample._check_magdefs()

self._set_a_cell()
self._sample.new_mm()
self.assertTrue(self._sample._check_magdefs())

self._sample._magdefs = 1
with self.assertRaises(MagDefError):
self._sample._check_magdefs()


def test_check_muon(self):
pass
self._sample._reset(cell=True,muon=True,sym=True,magdefs=True)

with self.assertRaises(MuonError):
self._sample._check_muon()

self._set_a_cell()
self._sample.add_muon(np.zeros(3))
self.assertTrue(self._sample._check_muon())

self._sample.add_muon(np.zeros(3))
self._sample._muon[1] = 'a'
with self.assertRaises(MuonError):
self._sample._check_muon()


if __name__ == '__main__':
unittest.main()
27 changes: 27 additions & 0 deletions muesr/tests/engines/test_clfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@

import lfcext


class TestLocalFields(unittest.TestCase):
def test_init(self):
with self.assertRaises(TypeError):
LocalFields('a',2,None)

with self.assertRaises(ValueError):
LocalFields(np.zeros(2),np.zeros(3),np.zeros(3))


with self.assertRaises(ValueError):
LocalFields(np.array(['a','a']),np.array(['a','a']),np.array(['a','a']))


lf = LocalFields(np.zeros(3),np.zeros(3),np.zeros(3))
np.testing.assert_array_equal(lf.T,np.zeros(3))


with self.assertRaises(TypeError):
lf.bubu = 1




class TestCLFC(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -54,6 +78,9 @@ def test_find_largest_sphere(self):
self.assertEqual(find_largest_sphere(self.sample,[1,1,1]),1.5)
self.sample.add_muon([1.,1.,1.],cartesian=True)
self.assertEqual(find_largest_sphere(self.sample,[1,1,1]),1.)




# http://stackoverflow.com/a/6802723
def rotation_matrix(axis, theta):
Expand Down

0 comments on commit bf14a55

Please sign in to comment.