Skip to content

Commit

Permalink
fix: enhance schema parsing and error messages, ensuring all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
atxtechbro committed Oct 19, 2024
1 parent 8f78dad commit 480ab62
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
21 changes: 9 additions & 12 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import json
import os
import re
import sys

from dotenv import load_dotenv
Expand Down Expand Up @@ -154,21 +155,17 @@ def parse_natural_language_properties(property_descriptions):
options = None
property_type = 'rich_text' # Default type

# Check for keywords to determine property type
if "date" in rest.lower():
property_type = "date"
elif "status" in rest.lower() or "select" in rest.lower():
elif any(keyword in rest.lower() for keyword in ["status", "select", "category"]):
property_type = "select"
# Here, you could implement logic to extract options from the rest
# For now, using default options
options = [
PropertyOption(name="To Do"),
PropertyOption(name="In Progress"),
PropertyOption(name="Done"),
]
elif "number" in rest.lower():
property_type = "number"
elif "title" in rest.lower():
property_type = "title"

# Detect options in parentheses
match = re.search(r'\((.*?)\)', rest)
if match:
options_str = match.group(1)
options = [PropertyOption(name=opt.strip()) for opt in options_str.split(",")]

properties[name.strip()] = PropertyConfig(
property_type=property_type, options=options
Expand Down
9 changes: 5 additions & 4 deletions notion_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EntryProperty(BaseModel):
def validate_property_type(cls, v, info: ValidationInfo):
allowed_types = ['title', 'select', 'date', 'rich_text', 'number', 'multi_select', 'checkbox']
if v not in allowed_types:
raise ValueError(f"Unsupported task property type: {v}")
raise ValueError(f"Unsupported entry property type: {v}")
return v

@staticmethod
Expand All @@ -92,7 +92,7 @@ def to_notion_format(self):
"text": {"content": str(self.value)}
}]
}
elif self.type == "multi_select": # Handle 'multi_select'
elif self.type == "multi_select":
if not isinstance(self.value, list):
raise ValueError("Value for multi_select must be a list.")
return {
Expand All @@ -110,14 +110,15 @@ def to_notion_format(self):
return {
"date": {"start": str(self.value)}
}
elif self.type == "checkbox": # Handle 'checkbox'
elif self.type == "checkbox":
if not isinstance(self.value, bool):
raise ValueError("Value for checkbox must be a boolean.")
return {
"checkbox": self.value
}
else:
raise ValueError(f"Unsupported task property type: {self.type}")
# Corrected error message
raise ValueError(f"Unsupported entry property type: {self.type}")

class EntryConfig(BaseModel):
properties: Dict[str, EntryProperty]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ def test_parse_schema_natural_language():
assert properties["Due Date"].property_type == "date"
assert "Category" in properties
assert properties["Category"].property_type == "select"

# Convert PropertyOption objects to dicts for comparison
expected_options = [
{"name": "Type A", "color": None},
{"name": "Type B", "color": None},
{"name": "Type C", "color": None}
]
actual_options = [option.dict() for option in properties["Category"].options]
assert actual_options == expected_options

def test_parse_schema_invalid_format():
schema_data = {
Expand Down

0 comments on commit 480ab62

Please sign in to comment.