def test_DecodeFromJson(self): """ Check decode from JSON format """ # Unit is only ever part of another structure, never alone. # No need to explicitly test if Unit can be decoded from JSON # Location is only ever part of another structure, never alone. # No need to explicitly test if Unit can be decoded from JSON datapoint = txpachube.Datapoint() datapoint.decode(TEST_DATAPOINT_JSON, format=txpachube.DataFormats.JSON) datastream = txpachube.Datastream() datastream.decode(TEST_DATASTREAM_JSON, format=txpachube.DataFormats.JSON) environment = txpachube.Environment() environment.decode(TEST_FEED_JSON, format=txpachube.DataFormats.JSON) environment_list = txpachube.EnvironmentList() environment_list.decode(TEST_FEEDS_LIST_JSON, format=txpachube.DataFormats.JSON) trigger = txpachube.Trigger() trigger.decode(TEST_TRIGGER_JSON, format=txpachube.DataFormats.JSON) trigger_list = txpachube.TriggerList() trigger_list.decode(TEST_TRIGGERS_LIST_JSON, format=txpachube.DataFormats.JSON) key = txpachube.Key() key.decode(TEST_API_KEY_JSON, format=txpachube.DataFormats.JSON) key_list = txpachube.KeyList() key_list.decode(TEST_API_KEYS_LIST_JSON, format=txpachube.DataFormats.JSON) user = txpachube.User() user.decode(TEST_USER_JSON, format=txpachube.DataFormats.JSON) user_list = txpachube.UserList() user_list.decode(TEST_USERS_LIST_JSON, format=txpachube.DataFormats.JSON)
def test_EncodeToXml(self): """ Check encode to XML format """ # This test performs a crude check to ensure the XML output # is valid XML. Eventually it should be expanded to confirm # the output content matches the values contained in local # attributes. unit_kwargs = { txpachube.DataFields.Label: 'Celcius', txpachube.DataFields.Type: txpachube.Unit.Basic_Si, txpachube.DataFields.Symbol: "C" } unit = txpachube.Unit(**unit_kwargs) unit_xml = unit.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(unit_xml) location_kwargs = { txpachube.DataFields.Disposition: txpachube.Location.Fixed, txpachube.DataFields.Domain: txpachube.Location.Physical, txpachube.DataFields.Elevation: "40", txpachube.DataFields.Exposure: txpachube.Location.Indoor, txpachube.DataFields.Latitude: 51.5235375648154, txpachube.DataFields.Longitude: -0.0807666778564453, txpachube.DataFields.Name: 'temp' } location = txpachube.Location(**location_kwargs) location_xml = location.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(location_xml) datapoint_kwargs = { txpachube.DataFields.At: "2010-04-12T11:31:51.133782Z", txpachube.DataFields.Value: "999" } datapoint = txpachube.Datapoint(**datapoint_kwargs) datapoint_xml = datapoint.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(datapoint_xml) datastream_kwargs = { txpachube.DataFields.At: "2010-04-12T11:31:52.133782Z", txpachube.DataFields.Current_Value: "27.0", txpachube.DataFields.Datapoints: [datapoint_kwargs], txpachube.DataFields.Id: 7021, txpachube.DataFields.Maximum_Value: "35.8", txpachube.DataFields.Minimum_Value: "15.9", txpachube.DataFields.Tags: ['temp', 'Temperature', 'C'], txpachube.DataFields.Unit: unit_kwargs, txpachube.DataFields.Updated: "2010-04-12T11:31:51.133782Z" } datastream = txpachube.Datastream(**datastream_kwargs) datastream_xml = datastream.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(datastream_xml) env_inDict = json.loads(TEST_FEED_JSON) environment = txpachube.Environment(**env_inDict) environment_xml = environment.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(environment_xml) envList_inDict = json.loads(TEST_FEEDS_LIST_JSON) environment_list = txpachube.EnvironmentList(**envList_inDict) environment_list_xml = environment_list.encode( txpachube.DataFormats.XML) valid_xml = etree.fromstring(environment_list_xml) trigger_inDict = json.loads(TEST_TRIGGER_JSON) trigger = txpachube.Trigger(**trigger_inDict) trigger_xml = trigger.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(trigger_xml) trigger_list_inDict = { txpachube.DataFields.Datastream_Trigger: json.loads(TEST_TRIGGERS_LIST_JSON) } trigger_list = txpachube.TriggerList(**trigger_list_inDict) trigger_list_xml = trigger_list.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(trigger_list_xml) key_inDict = json.loads(TEST_API_KEY_JSON) key = txpachube.Key(**key_inDict) key_xml = key.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(key_xml) key_list_inDict = json.loads(TEST_API_KEYS_LIST_JSON) key_list = txpachube.KeyList(**key_list_inDict) key_list_xml = key_list.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(key_list_xml) user_inDict = json.loads(TEST_USER_JSON) user = txpachube.User(**user_inDict) user_xml = user.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(user_xml) user_list_inDict = { txpachube.DataFields.Users: json.loads(TEST_USERS_LIST_JSON) } user_list = txpachube.UserList(**user_list_inDict) user_list_xml = user_list.encode(txpachube.DataFormats.XML) valid_xml = etree.fromstring(user_list_xml)
def test_Initialisation(self): """ Check data structure initialisation """ # test no arguments unit = txpachube.Unit() location = txpachube.Location() datapoint = txpachube.Datapoint() datastream = txpachube.Datastream() environment = txpachube.Environment() environment_list = txpachube.EnvironmentList() trigger = txpachube.Trigger() trigger_list = txpachube.TriggerList() key = txpachube.Key() key_list = txpachube.KeyList() user = txpachube.User() user_list = txpachube.UserList() # test expected keyword arguments unit_kwargs = { txpachube.DataFields.Label: 'Celcius', txpachube.DataFields.Type: txpachube.Unit.Basic_Si, txpachube.DataFields.Symbol: "C" } unit = txpachube.Unit(**unit_kwargs) self.assertEqual(unit.label, unit_kwargs[txpachube.DataFields.Label], "Unit label mismatch") self.assertEqual(unit.type, unit_kwargs[txpachube.DataFields.Type], "Unit type mismatch") self.assertEqual(unit.symbol, unit_kwargs[txpachube.DataFields.Symbol], "Unit symbol mismatch") location_kwargs = { txpachube.DataFields.Disposition: txpachube.Location.Fixed, txpachube.DataFields.Domain: txpachube.Location.Physical, txpachube.DataFields.Elevation: "40", txpachube.DataFields.Exposure: txpachube.Location.Indoor, txpachube.DataFields.Latitude: 51.5235375648154, txpachube.DataFields.Longitude: -0.0807666778564453, txpachube.DataFields.Name: 'temp' } location = txpachube.Location(**location_kwargs) self.assertEqual(location.disposition, location_kwargs[txpachube.DataFields.Disposition], "Location disposition mismatch") self.assertEqual(location.domain, location_kwargs[txpachube.DataFields.Domain], "Location domain mismatch") self.assertEqual(location.exposure, location_kwargs[txpachube.DataFields.Exposure], "Location exposure mismatch") self.assertEqual(location.ele, location_kwargs[txpachube.DataFields.Elevation], "Location elevation mismatch") self.assertEqual(location.lat, location_kwargs[txpachube.DataFields.Latitude], "Location latitude mismatch") self.assertEqual(location.lon, location_kwargs[txpachube.DataFields.Longitude], "Location longitude mismatch") self.assertEqual(location.name, location_kwargs[txpachube.DataFields.Name], "Location name mismatch") datapoint_kwargs = { txpachube.DataFields.At: "2010-04-12T11:31:51.133782Z", txpachube.DataFields.Value: "999" } datapoint = txpachube.Datapoint(**datapoint_kwargs) self.assertEqual(datapoint.at, datapoint_kwargs[txpachube.DataFields.At], "Datapoint at mismatch") self.assertEqual(datapoint.value, datapoint_kwargs[txpachube.DataFields.Value], "Datapoint value mismatch") datastream_kwargs = { txpachube.DataFields.At: "2010-04-12T11:31:52.133782Z", txpachube.DataFields.Current_Value: "27.0", txpachube.DataFields.Datapoints: [datapoint_kwargs], txpachube.DataFields.Id: 7021, txpachube.DataFields.Maximum_Value: "35.8", txpachube.DataFields.Minimum_Value: "15.9", txpachube.DataFields.Tags: ['temp', 'Temperature', 'C'], txpachube.DataFields.Unit: unit_kwargs, txpachube.DataFields.Updated: "2010-04-12T11:31:51.133782Z" } datastream = txpachube.Datastream(**datastream_kwargs) self.assertEqual(datastream.at, datastream_kwargs[txpachube.DataFields.At], "Datastream at mismatch") self.assertEqual(datastream.current_value, datastream_kwargs[txpachube.DataFields.Current_Value], "Datastream current value mismatch") self.assertEqual( len(datastream.datapoints), len(datastream_kwargs[txpachube.DataFields.Datapoints]), "Datastream datapoints count mismatch") self.assertEqual( datastream.datapoints[0].at, datastream_kwargs[ txpachube.DataFields.Datapoints][0][txpachube.DataFields.At], "Datastream datapoints at mismatch") self.assertEqual( datastream.datapoints[0].value, datastream_kwargs[txpachube.DataFields.Datapoints][0][ txpachube.DataFields.Value], "Datastream datapoints value mismatch") self.assertEqual(datastream.id, datastream_kwargs[txpachube.DataFields.Id], "Datastream id mismatch") self.assertEqual(datastream.max_value, datastream_kwargs[txpachube.DataFields.Maximum_Value], "Datastream maimum value mismatch") self.assertEqual(datastream.min_value, datastream_kwargs[txpachube.DataFields.Minimum_Value], "Datastream minimum value mismatch") self.assertEqual( datastream.unit.label, datastream_kwargs[txpachube.DataFields.Unit] [txpachube.DataFields.Label], "Datastream Unit label mismatch") self.assertEqual( datastream.unit.type, datastream_kwargs[txpachube.DataFields.Unit][ txpachube.DataFields.Type], "Datastream Unit type mismatch") self.assertEqual( datastream.unit.symbol, datastream_kwargs[ txpachube.DataFields.Unit][txpachube.DataFields.Symbol], "Datastream Unit symbol mismatch") self.assertEqual(datastream.updated, datastream_kwargs[txpachube.DataFields.Updated], "Datastream updated mismatch") env_inDict = json.loads(TEST_FEED_JSON) environment = txpachube.Environment(**env_inDict) self.assertEqual(environment.creator, env_inDict[txpachube.DataFields.Creator], "Environment creator mismatch") self.assertEqual(len(environment.datastreams), len(env_inDict[txpachube.DataFields.Datastreams]), "Environment datastreams count mismatch") datastream_01 = env_inDict[txpachube.DataFields.Datastreams][0] datastream_01_key = datastream_01[txpachube.DataFields.Id] environment_datastream_01 = environment.datastreams[datastream_01_key] self.assertEqual(environment_datastream_01.at, datastream_01[txpachube.DataFields.At], "Environment datastreams[0].at mismatch") self.assertEqual(environment_datastream_01.current_value, datastream_01[txpachube.DataFields.Current_Value], "Environment datastreams[0].value mismatch") self.assertEqual(environment_datastream_01.id, datastream_01[txpachube.DataFields.Id], "Environment datastreams[0].id mismatch") self.assertEqual(environment_datastream_01.max_value, datastream_01[txpachube.DataFields.Maximum_Value], "Environment datastreams[0].max_value mismatch") self.assertEqual(environment_datastream_01.min_value, datastream_01[txpachube.DataFields.Minimum_Value], "Environment datastreams[0].min_value mismatch") datastream_02 = env_inDict[txpachube.DataFields.Datastreams][1] datastream_02_key = datastream_02[txpachube.DataFields.Id] environment_datastream_02 = environment.datastreams[datastream_02_key] self.assertEqual(environment_datastream_02.at, datastream_02[txpachube.DataFields.At], "Environment datastreams[1].at mismatch") self.assertEqual(environment_datastream_02.current_value, datastream_02[txpachube.DataFields.Current_Value], "Environment datastreams[1].value mismatch") self.assertEqual(environment_datastream_02.id, datastream_02[txpachube.DataFields.Id], "Environment datastreams[1].id mismatch") self.assertEqual(environment_datastream_02.max_value, datastream_02[txpachube.DataFields.Maximum_Value], "Environment datastreams[1].max_value mismatch") self.assertEqual(environment_datastream_02.min_value, datastream_02[txpachube.DataFields.Minimum_Value], "Environment datastreams[1].min_value mismatch") self.assertEqual(environment.description, env_inDict[txpachube.DataFields.Description], "Environment description mismatch") self.assertEqual(environment.feed, env_inDict[txpachube.DataFields.Feed], "Environment feed mismatch") self.assertEqual(environment.icon, env_inDict[txpachube.DataFields.Icon], "Environment icon mismatch") self.assertEqual(environment.id, env_inDict[txpachube.DataFields.Id], "Environment id mismatch") self.assertEqual( environment.location.disposition, env_inDict[txpachube.DataFields.Location][ txpachube.DataFields.Disposition], "Environment Location disposition mismatch") self.assertEqual( environment.location.domain, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Domain], "Environment Location domain mismatch") self.assertEqual( environment.location.exposure, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Exposure], "Environment Location exposure mismatch") self.assertEqual( environment.location.ele, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Elevation], "Environment Location elevation mismatch") self.assertEqual( environment.location.lat, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Latitude], "Environment Location latitude mismatch") self.assertEqual( environment.location.lon, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Longitude], "Environment Location longitude mismatch") self.assertEqual( environment.location.name, env_inDict[ txpachube.DataFields.Location][txpachube.DataFields.Name], "Environment Location name mismatch") self.assertEqual(environment.private, env_inDict[txpachube.DataFields.Private], "Environment private mismatch") self.assertEqual(environment.status, env_inDict[txpachube.DataFields.Status], "Environment status mismatch") self.assertEqual(environment.tags, env_inDict[txpachube.DataFields.Tags], "Environment tags mismatch") self.assertEqual(environment.title, env_inDict[txpachube.DataFields.Title], "Environment title mismatch") self.assertEqual(environment.updated, env_inDict[txpachube.DataFields.Updated], "Environment updated mismatch") self.assertEqual(environment.version, env_inDict[txpachube.DataFields.Version], "Environment version mismatch") self.assertEqual(environment.website, env_inDict[txpachube.DataFields.Website], "Environment website mismatch") envList_inDict = json.loads(TEST_FEEDS_LIST_JSON) environment_list = txpachube.EnvironmentList(**envList_inDict) trigger_inDict = json.loads(TEST_TRIGGER_JSON) trigger = txpachube.Trigger(**trigger_inDict) trigger_list_inDict = { txpachube.DataFields.Datastream_Trigger: json.loads(TEST_TRIGGERS_LIST_JSON) } trigger_list = txpachube.TriggerList(**trigger_list_inDict) key_inDict = json.loads(TEST_API_KEY_JSON) key = txpachube.Key(**key_inDict) key_list_inDict = json.loads(TEST_API_KEYS_LIST_JSON) key_list = txpachube.KeyList(**key_list_inDict) user_inDict = json.loads(TEST_USER_JSON) user = txpachube.User(**user_inDict) user_list_inDict = { txpachube.DataFields.Users: json.loads(TEST_USERS_LIST_JSON) } user_list = txpachube.UserList(**user_list_inDict)
trigger_list) else: logging.error("Problem occurred listing triggers") except Exception, ex: logging.error("Error retrieving trigger list: %s" % str(ex)) original_value = "50" updated_value = "45" new_trigger_id = None # create a new trigger try: # feed id appears to want a int rather than a string. trigger = txpachube.Trigger( environment_id=new_feed_id, stream_id=new_datastream_name, threshold_value=original_value, trigger_type="gt", url="http://www.postbin.org/1ijyltn") logging.info("Creating a new trigger with content:\n%s\n" % trigger) new_trigger_id = yield client.create_trigger( api_key=user_api_key, data=trigger.encode()) if new_trigger_id: logging.info( "Success creating a new trigger. New trigger id is: %s" % new_trigger_id) else: logging.error("Problem occurred creating new trigger") except Exception, ex: logging.error("Error creating a trigger: %s" % str(ex))