class TestUnitPlanetModel(TestCase): def create_app(self): app.config["TESTING"] = True self.planet = Planet("Test Planet", ["tropical"], ["jungle"]) self.uid = str(uuid.uuid4()) self.planet._id = self.uid return app def test_planet_model_init(self): self.assertEqual(self.planet.name, "Test Planet") self.assertEqual(self.planet.climate, ["tropical"]) self.assertEqual(self.planet.terrain, ["jungle"]) def test_planet_model_to_dict(self): dict_planet = { "_id": self.uid, "name": "Test Planet", "climate": ["tropical"], "terrain": ["jungle"], "apparitions": None, "population": None, } planet_dict = self.planet.to_dict() self.assertEqual(planet_dict, dict_planet) def test_planet_model_add_outhers_infos(self): self.planet.add_outhers_infos(6, 1000) self.assertEqual(6, self.planet.apparitions) self.assertEqual(1000, self.planet.population)
def get_planet_by_name(self, name: str) -> list: query = {"name": {"$regex": name, "$options": "i"}} print(query) logging.info("[GET PLANETS BY NAME]: query %s" % query) planets = self.mongo_client.planets_collection.find(query) planets = list(planets) return [Planet.from_dict(planet) for planet in planets]
def setUp(self): self.planet_repo = PlanetRepository() self.planet_expected = Planet( _id="80884966-d276-4066-9f6c-718ce2ffc11e", name="Yavin IV", climate=["temperate", "tropical"], terrain=["jungle", "rainforests"], apparitions=None, population=None, )
def build_insert_planet(planet: dict): logging.info("[BUILDER_INSERT_PLANET]: Insert Planet") if "name" in planet and "climate" in planet and "terrain" in planet: if type(planet["climate"]) == list and type(planet["terrain"]) == list: logging.info("[BUILDER_INSERT_PLANET]: Planet Valid") planet = Planet.from_dict(planet) inserted_id = PlanetRepository().insert(planet.to_dict()) logging.info("[BUILDER_INSERT_PLANET]: Planet %s inserted" % inserted_id) message = "Planeta criado com sucesso id: %s" % inserted_id resp = {"message": message} return resp logging.info("[BUILDER_INSERT_PLANET]: Planet missing required params") return 400
def get_planet_list(self) -> list: planets = self.mongo_client.planets_collection.find() return [Planet.from_dict(planet) for planet in planets]
def get_planet_by_id(self, id: str) -> Planet: adict = self.mongo_client.planets_collection.find_one({"_id": id}) if adict is not None: planet = Planet.from_dict(adict) return planet
def create_app(self): app.config["TESTING"] = True self.planet = Planet("Test Planet", ["tropical"], ["jungle"]) self.uid = str(uuid.uuid4()) self.planet._id = self.uid return app
from unittest.mock import patch from app import app from flask_testing import TestCase from galaxy.domain.planet import Planet from galaxy.repository.planet_repository import PlanetRepository planet1 = Planet("Test Planet", ["tropical", "ice"], ["jungle"], _id="1234ABC") planet2 = Planet("Test Planet 1", ["tropical", "arid"], ["ocean"]) planet3 = Planet("Test Planet 2", ["arid"], ["temperate"]) planet4 = Planet("Test Planet 3", ["hell"], ["rainforests"]) planet5 = Planet( _id="80884966-d276-4066-9f6c-718ce2ffc11e", name="Yavin IV", climate=["temperate", "tropical"], terrain=["jungle", "rainforests"], apparitions=None, population=None, ) list_planet_expected = [planet1, planet2, planet3, planet4] planets_by_name_expec = [planet5] class TestUnitPlaneyRespository(TestCase): def create_app(self): app.config["TESTING"] = True return app def setUp(self): self.planet_repo = PlanetRepository() self.planet_expected = Planet(
"climate": "temperate, tropical", "gravity": "1 standard", "terrain": "jungle, rainforests", "surface_water": "8", "population": "1000", "residents": [], "films": ["https://swapi.co/api/films/1/"], "created": "2014-12-10T11:37:19.144000Z", "edited": "2014-12-20T20:58:18.421000Z", "url": "https://swapi.co/api/planets/3/", } planet_list_repo_mock = [ Planet( _id="80884966-d276-4066-9f6c-718ce2ffc11e", name="Yavin IV", climate=["temperate", "tropical"], terrain=["jungle", "rainforests"], apparitions=None, population=None, ), Planet( _id="80884966-d276-4066-9f6c-718ce2ffc11e", name="Yavin IV", climate=["temperate", "tropical"], terrain=["jungle", "rainforests"], apparitions=None, population=None, ), Planet( _id="80884966-d276-4066-9f6c-718ce2ffc11e", name="Yavin IV", climate=["temperate", "tropical"],