def test_slice_rates_to_data_as_array(self): ''' Test the function to slice some magnitude rate and add to dictionary ''' self.writer = WriteStrainCsv('some_random_file') self.model.seismicity_rate = np.array([[1., 2.], [1., 2.], [1., 2.]]) self.model.target_magnitudes = np.array([5.5, 6.6]) self.model, output_variables = self.writer.slice_rates_to_data( self.model) self.assertListEqual(output_variables, ['longitude', 'latitude', 'exx', 'eyy', 'exy', '5.500', '6.600']) np.testing.assert_array_almost_equal(self.model.data['5.500'], np.array([1., 1., 1.])) np.testing.assert_array_almost_equal(self.model.data['6.600'], np.array([2., 2., 2.]))
def test_write_to_file(self): ''' Tests the writer to a file ''' self.writer = WriteStrainCsv('a_test_file.csv') self.model.seismicity_rate = np.array([[1., 2.], [1., 2.], [1., 2.]]) self.model.target_magnitudes = np.array([5.5, 6.6]) expected = [['longitude', 'latitude', 'exx', 'eyy', 'exy', '5.500', '6.600'], ['30.0', '30.0', '1.0', '1.0', '1.0', '1.0', '2.0'], ['30.0', '30.0', '2.0', '2.0', '2.0', '1.0', '2.0'], ['30.0', '30.0', '3.0', '3.0', '3.0', '1.0', '2.0']] self.writer.write_file(self.model) f = open('a_test_file.csv') data = csv.reader(f) for iloc, row in enumerate(data): self.assertListEqual(expected[iloc], row) os.system('rm a_test_file.csv')
def test_instantiation(self): ''' Test basic instantiation with a filename ''' self.writer = WriteStrainCsv('some_random_file') self.assertEqual(self.writer.filename, 'some_random_file')
class TestStrainCsvWriter(unittest.TestCase): ''' Class to test strain model writer to csv ''' def setUp(self): ''' ''' self.writer = None self.model = GeodeticStrain() self.model.data = OrderedDict([ ('longitude', np.array([30., 30., 30.])), ('latitude', np.array([30., 30., 30.])), ('exx', np.array([1., 2., 3.])), ('eyy', np.array([1., 2., 3.])), ('exy', np.array([1., 2., 3.]))]) self.filename = None def test_instantiation(self): ''' Test basic instantiation with a filename ''' self.writer = WriteStrainCsv('some_random_file') self.assertEqual(self.writer.filename, 'some_random_file') def test_slice_rates_to_data_as_array(self): ''' Test the function to slice some magnitude rate and add to dictionary ''' self.writer = WriteStrainCsv('some_random_file') self.model.seismicity_rate = np.array([[1., 2.], [1., 2.], [1., 2.]]) self.model.target_magnitudes = np.array([5.5, 6.6]) self.model, output_variables = self.writer.slice_rates_to_data( self.model) self.assertListEqual(output_variables, ['longitude', 'latitude', 'exx', 'eyy', 'exy', '5.500', '6.600']) np.testing.assert_array_almost_equal(self.model.data['5.500'], np.array([1., 1., 1.])) np.testing.assert_array_almost_equal(self.model.data['6.600'], np.array([2., 2., 2.])) def test_write_to_file(self): ''' Tests the writer to a file ''' self.writer = WriteStrainCsv('a_test_file.csv') self.model.seismicity_rate = np.array([[1., 2.], [1., 2.], [1., 2.]]) self.model.target_magnitudes = np.array([5.5, 6.6]) expected = [['longitude', 'latitude', 'exx', 'eyy', 'exy', '5.500', '6.600'], ['30.0', '30.0', '1.0', '1.0', '1.0', '1.0', '2.0'], ['30.0', '30.0', '2.0', '2.0', '2.0', '1.0', '2.0'], ['30.0', '30.0', '3.0', '3.0', '3.0', '1.0', '2.0']] self.writer.write_file(self.model) f = open('a_test_file.csv') data = csv.reader(f) for iloc, row in enumerate(data): self.assertListEqual(expected[iloc], row) os.system('rm a_test_file.csv')