def test_set_state(self): """ Tests the Set State Method """ result = self.app.get('/api/v1.0/state/001') self.assertEqual(result.status_code, 405) result = self.app.patch('/api/v1.0/state/001') self.assertEqual(result.status_code, 200) state = json.loads(result.data) self.assertEqual(state, {'state': '001'}) self.assertEqual(mod_state_file.StateFile().read(), '001') result = self.app.patch('/api/v1.0/state/020') self.assertEqual(result.status_code, 200) state = json.loads(result.data) self.assertEqual(state, {'state': '020'}) self.assertEqual(mod_state_file.StateFile().read(), '020') result = self.app.patch('/api/v1.0/state/abc') self.assertEqual(result.status_code, 200) state = json.loads(result.data) test_result = { 'state': '020', 'error': 'Invalid Value: "abc" State must be 3 digits.' } self.assertEqual(state, test_result) self.assertEqual(mod_state_file.StateFile().read(), '020')
def setUp(self): # Setup state file into known quanity self.original_state = mod_state_file.StateFile().read() mod_state_file.StateFile().init("000") # creates a test client self.app = service_state_api.app.test_client() # propagate the exceptions to the test client self.app.testing = True service_state_api.app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
def test_get_state_file(self): ''' Test mod_state_file.get_state_file() - Get State File Path ''' import os # Prep for tests goodpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) goodpath += "/services/status_led/.state" try: os.remove(goodpath) except OSError: pass # Test state_file(): badpath = os.path.dirname(os.path.realpath(__file__)) badpath += "/.state" self.assertEqual(mod_state_file.StateFile().state_file(), goodpath) self.assertNotEqual(mod_state_file.StateFile().state_file(), badpath)
def test_get_state(self): """ Tests Get State Method """ result = self.app.get('/api/v1.0/state') self.assertEqual(result.status_code, 200) state = json.loads(result.data) self.assertEqual(state, {'state': '000'}) mod_state_file.StateFile().write('020') result = self.app.get('/api/v1.0/state') self.assertEqual(result.status_code, 200) state = json.loads(result.data) self.assertEqual(state, {'state': '020'})
def test_read_write_test(self): ''' Test Read and Write ''' self.assertEqual(mod_state_file.StateFile().init(), "000") self.assertEqual(mod_state_file.StateFile().read(), "000") self.assertNotEqual(mod_state_file.StateFile().read(), "001") self.assertEqual(mod_state_file.StateFile().init("010"), "010") self.assertNotEqual(mod_state_file.StateFile().write("100"), "000") self.assertEqual(mod_state_file.StateFile().read(), "100")
def test_read_write_exceptions(self): ''' Test Read with No File. ''' with self.assertRaises(IOError): mod_state_file.StateFile().read() with self.assertRaises(ValueError): mod_state_file.StateFile().write("") with self.assertRaises(ValueError): mod_state_file.StateFile().write("10") with self.assertRaises(ValueError): mod_state_file.StateFile().write("1000") with self.assertRaises(ValueError): mod_state_file.StateFile().write("-10") with self.assertRaises(ValueError): mod_state_file.StateFile().write("300") with self.assertRaises(ValueError): mod_state_file.StateFile().write("abc") with self.assertRaises(ValueError): mod_state_file.StateFile().init("abc")
def test_valid_state(self): ''' Test mod_state_file.valid_state(state) ''' self.assertFalse(mod_state_file.StateFile().valid_state("")) self.assertFalse(mod_state_file.StateFile().valid_state("10")) self.assertFalse(mod_state_file.StateFile().valid_state("1000")) self.assertFalse(mod_state_file.StateFile().valid_state("-10")) self.assertFalse(mod_state_file.StateFile().valid_state("abc")) self.assertTrue(mod_state_file.StateFile().valid_state("000")) self.assertTrue(mod_state_file.StateFile().valid_state("111")) self.assertTrue(mod_state_file.StateFile().valid_state("222")) self.assertTrue(mod_state_file.StateFile().valid_state("100")) self.assertTrue(mod_state_file.StateFile().valid_state("010")) self.assertTrue(mod_state_file.StateFile().valid_state("001")) self.assertTrue(mod_state_file.StateFile().valid_state("200")) self.assertTrue(mod_state_file.StateFile().valid_state("020")) self.assertTrue(mod_state_file.StateFile().valid_state("002")) self.assertTrue(mod_state_file.StateFile().valid_state("222")) self.assertFalse(mod_state_file.StateFile().valid_state("223")) self.assertFalse(mod_state_file.StateFile().valid_state("003")) self.assertTrue(mod_state_file.StateFile().valid_state("211")) self.assertTrue(mod_state_file.StateFile().valid_state("121")) self.assertTrue(mod_state_file.StateFile().valid_state("112"))
def tearDown(self): # Reset state mod_state_file.StateFile().write(self.original_state)