def test_delete_deletes(converter: PlanetConverter, sql_connection): planet = MockPlanet() converter.delete(planet) queries = sql_connection.get_queries() delete_query = queries[0] expected_query = f"DELETE FROM planets WHERE planet_id = {planet.ID}" assert strip_whitespace(expected_query) == strip_whitespace(delete_query)
def test_add_addsCorrectEntry(add_args, add_values, converter: PlanetConverter, sql_connection, monkeypatch): sql_connection.set_execute_read_results([False, [("deserialized")]]) with monkeypatch.context() as m: m.setattr(Planet, "deserialize", lambda n: n) returned = converter.add_or_update(name="mockplanet", coords=add_args[0], tl=None, bio=None, atmos=None, temp=None, pop=None, desc=add_args[1], notes=None) find_query = "SELECT planet_id FROM planets WHERE name='mockplanet'" add_query = """INSERT INTO planets ( name, coordinate, tl, biosphere, atmosphere, temperature, population, description, notes) VALUES """ + add_values + "SELECT * FROM planets WHERE name='mockplanet'" queries = sql_connection.get_queries() assert strip_whitespace(find_query) == strip_whitespace(queries[0]) assert strip_whitespace(add_query) == strip_whitespace(queries[1]) assert returned == "deserialized"
def test_load_by_name_returnsNoneWhenNoResult(converter: PlanetConverter, sql_connection, planet): sql_connection.set_execute_read_results([[]]) returned = converter.load_by_name(planet.name) expected_query = f"SELECT * FROM planets WHERE name = '{planet.name}'" queries = sql_connection.get_queries() load_query = queries[0] assert returned is None assert load_query == expected_query
def test_check_exists_returnsFalseIfNoIdFound(converter: PlanetConverter, sql_connection): sql_connection.set_execute_read_results([None]) name = "test planet" expected_query = "SELECT planet_id FROM planets WHERE name='test planet'" result = converter.check_exists(name) find_query = sql_connection.get_queries()[0] assert not result assert strip_whitespace(find_query) == strip_whitespace(expected_query)
def __init__(self, sql_link, player_character_id, *args, **kwargs): super().__init__(*args, **kwargs) self.sql_link = sql_link self.char_factory = CharacterFactory self.char_converter = CharacterConverter(sql_link, self.char_factory) self.character_id = player_character_id self.character = None self.planet_converter = PlanetConverter(sql_link) self.planet_log = PlanetLog(self.sql_link, self.planet_converter) self.phonebook = Phonebook(self.sql_link, self.char_converter, self.planet_converter)
def test_get_planet_name_getsCorrectName(converter: PlanetConverter, sql_connection, planet: Planet, monkeypatch): sql_connection.set_execute_read_results([[(planet.name, )]]) with monkeypatch.context() as m: m.setattr(Planet, "deserialize", lambda n: n) returned = converter.get_planet_name(planet.ID) expected_query = f"SELECT name FROM planets WHERE planet_id = {planet.ID}" queries = sql_connection.get_queries() load_query = queries[0] assert returned == planet.name assert load_query == expected_query
def __init__(self, sql_link, *args, **kwargs): super().__init__(*args, **kwargs) self.link = sql_link self.char_factory = CharacterFactory self.op_factory = OpinionFactory self.plan_converter = PlanetConverter(sql_link) self.op_converter = OpinionConverter(link=sql_link, opinion_factory=self.op_factory) self.char_converter = CharacterConverter(sql_link, self.char_factory) self.editor = CharacterEditor(sql_link=sql_link, char_converter=self.char_converter, plan_converter=self.plan_converter, op_converter=self.op_converter) self.link.connect()
def test_load_all_loadsAndSorts(converter: PlanetConverter, sql_connection, planet, monkeypatch): sql_connection.set_execute_read_results([[ planet, ]]) with monkeypatch.context() as m: m.setattr(Planet, "deserialize", lambda n: n) returned = converter.load_all(order="name") expected_query = f"SELECT * FROM planets ORDER BY name ASC" queries = sql_connection.get_queries() load_query = queries[0] assert returned == [planet] assert load_query == expected_query
def test_load_by_name_loads(converter: PlanetConverter, sql_connection, planet, monkeypatch): sql_connection.set_execute_read_results([[ planet, ]]) with monkeypatch.context() as m: m.setattr(Planet, "deserialize", lambda n: n) returned = converter.load_by_name(planet.name) expected_query = f"SELECT * FROM planets WHERE name = '{planet.name}'" queries = sql_connection.get_queries() load_query = queries[0] assert returned == planet assert load_query == expected_query
def test_update_planet_executesCorrectQuery(converter: PlanetConverter, sql_connection, planet, monkeypatch): sql_connection.set_execute_read_results([[ "deserialized", ]]) update_query = """UPDATE planets SET description='desc' WHERE name = 'mockplanet'; SELECT * FROM planets WHERE name='mockplanet'""" planet.description = "desc" with monkeypatch.context() as m: m.setattr(Planet, "deserialize", lambda n: n) returned = converter.update_planet(planet) queries = sql_connection.get_queries() assert strip_whitespace(update_query) == strip_whitespace(queries[0]) assert returned == "deserialized"
def initialize_database(link: SQLDatabaseLink): link.connect() link.execute_query(PlanetConverter(link=None).create_table_query) link.execute_query(ConnectionConverter(link=None).create_table_query) link.execute_query(CharacterConverter.create_table_query) link.execute_query(OpinionConverter.create_table_query)
def __init__(self, link): self.planet = PlanetConverter(link) self.connection = ConnectionConverter(link)
def converter(sql_connection): return PlanetConverter(sql_connection)
def test_update_returnsNoneIfFailsToUpdate(converter: PlanetConverter, sql_connection): sql_connection.set_execute_read_results([True, None]) returned = converter.add_or_update("mockplanet", desc="description") assert returned is None
def test_update_returnsNoneIfNoValuesToUpdate(converter: PlanetConverter): returned = converter.update("mockplanet") assert returned is None
def test_add_returnsNoneIfFailsToAdd(converter: PlanetConverter, sql_connection): sql_connection.set_execute_read_results([False, None]) returned = converter.add_or_update("mockplanet") assert returned is None