コード例 #1
0
 def test_get_films(self):
     with contextlib.closing(db_utils.open_db(True).cursor()) as cursor:
         insert_data = TestDBUtils.helper__insert_characters(cursor)
         films = db_utils.get_films(cursor)
         for row, film in zip(insert_data, films):
             self.assertIn(row[0], film.values())
             self.assertIn(row[1], film.values())
コード例 #2
0
    def test_add_characters_films(self):
        char_url = utils.generate_url("people")
        char_insert_data = [("test character 1", char_url(1)),
                            ("test character 2", char_url(2)),
                            ("test character 3", char_url(3))]
        film_url = utils.generate_url("films")
        film_insert_data = [("test film 1", film_url(1)),
                            ("test film 2", film_url(2)),
                            ("test film 3", film_url(3))]
        characters_films_insert_data = [(character[0], character[1],
                                         film_insert_data)
                                        for character in char_insert_data]
        with contextlib.closing(db_utils.open_db(True)) as conn:
            task_one.add_characters_films(conn, characters_films_insert_data,
                                          char_insert_data, film_insert_data)
            with contextlib.closing(conn.cursor()) as cursor:
                for row in char_insert_data:
                    character = db_utils.get_character(cursor, row[0])
                    self.assertEqual(character.get("name"), row[0])
                    self.assertEqual(character.get("endpoint"), row[1])

                for row in film_insert_data:
                    film = db_utils.get_film(cursor, row[0])
                    self.assertEqual(film.get("title"), row[0])
                    self.assertEqual(film.get("endpoint"), row[1])
コード例 #3
0
def main():
    """entrypoint to the program"""
    # building list of unique character-film combinations
    characters_films = utils.remove_none(
        [get_characters_films(random.randint(1, 87)) for _ in range(1, 15)])
    # building list of unique characters
    characters = utils.remove_none(
        list(set([character_film[:2] for character_film in characters_films])))
    # building list of unique films
    films = utils.remove_none(
        list(
            set([
                film for character_film in characters_films
                for film in character_film[2]
            ])))

    with contextlib.closing(db_utils.open_db(True)) as connection:
        # inserting data into database
        add_characters_films(connection, characters_films, characters, films)
        # retrieving the output
        output = get_output(connection)

    json_output = utils.get_json(output)
    print(json_output)
    return json_output
コード例 #4
0
ファイル: create_db.py プロジェクト: jhwinter/star-wars-etl
def main():
    """entrypoint of program"""
    with contextlib.closing(db_utils.open_db()) as conn:
        drop_database(conn)

        create_database(conn)
        conn.select_db(db_utils.DB_NAME)

        create_tables(conn)

    return True
コード例 #5
0
    def test_get_output(self):
        self.test_add_characters_films()
        with contextlib.closing(db_utils.open_db(True)) as conn:
            output = task_one.get_output(conn)

        self.assertIsInstance(output, list)
        for row in output:
            self.assertIsInstance(row, dict)
            self.assertIn("film", row)
            self.assertIn("character", row)
            self.assertIsInstance(row["film"], str)
            self.assertIsInstance(row["character"], list)
コード例 #6
0
 def test_get_film_join_character(self):
     with contextlib.closing(db_utils.open_db(True).cursor()) as cursor:
         insert_data = [(1, 10), (2, 20), (3, 10)]
         db_utils.insert_character_film(cursor, insert_data)
         film_chars = db_utils.get_film_join_character(
             cursor, insert_data[0][1])
         for row, film_char in zip(insert_data, film_chars):
             if row[1] == 10:
                 self.assertIn(row[0], film_char.values())
                 self.assertIn(row[1], film_char.values())
             else:
                 self.assertNotIn(row[0], film_char.values())
                 self.assertNotIn(row[1], film_char.values())
コード例 #7
0
 def test_insert_character_film(self):
     with contextlib.closing(db_utils.open_db(True).cursor()) as cursor:
         insert_data = [(1, 10), (2, 20), (1, 30)]
         db_utils.insert_character_film(cursor, insert_data)
         char_films = db_utils.get_character_join_film(
             cursor, insert_data[0][0])
         for row, char_film in zip(insert_data, char_films):
             if row[0] == 1:
                 self.assertIn(row[0], char_film.values())
                 self.assertIn(row[1], char_film.values())
             else:
                 self.assertNotIn(row[0], char_film.values())
                 self.assertNotIn(row[1], char_film.values())
コード例 #8
0
 def test_get_film(self):
     with contextlib.closing(db_utils.open_db(True).cursor()) as cursor:
         insert_data = TestDBUtils.helper__insert_characters(cursor)
         film = db_utils.get_character(cursor, insert_data[0][0])
         self.assertIn(insert_data[0][0], film.values())
         self.assertIn(insert_data[0][1], film.values())
コード例 #9
0
 def test_open_db(self):
     self.assertEqual(db_utils.open_db().open, True)
コード例 #10
0
 def test_main(self):
     create_db.main()
     with contextlib.closing(db_utils.open_db()) as conn:
         self.helper__database_tables_created(conn, 1)
コード例 #11
0
 def test_create_tables(self):
     with contextlib.closing(db_utils.open_db()) as conn:
         create_db.create_database(conn)
         conn.select_db(db_utils.DB_NAME)
         create_db.create_tables(conn)
         self.helper__tables_created(conn, 1)
コード例 #12
0
 def test_create_database(self):
     with contextlib.closing(db_utils.open_db()) as conn:
         create_db.create_database(conn)
         self.helper__database_exists(conn, 1)