예제 #1
0
    def put(self, people_id):

        try:

            json_request_body = json.loads(self.request.body)

            validate_json_schema(json_request_body, schemas.people_json_schema)

            put_people = people.People(json_request_body)

            if (self.database.get_people_by_id(put_people.get_id())):

                self.database.update_people(put_people)
                self.json_response({
                    dictionary.message:
                    dictionary.people_updated.format(put_people.get_id())
                })
            else:
                raise error.ClientError(
                    404,
                    dictionary.people_not_exists.format(put_people.get_id()))

        except error.ServerError as server_error:
            self.json_error(server_error.to_json(), server_error.status)

        except error.ClientError as client_error:
            self.json_error(client_error.to_json(), client_error.status)
예제 #2
0
def test_people():

    assert handlers.validate_json_schema(parameters.json_r2d2,
                                         schemas.people_json_schema) is None
    assert handlers.validate_json_schema(parameters.json_padme,
                                         schemas.people_json_schema) is None

    people_for_test = people.People(parameters.json_r2d2)

    assert people_for_test.get_name() == parameters.json_r2d2[dictionary.name]
    assert people_for_test.get_height() == parameters.json_r2d2[
        dictionary.height]
    assert people_for_test.get_mass() == parameters.json_r2d2[dictionary.mass]
    assert people_for_test.get_hair_color() == parameters.json_r2d2[
        dictionary.hair_color]
    assert people_for_test.get_skin_color() == parameters.json_r2d2[
        dictionary.skin_color]
    assert people_for_test.get_eye_color() == parameters.json_r2d2[
        dictionary.eye_color]
    assert people_for_test.get_birth_year() == parameters.json_r2d2[
        dictionary.birth_year]
    assert people_for_test.get_gender() == parameters.json_r2d2[
        dictionary.gender]
    assert people_for_test.get_homeworld() == parameters.json_r2d2[
        dictionary.homeworld]
    assert people_for_test.get_id() == parameters.json_r2d2[
        dictionary.people_id]

    assert people_for_test.get_films() == parameters.json_r2d2[
        dictionary.films]
    assert people_for_test.get_starships() == parameters.json_r2d2[
        dictionary.starships]
    assert people_for_test.get_species() == parameters.json_r2d2[
        dictionary.species]
    assert people_for_test.get_vehicles() == parameters.json_r2d2[
        dictionary.vehicles]

    people_for_test.add_film("8")
    people_for_test.add_species("4")
    people_for_test.add_starship("5")
    people_for_test.add_vehicle("6")

    parameters.json_r2d2[dictionary.films].append("8")
    parameters.json_r2d2[dictionary.starships].append("5")
    parameters.json_r2d2[dictionary.species].append("4")
    parameters.json_r2d2[dictionary.vehicles].append("6")

    assert people_for_test.get_starships() == parameters.json_r2d2[
        dictionary.starships]
    assert people_for_test.get_films() == parameters.json_r2d2[
        dictionary.films]
    assert people_for_test.get_species() == parameters.json_r2d2[
        dictionary.species]
    assert people_for_test.get_vehicles() == parameters.json_r2d2[
        dictionary.vehicles]

    assert handlers.validate_json_schema(people_for_test.to_json(),
                                         schemas.people_json_schema) is None
예제 #3
0
    def get(self, people_id):

        try:

            if (people_id):

                people_get = self.database.get_people_by_id(people_id)

                if (people_get):
                    self.json_response(
                        self.fetch_people_extra_informations_by_id(
                            people.People(people_get)).to_json())
                else:
                    raise error.ClientError(
                        404, dictionary.people_not_exists.format(people_id))
            else:

                peoples = self.database.get_peoples()

                peoples_result = []

                [
                    peoples_result.append(
                        self.fetch_people_extra_informations_by_id(
                            people.People(
                                self.database.get_people_by_id(people_get[
                                    dictionary.people_id]))).to_json())
                    for people_get in peoples
                ]
                if (len(peoples_result) == 0):
                    raise error.ClientError(404,
                                            dictionary.no_people_in_database)
                else:
                    self.json_response(json.dumps(peoples_result))

        except error.ServerError as server_error:
            self.json_error(server_error.to_json(), server_error.status)

        except error.ClientError as client_error:
            self.json_error(client_error.to_json(), client_error.status)
def test_database_queries():

    database_for_test = database.Database(parameters.temporary_database)

    assert database_for_test.connection.execute(parameters.create_table_people)

    assert database_for_test.connection.execute(
        parameters.create_table_films_people)
    assert database_for_test.connection.execute(
        parameters.create_table_people_species)
    assert database_for_test.connection.execute(
        parameters.create_table_people_starships)
    assert database_for_test.connection.execute(
        parameters.create_table_people_vehicles)

    assert len(database_for_test.get_peoples()) == 0

    r2d2 = people.People(parameters.json_r2d2)
    padme = people.People(parameters.json_padme)

    assert database_for_test.add_people(r2d2) is None
    assert database_for_test.add_people(padme) is None

    assert len(database_for_test.get_peoples()) == 2
    assert not (database_for_test.get_people_by_id(padme.get_id()) is None)

    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_id() == padme.get_id()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_height() == padme.get_height()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_mass() == padme.get_mass()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_hair_color() == padme.get_hair_color()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_skin_color() == padme.get_skin_color()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_eye_color() == padme.get_eye_color()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_birth_year() == padme.get_birth_year()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_gender() == padme.get_gender()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_homeworld() == padme.get_homeworld()
    assert people.People(database_for_test.get_people_by_id(
        padme.get_id())).get_name() == padme.get_name()

    assert len(database_for_test.get_people_films_by_people_id(
        r2d2.get_id())) == len(r2d2.get_films())
    assert len(
        database_for_test.get_people_starships_by_people_id(
            r2d2.get_id())) == len(r2d2.get_starships())
    assert len(database_for_test.get_people_species_by_people_id(
        r2d2.get_id())) == len(r2d2.get_species())
    assert len(
        database_for_test.get_people_vehicles_by_people_id(
            r2d2.get_id())) == len(r2d2.get_vehicles())

    r2d2.add_film("8")
    r2d2.add_species("4")
    r2d2.add_starship("5")
    r2d2.add_vehicle("6")

    assert database_for_test.update_people(r2d2) is None

    assert len(database_for_test.get_people_films_by_people_id(
        r2d2.get_id())) == len(r2d2.get_films())
    assert len(
        database_for_test.get_people_starships_by_people_id(
            r2d2.get_id())) == len(r2d2.get_starships())
    assert len(database_for_test.get_people_species_by_people_id(
        r2d2.get_id())) == len(r2d2.get_species())
    assert len(
        database_for_test.get_people_vehicles_by_people_id(
            r2d2.get_id())) == len(r2d2.get_vehicles())

    assert database_for_test.delete_people_by_id(r2d2.get_id()) is None

    assert len(database_for_test.get_people_films_by_people_id(
        r2d2.get_id())) == 0
    assert len(
        database_for_test.get_people_starships_by_people_id(
            r2d2.get_id())) == 0
    assert len(database_for_test.get_people_species_by_people_id(
        r2d2.get_id())) == 0
    assert len(
        database_for_test.get_people_vehicles_by_people_id(r2d2.get_id())) == 0

    assert database_for_test.get_people_by_id(r2d2.get_id()) is None
    assert len(database_for_test.get_peoples()) == 1

    database_for_test.end_connection()