File: D:/bin/scripts/python3.4/expressmusic/test/test_exclusion.py
import datetime
import os
def print_log(message):
print(f"[LOG]: {message}")
def test_stock_update_exclusion_date(file_path):
valid_days = {
"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"
}
try:
if not os.path.exists(file_path):
print_log(f"Error: File '{file_path}' not found.")
return False
with open(file_path, 'r') as f:
token_text = f.read()
lines = token_text.splitlines()
active_rules = []
print_log("--- Starting Validation ---")
for line in lines:
clean_line = line.strip()
if not clean_line or clean_line.startswith("#"):
continue
is_valid_rule = False
# Check Date Format
if clean_line[0].isdigit():
try:
datetime.datetime.strptime(clean_line, "%d/%m/%Y")
is_valid_rule = True
except ValueError:
print_log(f"WARNING: Rule '{clean_line}' is malformed (expected DD/MM/YYYY).")
# Check Day Name
else:
if clean_line.lower() in valid_days:
is_valid_rule = True
else:
print_log(f"WARNING: Rule '{clean_line}' is not a valid day name.")
if is_valid_rule:
active_rules.append(clean_line)
print_log(f"Rules loaded: {active_rules}")
# Current Time Simulation
now = datetime.datetime.now()
current_day_name = now.strftime("%A")
current_date_str = now.strftime("%d/%m/%Y")
print_log(f"Simulating check for: {current_day_name}, {current_date_str}")
for rule in active_rules:
if rule.lower() == current_day_name.lower():
print_log(f"MATCH FOUND: Day '{rule}' matches. Exclude update.")
return True
if rule == current_date_str:
print_log(f"MATCH FOUND: Date '{rule}' matches. Exclude update.")
return True
print_log("NO MATCH: Update should proceed.")
return False
except Exception as e:
print_log(f"Exception: {e}")
return False
if __name__ == "__main__":
dummy_filename = "exclusion_rules_test.txt"
# Test content with typos
test_content = """# Test Rules v2
Monday
Munday # TYPO: Should trigger warning
Sunday
25/12/2025
2025/12/25 # WRONG FORMAT: Should trigger warning
Thurs # ABBREVIATION: Should trigger warning (must be full name)
"""
#with open(dummy_filename, "w") as f:
# f.write(test_content)
#print(f"Created test file: {dummy_filename}")
print("Running test...\n")
# Run the test
result = test_stock_update_exclusion_date(dummy_filename)
print(f"\nFinal Result (Is Excluded?): {result}")