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())
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)
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)
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)
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)
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)
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)
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))
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)
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)
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)
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)
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)