Skip to content

Commit fbd0302

Browse files
committed
tests added. parser coded
1 parent 0e8fa9f commit fbd0302

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

src/rowgen/hf_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def prompt_fake_data(self, db_schema: str, num_rows: int = 10) -> str:
3030
:return: str. the response of ai api.
3131
"""
3232
prompt = f"""
33-
Generate {num_rows} rows of fake sql data for the following table:column schema. Make the data look believable and realistic (not john doe or anything like that).
33+
Generate {num_rows} rows of fake sql data for the following table:column schema. Make the data look believable and realistic (not john doe or anything like that). make sure there's consistency between tables (such as foregin keys and so on).
3434
Your response should be only in plain sql insert statements with no words before or after. table:column schema: {db_schema}
3535
"""
3636
return self.send_message_to_api(prompt)

src/rowgen/main.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import argparse
55

66
from rowgen.extract_from_db import DBconnect
7-
from rowgen.parser import parse_sql_from_code_block
7+
from rowgen.sql_parser import parse_sql_from_code_block
8+
import os
89

910

1011
def main():
@@ -25,16 +26,21 @@ def main():
2526
action="store_true",
2627
help="Executes insert statements into the database directly.",
2728
)
28-
29+
parser.add_argument("--apikey", help="Enter your huggingface_hub api key.")
2930
args = parser.parse_args()
3031

32+
if args.apikey:
33+
api_key = args.apikey
34+
else:
35+
api_key = get_api_key_from_config()
36+
3137
if args.db_url:
3238
db_url = args.db_url
3339
else:
3440
db_url = "{args.db_type}://{args.user}:{args.password}@{args.host}:{args.port}/{args.database}"
3541

3642
dbc = DBconnect(db_url)
37-
hf = HFapi()
43+
hf = HFapi(api_key=api_key)
3844
ai_sql_response = hf.prompt_fake_data(dbc.table_columns, 20)
3945
sql_statements = parse_sql_from_code_block(ai_sql_response)
4046
# execute
@@ -54,5 +60,46 @@ def main():
5460
print("Saved to sql file.")
5561

5662

63+
def get_api_key_from_config() -> str:
64+
"""
65+
Retrieves API key from the config file (~/.config/rowgen/conf).
66+
If no key is stored, prompts the user for input and saves it.
67+
68+
:return: str. The API key.
69+
"""
70+
config_path = os.path.expanduser("~/.config/rowgen/conf")
71+
apikey = None
72+
73+
try:
74+
with open(config_path, "r") as file:
75+
for line in file:
76+
if line.startswith("apikey:"):
77+
apikey = line.split(":", 1)[1].strip()
78+
break # no need to keep reading
79+
80+
except FileNotFoundError:
81+
pass # we'll prompt the user below
82+
except PermissionError:
83+
print("You do not have permission to read the config file.")
84+
return ""
85+
except Exception as e:
86+
print(f"An unexpected error occurred while reading: {e}")
87+
return ""
88+
89+
if not apikey:
90+
# Prompt the user
91+
apikey = input("Enter your API key: ").strip()
92+
93+
try:
94+
os.makedirs(os.path.dirname(config_path), exist_ok=True)
95+
with open(config_path, "w") as file:
96+
file.write(f"apikey: {apikey}\n")
97+
print(f"API key was store in the {config_path}")
98+
except Exception as e:
99+
print(f"Failed to save API key: {e}")
100+
101+
return apikey
102+
103+
57104
if __name__ == "__main__":
58105
main()

src/rowgen/testrowgendb.sqlite

24 KB
Binary file not shown.

tests/test_ai_api.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,12 @@ def test_hf_hub_ping(hf_api_client):
3939
pytest.fail(f"Failed to ping Hugging Face Hub: {e}")
4040

4141

42-
def test_hf_inference_connection(inference_client):
42+
def test_hf_api_send_message_to_api(hf_api):
4343
prompt_message = "Hi! This is a test. say banana if you can hear me."
4444
try:
45-
completion = inference_client.chat.completions.create(
46-
model="deepseek-ai/DeepSeek-V3",
47-
messages=[{"role": "user", "content": prompt_message}],
48-
)
49-
message: str = completion.choices[0].message["content"]
50-
assert len(message) > 0
51-
assert "banana" in message.casefold()
45+
response_message = hf_api.send_message_to_api(prompt_message)
46+
assert len(response_message) > 0
47+
assert "banana" in response_message.casefold()
5248

5349
except BadRequestError as e:
5450
print(e)
@@ -57,12 +53,13 @@ def test_hf_inference_connection(inference_client):
5753
pytest.fail(f"Failed inference call: {d}")
5854

5955

60-
def test_hf_api_send_message_to_api(hf_api):
61-
prompt_message = "Hi! This is a test. say banana if you can hear me."
56+
def test_hf_sql_message(hf_api):
57+
prompt_message = "Write five sql statements for the table Books made up of id, title, isbn, genre, publication_date, author. Your message should only consist of sql statements with no sentence before and after."
6258
try:
6359
response_message = hf_api.send_message_to_api(prompt_message)
6460
assert len(response_message) > 0
65-
assert "banana" in response_message.casefold()
61+
assert response_message.startswith("```sql")
62+
assert response_message.endswith("```")
6663

6764
except BadRequestError as e:
6865
print(e)

tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from rowgen.parser import parse_sql_from_code_block
2+
from rowgen.sql_parser import parse_sql_from_code_block
33
from rowgen.hf_api import HFapi
44
from pytest import fixture
55

0 commit comments

Comments
 (0)