def test_name_should_not_accept_non_unicode_string(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Not supported on Mysql 5.6') 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_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_id_should_not_accept_non_integer_string(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Not supported on Mysql 5.6') 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_weight_should_not_accept_non_float_string(self): if self.ddbb.engine.name == 'mysql': self.skipTest('Not supported on Mysql 5.6') sport = Sport() sport.weight = "22.5kg" try: self.ddbb.session.add(sport) self.ddbb.session.flush() except StatementError: pass else: self.fail()
def test_store_sport_should_update_row_when_sport_has_id(self): def mock_select(table, columns, where): if columns == "id_sports": return [[1]] else: return [(1, u"", 0, 0, 0, "0")] self.mock_ddbb.select = mock.Mock(wraps=mock_select) sport = Sport() sport.id = 1 sport.name = u"New name" self.sport_service.store_sport(sport) self.mock_ddbb.update.assert_called_with("sports", "name,weight,met,max_pace,color", [u"New name", 0.0, None, None, "0000ff"], "id_sports=1")
def create_tables(self, add_default=True): """Initialise the database schema from an empty database.""" logging.info("Creating database tables") from pytrainer.core.sport import Sport from pytrainer.core.equipment import Equipment from pytrainer.waypoint import Waypoint from pytrainer.core.activity import Lap from pytrainer.athlete import Athletestat DeclarativeBase.metadata.create_all(self.engine) if add_default: for item in [ Sport(name=u"Mountain Bike", weight=0.0, color=color_from_hex_string("0000ff")), Sport(name=u"Bike", weight=0.0, color=color_from_hex_string("00ff00")), Sport(name=u"Run", weight=0.0, color=color_from_hex_string("ffff00")) ]: self.session.add(item) self.session.commit()
def test_store_sport_should_return_stored_sport(self): sport_ids = [] def update_sport_ids(*args): sport_ids.append([1]) self.mock_ddbb.insert.side_effect = update_sport_ids def mock_select(table, columns, where): if columns == "id_sports": return sport_ids else: return [(2, u"", 0, 0, 0, "0")] self.mock_ddbb.select = mock.Mock(wraps=mock_select) sport = Sport() stored_sport = self.sport_service.store_sport(sport) self.assertEquals(2, stored_sport.id)
def test_store_sport_should_error_when_existing_sport_has_duplicate_name(self): def mock_select(table, columns, where): if columns == pytrainer.core.sport._ID_COLUMN: return [[2]] else: return [(1, u"Test name", 0, 0.0, "0"), (2, u"New name", 0, 0.0, "0")] self.mock_ddbb.select = mock.Mock(wraps=mock_select) sport = Sport() sport.id = 1 sport.name = u"New name" try: self.sport_service.store_sport(sport) except(SportServiceException): pass else: self.fail()
def getSportId(self, sport_name, add=None): """Deprecated: use sport_service.get_sport_by_name() Get the id of a sport by name, optionally adding a new sport if none exists with the given name. arguments: sport_name: sport's name to get id for add: whether the sport should be added if not found returns: id for sport with given name or None""" if sport_name is None: return None sport = self._get_sport(sport_name) if sport is None: logging.debug("No sport with name: '%s'", str(sport_name)) if add is not None: logging.debug("Adding sport '%s'", str(sport_name)) new_sport = Sport() new_sport.name = unicode(sport_name) sport = self._sport_service.store_sport(new_sport) return None if sport is None else sport.id
def test_id_should_accept_integer(self): sport = Sport() sport.id = 1 self.assertEquals(1, sport.id)
def test_id_should_default_to_none(self): sport = Sport() self.assertEquals(None, sport.id)
def test_met_should_accept_float_string(self): sport = Sport() sport.met = "22.5" self.assertEquals(22.5, sport.met)
def test_met_should_default_to_None(self): sport = Sport() self.assertEquals(None, sport.met)
def test_name_should_default_to_empty_string(self): sport = Sport() self.assertEquals(u"", sport.name)
def test_max_pace_should_take_floor_of_float(self): sport = Sport() sport.max_pace = 220.6 self.assertEquals(220, sport.max_pace)
def test_max_pace_should_accept_integer_string(self): sport = Sport() sport.max_pace = "220" self.assertEquals(220, sport.max_pace)
def test_max_pace_should_accept_integer(self): sport = Sport() sport.max_pace = 220 self.assertEquals(220, sport.max_pace)
def test_max_pace_should_default_to_none(self): sport = Sport() self.assertEquals(None, sport.max_pace)
def test_weight_should_accept_float(self): sport = Sport() sport.weight = 22.5 self.assertEquals(22.5, sport.weight)
def test_weight_should_default_to_zero(self): sport = Sport() self.assertEquals(0, sport.weight)
def test_met_should_accept_none(self): sport = Sport() sport.met = None self.assertEquals(None, sport.met)
def test_id_should_accept_integer_string(self): sport = Sport() sport.id = "1" self.assertEquals(1, sport.id)
def test_remove_sport_should_delete_sport_with_specified_id(self): self.mock_ddbb.select.return_value = [[1]] sport = Sport() sport.id = 1 self.sport_service.remove_sport(sport) self.mock_ddbb.delete.assert_called_with("sports", "id_sports=1")
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_name_should_accept_unicode_string(self): sport = Sport() sport.name = u"Unicycling" self.assertEquals(u"Unicycling", sport.name)
def test_color_should_default_to_blue(self): sport = Sport() self.assertEquals(0x0000ff, sport.color.rgb_val)
def test_met_should_accept_float(self): sport = Sport() sport.met = 22.5 self.assertEquals(22.5, sport.met)
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_max_pace_should_accept_none(self): sport = Sport() sport.max_pace = None self.assertEquals(None, sport.max_pace)
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)