Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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 äö')
Ejemplo n.º 4
0
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')
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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 äö')
Ejemplo n.º 8
0
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.")
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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))
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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()
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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()
Ejemplo n.º 15
0
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))