Refactor: repo hygiene, shared utils, focused tests, and CI improvements

- Remove dead imports/duplication from Google2xls.py and social.py
- Centralize shared logic in review_workflow_utils.py
- Add focused posting-flow tests in tests/test_social.py (all pass)
- Update .gitignore for secrets and local artifacts
- Trim requirements.txt to direct deps
- Improve CI: compile all scripts, run focused tests
- Prep repo for clean commit (no secrets, no local artifacts)
This commit is contained in:
timberjoegithub
2026-05-27 13:17:56 +00:00
parent eae7276147
commit 13ae301667
10 changed files with 3197 additions and 2545 deletions
+62
View File
@@ -0,0 +1,62 @@
import unittest
from datetime import datetime
from review_workflow_utils import (
build_review_media_path,
count_google_review_pages,
normalize_google_media_url,
normalize_wordpress_date,
sanitize_review_name,
)
class FakeElement:
def __init__(self, text):
self.text = text
class FakeDriver:
def __init__(self, text):
self.text = text
def find_element(self, by, value):
self.last_lookup = (by, value)
return FakeElement(self.text)
class ReviewWorkflowUtilsTests(unittest.TestCase):
def test_count_google_review_pages_rounds_up_by_review_page(self):
driver = FakeDriver("25 reviews")
page_count = count_google_review_pages(driver)
self.assertEqual(page_count, 3)
self.assertEqual(driver.last_lookup, ("class name", "Qha3nb"))
def test_normalize_wordpress_date_handles_relative_weeks(self):
publish_date = normalize_wordpress_date("2 weeks ago", now=datetime(2026, 5, 27, 9, 30, 0))
self.assertEqual(publish_date, "2026-05-13T22:00:00")
def test_normalize_wordpress_date_handles_month_year_strings(self):
publish_date = normalize_wordpress_date("Jan 2025")
self.assertEqual(publish_date, "2025-01-01T22:00:00")
def test_normalize_google_media_url_strips_thumbnail_suffix(self):
style_text = 'background-image: url("https://example.com/photo=s1600-p-k-no");'
self.assertEqual(
normalize_google_media_url(style_text),
"https://example.com/photo=-no",
)
def test_build_review_media_path_sanitizes_review_name(self):
media_path = build_review_media_path("Joe's Diner!", "20260527", "front.jpg", base_dir="/tmp/review-tests")
self.assertEqual(media_path, "/tmp/review-tests/JoesDiner/20260527/front.jpg")
self.assertEqual(sanitize_review_name("Joe's Diner!"), "JoesDiner")
if __name__ == "__main__":
unittest.main()
+50
View File
@@ -0,0 +1,50 @@
import unittest
from unittest.mock import patch, MagicMock
import social
class SocialPostingFlowTests(unittest.TestCase):
def setUp(self):
# Patch env and Posts for isolation
self.env_patch = patch('social.env')
self.mock_env = self.env_patch.start()
self.mock_env.request_timeout = 1
self.mock_env.facebooksleep = 0
self.mock_env.forcegoogleupdate = False
self.mock_env.xls = 'dummy.xlsx'
self.mock_env.mariadb = False
self.mock_env.mariadbuser = 'user'
self.mock_env.mariadbpass = 'pass'
self.mock_env.mariadbserver = 'localhost'
self.mock_env.mariadbdb = 'db'
self.mock_env.needreversed = False
self.mock_env.postssession = MagicMock()
self.mock_env.posts = []
self.mock_env.xlsdf = MagicMock()
self.mock_env.posts = []
self.mock_env.postssession.query.return_value.count.return_value = 0
self.addCleanup(self.env_patch.stop)
@patch('social.requests.post')
@patch('builtins.open', new_callable=MagicMock)
def test_post_facebook_video_handles_success(self, mock_open, mock_post):
mock_post.return_value.json.return_value = {'id': '123'}
mock_open.return_value.__enter__.return_value = MagicMock()
result = social.post_facebook_video('groupid', ['video.mp4'], 'token', 'title', 'content', 'date', 'rating', 'address')
self.assertEqual(result, {'id': '123'})
@patch('social.requests.post', side_effect=AttributeError('fail'))
@patch('builtins.open', new_callable=MagicMock)
def test_post_facebook_video_handles_error(self, mock_open, mock_post):
mock_open.return_value.__enter__.return_value = MagicMock()
result = social.post_facebook_video('groupid', ['video.mp4'], 'token', 'title', 'content', 'date', 'rating', 'address')
self.assertFalse(result)
def test_write_to_database_handles_empty(self):
# Should not raise
data = []
local_outputs = {'xlsdf': MagicMock(), 'posts': [], 'postssession': MagicMock()}
result = social.write_to_database(data, local_outputs)
self.assertEqual(result, data)
if __name__ == '__main__':
unittest.main()