<s> import json <EOL> import mock <EOL> from solum . api . controllers . v1 . datamodel import sensor as model <EOL> from solum . api . controllers . v1 import sensor as controller <EOL> from solum . common import exception <EOL> from solum import objects <EOL> from solum . tests import base <EOL> from solum . tests import fakes <EOL> class TestSensorValueTypeGood ( base . BaseTestCase ) : <EOL> scenarios = [ <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT:3> , in_type = '<STR_LIT:str>' , out_value = '<STR_LIT:3>' ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT:3> , in_type = '<STR_LIT:int>' , out_value = <NUM_LIT:3> ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = '<STR_LIT:3>' , in_type = '<STR_LIT:int>' , out_value = <NUM_LIT:3> ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT> , in_type = '<STR_LIT:str>' , out_value = '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = '<STR_LIT>' , in_type = '<STR_LIT:float>' , out_value = <NUM_LIT> ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT> , in_type = '<STR_LIT:float>' , out_value = <NUM_LIT> ) ) , <EOL> ] <EOL> def test_values ( self ) : <EOL> s = model . Sensor ( sensor_type = self . in_type , value = self . in_value ) <EOL> self . assertEqual ( self . out_value , s . value ) <EOL> class TestSensorValueTypeBad ( base . BaseTestCase ) : <EOL> scenarios = [ <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT> , in_type = '<STR_LIT:int>' ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = <NUM_LIT> , in_type = '<STR_LIT:int>' ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = - <NUM_LIT> , in_type = '<STR_LIT:int>' ) ) , <EOL> ( '<STR_LIT>' , dict ( <EOL> in_value = - <NUM_LIT> , in_type = '<STR_LIT:int>' ) ) , <EOL> ( '<STR_LIT:float>' , dict ( <EOL> in_value = '<STR_LIT>' , in_type = '<STR_LIT:float>' ) ) , <EOL> ] <EOL> def test_values ( self ) : <EOL> s = model . Sensor ( sensor_type = self . in_type , value = self . in_value ) <EOL> self . assertRaises ( ValueError , getattr , s , '<STR_LIT:value>' ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanRequest ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanResponse ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestSensorController ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSensorController , self ) . setUp ( ) <EOL> objects . load ( ) <EOL> def test_sensor_get ( self , handler_mock , resp_mock , request_mock ) : <EOL> handler_get = handler_mock . return_value . get <EOL> fake_sensor = fakes . FakeSensor ( ) <EOL> handler_get . return_value = fake_sensor <EOL> obj = controller . SensorController ( '<STR_LIT>' ) <EOL> result = obj . get ( ) <EOL> self . assertIsNotNone ( result ) <EOL> self . assertEqual ( fake_sensor . name , result [ '<STR_LIT:result>' ] . name ) <EOL> self . assertEqual ( fake_sensor . documentation , <EOL> result [ '<STR_LIT:result>' ] . documentation ) <EOL> self . assertEqual ( fake_sensor . description , result [ '<STR_LIT:result>' ] . description ) <EOL> self . assertEqual ( fake_sensor . project_id , result [ '<STR_LIT:result>' ] . project_id ) <EOL> self . assertEqual ( fake_sensor . uuid , result [ '<STR_LIT:result>' ] . uuid ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> self . assertIsNotNone ( result ) <EOL> handler_get . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_sensor_get_not_found ( self , handler_mock , resp_mock , request_mock ) : <EOL> handler_get = handler_mock . return_value . get <EOL> handler_get . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , sensor_id = '<STR_LIT>' ) <EOL> obj = controller . SensorController ( '<STR_LIT>' ) <EOL> obj . get ( ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> handler_get . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_sensor_put ( self , handler_mock , resp_mock , request_mock ) : <EOL> json_update = { '<STR_LIT:description>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request_mock . body = json . dumps ( json_update ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_update = handler_mock . return_value . update <EOL> handler_update . return_value = fakes . FakeSensor ( ) <EOL> obj = controller . SensorController ( '<STR_LIT>' ) <EOL> obj . put ( fakes . FakeSensor ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> handler_update . assert_called_once_with ( '<STR_LIT>' , json_update ) <EOL> def test_sensor_put_none ( self , handler_mock , resp_mock , request_mock ) : <EOL> request_mock . body = None <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_put = handler_mock . return_value . put <EOL> handler_put . return_value = fakes . FakeSensor ( ) <EOL> controller . SensorController ( '<STR_LIT>' ) . put ( ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_sensor_put_not_found ( self , handler_mock , resp_mock , request_mock ) : <EOL> json_update = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT:0>' } <EOL> request_mock . body = json . dumps ( json_update ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_update = handler_mock . return_value . update <EOL> handler_update . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , sensor_id = '<STR_LIT>' ) <EOL> controller . SensorController ( '<STR_LIT>' ) . put ( ) <EOL> handler_update . assert_called_with ( '<STR_LIT>' , json_update ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_sensor_delete ( self , mock_handler , resp_mock , request_mock ) : <EOL> handler_delete = mock_handler . return_value . delete <EOL> handler_delete . return_value = None <EOL> obj = controller . SensorController ( '<STR_LIT>' ) <EOL> obj . delete ( ) <EOL> handler_delete . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_sensor_delete_not_found ( self , mock_handler , <EOL> resp_mock , request_mock ) : <EOL> handler_delete = mock_handler . return_value . delete <EOL> handler_delete . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , sensor_id = '<STR_LIT>' ) <EOL> obj = controller . SensorController ( '<STR_LIT>' ) <EOL> obj . delete ( ) <EOL> handler_delete . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanRequest ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanResponse ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestSensorsController ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSensorsController , self ) . setUp ( ) <EOL> objects . load ( ) <EOL> def test_sensors_get_all ( self , handler_mock , resp_mock , request_mock ) : <EOL> hand_get_all = handler_mock . return_value . get_all <EOL> fake_sensor = fakes . FakeSensor ( ) <EOL> hand_get_all . return_value = [ fake_sensor ] <EOL> obj = controller . SensorsController ( ) <EOL> resp = obj . get_all ( ) <EOL> self . assertIsNotNone ( resp ) <EOL> self . assertEqual ( fake_sensor . name , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . name ) <EOL> self . assertEqual ( fake_sensor . documentation , <EOL> resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . documentation ) <EOL> self . assertEqual ( fake_sensor . description , <EOL> resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . description ) <EOL> self . assertEqual ( fake_sensor . project_id , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . project_id ) <EOL> self . assertEqual ( fake_sensor . uuid , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . uuid ) <EOL> hand_get_all . assert_called_with ( ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> def test_sensors_post ( self , handler_mock , resp_mock , request_mock ) : <EOL> json_update = { '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:str>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> request_mock . body = json . dumps ( json_update ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_create = handler_mock . return_value . create <EOL> handler_create . return_value = fakes . FakeSensor ( ) <EOL> obj = controller . SensorsController ( ) <EOL> obj . post ( fakes . FakeSensor ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> handler_create . assert_called_once_with ( json_update ) <EOL> def test_sensors_post_nodata ( self , handler_mock , resp_mock , request_mock ) : <EOL> request_mock . body = '<STR_LIT>' <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_create = handler_mock . return_value . create <EOL> handler_create . return_value = fakes . FakeSensor ( ) <EOL> ret_val = controller . SensorsController ( ) . post ( ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> str ( ret_val [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> class TestSensorAsDict ( base . BaseTestCase ) : <EOL> scenarios = [ <EOL> ( '<STR_LIT>' , dict ( data = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:value>' : '<STR_LIT:0>' } ) ) , <EOL> ( '<STR_LIT>' , dict ( data = { '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:str>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> ] <EOL> def test_as_dict ( self ) : <EOL> objects . load ( ) <EOL> s = model . Sensor ( ** self . data ) <EOL> self . data . pop ( '<STR_LIT>' , None ) <EOL> self . data . pop ( '<STR_LIT:type>' , None ) <EOL> self . assertEqual ( self . data , s . as_dict ( objects . registry . Sensor ) ) </s>
