def test_get_pokemon_encounters_if_id_doesnt_exist(db, encounter): assert Pokemon.objects().count() == 0 PokemonService.add_pokemon_encounter(23, encounter) assert Pokemon.objects().count() == 1 assert Pokemon.objects().first().name == 'ekans'
def test_post_new_pokemon_which_doesnt_exist(test_client): assert Pokemon.objects().count() == 0 response = test_client.post('/api/pokemon/', json={"name": "doesnt_exist"}) assert response.status_code == 404 assert Pokemon.objects().count() == 0
def test_post_new_encounter(test_client): assert Pokemon.objects().count() == 0 test_client.post('/api/pokemon/', json={"name": "ekans"}) response = test_client.post('/pokemon/23/encounters', json={"place": "city"}) assert response.status_code == 201 assert Pokemon.objects(name='ekans').first().encounters[0]['place'] == 'city'
def test_saving_pokemon_to_db(db, ekans, hitmonlee, snorlax): assert Pokemon.objects().count() == 0 for pokemon in (ekans, hitmonlee, snorlax): Pokemon(**pokemon).save() assert Pokemon.objects().count() == 3 assert set(ekans.keys()).issubset( Pokemon.objects(name='ekans').first()._fields)
def get_all_pokemons() -> List[dict]: """ Return all Pokemons saved in the database in a required format. :return: Response object containing list of all Pokemon documents saved in the database """ return [pokemon.to_mongo().to_dict() for pokemon in Pokemon.objects()]
def test_return_all_pokemons_is_a_list(db, ekans, hitmonlee): assert Pokemon.objects().count() == 0 assert isinstance(PokemonService.get_all_pokemons(), list) Pokemon(**ekans).save() assert isinstance(PokemonService.get_all_pokemons(), list) Pokemon(**hitmonlee).save() assert isinstance(PokemonService.get_all_pokemons(), list)
def test_get_pokemon_by_id(db, snorlax): assert Pokemon.objects().count() == 0 Pokemon(**snorlax).save() mongo_pokemon = PokemonService.get_by_id(143) assert mongo_pokemon['weight'] == snorlax['weight'] assert mongo_pokemon['height'] == snorlax['height']
def test_post_new_pokemon_which_already_exists(test_client): assert Pokemon.objects().count() == 0 test_client.post('/api/pokemon/', json={"name": "ekans"}) response = test_client.post('/api/pokemon/', json={"name": "ekans"}) assert response.status_code == 200 assert {'base_experience', 'name', 'height', 'weight', 'sprites', 'id'}.issubset(json.loads(response.data))
def test_post_new_pokemon(test_client): assert Pokemon.objects().count() == 0 response = test_client.post('/api/pokemon/', json={"name": "ekans"}) assert response.status_code == 201 assert json.loads(response.data) is None pokemon_in_db = json.loads(test_client.get('/api/pokemon/').data) assert pokemon_in_db[0]['name'] == 'ekans'
def get_by_id(pokemon_id: int) -> Pokemon: """ Searching database for Pokemon with given id value. Raise NonExistingPokemon error if not found. :param pokemon_id: id of Pokemon that needs to be returned :return: Pokemon Object with given id """ pokemon = Pokemon.objects(id=pokemon_id).first() if not pokemon: raise NonExistingPokemon return pokemon
def test_return_all_pokemons_has_all_specified_fields(db, ekans, snorlax): assert Pokemon.objects().count() == 0 # '_id' field is renamed to 'id' on the API layer, just before returning to client. specified_fields = { "base_experience", "height", "_id", "name", "sprites", "weight" } specified_sprite_fields = { "back_default", "back_female", "back_shiny", "back_shiny_female", "front_default", "front_female", "front_shiny", "front_shiny_female" } for pokemon in (ekans, snorlax): Pokemon(**pokemon).save() pokemons = [pokemon.to_mongo().to_dict() for pokemon in Pokemon.objects()] assert all([ specified_fields.issubset(pokemon.keys()) and specified_sprite_fields.issubset(pokemon['sprites'].keys()) for pokemon in pokemons ])
def test_get_all_encounters(test_client): assert Pokemon.objects().count() == 0 test_client.post('api/pokemon/', json={"name": "ekans"}) test_client.post('/pokemon/23/encounters', json={"place": "city"}) response = test_client.get(r'/pokemon/23/encounters') returned_data = json.loads(response.data) assert isinstance(returned_data, list) assert len(returned_data) == 1 assert 'timestamp' in returned_data[0] assert returned_data[0]['place'] == 'city'
def test_add_pokemon_encounter(db, ekans): Pokemon(**ekans).save() encounter = { 'place': 'city', 'note': 'Ekans was waiting on the sidewalk for someone.' } PokemonService.add_pokemon_encounter(pokemon_id=23, encounter_json=encounter) mongo_encounter = Pokemon.objects(name='ekans').first().encounters[0] assert mongo_encounter.place == encounter['place'] assert mongo_encounter.note == encounter['note'] assert mongo_encounter.timestamp
def test_get_all_encounters_for_missing_id(test_client): assert Pokemon.objects().count() == 0 response = test_client.get('/pokemon/23/encounters') assert response.status_code == 404