Skip to content

Commit

Permalink
fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
new-village committed Dec 7, 2024
1 parent b484931 commit ebe0c6e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
41 changes: 26 additions & 15 deletions keibascraper/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,34 @@ def load_config(data_type):


def create_table_sql(data_type=None):
""" The function generate create table SQL strings based on SQLite3 by config file.
:param data_type: Data Type is identifier of data types such as ENTRY, ODDS, RACE and RESULT.
"""
Generates a CREATE TABLE SQL statement for SQLite3 based on the configuration file.
:param data_type: A string that identifies the data type such as ENTRY, ODDS, RACE, RESULT, etc.
:return: A string containing the CREATE TABLE SQL statement.
"""
# Validating Arguments
# Validate arguments
if data_type is None:
raise SystemExit("There is no race_id in HTML")

# load config file
keys = [key["col_name"] for key in load_config(data_type)["columns"]]
types = [tp["var_type"] for tp in load_config(data_type)["columns"]]
# Create comma separated strings
cols = [k + ' ' + v for k, v in zip(keys, types)]
# Add PRIMARY KEY string to first column
cols[0] = cols[0] + " PRIMARY KEY"
cols = ", ".join(cols)

return f"CREATE TABLE IF NOT EXISTS {data_type} ({cols});"
raise SystemExit("Data type is not specified.")

# Load configuration file
columns = load_config(data_type)["columns"]
keys = [column["col_name"] for column in columns]
types = [column["var_type"] for column in columns]

# Create column definitions
cols = []
for k, v in zip(keys, types):
col_def = f"{k} {v}"
# Add "PRIMARY KEY" string if the column name is "id"
if k.lower() == "id":
col_def += " PRIMARY KEY"
cols.append(col_def)

cols_str = ", ".join(cols)

return f"CREATE TABLE IF NOT EXISTS {data_type} ({cols_str});"


def create_index_sql(data_type=None):
""" The function generate create index SQL strings based on SQLite3 by config file.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='keibascraper',
version='3.1.1',
version='3.1.2',
author='new-village',
url='https://github.com/new-village/KeibaScraper',
description='keibascraper is a simple scraping library for netkeiba.com',
Expand Down
9 changes: 5 additions & 4 deletions test/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def test_create_table_sql(self, mock_load_config):
# モックされたJSONデータ
mock_load_config.return_value = {
"columns": [
{"col_name": "race_id", "var_type": "text"},
{"col_name": "bracket", "var_type": "integer"},
{"col_name": "horse_number", "var_type": "integer"},
{"col_name": "horse_name", "var_type": "text"}
{"col_name": "horse_name", "var_type": "text"},
{"col_name": "id", "var_type": "text"},

]
}

Expand All @@ -23,10 +24,10 @@ def test_create_table_sql(self, mock_load_config):
# 期待されるSQL
expected_sql = (
"CREATE TABLE IF NOT EXISTS entry ("
"race_id text PRIMARY KEY, "
"bracket integer, "
"horse_number integer, "
"horse_name text);"
"horse_name text, "
"id text PRIMARY KEY);"
)

# 検証
Expand Down

0 comments on commit ebe0c6e

Please sign in to comment.