class GarminTCXv2PluginTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) main = mock.Mock() main.ddbb = self.ddbb main.startup_options = mock.Mock() main.profile = mock.Mock() main.profile.plugindir = 'plugins' plugins = Plugins(parent=main) self.plugin = plugins.importClass('plugins/garmin-tcxv2') tree = etree.parse('pytrainer/test/imports/sample.tcx') self.activity = self.plugin.getActivities(tree)[0] def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_not_inDatabase(self): self.assertFalse(self.plugin.inDatabase(self.activity)) def test_inDatabase(self): activity = Activity(date_time_utc='2012-10-14T10:02:42.000Z', sport_id='1') self.ddbb.session.add(activity) self.ddbb.session.commit() self.assertTrue(self.plugin.inDatabase(self.activity)) def test_detailsFromTCX(self): self.assertEqual(self.plugin.detailsFromTCX(self.activity), '2012-10-14T10:02:42.000Z')
class GarminFitTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.parent = mock.Mock() self.parent.parent = mock.Mock() self.parent.parent.ddbb = self.ddbb def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_parse_fit_file(self): try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname( os.path.dirname(os.path.dirname(current_path))) + "/" fit_file = current_path + "/sample.fit" garmin_fit = garminfit(self.parent, data_path) xmldoc = etree.fromstring(garmin_fit.fromFIT2TCXv2(fit_file)) valid_xml = garmin_fit.validate( xmldoc, "schemas/GarminTrainingCenterDatabase_v2.xsd") self.assertTrue(valid_xml) except (): self.fail()
class GarminFitTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.parent = mock.Mock() self.parent.parent = mock.Mock() self.parent.parent.ddbb = self.ddbb def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_parse_fit_file(self): try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname(os.path.dirname(os.path.dirname(current_path))) + "/" fit_file = current_path + "/sample.fit" garmin_fit = garminfit(self.parent, data_path) xmldoc = etree.fromstring(garmin_fit.fromFIT2TCXv2(fit_file)) valid_xml = garmin_fit.validate(xmldoc, "schemas/GarminTrainingCenterDatabase_v2.xsd") self.assertTrue(valid_xml) except(): self.fail()
class EquipmentUiTest(TestCase): def setUp(self): initialize_gettext('locale/') self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables() self.equipment_service = EquipmentService(self.ddbb) self.equipment_ui = EquipmentUi('glade/', self.equipment_service) def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_equipment_add(self): self.equipment_ui._builder.get_object("entryEquipmentAddDescription").set_text('Test') self.equipment_ui._builder.get_object("entryEquipmentAddLifeExpectancy").set_text('500') self.equipment_ui._builder.get_object("entryEquipmentAddPriorUsage").set_text('100') self.equipment_ui._builder.get_object("checkbuttonEquipmentAddActive").set_active(is_active=True) self.equipment_ui._confirm_add_equipment_clicked(None) equipment = self.equipment_service.get_equipment_item(1) self.assertEqual(equipment.description, 'Test') self.assertEqual(equipment.life_expectancy, 500) self.assertEqual(equipment.prior_usage, 100) self.assertTrue(equipment.active) def test_equipment_add_unicode(self): self.equipment_ui._builder.get_object("entryEquipmentAddDescription").set_text(u'Test äö') self.equipment_ui._builder.get_object("entryEquipmentAddLifeExpectancy").set_text('500') self.equipment_ui._builder.get_object("entryEquipmentAddPriorUsage").set_text('100') self.equipment_ui._builder.get_object("checkbuttonEquipmentAddActive").set_active(is_active=True) self.equipment_ui._confirm_add_equipment_clicked(None) equipment = self.equipment_service.get_equipment_item(1) self.assertEqual(equipment.description, u'Test äö')
class GarminTCXv2Test(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.parent = mock.Mock() self.parent.parent = mock.Mock() self.parent.parent.ddbb = self.ddbb def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_valid_file(self): try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname( os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(None, data_path) xmldoc = etree.parse(tcx_file) valid_xml = garmin_tcxv2.validate( xmldoc, "schemas/GarminTrainingCenterDatabase_v2.xsd") self.assertTrue(valid_xml) except (): self.fail() def test_workout_summary(self): summary = [(0, False, '2012-10-14T12:02:42', '10.12', '00:39:51', 'Running')] try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname( os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(self.parent, data_path) garmin_tcxv2.xmldoc = etree.parse(tcx_file) garmin_tcxv2.buildActivitiesSummary() self.assertEquals(summary, garmin_tcxv2.activitiesSummary) except (): self.fail() def test_summary_in_database(self): summary = [(0, True, '2012-10-14T12:02:42', '10.12', '00:39:51', 'Running')] activity = Activity(date_time_utc='2012-10-14T10:02:42Z', sport_id='1') self.ddbb.session.add(activity) self.ddbb.session.commit() current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname( os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(self.parent, data_path) garmin_tcxv2.xmldoc = etree.parse(tcx_file) garmin_tcxv2.buildActivitiesSummary() self.assertEquals(summary, garmin_tcxv2.activitiesSummary)
class GarminTCXv2Test(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.parent = mock.Mock() self.parent.parent = mock.Mock() self.parent.parent.ddbb = self.ddbb def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_valid_file(self): try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname(os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(None, data_path) xmldoc = etree.parse(tcx_file) valid_xml = garmin_tcxv2.validate(xmldoc, "schemas/GarminTrainingCenterDatabase_v2.xsd") self.assertTrue(valid_xml) except(): self.fail() def test_workout_summary(self): summary = [(0, False, '2012-10-14T12:02:42', '10.12', '00:39:51', 'Running')] try: current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname(os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(self.parent, data_path) garmin_tcxv2.xmldoc = etree.parse(tcx_file) garmin_tcxv2.buildActivitiesSummary() self.assertEqual(summary, garmin_tcxv2.activitiesSummary) except(): self.fail() def test_summary_in_database(self): summary = [(0, True, '2012-10-14T12:02:42', '10.12', '00:39:51', 'Running')] activity = Activity(date_time_utc='2012-10-14T10:02:42Z', sport_id='1') self.ddbb.session.add(activity) self.ddbb.session.commit() current_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname(os.path.dirname(os.path.dirname(current_path))) + "/" tcx_file = current_path + "/sample.tcx" garmin_tcxv2 = garmintcxv2(self.parent, data_path) garmin_tcxv2.xmldoc = etree.parse(tcx_file) garmin_tcxv2.buildActivitiesSummary() self.assertEqual(summary, garmin_tcxv2.activitiesSummary)
class WaypointTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() main = Mock() main.ddbb = self.ddbb main.ddbb.connect() main.ddbb.create_tables(add_default=False) self.waypoint = WaypointService(parent=main) def tearDown(self): self.waypoint = None self.ddbb.disconnect() self.ddbb.drop_tables() def test_waypoint_add_and_get(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) data2 = self.waypoint.getwaypointInfo(dbid) self.assertEquals(data, data2[0]) def test_waypoint_update(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.waypoint.updateWaypoint(dbid, data[0], data[1], data[5], data[3], data[6]) data2 = self.waypoint.getwaypointInfo(dbid) self.assertEquals(data, data2[0]) def test_waypoint_get_all(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.assertEquals(len(self.waypoint.getAllWaypoints()), 2) def test_waypoint_remove(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.waypoint.removeWaypoint(dbid) self.assertEquals(len(self.waypoint.getAllWaypoints()), 1)
class WaypointTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() main = Mock() main.ddbb = self.ddbb main.ddbb.connect() main.ddbb.create_tables(add_default=False) self.waypoint = WaypointService(parent=main) def tearDown(self): self.waypoint = None self.ddbb.disconnect() self.ddbb.drop_tables() def test_waypoint_add_and_get(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) data2 = self.waypoint.getwaypointInfo(dbid) self.assertEqual(data, data2[0]) def test_waypoint_update(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.waypoint.updateWaypoint(dbid, data[0], data[1], data[5], data[3], data[6]) data2 = self.waypoint.getwaypointInfo(dbid) self.assertEqual(data, data2[0]) def test_waypoint_get_all(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.assertEqual(len(self.waypoint.getAllWaypoints()), 2) def test_waypoint_remove(self): data = (30.0, 20.0, None, u'Comment', None, u'Test', u'sym') dbid = self.waypoint.addWaypoint(lat=data[0], lon=data[1], name=data[5], comment=data[3], sym=data[6]) dbid = self.waypoint.addWaypoint(lat=50, lon=60, name='Test2', comment='Comment 2', sym='sym2') self.waypoint.removeWaypoint(dbid) self.assertEqual(len(self.waypoint.getAllWaypoints()), 1)
class AthleteTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() main = Mock() main.ddbb = self.ddbb main.profile = Profile() main.ddbb.connect() main.ddbb.create_tables(add_default=False) self.athlete = Athlete(parent=main) def tearDown(self): self.athlete = None self.ddbb.disconnect() self.ddbb.drop_tables() def test_athlete_insert_and_get(self): data = {'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1} self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data2 = self.athlete.get_athlete_stats() self.assertEqual(data, data2[0]) def test_athlete_update_and_get(self): data = {'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1} self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data['maxhr'] = 180 data['bodyfat'] = 30.0 self.athlete.update_athlete_stats(1, str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data2 = self.athlete.get_athlete_stats() self.assertEqual(data, data2[0]) def test_athlete_delete_record(self): data = {'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1} self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) self.athlete.delete_record(1) self.assertFalse(self.athlete.get_athlete_stats())
class EquipmentUiTest(TestCase): def setUp(self): initialize_gettext('locale/') self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables() self.equipment_service = EquipmentService(self.ddbb) self.equipment_ui = EquipmentUi('glade/', self.equipment_service) def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_equipment_add(self): self.equipment_ui._builder.get_object( "entryEquipmentAddDescription").set_text('Test') self.equipment_ui._builder.get_object( "entryEquipmentAddLifeExpectancy").set_text('500') self.equipment_ui._builder.get_object( "entryEquipmentAddPriorUsage").set_text('100') self.equipment_ui._builder.get_object( "checkbuttonEquipmentAddActive").set_active(is_active=True) self.equipment_ui._confirm_add_equipment_clicked(None) equipment = self.equipment_service.get_equipment_item(1) self.assertEqual(equipment.description, 'Test') self.assertEqual(equipment.life_expectancy, 500) self.assertEqual(equipment.prior_usage, 100) self.assertTrue(equipment.active) def test_equipment_add_unicode(self): self.equipment_ui._builder.get_object( "entryEquipmentAddDescription").set_text(u'Test äö') self.equipment_ui._builder.get_object( "entryEquipmentAddLifeExpectancy").set_text('500') self.equipment_ui._builder.get_object( "entryEquipmentAddPriorUsage").set_text('100') self.equipment_ui._builder.get_object( "checkbuttonEquipmentAddActive").set_active(is_active=True) self.equipment_ui._confirm_add_equipment_clicked(None) equipment = self.equipment_service.get_equipment_item(1) self.assertEqual(equipment.description, u'Test äö')
class EquipmentTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables() def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_id_defaults_to_none(self): equipment = Equipment() self.assertEqual(None, equipment.id) def test_id_set_to_integer(self): equipment = Equipment() equipment.id = 2 self.assertEquals(2, equipment.id) def test_id_set_to_numeric_string(self): equipment = Equipment() equipment.id = "3" self.ddbb.session.add(equipment) self.ddbb.session.commit() self.assertEquals(3, equipment.id) def test_id_set_to_non_numeric_string(self): equipment = Equipment() equipment.id = "test" try: self.ddbb.session.add(equipment) self.ddbb.session.flush() except (IntegrityError, OperationalError, DataError): pass else: self.fail("Should not be able to set equipment id to non numeric value.") def test_description_defaults_to_empty_string(self): equipment = Equipment() self.assertEquals(u"", equipment.description) def test_description_set_to_non_unicode_string(self): equipment = Equipment() equipment.description = "100$ Shoes" + chr(255) try: self.ddbb.session.add(equipment) self.ddbb.session.flush() except (ProgrammingError, DataError): pass else: self.fail("Should not be able to set description to non unicode string value.") def test_description_set_to_unicode_string(self): equipment = Equipment() equipment.description = u"Zapatos de €100" self.assertEquals(u"Zapatos de €100", equipment.description) def test_description_set_to_non_string(self): equipment = Equipment() equipment.description = 42 self.ddbb.session.add(equipment) self.ddbb.session.commit() self.assertEquals(u"42", equipment.description) def test_active_defaults_to_true(self): equipment = Equipment() self.assertTrue(equipment.active) def test_active_set_to_boolean(self): equipment = Equipment() equipment.active = False self.assertFalse(equipment.active) def test_active_set_to_non_boolean(self): equipment = Equipment() equipment.active = "test" self.ddbb.session.add(equipment) try: self.ddbb.session.commit() self.assertTrue(equipment.active) except StatementError: pass def test_life_expectancy_defaults_to_zero(self): equipment = Equipment() self.assertEqual(0, equipment.life_expectancy) def test_life_expectancy_set_to_integer(self): equipment = Equipment() equipment.life_expectancy = 2 self.assertEquals(2, equipment.life_expectancy) def test_life_expectancy_set_to_numeric_string(self): equipment = Equipment() equipment.life_expectancy = "3" self.ddbb.session.add(equipment) self.ddbb.session.commit() self.assertEquals(3, equipment.life_expectancy) def test_life_expectancy_set_to_non_numeric_string(self): equipment = Equipment() equipment.life_expectancy = "test" try: self.ddbb.session.add(equipment) self.ddbb.session.flush() except StatementError: pass else: self.fail("Should not be able to set life expectancy to non numeric value.") def test_prior_usage_defaults_to_zero(self): equipment = Equipment() self.assertEqual(0, equipment.prior_usage) def test_prior_usage_set_to_integer(self): equipment = Equipment() equipment.prior_usage = 2 self.assertEquals(2, equipment.prior_usage) def test_prior_usage_set_to_numeric_string(self): equipment = Equipment() equipment.prior_usage = "3" self.ddbb.session.add(equipment) self.ddbb.session.commit() self.assertEquals(3, equipment.prior_usage) def test_prior_usage_set_to_non_numeric_string(self): equipment = Equipment() equipment.prior_usage = "test" try: self.ddbb.session.add(equipment) self.ddbb.session.flush() except StatementError: pass else: self.fail("Should not be able to set life expectancy to non numeric value.") def test_notes_defaults_to_empty_string(self): equipment = Equipment() self.assertEquals(u"", equipment.notes) def test_notes_set_to_string(self): equipment = Equipment() equipment.notes = "100$ Shoes" + chr(255) try: self.ddbb.session.add(equipment) self.ddbb.session.flush() except (ProgrammingError, DataError): pass else: self.fail("Should not be able to set notes to non-unicode string value.") def test_notes_set_to_unicode_string(self): equipment = Equipment() equipment.notes = u"Zapatos de €100." self.assertEquals(u"Zapatos de €100.", equipment.notes) def test_notes_set_to_non_string(self): equipment = Equipment() equipment.notes = 42 self.ddbb.session.add(equipment) self.ddbb.session.commit() self.assertEquals(u"42", equipment.notes) def test_equals_new_instances(self): equipment1 = Equipment() equipment2 = Equipment() self.assertNotEqual(equipment1, equipment2, "") def test_equals_instances_with_same_id(self): equipment1 = Equipment() equipment1.id = 1 equipment2 = Equipment() equipment2.id = 1 self.assertEqual(equipment1, equipment2, "Equipment instances with same id should be equal.") def test_equals_instances_with_different_ids(self): equipment1 = Equipment() equipment1.id = 1 equipment2 = Equipment() equipment2.id = 2 self.assertNotEqual(equipment1, equipment2, "Equipment instances with different ids should not be equal.")
class EquipmentServiceTest(unittest.TestCase): def setUp(self): self.mock_ddbb = DDBB() self.mock_ddbb.connect() self.mock_ddbb.create_tables() self.equipment_service = EquipmentService(self.mock_ddbb) self.equipment_table = DeclarativeBase.metadata.tables['equipment'] def tearDown(self): self.mock_ddbb.disconnect() self.mock_ddbb.drop_tables() def test_get_equipment_item(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"Test Description", "prior_usage": 200, "active": True}) item = self.equipment_service.get_equipment_item(1) self.assertEquals(1, item.id) self.assertEquals("Test Description", item.description) self.assertTrue(item.active) self.assertEquals(500, item.life_expectancy) self.assertEquals(200, item.prior_usage) self.assertEquals("Test notes.", item.notes) def test_get_equipment_item_non_unicode(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"Test Description", "prior_usage": 200, "active": True}) item = self.equipment_service.get_equipment_item(1) self.assertEquals("Test Description", item.description) self.assertEquals("Test notes.", item.notes) def test_get_equipment_item_non_existant(self): item = self.equipment_service.get_equipment_item(1) self.assertEquals(None, item) def test_get_all_equipment(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes 1.", "description": u"Test item 1", "prior_usage": 200, "active": True}) self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 600, "notes": u"Test notes 2.", "description": u"Test item 2", "prior_usage": 300, "active": False}) items = self.equipment_service.get_all_equipment() item = items[0] self.assertEquals(1, item.id) self.assertEquals("Test item 1", item.description) self.assertTrue(item.active) self.assertEquals(500, item.life_expectancy) self.assertEquals(200, item.prior_usage) self.assertEquals("Test notes 1.", item.notes) item = items[1] self.assertEquals(2, item.id) self.assertEquals("Test item 2", item.description) self.assertFalse(item.active) self.assertEquals(600, item.life_expectancy) self.assertEquals(300, item.prior_usage) self.assertEquals("Test notes 2.", item.notes) def test_get_all_equipment_non_existant(self): items = self.equipment_service.get_all_equipment() self.assertEquals([], items) def test_get_active_equipment(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes 1.", "description": u"Test item 1", "prior_usage": 200, "active": True}) self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 600, "notes": u"Test notes 2.", "description": u"Test item 2", "prior_usage": 300, "active": True}) items = self.equipment_service.get_active_equipment() item = items[0] self.assertEquals(1, item.id) self.assertEquals("Test item 1", item.description) self.assertTrue(item.active) self.assertEquals(500, item.life_expectancy) self.assertEquals(200, item.prior_usage) self.assertEquals("Test notes 1.", item.notes) item = items[1] self.assertEquals(2, item.id) self.assertEquals("Test item 2", item.description) self.assertTrue(item.active) self.assertEquals(600, item.life_expectancy) self.assertEquals(300, item.prior_usage) self.assertEquals("Test notes 2.", item.notes) def test_get_active_equipment_non_existant(self): items = self.equipment_service.get_active_equipment() self.assertEquals([], items) def test_store_equipment(self): equipment = Equipment() equipment.description = u"test description" stored_equipment = self.equipment_service.store_equipment(equipment) self.assertEquals(1, stored_equipment.id) def test_store_equipment_duplicate_description(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"test item", "prior_usage": 200, "active": True}) equipment = Equipment() equipment.description = u"test item" try: self.equipment_service.store_equipment(equipment) self.fail("Should not be able to store new item with non-unique description.") except(EquipmentServiceException): pass def test_update_equipment(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"old description", "prior_usage": 200, "active": True}) equipment = self.equipment_service.get_equipment_item(1) equipment.description = u"new description" self.equipment_service.store_equipment(equipment) equipment = self.equipment_service.get_equipment_item(1) self.assertEquals("new description", equipment.description) def test_update_equipment_duplicate_description(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"test item", "prior_usage": 200, "active": True}) equipment = Equipment() equipment.id = 2 equipment.description = u"test item" try: self.equipment_service.store_equipment(equipment) self.fail("Should not be able to change item description to non-unique value.") except(EquipmentServiceException): pass def test_get_equipment_usage(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"test item", "prior_usage": 0, "active": True}) self.mock_ddbb.session.execute("insert into records (distance,sport) values (250,1)") self.mock_ddbb.session.execute("insert into record_equipment (record_id,equipment_id) values (1,1)") equipment = self.equipment_service.get_equipment_item(1) usage = self.equipment_service.get_equipment_usage(equipment) self.assertEquals(250, usage) def test_get_equipment_usage_none(self): self.mock_ddbb.session.execute(self.equipment_table.insert(), {"life_expectancy": 500, "notes": u"Test notes.", "description": u"test item", "prior_usage": 0, "active": True}) equipment = self.equipment_service.get_equipment_item(1) usage = self.equipment_service.get_equipment_usage(equipment) self.assertEquals(0, usage) def test_get_equipment_prior_usage(self): equipment = Equipment() equipment.id = 1 equipment.prior_usage = 250 usage = self.equipment_service.get_equipment_usage(equipment) self.assertEquals(250, usage) def test_get_equipment_prior_usage(self): equipment = Equipment() equipment.id = 1 equipment.prior_usage = 250 usage = self.equipment_service.get_equipment_usage(equipment) self.assertEquals(250, usage)
class RecordTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.main = mock.Mock() self.main.ddbb = self.ddbb self.main.activitypool = ActivityService(pytrainer_main=self.main) self.main.profile.gpxdir = '/nonexistent' self.record = Record(SportService(self.ddbb), parent=self.main) time = 7426 distance = 46.18 speed = distance * 3600 / time time_hhmmss = [time // 3600, (time / 60) % 60, time % 60] self.summary = {} self.summary['rcd_gpxfile'] = u'/nonexistent' self.summary['rcd_sport'] = u"Run" self.summary['rcd_date'] = u'2016-07-24' self.summary['rcd_calories'] = 1462 self.summary['rcd_comments'] = u'test comment' self.summary['rcd_title'] = u'test 1' self.summary['rcd_time'] = time_hhmmss self.summary['rcd_distance'] = "%0.2f" % distance self.summary['rcd_pace'] = "%d.%02d" % ((3600 / speed) / 60, (3600 / speed) % 60) self.summary['rcd_maxpace'] = "%d.%02d" % ((3600 / 72) / 60, (3600 / 72) % 60) self.summary['rcd_average'] = speed self.summary['rcd_maxvel'] = 72 self.summary['rcd_beats'] = 115.0 self.summary['rcd_maxbeats'] = 120 self.summary['rcd_upositive'] = 553.1 self.summary['rcd_unegative'] = 564.1 self.summary['date_time_local'] = u'2016-07-24 12:58:23+03:00' self.summary['date_time_utc'] = u'2016-07-24T09:58:23Z' self.laps = [{ 'distance': 46181.9, 'lap_number': 0, 'calories': 1462, 'elapsed_time': u'7426.0', 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'laptrigger': u'manual' }] def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_insert_record(self): newid = self.record.insertRecord(self.summary, laps=self.laps) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.unegative, 564.1) self.assertEqual(activity.upositive, 553.1) self.assertEqual(activity.beats, 115.0) self.assertEqual(activity.maxbeats, 120) self.assertEqual( activity.date_time, datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800))) self.assertEqual(activity.date_time_utc, u'2016-07-24T09:58:23Z') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Run")) self.assertEqual(activity.title, u'test 1') self.assertEqual( activity.laps[0], { 'distance': 46181.9, 'end_lon': None, 'lap_number': 0, 'start_lon': None, 'id_lap': 1, 'calories': 1462, 'comments': None, 'laptrigger': u'manual', 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'end_lat': None, 'start_lat': None, 'max_speed': None }) def test_insert_record_datetime(self): """Importing multiple activities uses a datetime object for list_options['date_time_local'], also test that code path""" self.summary['date_time_local'] = datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset( None, 10800)) newid = self.record.insertRecord(self.summary, laps=self.laps) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.unegative, 564.1) self.assertEqual(activity.upositive, 553.1) self.assertEqual(activity.beats, 115.0) self.assertEqual(activity.maxbeats, 120) self.assertEqual( activity.date_time, datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800))) self.assertEqual(activity.date_time_utc, u'2016-07-24T09:58:23Z') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Run")) self.assertEqual(activity.title, u'test 1') self.assertEqual( activity.laps[0], { 'distance': 46181.9, 'end_lon': None, 'lap_number': 0, 'start_lon': None, 'id_lap': 1, 'calories': 1462, 'comments': None, 'laptrigger': u'manual', 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'end_lat': None, 'start_lat': None, 'max_speed': None }) def test_update_record(self): newid = self.record.insertRecord(self.summary) update_dict = self.summary.copy() update_dict['rcd_title'] = u'test 2' update_dict['rcd_sport'] = u"Bike" self.record.updateRecord(update_dict, newid) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.title, u'test 2') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Bike")) def test_get_day_list(self): self.record.insertRecord(self.summary) daylist = list( self.record.getRecordDayList(datetime(2016, 7, 24, 9, 58, 23))) self.assertEqual(daylist, [24]) def test_getLastRecordDateString(self): self.record.insertRecord(self.summary) self.assertEqual(self.record.getLastRecordDateString(), self.summary['rcd_date']) def test_record_midnight_date_bug(self): self.summary['date_time_local'] = u'2016-07-24 0:00:00+03:00' self.summary['date_time_utc'] = u'2016-07-23T21:00:00Z' newid = self.record.insertRecord(self.summary) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.date, date(2016, 7, 24))
class AthleteTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() main = Mock() main.ddbb = self.ddbb main.profile = Profile() main.ddbb.connect() main.ddbb.create_tables(add_default=False) self.athlete = Athlete(parent=main) def tearDown(self): self.athlete = None self.ddbb.disconnect() self.ddbb.drop_tables() def test_athlete_insert_and_get(self): data = { 'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1 } self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data2 = self.athlete.get_athlete_stats() self.assertEqual(data, data2[0]) def test_athlete_update_and_get(self): data = { 'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1 } self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data['maxhr'] = 180 data['bodyfat'] = 30.0 self.athlete.update_athlete_stats(1, str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) data2 = self.athlete.get_athlete_stats() self.assertEqual(data, data2[0]) def test_athlete_delete_record(self): data = { 'date': date(2017, 4, 3), 'weight': 60.0, 'bodyfat': 20.0, 'restinghr': 60, 'maxhr': 190, 'id_athletestat': 1 } self.athlete.insert_athlete_stats(str(data['date']), data['weight'], data['bodyfat'], data['restinghr'], data['maxhr']) self.athlete.delete_record(1) self.assertFalse(self.athlete.get_athlete_stats())
class SportTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=False) def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_id_should_default_to_none(self): sport = Sport() self.assertEquals(None, sport.id) def test_id_should_accept_integer(self): sport = Sport() sport.id = 1 self.assertEquals(1, sport.id) def test_id_should_accept_integer_string(self): sport = Sport() sport.id = "1" self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEquals(1, sport.id) def test_id_should_not_accept_non_integer_string(self): sport = Sport() try: sport.id = "test" self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, DataError): pass else: self.fail() def test_name_should_default_to_empty_string(self): sport = Sport() self.assertEquals(u"", sport.name) def test_name_should_accept_unicode_string(self): sport = Sport() sport.name = u"Unicycling" self.assertEquals(u"Unicycling", sport.name) def test_name_should_not_accept_non_unicode_string(self): sport = Sport() sport.name = "Juggling" + chr(255) try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (ProgrammingError, DataError): pass else: self.fail() def test_name_should_not_accept_none(self): sport = Sport() sport.name = None try: self.ddbb.session.add(sport) self.ddbb.session.commit() except (IntegrityError, OperationalError): pass else: self.fail() def test_met_should_default_to_None(self): sport = Sport() self.assertEquals(None, sport.met) def test_met_should_accept_float(self): sport = Sport() sport.met = 22.5 self.ddbb.session.add(sport) self.ddbb.session.flush() self.assertEquals(22.5, sport.met) def test_met_should_accept_float_string(self): sport = Sport() sport.name = "test1" sport.met = "22.5" self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEquals(22.5, sport.met) def test_met_should_not_accept_non_float_string(self): sport = Sport() sport.met = "22.5kg" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (ValueError, StatementError): pass else: self.fail() def test_met_should_not_accept_negative_value(self): sport = Sport() sport.met = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_met_should_accept_none(self): sport = Sport() sport.met = None self.assertEquals(None, sport.met) def test_weight_should_default_to_zero(self): sport = Sport() self.assertEquals(0, sport.weight) def test_weight_should_accept_float(self): sport = Sport() sport.weight = 22.5 self.assertEquals(22.5, sport.weight) def test_weight_should_accept_float_string(self): sport = Sport() sport.weight = "22.5" self.ddbb.session.add(sport) self.ddbb.session.commit() self.assertEquals(22.5, sport.weight) def test_weight_should_not_accept_non_float_string(self): sport = Sport() sport.weight = "22.5kg" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except StatementError: pass else: self.fail() def test_weight_should_not_accept_negative_value(self): sport = Sport() sport.weight = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_weight_should_not_accept_none(self): sport = Sport() sport.weight = None try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, OperationalError): pass else: self.fail() def test_max_pace_should_default_to_none(self): sport = Sport() self.assertEquals(None, sport.max_pace) def test_max_pace_should_accept_integer(self): sport = Sport() sport.max_pace = 220 self.ddbb.session.add(sport) self.ddbb.session.flush() self.assertEquals(220, sport.max_pace) def test_max_pace_should_accept_integer_string(self): sport = Sport() sport.max_pace = "220" self.ddbb.session.add(sport) self.ddbb.session.commit() self.assertEquals(220, sport.max_pace) def test_max_pace_should_not_accept_non_integer_string(self): sport = Sport() sport.max_pace = "225s" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (ValueError, StatementError): pass else: self.fail() def test_max_pace_should_take_floor_of_float(self): sport = Sport() sport.max_pace = 220.6 self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEquals(220, sport.max_pace) def test_max_pace_should_not_accept_negative_value(self): sport = Sport() sport.max_pace = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_max_pace_should_accept_none(self): sport = Sport() sport.max_pace = None self.assertEquals(None, sport.max_pace) def test_color_should_default_to_blue(self): sport = Sport() self.assertEquals(0x0000ff, sport.color.rgb_val) def test_color_should_not_accept_none(self): sport = Sport() sport.color = None try: self.ddbb.session.add(sport) self.ddbb.session.commit() except StatementError: pass else: self.fail()
class SportServiceTest(unittest.TestCase): def setUp(self): self.mock_ddbb = DDBB() self.mock_ddbb.connect() self.mock_ddbb.create_tables(add_default=False) self.sport_service = SportService(self.mock_ddbb) def tearDown(self): self.mock_ddbb.disconnect() self.mock_ddbb.drop_tables() def test_store_sport_should_insert_row_when_sport_has_no_id(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) self.assertEqual(1, sport.id) def test_store_sport_should_update_row_when_sport_has_id(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) sport.name = u"New name" self.sport_service.store_sport(sport) sport = self.sport_service.get_sport(1) self.assertEqual(sport.name, u"New name") def test_store_sport_should_return_stored_sport(self): sport = Sport() stored_sport = self.sport_service.store_sport(sport) self.assertEqual(1, stored_sport.id) def test_store_sport_should_error_when_new_sport_has_duplicate_name(self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"Test name" try: self.sport_service.store_sport(sport2) except(SportServiceException): pass else: self.fail() def test_store_sport_should_error_when_existing_sport_has_duplicate_name(self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"New name" self.sport_service.store_sport(sport2) sport1.name = u"New name" try: self.sport_service.store_sport(sport1) except(SportServiceException): pass else: self.fail() def test_get_sport_returns_none_for_nonexistant_sport(self): sport = self.sport_service.get_sport(1) self.assertEqual(None, sport) def test_get_sport_returns_sport_with_id(self): sport = Sport() sport.name = u"Test name" self.sport_service.store_sport(sport) sport = self.sport_service.get_sport(1) self.assertEqual(1, sport.id) def test_get_sport_raises_error_for_id_none(self): try: self.sport_service.get_sport(None) except(ValueError): pass else: self.fail() def test_get_sport_by_name_returns_none_for_nonexistant_sport(self): sport = self.sport_service.get_sport_by_name("no such sport") self.assertEqual(None, sport) def test_get_sport_by_name_returns_sport_with_name(self): sport1 = Sport() sport1.name = u"rugby" self.sport_service.store_sport(sport1) sport2 = self.sport_service.get_sport_by_name("rugby") self.assertEqual(u"rugby", sport2.name) def test_get_sport_by_name_raises_error_for_none_sport_name(self): try: self.sport_service.get_sport_by_name(None) except(ValueError): pass else: self.fail() def test_get_all_sports_should_return_all_sports_in_query_result(self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"Test name 2" self.sport_service.store_sport(sport2) sports = self.sport_service.get_all_sports() self.assertEqual(2, len(sports)) sport1 = sports[0] self.assertEqual(1, sport1.id) sport2 = sports[1] self.assertEqual(2, sport2.id) def test_get_all_sports_should_return_no_sports_when_query_result_empty(self): sports = self.sport_service.get_all_sports() self.assertEqual(0, len(sports)) def test_remove_sport_should_error_when_sport_has_no_id(self): sport = Sport() try: self.sport_service.remove_sport(sport) except(SportServiceException): pass else: self.fail() def test_remove_sport_should_error_when_sport_has_unknown_id(self): sport = Sport() sport.id = 100 try: self.sport_service.remove_sport(sport) except(SportServiceException): pass else: self.fail() def test_remove_sport_should_remove_associated_entries(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) self.sport_service.remove_sport(sport) result = self.sport_service.get_sport(1) self.assertEqual(result, None)
class SportTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=False) def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_id_should_default_to_none(self): sport = Sport() self.assertEqual(None, sport.id) def test_id_should_accept_integer(self): sport = Sport() sport.id = 1 self.assertEqual(1, sport.id) def test_id_should_accept_integer_string(self): sport = Sport() sport.id = "1" self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEqual(1, sport.id) def test_id_should_not_accept_non_integer_string(self): sport = Sport() try: sport.id = "test" self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, DataError, OperationalError): pass else: self.fail() def test_name_should_default_to_empty_string(self): sport = Sport() self.assertEqual(u"", sport.name) def test_name_should_accept_unicode_string(self): sport = Sport() sport.name = u"Unicycling" self.assertEqual(u"Unicycling", sport.name) @unittest.skipIf(sys.version_info > (3, 0), "All strings are unicode in Python 3") def test_name_should_not_accept_non_unicode_string(self): sport = Sport() sport.name = "Juggling" + chr(255) try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (ProgrammingError, DataError, OperationalError): pass else: self.fail() def test_name_should_not_accept_none(self): sport = Sport() sport.name = None try: self.ddbb.session.add(sport) self.ddbb.session.commit() except (IntegrityError, OperationalError): pass else: self.fail() def test_met_should_default_to_None(self): sport = Sport() self.assertEqual(None, sport.met) def test_met_should_accept_float(self): sport = Sport() sport.met = 22.5 self.ddbb.session.add(sport) self.ddbb.session.flush() self.assertEqual(22.5, sport.met) def test_met_should_accept_float_string(self): sport = Sport() sport.name = "test1" sport.met = "22.5" self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEqual(22.5, sport.met) def test_met_should_not_accept_non_float_string(self): sport = Sport() sport.met = "22.5kg" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except(ValueError, StatementError): pass else: self.fail() def test_met_should_not_accept_negative_value(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Check constraints not available on Mysql') sport = Sport() sport.met = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_met_should_accept_none(self): sport = Sport() sport.met = None self.assertEqual(None, sport.met) def test_weight_should_default_to_zero(self): sport = Sport() self.assertEqual(0, sport.weight) def test_weight_should_accept_float(self): sport = Sport() sport.weight = 22.5 self.assertEqual(22.5, sport.weight) def test_weight_should_accept_float_string(self): sport = Sport() sport.weight = "22.5" self.ddbb.session.add(sport) self.ddbb.session.commit() self.assertEqual(22.5, sport.weight) def test_weight_should_not_accept_non_float_string(self): sport = Sport() sport.weight = "22.5kg" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except StatementError: pass else: self.fail() def test_weight_should_not_accept_negative_value(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Check constraints not available on Mysql') sport = Sport() sport.weight = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_weight_should_not_accept_none(self): sport = Sport() sport.weight = None try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, OperationalError): pass else: self.fail() def test_max_pace_should_default_to_none(self): sport = Sport() self.assertEqual(None, sport.max_pace) def test_max_pace_should_accept_integer(self): sport = Sport() sport.max_pace = 220 self.ddbb.session.add(sport) self.ddbb.session.flush() self.assertEqual(220, sport.max_pace) def test_max_pace_should_accept_integer_string(self): sport = Sport() sport.max_pace = "220" self.ddbb.session.add(sport) self.ddbb.session.commit() self.assertEqual(220, sport.max_pace) def test_max_pace_should_not_accept_non_integer_string(self): sport = Sport() sport.max_pace = "225s" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except(ValueError, StatementError): pass else: self.fail() def test_max_pace_should_take_floor_of_float(self): sport = Sport() sport.max_pace = 220.6 self.ddbb.session.add(sport) self.ddbb.session.commit() sport = self.ddbb.session.query(Sport).filter(Sport.id == 1).one() self.assertEqual(220, sport.max_pace) def test_max_pace_should_not_accept_negative_value(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Check constraints not available on Mysql') sport = Sport() sport.max_pace = -1 try: self.ddbb.session.add(sport) self.ddbb.session.flush() except (IntegrityError, InterfaceError): pass else: self.fail() def test_max_pace_should_accept_none(self): sport = Sport() sport.max_pace = None self.assertEqual(None, sport.max_pace) def test_color_should_default_to_blue(self): sport = Sport() self.assertEqual(0x0000ff, sport.color.rgb_val) def test_color_should_not_accept_none(self): sport = Sport() sport.color = None try: self.ddbb.session.add(sport) self.ddbb.session.commit() except StatementError: pass else: self.fail()
class SportServiceTest(unittest.TestCase): def setUp(self): self.mock_ddbb = DDBB() self.mock_ddbb.connect() self.mock_ddbb.create_tables(add_default=False) self.sport_service = SportService(self.mock_ddbb) def tearDown(self): self.mock_ddbb.disconnect() self.mock_ddbb.drop_tables() def test_store_sport_should_insert_row_when_sport_has_no_id(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) self.assertEquals(1, sport.id) def test_store_sport_should_update_row_when_sport_has_id(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) sport.name = u"New name" self.sport_service.store_sport(sport) sport = self.sport_service.get_sport(1) self.assertEquals(sport.name, u"New name") def test_store_sport_should_return_stored_sport(self): sport = Sport() stored_sport = self.sport_service.store_sport(sport) self.assertEquals(1, stored_sport.id) def test_store_sport_should_error_when_new_sport_has_duplicate_name(self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"Test name" try: self.sport_service.store_sport(sport2) except (SportServiceException): pass else: self.fail() def test_store_sport_should_error_when_existing_sport_has_duplicate_name( self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"New name" self.sport_service.store_sport(sport2) sport1.name = u"New name" try: self.sport_service.store_sport(sport1) except (SportServiceException): pass else: self.fail() def test_get_sport_returns_none_for_nonexistant_sport(self): sport = self.sport_service.get_sport(1) self.assertEquals(None, sport) def test_get_sport_returns_sport_with_id(self): sport = Sport() sport.name = u"Test name" self.sport_service.store_sport(sport) sport = self.sport_service.get_sport(1) self.assertEquals(1, sport.id) def test_get_sport_raises_error_for_id_none(self): try: self.sport_service.get_sport(None) except (ValueError): pass else: self.fail() def test_get_sport_by_name_returns_none_for_nonexistant_sport(self): sport = self.sport_service.get_sport_by_name("no such sport") self.assertEquals(None, sport) def test_get_sport_by_name_returns_sport_with_name(self): sport1 = Sport() sport1.name = u"rugby" self.sport_service.store_sport(sport1) sport2 = self.sport_service.get_sport_by_name("rugby") self.assertEquals(u"rugby", sport2.name) def test_get_sport_by_name_raises_error_for_none_sport_name(self): try: self.sport_service.get_sport_by_name(None) except (ValueError): pass else: self.fail() def test_get_all_sports_should_return_all_sports_in_query_result(self): sport1 = Sport() sport1.name = u"Test name" self.sport_service.store_sport(sport1) sport2 = Sport() sport2.name = u"Test name 2" self.sport_service.store_sport(sport2) sports = self.sport_service.get_all_sports() self.assertEquals(2, len(sports)) sport1 = sports[0] self.assertEquals(1, sport1.id) sport2 = sports[1] self.assertEquals(2, sport2.id) def test_get_all_sports_should_return_no_sports_when_query_result_empty( self): sports = self.sport_service.get_all_sports() for i in sports: print i.name self.assertEquals(0, len(sports)) def test_remove_sport_should_error_when_sport_has_no_id(self): sport = Sport() try: self.sport_service.remove_sport(sport) except (SportServiceException): pass else: self.fail() def test_remove_sport_should_error_when_sport_has_unknown_id(self): sport = Sport() sport.id = 100 try: self.sport_service.remove_sport(sport) except (SportServiceException): pass else: self.fail() def test_remove_sport_should_remove_associated_entries(self): sport = Sport() sport.name = u"Test name" sport = self.sport_service.store_sport(sport) self.sport_service.remove_sport(sport) result = self.sport_service.get_sport(1) self.assertEquals(result, None)
class ActivityTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() main = Mock() main.ddbb = self.ddbb main.profile = Profile() main.ddbb.connect() main.ddbb.create_tables(add_default=True) # We need a sport self.uc = UC() self.uc.set_us(False) self.service = ActivityService(pytrainer_main=main) records_table = DeclarativeBase.metadata.tables['records'] self.ddbb.session.execute(records_table.insert({'distance': 46.18, 'maxspeed': 44.6695617695, 'maxpace': 1.2, 'title': u'test activity', 'unegative': 564.08076273, 'upositive': 553.05993673, 'average': 22.3882142185, 'date_time_local': u'2016-07-24 12:58:23+0300', 'calories': 1462, 'beats': 115.0, 'comments': u'test comment', 'pace': 2.4, 'date_time_utc': u'2016-07-24T09:58:23Z', 'date': datetime.date(2016, 7, 24), 'duration': 7426, 'sport': 1, 'maxbeats': 120.0})) laps_table = DeclarativeBase.metadata.tables['laps'] self.ddbb.session.execute(laps_table.insert({'distance': 46181.9, 'lap_number': 0, 'calories': 1462, 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'laptrigger': u'manual'})) self.activity = self.service.get_activity(1) def tearDown(self): self.service.clear_pool() self.ddbb.disconnect() self.ddbb.drop_tables() self.uc.set_us(False) def test_activity_date_time(self): self.assertEqual(self.activity.date_time, datetime.datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800))) def test_activity_distance(self): self.assertEqual(self.activity.distance, 46.18) def test_activity_sport_name(self): self.assertEqual(self.activity.sport_name, 'Mountain Bike') def test_activity_duration(self): self.assertEqual(self.activity.duration, 7426) def test_activity_time(self): self.assertEqual(self.activity.time, self.activity.duration) def test_activity_starttime(self): self.assertEqual(self.activity.starttime, '12:58:23 PM') def test_activity_time_tuple(self): self.assertEqual(self.activity.time_tuple, (2, 3, 46)) def test_activity_lap(self): self.maxDiff = None self.assertEqual(self.activity.laps[0], {'distance': 46181.9, 'end_lon': None, 'lap_number': 0, 'start_lon': None, 'id_lap': 1, 'calories': 1462, 'comments': None, 'laptrigger': u'manual', 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'end_lat': None, 'start_lat': None, 'max_speed': None}) lap = self.activity.Laps[0] self.assertEqual(lap.distance, 46181.9) self.assertEqual(lap.duration, 7426.0) self.assertEqual(lap.calories, 1462) self.assertEqual(lap.avg_hr, 136) self.assertEqual(lap.max_hr, 173) self.assertEqual(lap.activity, self.activity) self.assertEqual(lap.lap_number, 0) self.assertEqual(lap.intensity, u'active') self.assertEqual(lap.laptrigger, u'manual') def test_activity_get_value_f(self): self.assertEqual(self.activity.get_value_f('distance', "%0.2f"), '46.18') self.assertEqual(self.activity.get_value_f('average', "%0.2f"), '22.39') self.assertEqual(self.activity.get_value_f('maxspeed', "%0.2f"), '44.67') self.assertEqual(self.activity.get_value_f('time', '%s'), '2:03:46') self.assertEqual(self.activity.get_value_f('calories', "%0.0f"), '1462') self.assertEqual(self.activity.get_value_f('pace', "%s"), '2:24') self.assertEqual(self.activity.get_value_f('maxpace', "%s"), '1:12') self.assertEqual(self.activity.get_value_f('upositive', "%0.2f"), '553.06') self.assertEqual(self.activity.get_value_f('unegative', "%0.2f"), '564.08') def test_activity_get_value_f_us(self): self.uc.set_us(True) self.assertEqual(self.activity.get_value_f('distance', "%0.2f"), '28.69') self.assertEqual(self.activity.get_value_f('average', "%0.2f"), '13.91') self.assertEqual(self.activity.get_value_f('maxspeed', "%0.2f"), '27.76') self.assertEqual(self.activity.get_value_f('time', '%s'), '2:03:46') self.assertEqual(self.activity.get_value_f('calories', "%0.0f"), '1462') self.assertEqual(self.activity.get_value_f('pace', "%s"), '3:52') self.assertEqual(self.activity.get_value_f('maxpace', "%s"), '1:56') self.assertEqual(self.activity.get_value_f('upositive', "%0.2f"), '1814.50') self.assertEqual(self.activity.get_value_f('unegative', "%0.2f"), '1850.66') def test_activity_service_null(self): none_activity = self.service.get_activity(None) self.assertIsNone(none_activity.id) def test_activity_remove(self): self.service.remove_activity_from_db(self.activity) try: self.service.get_activity(1) except NoResultFound: pass else: self.fail() def test_activities_for_day(self): activity = list(self.service.get_activities_for_day(datetime.date(2016, 7, 24)))[0] self.assertEqual(self.activity, activity) def test_activities_period(self): activity = list(self.service.get_activities_period(DateRange.for_week_containing(datetime.date(2016, 7, 24))))[0] self.assertEqual(self.activity, activity) def test_all_activities(self): activity = list(self.service.get_all_activities())[0] self.assertEqual(self.activity, activity)
class RecordTest(unittest.TestCase): def setUp(self): self.ddbb = DDBB() self.ddbb.connect() self.ddbb.create_tables(add_default=True) self.main = mock.Mock() self.main.ddbb = self.ddbb self.main.activitypool = ActivityService(pytrainer_main=self.main) self.main.profile.gpxdir = '/nonexistent' self.record = Record(SportService(self.ddbb), parent=self.main) time = 7426 distance = 46.18 speed = distance * 3600 / time time_hhmmss = [time // 3600, (time / 60) % 60, time % 60] self.summary = {} self.summary['rcd_gpxfile'] = u'/nonexistent' self.summary['rcd_sport'] = u"Run" self.summary['rcd_date'] = u'2016-07-24' self.summary['rcd_calories'] = 1462 self.summary['rcd_comments'] = u'test comment' self.summary['rcd_title'] = u'test 1' self.summary['rcd_time'] = time_hhmmss self.summary['rcd_distance'] = "%0.2f" %distance self.summary['rcd_pace'] = "%d.%02d" % ((3600 / speed) / 60, (3600 / speed) % 60) self.summary['rcd_maxpace'] = "%d.%02d" %((3600 / 72) / 60, (3600 / 72)% 60) self.summary['rcd_average'] = speed self.summary['rcd_maxvel'] = 72 self.summary['rcd_beats'] = 115.0 self.summary['rcd_maxbeats'] = 120 self.summary['rcd_upositive'] = 553.1 self.summary['rcd_unegative'] = 564.1 self.summary['date_time_local'] = u'2016-07-24 12:58:23+03:00' self.summary['date_time_utc'] = u'2016-07-24T09:58:23Z' self.laps = [{'distance': 46181.9, 'lap_number': 0, 'calories': 1462, 'elapsed_time': u'7426.0', 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'laptrigger': u'manual'}] def tearDown(self): self.ddbb.disconnect() self.ddbb.drop_tables() def test_insert_record(self): newid = self.record.insertRecord(self.summary, laps=self.laps) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.unegative, 564.1) self.assertEqual(activity.upositive, 553.1) self.assertEqual(activity.beats, 115.0) self.assertEqual(activity.maxbeats, 120) self.assertEqual(activity.date_time, datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800))) self.assertEqual(activity.date_time_utc, u'2016-07-24T09:58:23Z') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Run")) self.assertEqual(activity.title, u'test 1') self.assertEqual(activity.laps[0], {'distance': 46181.9, 'end_lon': None, 'lap_number': 0, 'start_lon': None, 'id_lap': 1, 'calories': 1462, 'comments': None, 'laptrigger': u'manual', 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'end_lat': None, 'start_lat': None, 'max_speed': None}) def test_insert_record_datetime(self): """Importing multiple activities uses a datetime object for list_options['date_time_local'], also test that code path""" self.summary['date_time_local'] = datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800)) newid = self.record.insertRecord(self.summary, laps=self.laps) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.unegative, 564.1) self.assertEqual(activity.upositive, 553.1) self.assertEqual(activity.beats, 115.0) self.assertEqual(activity.maxbeats, 120) self.assertEqual(activity.date_time, datetime(2016, 7, 24, 12, 58, 23, tzinfo=tzoffset(None, 10800))) self.assertEqual(activity.date_time_utc, u'2016-07-24T09:58:23Z') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Run")) self.assertEqual(activity.title, u'test 1') self.assertEqual(activity.laps[0], {'distance': 46181.9, 'end_lon': None, 'lap_number': 0, 'start_lon': None, 'id_lap': 1, 'calories': 1462, 'comments': None, 'laptrigger': u'manual', 'elapsed_time': u'7426.0', 'record': 1, 'intensity': u'active', 'avg_hr': 136, 'max_hr': 173, 'end_lat': None, 'start_lat': None, 'max_speed': None}) def test_update_record(self): newid = self.record.insertRecord(self.summary) update_dict = self.summary.copy() update_dict['rcd_title'] = u'test 2' update_dict['rcd_sport'] = u"Bike" self.record.updateRecord(update_dict, newid) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.title, u'test 2') self.assertEqual(activity.sport, self.record._sport_service.get_sport_by_name(u"Bike")) def test_get_day_list(self): self.record.insertRecord(self.summary) daylist = list(self.record.getRecordDayList(datetime(2016, 7, 24, 9, 58, 23))) self.assertEqual(daylist, [24]) def test_getLastRecordDateString(self): self.record.insertRecord(self.summary) self.assertEqual(self.record.getLastRecordDateString(), self.summary['rcd_date']) def test_record_midnight_date_bug(self): self.summary['date_time_local'] = u'2016-07-24 0:00:00+03:00' self.summary['date_time_utc'] = u'2016-07-23T21:00:00Z' newid = self.record.insertRecord(self.summary) activity = self.main.activitypool.get_activity(newid) self.assertEqual(activity.date, date(2016, 7, 24))