def test_impersonate_subject(self): resp = {'token': WA_ADMIN_TOKEN} responses.add(responses.POST, 'http://localhost/tokens', json=resp, status=200) auth_token = Token(WA_ADMIN_TOKEN) token = Tokens.impersonate(auth_token, subject_id=1) self.assertIsInstance(token, Token)
def test_impersonate_tenant_and_role(self): resp = {'token': WA_ADMIN_TOKEN} responses.add(responses.POST, 'http://localhost/tokens', json=resp, status=200) auth_token = Token(WA_ADMIN_TOKEN) token = Tokens.impersonate(auth_token, tenant_id=1, role='Admin') self.assertIsInstance(token, Token)
class PermissionTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'permissions') @responses.activate def test_create_permission(self): permission = Permission(name='Test Permission') resp = {'name': 'Test Permission', 'id': 10} responses.add(responses.POST, 'http://localhost/roles/1/permissions', json=resp, status=200) e = Permissions.create(self.token, role_id=1, permission=permission) self.assertIsInstance(e, Permission) self.assertEquals(permission.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'permissions') @responses.activate def test_retrieve_one_permission(self): permission = Permission(name='Test Permission', id=10) resp = {'name': 'Test Permission', 'id': 10} responses.add(responses.GET, 'http://localhost/roles/1/permissions/10', json=resp, status=200) e = Permissions.retrieve_one(self.token, role_id=1, permission_id=permission) self.assertIsInstance(e, Permission) self.assertEquals(permission.name, e.name) self.assertEquals(10, e.id) e = Permissions.retrieve_one(self.token, role_id=1, permission_id=permission.id) self.assertIsInstance(e, Permission) self.assertEquals(permission.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'permissions') @responses.activate def test_retrieve_all_permissions(self): permission = Permission(name='Test Permission', id=10) resp = [{'name': 'Test Permission', 'id': 10}] responses.add(responses.GET, 'http://localhost/roles/1/permissions', json=resp, status=200) el = Permissions.retrieve_all(self.token, role_id=1) self.assertIsInstance(el, list) e = el[0] self.assertEquals(permission.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'permissions') @responses.activate def test_update_permission(self): permission = Permission(name='Test Permission', id=10) resp = {'name': 'Test Permission', 'id': 10} responses.add(responses.PUT, 'http://localhost/roles/1/permissions/10', json=resp, status=200) e = Permissions.update(self.token, role_id=1, permission=permission) self.assertIsInstance(e, Permission) self.assertEquals(permission.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'permissions') @responses.activate def test_delete_permission(self): permission = Permission(name='Test Permission', id=10) resp = {'name': 'Test Permission', 'id': 10} responses.add(responses.DELETE, 'http://localhost/roles/1/permissions/10', body='', status=204) r = Permissions.delete(self.token, role_id=1, permission_id=permission) self.assertEquals(204, r.status_code) r = Permissions.delete(self.token, role_id=1, permission_id=permission.id) self.assertEquals(204, r.status_code)
class ReverseLookupTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'reverselookups') @responses.activate def test_single_reverse_lookup(self): reverse_lookup = ReverseLookup(tenant_id=100, tenant_uuid='12345', entity_type='point', entity={ 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 }) resp = { 'tenantId': 100, 'tenantUuid': '12345', 'type': 'point', 'entity': { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } } responses.add(responses.GET, 'http://localhost/reverselookup/?id=10&expand=True', json=resp, status=200, match_querystring=True) e = ReverseLookups.lookup(self.token, entity_id=10, expand=True) self.assertIsInstance(e, ReverseLookup) self.assertEquals(100, e.tenant_id) self.assertEquals('12345', e.tenant_uuid) self.assertEquals('point', e.entity_type) p = e.get_entity_as(entity_class=Point) self.assertIsInstance(p, Point) self.assertEquals(10, p.id) self.assertEquals('America/Chicago', p.timezone) self.assertEquals('Test Point', p.name) self.assertEquals('VOLTAGE', p.point_type) self.assertEquals('double', p.data_type) self.assertFalse(p.is_hidden) @attr('unit', 'reverselookups') @responses.activate def test_multi_reverse_lookup(self): reverse_lookup = ReverseLookup(tenant_id=100, tenant_uuid='12345', entity_type='point', entity={ 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 }) multi_reverse_lookup = MultiReverseLookup( lookups=[reverse_lookup], errorIds={ '11': HttpError(error_code=404, error_message="Not Found") }) resp = { 'lookups': [{ 'tenantId': 100, 'tenantUuid': '12345', 'type': 'point', 'entity': { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } }], 'errorIds': { '11': { 'errorCode': 404, 'errorMessage': 'Not Found' } } } responses.add(responses.POST, 'http://localhost/reverselookup/?expand=True', json=resp, status=200, match_querystring=True) e = ReverseLookups.multi_lookup(self.token, entity_ids=[10, 11], expand=True) self.assertIsInstance(e, MultiReverseLookup) self.assertEquals(1, len(e.lookups)) self.assertEquals(1, len(e.error_ids)) # Check lookup rl = e.lookups[0] self.assertIsInstance(rl, ReverseLookup) self.assertEquals(100, rl.tenant_id) self.assertEquals('12345', rl.tenant_uuid) self.assertEquals('point', rl.entity_type) p = rl.get_entity_as(entity_class=Point) self.assertIsInstance(p, Point) self.assertEquals(10, p.id) self.assertEquals('America/Chicago', p.timezone) self.assertEquals('Test Point', p.name) self.assertEquals('VOLTAGE', p.point_type) self.assertEquals('double', p.data_type) self.assertFalse(p.is_hidden) # Check errors error_id = e.error_ids.get(11) self.assertDictContainsKey(e.error_ids, 11) self.assertIsInstance(error_id, HttpError) self.assertEquals(404, error_id.error_code) self.assertEquals('Not Found', error_id.error_message)
class AssetPatternsTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'assets', 'patterns') @responses.activate def test_asset_retrieve_data(self): # GET /assets?assetType=TANK asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK') resp = [{ 'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10 }] responses.add( responses.GET, 'http://localhost/assets?assetType=TANK?limit=1000&offset=0', json=resp, status=200, match_querystring=True) # GET /sites/0/assets/<asset.id>/points point = Point(name='Test Point', timezone='America/Chicago', point_type='TOP_GAUGE') resp = [{ 'name': 'Test Point', 'timezone': 'America/Chicago', 'point_type': 'TOP_GAUGE', 'id': 11 }] responses.add( responses.GET, 'http://localhost/sites/0/assets/10/points?limit=1000&offset=0', json=resp, status=200, match_querystring=True) # POST /data/retrieve resp = { 'errors': [], 'observations': { '11': [{ 'timestamp': 12345, 'value': 'test' }] } } responses.add(responses.POST, 'http://localhost/data/retrieve?start=0&end=100000', json=resp, status=200, match_querystring=True) data = AssetPatterns.retrieve_data(self.token, asset_id=10, start=0, end=100000) self.assertIsInstance(data, DataRetrieveResponse) self.assertEquals(1, len(data.observations)) self.assertIsInstance(data.observations[11][0], Observation) o = data.observations[11][0] self.assertEquals(12345, o.timestamp) self.assertEquals('test', o.value) @attr('unit', 'assets', 'patterns') @responses.activate def test_find_assets_by_asset_type(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK') resp = [{ 'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10 }] responses.add( responses.GET, 'http://localhost/assets?assetType=TANK&offset=0&limit=1000', json=resp, status=200, match_querystring=True) al = AssetPatterns.find_assets_by_asset_type( self.token, asset_type_filter=asset.asset_type) self.assertIsInstance(al, array_types) self.assertEqual(1, len(al)) a = al[0] self.assertIsInstance(a, Asset) self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) @attr('unit', 'assets', 'patterns') @responses.activate def test_find_assets_by_parameter_point_value(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='WELL') point = Point(name='Test Parameter', timezone='America/Chicago', point_type='API_NUMBER', data_type='string', unit='none') assets_resp = [{ 'id': 10, 'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'WELL' }] points_resp = [{ 'id': 11, 'name': 'Test Parameter', 'timezone': 'America/Chicago', 'pointType': 'API_NUMBER', 'dataType': 'string', 'unit': 'none' }] data_resp = { 'errors': [], 'observations': { '11': [{ 'timestamp': 12345, 'value': 'test' }] } } responses.add( responses.GET, 'http://localhost/assets?assetType=WELL&limit=1000&offset=0', json=assets_resp, status=200, match_querystring=True) responses.add( responses.GET, 'http://localhost/sites/0/assets/10/points?pointType=API_NUMBER&limit=1000&offset=0', json=points_resp, status=200, match_querystring=True) responses.add( responses.POST, 'http://localhost/data/retrieve?start=0&limit=1&order=-timestamp', json=data_resp, status=200, match_querystring=True) matching_assets = AssetPatterns.find_assets_by_parameter_point_value( token=self.token, asset_type_filter=asset.asset_type, parameter_point_type=point.point_type, parameter_point_value='test') self.assertIsInstance(matching_assets, dict) asset, obs = matching_assets.items()[0] self.assertIsInstance(asset, Asset) self.assertIsInstance(obs, Observation) self.assertEquals(10, asset.id) self.assertEquals('test', obs.value)
class PointTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'points') @responses.activate def test_create_point(self): point = Point(name='Test Point', timezone='America/Chicago', point_type='VOLTAGE', data_type='double', is_hidden=False) resp = { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } responses.add(responses.POST, 'http://localhost/sites/1/assets/2/points', json=resp, status=200) e = Points.create(self.token, site_id=1, asset_id=2, point=point) self.assertIsInstance(e, Point) self.assertEquals(point.name, e.name) self.assertEquals(point.timezone, e.timezone) self.assertEquals(point.point_type, e.point_type) self.assertEquals(point.data_type, e.data_type) self.assertEquals(point.is_hidden, e.is_hidden) self.assertEquals(10, e.id) @attr('unit', 'points') @responses.activate def test_retrieve_one_point(self): point = Point(name='Test Point', timezone='America/Chicago', point_type='VOLTAGE', data_type='double', is_hidden=False, id=10) resp = { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } responses.add(responses.GET, 'http://localhost/sites/1/assets/2/points/10', json=resp, status=200) e = Points.retrieve_one(self.token, site_id=1, asset_id=2, point_id=point.id) self.assertIsInstance(e, Point) self.assertEquals(point.name, e.name) self.assertEquals(point.timezone, e.timezone) self.assertEquals(point.point_type, e.point_type) self.assertEquals(point.data_type, e.data_type) self.assertEquals(point.is_hidden, e.is_hidden) self.assertEquals(10, e.id) e = Points.retrieve_one(self.token, site_id=1, asset_id=2, point_id=point) self.assertIsInstance(e, Point) self.assertEquals(point.name, e.name) self.assertEquals(point.timezone, e.timezone) self.assertEquals(point.point_type, e.point_type) self.assertEquals(point.data_type, e.data_type) self.assertEquals(point.is_hidden, e.is_hidden) self.assertEquals(10, e.id) @attr('unit', 'points') @responses.activate def test_retrieve_all_points(self): point = Point(name='Test Point', timezone='America/Chicago', point_type='VOLTAGE', data_type='double', is_hidden=False) resp = [{ 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 }] responses.add(responses.GET, 'http://localhost/sites/1/assets/2/points', json=resp, status=200) el = Points.retrieve_all(self.token, site_id=1, asset_id=2) self.assertIsInstance(el, list) e = el[0] self.assertEquals(point.name, e.name) self.assertEquals(point.timezone, e.timezone) self.assertEquals(point.point_type, e.point_type) self.assertEquals(point.data_type, e.data_type) self.assertEquals(point.is_hidden, e.is_hidden) self.assertEquals(10, e.id) @attr('unit', 'points') @responses.activate def test_update_point(self): point = Point(name='Test Point', timezone='America/Chicago', point_type='VOLTAGE', data_type='double', is_hidden=False, id=10) resp = { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } responses.add(responses.PUT, 'http://localhost/sites/1/assets/2/points/10', json=resp, status=200) e = Points.update(self.token, site_id=1, asset_id=2, point=point) self.assertIsInstance(e, Point) self.assertEquals(point.name, e.name) self.assertEquals(point.timezone, e.timezone) self.assertEquals(point.point_type, e.point_type) self.assertEquals(point.data_type, e.data_type) self.assertEquals(point.is_hidden, e.is_hidden) self.assertEquals(10, e.id) @attr('unit', 'points') @responses.activate def test_delete_point(self): point = Point(name='Test Point', timezone='America/Chicago', point_type='VOLTAGE', data_type='double', is_hidden=False, id=10) resp = { 'name': 'Test Point', 'timezone': 'America/Chicago', 'pointType': 'VOLTAGE', 'dataType': 'double', 'isHidden': False, 'id': 10 } responses.add(responses.DELETE, 'http://localhost/sites/1/assets/2/points/10', body='', status=204) r = Points.delete(self.token, site_id=1, asset_id=2, point_id=point) self.assertEquals(204, r.status_code) r = Points.delete(self.token, site_id=1, asset_id=2, point_id=point.id) self.assertEquals(204, r.status_code)
class ControlPointTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'control_points') def test_control_point_serialization(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False) jdict = control_point.get_json_data() self.assertDictContainsKeyWithValue(jdict, 'dataType', 'double') self.assertDictContainsKeyWithValue(jdict, 'name', 'Test ControlPoint') self.assertDictContainsKeyWithValue(jdict, 'enabled', True) self.assertDictContainsKeyWithValue(jdict, 'hidden', False) @attr('unit', 'control_points') def test_control_point_deserialization(self): jdata = { 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 } control_point = ControlPoint.from_dict(jdata) self.assertEquals('Test ControlPoint', control_point.name) self.assertEquals(False, control_point.hidden) self.assertEquals(True, control_point.enabled) self.assertEquals('double', control_point.data_type) self.assertEquals(10, control_point.id) @attr('unit', 'control_points') @responses.activate def test_create_control_point(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False) resp = { 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 } responses.add( responses.POST, 'http://localhost/sites/1/assets/2/points/3/controlpoints', json=resp, status=200) e = ControlPoints.create(self.token, site_id=1, asset_id=2, point_id=3, control_point=control_point) self.assertIsInstance(e, ControlPoint) self.assertEquals(control_point.name, e.name) self.assertEquals(control_point.enabled, e.enabled) self.assertEquals(control_point.data_type, e.data_type) self.assertEquals(control_point.hidden, e.hidden) self.assertEquals(10, e.id) @attr('unit', 'control_points') @responses.activate def test_retrieve_one_control_point(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) resp = { 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 } responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10', json=resp, status=200) e = ControlPoints.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point.id) self.assertIsInstance(e, ControlPoint) self.assertEquals(control_point.name, e.name) self.assertEquals(control_point.enabled, e.enabled) self.assertEquals(control_point.data_type, e.data_type) self.assertEquals(control_point.hidden, e.hidden) self.assertEquals(10, e.id) e = ControlPoints.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point) self.assertIsInstance(e, ControlPoint) self.assertEquals(control_point.name, e.name) self.assertEquals(control_point.enabled, e.enabled) self.assertEquals(control_point.data_type, e.data_type) self.assertEquals(control_point.hidden, e.hidden) self.assertEquals(10, e.id) @attr('unit', 'control_points') @responses.activate def test_retrieve_all_control_points(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) resp = [{ 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 }] responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints', json=resp, status=200) el = ControlPoints.retrieve_all(self.token, site_id=1, asset_id=2, point_id=3) self.assertIsInstance(el, list) e = el[0] self.assertEquals(control_point.name, e.name) self.assertEquals(control_point.enabled, e.enabled) self.assertEquals(control_point.data_type, e.data_type) self.assertEquals(control_point.hidden, e.hidden) self.assertEquals(10, e.id) @attr('unit', 'control_points') @responses.activate def test_update_control_point(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) resp = { 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 } responses.add( responses.PUT, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10', json=resp, status=200) e = ControlPoints.update(self.token, site_id=1, asset_id=2, point_id=3, control_point=control_point) self.assertIsInstance(e, ControlPoint) self.assertEquals(control_point.name, e.name) self.assertEquals(control_point.enabled, e.enabled) self.assertEquals(control_point.data_type, e.data_type) self.assertEquals(control_point.hidden, e.hidden) self.assertEquals(10, e.id) @attr('unit', 'control_points') @responses.activate def test_delete_control_point(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) resp = { 'name': 'Test ControlPoint', 'enabled': True, 'dataType': 'double', 'hidden': False, 'id': 10 } responses.add( responses.DELETE, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10', body='', status=204) r = ControlPoints.delete(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point) self.assertEquals(204, r.status_code) r = ControlPoints.delete(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point.id) self.assertEquals(204, r.status_code) @attr('unit', 'control_points') @responses.activate def test_control_on_demand_read(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) resp = { 'status': 'pending', 'quality': 'uncertain', 'id': 100, 'requestType': 'ondemandread' } responses.add( responses.POST, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10/read', json=resp, status=200) e = ControlPoints.on_demand_read(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point) self.assertIsInstance(e, ControlAudit) self.assertEquals('pending', e.status) self.assertEquals('uncertain', e.quality) self.assertEquals('ondemandread', e.request_type) self.assertEquals(100, e.id) @attr('unit', 'control_points') @responses.activate def test_control_on_demand_read_options(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) headers = {'Allow': 'OPTIONS,POST'} responses.add( responses.OPTIONS, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10/read', body='', adding_headers=headers, status=200) e = ControlPoints.on_demand_read_options( self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point) self.assertIsInstance(e, list) self.assertListEqual(['OPTIONS', 'POST'], e) @attr('unit', 'control_points') @responses.activate def test_control_set(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) set_point_request = SetPointRequest(value=3, previous_value=1) resp = { 'status': 'pending', 'quality': 'uncertain', 'id': 100, 'requestType': 'setpointchange' } responses.add( responses.POST, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10/set', json=resp, status=200) e = ControlPoints.on_demand_set(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point, set_point_request=set_point_request) self.assertIsInstance(e, ControlAudit) self.assertEquals('pending', e.status) self.assertEquals('uncertain', e.quality) self.assertEquals('setpointchange', e.request_type) self.assertEquals(100, e.id) @attr('unit', 'control_points') @responses.activate def test_control_set_options(self): control_point = ControlPoint(name='Test ControlPoint', enabled=True, data_type='double', hidden=False, id=10) headers = {'Allow': 'OPTIONS,POST'} responses.add( responses.OPTIONS, 'http://localhost/sites/1/assets/2/points/3/controlpoints/10/set', body='', adding_headers=headers, status=200) e = ControlPoints.on_demand_set_options(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=control_point) self.assertIsInstance(e, list) self.assertListEqual(['OPTIONS', 'POST'], e)
class SiteTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'sites') @responses.activate def test_create_site(self): site = Site(name='Test Site', timezone='America/Chicago', latitude=1.0, longitude=2.0) resp = { 'name': 'Test Site', 'timezone': 'America/Chicago', 'latitude': 1.0, 'id': 10, 'longitude': 2.0 } responses.add(responses.POST, 'http://localhost/sites', json=resp, status=200) s = Sites.create(self.token, site=site) self.assertIsInstance(s, Site) self.assertEquals(site.name, s.name) self.assertEquals(site.timezone, s.timezone) self.assertEquals(site.latitude, s.latitude) self.assertEquals(site.longitude, s.longitude) self.assertEquals(10, s.id) @attr('unit', 'sites') @responses.activate def test_retrieve_one_site(self): site = Site(name='Test Site', timezone='America/Chicago', latitude=1.0, longitude=2.0, id=10) resp = { 'name': 'Test Site', 'timezone': 'America/Chicago', 'latitude': 1.0, 'id': 10, 'longitude': 2.0 } responses.add(responses.GET, 'http://localhost/sites/10', json=resp, status=200) s = Sites.retrieve_one(self.token, site_id=site.id) self.assertIsInstance(s, Site) self.assertEquals(site.name, s.name) self.assertEquals(site.timezone, s.timezone) self.assertEquals(site.latitude, s.latitude) self.assertEquals(site.longitude, s.longitude) self.assertEquals(10, s.id) s = Sites.retrieve_one(self.token, site_id=site) self.assertIsInstance(s, Site) self.assertEquals(site.name, s.name) self.assertEquals(site.timezone, s.timezone) self.assertEquals(site.latitude, s.latitude) self.assertEquals(site.longitude, s.longitude) self.assertEquals(10, s.id) @attr('unit', 'sites') @responses.activate def test_retrieve_all_sites(self): site = Site(name='Test Site', timezone='America/Chicago', latitude=1.0, longitude=2.0) resp = [{ 'name': 'Test Site', 'timezone': 'America/Chicago', 'latitude': 1.0, 'id': 10, 'longitude': 2.0 }] responses.add(responses.GET, 'http://localhost/sites', json=resp, status=200) sl = Sites.retrieve_all(self.token) self.assertIsInstance(sl, list) s = sl[0] self.assertEquals(site.name, s.name) self.assertEquals(site.timezone, s.timezone) self.assertEquals(site.latitude, s.latitude) self.assertEquals(site.longitude, s.longitude) self.assertEquals(10, s.id) @attr('unit', 'sites') @responses.activate def test_update_site(self): site = Site(name='Test Site', timezone='America/Chicago', latitude=1.0, longitude=2.0, id=10) resp = { 'name': 'Test Site', 'timezone': 'America/Chicago', 'latitude': 1.0, 'id': 10, 'longitude': 2.0 } responses.add(responses.PUT, 'http://localhost/sites/10', json=resp, status=200) s = Sites.update(self.token, site=site) self.assertIsInstance(s, Site) self.assertEquals(site.name, s.name) self.assertEquals(site.timezone, s.timezone) self.assertEquals(site.latitude, s.latitude) self.assertEquals(site.longitude, s.longitude) self.assertEquals(10, s.id) @attr('unit', 'sites') @responses.activate def test_delete_site(self): site = Site(name='Test Site', timezone='America/Chicago', latitude=1.0, longitude=2.0, id=10) resp = { 'name': 'Test Site', 'timezone': 'America/Chicago', 'latitude': 1.0, 'id': 10, 'longitude': 2.0 } responses.add(responses.DELETE, 'http://localhost/sites/10', body='', status=204) r = Sites.delete(self.token, site_id=site) self.assertEquals(204, r.status_code) r = Sites.delete(self.token, site_id=site.id) self.assertEquals(204, r.status_code)
class AssetTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'assets') @responses.activate def test_create_asset(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK') resp = {'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10} responses.add(responses.POST, 'http://localhost/sites/1/assets', json=resp, status=200) a = Assets.create(self.token, site_id=1, asset=asset) self.assertIsInstance(a, Asset) self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) @attr('unit', 'assets') @responses.activate def test_retrieve_one_asset(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK', id=10) resp = {'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10} responses.add(responses.GET, 'http://localhost/sites/1/assets/10', json=resp, status=200) a = Assets.retrieve_one(self.token, site_id=1, asset_id=asset.id) self.assertIsInstance(a, Asset) self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) a = Assets.retrieve_one(self.token, site_id=1, asset_id=asset) self.assertIsInstance(a, Asset) self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) @attr('unit', 'assets') @responses.activate def test_retrieve_all_assets(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK') resp = [{'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10}] responses.add(responses.GET, 'http://localhost/sites/1/assets', json=resp, status=200) al = Assets.retrieve_all(self.token, site_id=1) self.assertIsInstance(al, list) a = al[0] self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) @attr('unit', 'assets') @responses.activate def test_update_asset(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK', id=10) resp = {'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10} responses.add(responses.PUT, 'http://localhost/sites/1/assets/10', json=resp, status=200) a = Assets.update(self.token, site_id=1, asset=asset) self.assertIsInstance(a, Asset) self.assertEquals(asset.name, a.name) self.assertEquals(asset.timezone, a.timezone) self.assertEquals(asset.asset_type, a.asset_type) self.assertEquals(10, a.id) @attr('unit', 'assets') @responses.activate def test_delete_asset(self): asset = Asset(name='Test Asset', timezone='America/Chicago', asset_type='TANK', id=10) resp = {'name': 'Test Asset', 'timezone': 'America/Chicago', 'assetType': 'TANK', 'id': 10} responses.add(responses.DELETE, 'http://localhost/sites/1/assets/10', body='', status=204) r = Assets.delete(self.token, site_id=1, asset_id=asset) self.assertEquals(204, r.status_code) r = Assets.delete(self.token, site_id=1, asset_id=asset.id) self.assertEquals(204, r.status_code)
class ControlAuditTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'control_audits') def test_control_audit_serialization(self): control_audit = ControlAudit(status='pending', quality='uncertain', request_type='ondemandread') jdict = control_audit.get_json_data() self.assertDictContainsKeyWithValue(jdict, 'status', 'pending') self.assertDictContainsKeyWithValue(jdict, 'quality', 'uncertain') self.assertDictContainsKeyWithValue(jdict, 'requestType', 'ondemandread') @attr('unit', 'control_audits') def test_control_audit_deserialization(self): jdata = { 'status': 'pending', 'quality': 'uncertain', 'requestType': 'ondemandread', 'id': 10 } control_audit = ControlAudit.from_dict(jdata) self.assertEquals('pending', control_audit.status) self.assertEquals('uncertain', control_audit.quality) self.assertEquals('ondemandread', control_audit.request_type) self.assertEquals(10, control_audit.id) self.assertTrue(control_audit.quality_is_uncertain) self.assertFalse(control_audit.quality_is_bad) self.assertFalse(control_audit.quality_is_good) self.assertTrue(control_audit.status_is_pending) self.assertFalse(control_audit.status_is_failed) self.assertFalse(control_audit.status_is_success) self.assertTrue(control_audit.is_on_demand_read) self.assertFalse(control_audit.is_on_demand_set) @attr('unit', 'control_audits') @responses.activate def test_retrieve_one_control_audit(self): control_audit = ControlAudit(status='pending', quality='uncertain', request_type='ondemandread', id=10) resp = { 'status': 'pending', 'quality': 'uncertain', 'requestType': 'ondemandread', 'id': 10 } responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/audits/10', json=resp, status=200) e = ControlAudits.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_audit_id=control_audit.id) self.assertIsInstance(e, ControlAudit) self.assertEquals(control_audit.quality, e.quality) self.assertEquals(control_audit.status, e.status) self.assertEquals(control_audit.request_type, e.request_type) self.assertEquals(10, e.id) e = ControlAudits.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_audit_id=control_audit) self.assertIsInstance(e, ControlAudit) self.assertEquals(control_audit.quality, e.quality) self.assertEquals(control_audit.status, e.status) self.assertEquals(control_audit.request_type, e.request_type) self.assertEquals(10, e.id) @attr('unit', 'control_audits') @responses.activate def test_retrieve_all_control_audits(self): control_audit = ControlAudit(status='pending', quality='uncertain', request_type='ondemandread', id=10) resp = [{ 'status': 'pending', 'quality': 'uncertain', 'requestType': 'ondemandread', 'id': 10 }] responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/audits', json=resp, status=200) el = ControlAudits.retrieve_all(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4) self.assertIsInstance(el, list) e = el[0] self.assertEquals(control_audit.quality, e.quality) self.assertEquals(control_audit.status, e.status) self.assertEquals(control_audit.request_type, e.request_type) self.assertEquals(10, e.id) @attr('unit', 'control_audits') @responses.activate def test_update_control_audit(self): control_audit = ControlAudit(status='pending', quality='uncertain', request_type='ondemandread', id=10) resp = { 'status': 'pending', 'quality': 'uncertain', 'requestType': 'ondemandread', 'id': 10 } responses.add( responses.PUT, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/audits/10', json=resp, status=200) e = ControlAudits.update(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_audit=control_audit) self.assertIsInstance(e, ControlAudit) self.assertEquals(control_audit.quality, e.quality) self.assertEquals(control_audit.status, e.status) self.assertEquals(control_audit.request_type, e.request_type) self.assertEquals(10, e.id)
class SiteGroupTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'sitegroups') @responses.activate def test_create_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup') resp = {'name': 'Test SiteGroup', 'id': 10} responses.add(responses.POST, 'http://localhost/sitegroups', json=resp, status=200) e = SiteGroups.create(self.token, sitegroup=sitegroup) self.assertIsInstance(e, SiteGroup) self.assertEquals(sitegroup.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'sitegroups') @responses.activate def test_retrieve_one_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = {'name': 'Test SiteGroup', 'id': 10} responses.add(responses.GET, 'http://localhost/sitegroups/10', json=resp, status=200) e = SiteGroups.retrieve_one(self.token, sitegroup_id=sitegroup.id) self.assertIsInstance(e, SiteGroup) self.assertEquals(sitegroup.name, e.name) self.assertEquals(10, e.id) e = SiteGroups.retrieve_one(self.token, sitegroup_id=sitegroup) self.assertIsInstance(e, SiteGroup) self.assertEquals(sitegroup.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'sitegroups') @responses.activate def test_retrieve_all_sitegroups(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = [{'name': 'Test SiteGroup', 'id': 10}] responses.add(responses.GET, 'http://localhost/sitegroups', json=resp, status=200) el = SiteGroups.retrieve_all(self.token) self.assertIsInstance(el, list) e = el[0] self.assertEquals(sitegroup.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'sitegroups') @responses.activate def test_update_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = {'name': 'Test SiteGroup', 'id': 10} responses.add(responses.PUT, 'http://localhost/sitegroups/10', json=resp, status=200) e = SiteGroups.update(self.token, sitegroup=sitegroup) self.assertIsInstance(e, SiteGroup) self.assertEquals(sitegroup.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'sitegroups') @responses.activate def test_delete_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = {'name': 'Test SiteGroup', 'id': 10} responses.add(responses.DELETE, 'http://localhost/sitegroups/10', body='', status=204) r = SiteGroups.delete(self.token, sitegroup_id=sitegroup) self.assertEquals(204, r.status_code) r = SiteGroups.delete(self.token, sitegroup_id=sitegroup.id) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_assign_subject_to_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) subject = Subject(username='******', id=100) resp = {'name': 'Test SiteGroup', 'id': 10} responses.add( responses.POST, 'http://localhost/sitegroups/10/subjects/rel/100?notifyEmail=True¬ifySms=False', body='', status=204, match_querystring=True) notification_setting = SiteGroupNotificationSetting(notify_email=True) r = SiteGroups.assign_subject_to_sitegroup( self.token, sitegroup_id=sitegroup, subject_id=subject, notification_setting=notification_setting) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_get_all_subject_ids_of_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = [ 100, ] responses.add(responses.GET, 'http://localhost/sitegroups/10/subjects/rel', json=resp, status=204, match_querystring=True) r = SiteGroups.get_all_subject_ids_of_sitegroup(self.token, sitegroup_id=sitegroup) self.assertListEqual([ 100, ], r) @attr('unit', 'sitegroups') @responses.activate def test_get_all_subject_notification_settings_of_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = {'settings': {'100': {'notifyEmail': False, 'notifySms': True}}} responses.add( responses.GET, 'http://localhost/sitegroups/10/subjects/rel/notifications', json=resp, status=204, match_querystring=True) r = SiteGroups.get_all_subject_notification_settings_of_sitegroup( self.token, sitegroup_id=sitegroup) self.assertIsInstance(r, SiteGroupNotificationResponse) nsm = r.settings self.assertIsInstance(nsm, dict) ns = nsm[100] self.assertIsInstance(ns, SiteGroupNotificationSetting) self.assertTrue(ns.notify_sms) self.assertFalse(ns.notify_email) @attr('unit', 'sitegroups') @responses.activate def test_update_subject_notification_settings(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) subject = Subject(username='******', id=100) responses.add( responses.PUT, 'http://localhost/sitegroups/10/subjects/rel/100?notifyEmail=True¬ifySms=False', body='', status=204, match_querystring=True) notification_setting = SiteGroupNotificationSetting(notify_email=True) r = SiteGroups.update_subject_notification_settings( self.token, sitegroup_id=sitegroup, subject_id=subject, notification_setting=notification_setting) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_remove_subject_from_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) subject = Subject(username='******', id=100) responses.add(responses.DELETE, 'http://localhost/sitegroups/10/subjects/rel/100', body='', status=204, match_querystring=True) r = SiteGroups.remove_subject_from_sitegroup(self.token, sitegroup_id=sitegroup, subject_id=subject) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_get_site_ids_in_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = [ 100, ] responses.add(responses.GET, 'http://localhost/sitegroups/10/sites/rel', json=resp, status=204, match_querystring=True) r = SiteGroups.get_site_ids_in_sitegroup(self.token, sitegroup_id=sitegroup) self.assertListEqual([ 100, ], r) @attr('unit', 'sitegroups') @responses.activate def test_assign_site_to_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) site = Site(username='******', id=100) responses.add(responses.PUT, 'http://localhost/sitegroups/10/sites/rel/100', body='', status=204, match_querystring=True) r = SiteGroups.assign_site_to_sitegroup(self.token, sitegroup_id=sitegroup, site_id=site) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_remove_site_from_sitegroup(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) site = Site(username='******', id=100) responses.add(responses.DELETE, 'http://localhost/sitegroups/10/sites/rel/100', body='', status=204, match_querystring=True) r = SiteGroups.remove_site_from_sitegroup(self.token, sitegroup_id=sitegroup, site_id=site) self.assertEquals(204, r.status_code) @attr('unit', 'sitegroups') @responses.activate def test_get_sitegroup_view_config(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) resp = { 'summaries': [{ 'aggregationType': 'LAST', 'assetType': 'TANK', 'label': 'Test', 'pointType': 'TOP_GAUGE', 'unit': 'feet' }] } responses.add(responses.GET, 'http://localhost/sitegroups/10/meta/viewconfig', json=resp, status=204, match_querystring=True) r = SiteGroups.get_sitegroup_view_config(self.token, sitegroup_id=sitegroup) self.assertIsInstance(r, SiteGroupViewConfig) summaries = r.summaries self.assertEquals(1, len(summaries)) summary = summaries[0] self.assertIsInstance(summary, SiteGroupSummary) self.assertEquals('TANK', summary.asset_type) self.assertEquals('LAST', summary.aggregation_type) self.assertEquals('Test', summary.label) self.assertEquals('TOP_GAUGE', summary.point_type) self.assertEquals('feet', summary.unit) @attr('unit', 'sitegroups') @responses.activate def test_update_sitegroup_view_config(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) summary = SiteGroupSummary(aggregation_type='LAST', asset_type='TANK', label='Test', point_type='TOP_GAUGE', unit='feet') view_config = SiteGroupViewConfig(summaries=[ summary, ]) resp = { 'summaries': [{ 'aggregationType': 'LAST', 'assetType': 'TANK', 'label': 'Test', 'pointType': 'TOP_GAUGE', 'unit': 'feet' }] } responses.add(responses.PUT, 'http://localhost/sitegroups/10/meta/viewconfig', json=resp, status=204, match_querystring=True) r = SiteGroups.update_sitegroup_view_config(self.token, sitegroup_id=sitegroup, view_config=view_config) self.assertIsInstance(r, SiteGroupViewConfig) summaries = r.summaries self.assertEquals(1, len(summaries)) summary = summaries[0] self.assertIsInstance(summary, SiteGroupSummary) self.assertEquals('TANK', summary.asset_type) self.assertEquals('LAST', summary.aggregation_type) self.assertEquals('Test', summary.label) self.assertEquals('TOP_GAUGE', summary.point_type) self.assertEquals('feet', summary.unit) @attr('unit', 'sitegroups') @responses.activate def test_delete_sitegroup_view_config(self): sitegroup = SiteGroup(name='Test SiteGroup', id=10) responses.add(responses.DELETE, 'http://localhost/sitegroups/10/meta/viewconfig', body='', status=204, match_querystring=True) r = SiteGroups.delete_sitegroup_view_config(self.token, sitegroup_id=sitegroup) self.assertEquals(204, r.status_code)
class BaseEntityTest(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'base_entity') def test_dict_keys(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') keys = asset.keys() self.assertEquals(4, len(keys)) self.assertIn('id', keys) self.assertIn('name', keys) self.assertIn('timezone', keys) self.assertIn('asset_type', keys) @attr('unit', 'base_entity') def test_dict_values(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') values = asset.values() self.assertEquals(4, len(values)) self.assertIn(10, values) self.assertIn('Test Asset', values) self.assertIn('America/Chicago', values) self.assertIn('TANK', values) @attr('unit', 'base_entity') def test_dict_items(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') items = asset.items() self.assertEquals(4, len(items)) self.assertIn(('id', 10), items) self.assertIn(('name', 'Test Asset'), items) self.assertIn(('timezone', 'America/Chicago'), items) self.assertIn(('asset_type', 'TANK'), items) @attr('unit', 'base_entity') def test_equality(self): asset1 = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') asset2 = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') self.assertEquals(asset1, asset2) asset3 = Asset(id=11, name='Test Asset', timezone='America/Chicago', asset_type='TANK') self.assertNotEqual(asset1, asset3) @attr('unit', 'base_entity') def test_string_representation(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') self.assertEquals( "Asset(asset_type=TANK, id=10, name=Test Asset, timezone=America/Chicago)", str(asset)) @attr('unit', 'base_entity') def test_json_representation(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') self.assertEquals( '{"id": 10, "timezone": "America/Chicago", "name": "Test Asset", "assetType": "TANK"}', asset.to_json()) @attr('unit', 'base_entity') def test_from_json_representation(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') asset_decoded = Asset.from_json( '{"id": 10, "timezone": "America/Chicago", "name": "Test Asset", "assetType": "TANK"}' ) self.assertEquals(asset, asset_decoded) @attr('unit', 'base_entity') def test_from_dict_representation(self): asset = Asset(id=10, name='Test Asset', timezone='America/Chicago', asset_type='TANK') asset_decoded = Asset.from_dict({ "id": 10, "timezone": "America/Chicago", "name": "Test Asset", "assetType": "TANK" }) self.assertEquals(asset, asset_decoded)
class RoleTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'roles') @responses.activate def test_create_role(self): role = Role(name='Test Role') resp = {'name': 'Test Role', 'id': 10} responses.add(responses.POST, 'http://localhost/roles', json=resp, status=200) e = Roles.create(self.token, role=role) self.assertIsInstance(e, Role) self.assertEquals(role.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'roles') @responses.activate def test_retrieve_one_role(self): role = Role(name='Test Role', id=10) resp = {'name': 'Test Role', 'id': 10} responses.add(responses.GET, 'http://localhost/roles/10', json=resp, status=200) e = Roles.retrieve_one(self.token, role_id=role.id) self.assertIsInstance(e, Role) self.assertEquals(role.name, e.name) self.assertEquals(10, e.id) e = Roles.retrieve_one(self.token, role_id=role) self.assertIsInstance(e, Role) self.assertEquals(role.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'roles') @responses.activate def test_retrieve_all_roles(self): role = Role(name='Test Role', id=10) resp = [{'name': 'Test Role', 'id': 10}] responses.add(responses.GET, 'http://localhost/roles', json=resp, status=200) el = Roles.retrieve_all(self.token) self.assertIsInstance(el, list) e = el[0] self.assertEquals(role.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'roles') @responses.activate def test_update_role(self): role = Role(name='Test Role', id=10) resp = {'name': 'Test Role', 'id': 10} responses.add(responses.PUT, 'http://localhost/roles/10', json=resp, status=200) e = Roles.update(self.token, role=role) self.assertIsInstance(e, Role) self.assertEquals(role.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'roles') @responses.activate def test_delete_role(self): role = Role(name='Test Role', id=10) resp = {'name': 'Test Role', 'id': 10} responses.add(responses.DELETE, 'http://localhost/roles/10', body='', status=204) r = Roles.delete(self.token, role_id=role) self.assertEquals(204, r.status_code) r = Roles.delete(self.token, role_id=role.id) self.assertEquals(204, r.status_code)
class TenantTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'tenants') @responses.activate def test_create_tenant(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=12345) resp = {'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 12345, 'id': 10} responses.add(responses.POST, 'http://localhost/tenants', json=resp, status=200) t = Tenants.create(self.token, tenant=tenant) self.assertIsInstance(t, Tenant) self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) @attr('unit', 'tenants') @responses.activate def test_retrieve_one_tenant(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=56789, id=10) resp = {'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 56789, 'id': 10} responses.add(responses.GET, 'http://localhost/tenants/10', json=resp, status=200) t = Tenants.retrieve_one(self.token, tenant_id=tenant.id) self.assertIsInstance(t, Tenant) self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) t = Tenants.retrieve_one(self.token, tenant_id=tenant) self.assertIsInstance(t, Tenant) self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) @attr('unit', 'tenants') @responses.activate def test_retrieve_all_tenants(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=12345) resp = [{'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 12345, 'id': 10}] responses.add(responses.GET, 'http://localhost/tenants', json=resp, status=200) tl = Tenants.retrieve_all(self.token) self.assertIsInstance(tl, list) t = tl[0] self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) @attr('unit', 'tenants') @responses.activate def test_retrieve_my_tenant(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=56789, id=10) resp = {'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 56789, 'id': 10} responses.add(responses.GET, 'http://localhost/tenants/me', json=resp, status=200) t = Tenants.me(self.token) self.assertIsInstance(t, Tenant) self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) @attr('unit', 'tenants') @responses.activate def test_update_tenant(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=56789, id=10) resp = {'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 56789, 'id': 10} responses.add(responses.PUT, 'http://localhost/tenants/10', json=resp, status=200) t = Tenants.update(self.token, tenant=tenant) self.assertIsInstance(t, Tenant) self.assertEquals(tenant.name, t.name) self.assertEquals(tenant.timezone, t.timezone) self.assertEquals(tenant.effective_timestamp, t.effective_timestamp) self.assertEquals(10, t.id) @attr('unit', 'tenants') @responses.activate def test_delete_tenant(self): tenant = Tenant(name='Test Tenant', timezone='America/Chicago', effective_timestamp=56789, id=10) resp = {'name': 'Test Tenant', 'timezone': 'America/Chicago', 'effectiveTimestamp': 56789, 'id': 10} responses.add(responses.DELETE, 'http://localhost/tenants/10', body='', status=204) r = Tenants.delete(self.token, tenant_id=tenant) self.assertEquals(204, r.status_code) r = Tenants.delete(self.token, tenant_id=tenant.id) self.assertEquals(204, r.status_code)
class SubjectTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'subjects') @responses.activate def test_create_subject(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******') resp = { 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' } responses.add(responses.POST, 'http://localhost/subjects', json=resp, status=200) s = Subjects.create(self.token, subject=subject) self.assertIsInstance(s, Subject) self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) @attr('unit', 'subjects') @responses.activate def test_retrieve_one_subject(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******', id=10) resp = { 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' } responses.add(responses.GET, 'http://localhost/subjects/10', json=resp, status=200) s = Subjects.retrieve_one(self.token, subject_id=subject.id) self.assertIsInstance(s, Subject) self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) s = Subjects.retrieve_one(self.token, subject_id=subject) self.assertIsInstance(s, Subject) self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) @attr('unit', 'subjects') @responses.activate def test_retrieve_subject_me(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******', id=10) resp = { 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' } responses.add(responses.GET, 'http://localhost/subjects/me', json=resp, status=200) s = Subjects.me(self.token) self.assertIsInstance(s, Subject) self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) @attr('unit', 'subjects') @responses.activate def test_retrieve_all_subjects(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******', id=10) resp = [{ 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' }] responses.add(responses.GET, 'http://localhost/subjects', json=resp, status=200) sl = Subjects.retrieve_all(self.token) self.assertIsInstance(sl, list) s = sl[0] self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) @attr('unit', 'subjects') @responses.activate def test_update_subject(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******', id=10) resp = { 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' } responses.add(responses.PUT, 'http://localhost/subjects/10', json=resp, status=200) s = Subjects.update(self.token, subject=subject) self.assertIsInstance(s, Subject) self.assertEquals(subject.username, s.username) self.assertEquals(subject.given_name, s.given_name) self.assertEquals(subject.family_name, s.family_name) self.assertEquals(subject.email, s.email) self.assertEquals(subject.phone, s.phone) self.assertIsNone(s.password) self.assertEquals(10, s.id) @attr('unit', 'subjects') @responses.activate def test_delete_subject(self): subject = Subject(username='******', given_name='John', family_name='Doe', email='*****@*****.**', phone='+15555555555', password='******', id=10) resp = { 'username': '******', 'firstName': 'John', 'lastName': 'Doe', 'id': 10, 'email': '*****@*****.**', 'phone': '+15555555555' } responses.add(responses.DELETE, 'http://localhost/subjects/10', body='', status=204) r = Subjects.delete(self.token, subject_id=subject) self.assertEquals(204, r.status_code) r = Subjects.delete(self.token, subject_id=subject.id) self.assertEquals(204, r.status_code)
class ControlRuleTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'controlrules') @responses.activate def test_create_controlrule(self): rule = ControlRule(name='Test ControlRule') resp = {'name': 'Test ControlRule', 'id': 10} responses.add( responses.POST, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/rules', json=resp, status=200) e = ControlRules.create(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule=rule) self.assertIsInstance(e, ControlRule) self.assertEquals(rule.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'controlrules') @responses.activate def test_retrieve_one_controlrule(self): rule = ControlRule(name='Test ControlRule', id=10) resp = {'name': 'Test ControlRule', 'id': 10} responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/rules/10', json=resp, status=200) e = ControlRules.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule_id=rule) self.assertIsInstance(e, ControlRule) self.assertEquals(rule.name, e.name) self.assertEquals(10, e.id) e = ControlRules.retrieve_one(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule_id=rule.id) self.assertIsInstance(e, ControlRule) self.assertEquals(rule.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'controlrules') @responses.activate def test_retrieve_all_controlrules(self): rule = ControlRule(name='Test ControlRule', id=10) resp = [{'name': 'Test ControlRule', 'id': 10}] responses.add( responses.GET, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/rules', json=resp, status=200) el = ControlRules.retrieve_all(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4) self.assertIsInstance(el, list) e = el[0] self.assertEquals(rule.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'controlrules') @responses.activate def test_update_controlrule(self): rule = ControlRule(name='Test ControlRule', id=10) resp = {'name': 'Test ControlRule', 'id': 10} responses.add( responses.PUT, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/rules/10', json=resp, status=200) e = ControlRules.update(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule=rule) self.assertIsInstance(e, ControlRule) self.assertEquals(rule.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'controlrules') @responses.activate def test_delete_controlrule(self): rule = ControlRule(name='Test ControlRule', id=10) resp = {'name': 'Test ControlRule', 'id': 10} responses.add( responses.DELETE, 'http://localhost/sites/1/assets/2/points/3/controlpoints/4/rules/10', body='', status=204) r = ControlRules.delete(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule_id=rule) self.assertEquals(204, r.status_code) r = ControlRules.delete(self.token, site_id=1, asset_id=2, point_id=3, control_point_id=4, control_rule_id=rule.id) self.assertEquals(204, r.status_code)
class UnitTests(BaseClientTestCase): token = Token(WA_ADMIN_TOKEN) @attr('unit', 'units') def test_unit_serialization(self): unit = Unit(name='feet', abbreviation="ft", id=10) jdata = unit.get_json_data() self.assertDictContainsKeyWithValue(jdata, 'name', 'feet') self.assertDictContainsKeyWithValue(jdata, 'abbreviation', 'ft') self.assertDictContainsKeyWithValue(jdata, 'id', 10) @attr('unit', 'units') def test_unit_deserialization(self): jdata = {'name': 'feet', 'abbreviation': 'ft', 'id': 10} unit = Unit.from_dict(jdata) self.assertEquals('feet', unit.name) self.assertEquals('ft', unit.abbreviation) self.assertEquals(10, unit.id) @attr('unit', 'units') @responses.activate def test_create_unit(self): unit = Unit(name='Test Unit') resp = {'name': 'Test Unit', 'id': 10} responses.add(responses.POST, 'http://localhost/units', json=resp, status=200) e = Units.create(self.token, unit=unit) self.assertIsInstance(e, Unit) self.assertEquals(unit.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'units') @responses.activate def test_retrieve_one_unit(self): unit = Unit(name='Test Unit', id=10) resp = {'name': 'Test Unit', 'id': 10} responses.add(responses.GET, 'http://localhost/units/10', json=resp, status=200) e = Units.retrieve_one(self.token, unit_id=unit.id) self.assertIsInstance(e, Unit) self.assertEquals(unit.name, e.name) self.assertEquals(10, e.id) e = Units.retrieve_one(self.token, unit_id=unit) self.assertIsInstance(e, Unit) self.assertEquals(unit.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'units') @responses.activate def test_retrieve_all_units(self): unit = Unit(name='Test Unit', id=10) resp = [{'name': 'Test Unit', 'id': 10}] responses.add(responses.GET, 'http://localhost/units', json=resp, status=200) el = Units.retrieve_all(self.token) self.assertIsInstance(el, list) e = el[0] self.assertEquals(unit.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'units') @responses.activate def test_update_unit(self): unit = Unit(name='Test Unit', id=10) resp = {'name': 'Test Unit', 'id': 10} responses.add(responses.PUT, 'http://localhost/units/10', json=resp, status=200) e = Units.update(self.token, unit=unit) self.assertIsInstance(e, Unit) self.assertEquals(unit.name, e.name) self.assertEquals(10, e.id) @attr('unit', 'units') @responses.activate def test_delete_unit(self): unit = Unit(name='Test Unit', id=10) resp = {'name': 'Test Unit', 'id': 10} responses.add(responses.DELETE, 'http://localhost/units/10', body='', status=204) r = Units.delete(self.token, unit_id=unit) self.assertEquals(204, r.status_code) r = Units.delete(self.token, unit_id=unit.id) self.assertEquals(204, r.status_code)