import unittest

from web.common import parse_ordering


class TaskOrderingTests(unittest.TestCase):
    def test_default_ordering_uses_latest_update_first(self):
        ordering = parse_ordering(None)

        self.assertEqual(
            [str(clause) for clause in ordering],
            ["tasks.updated_at DESC", "tasks.task_id DESC"],
        )

    def test_invalid_ordering_falls_back_to_latest_update_first(self):
        ordering = parse_ordering("not_a_field")

        self.assertEqual(
            [str(clause) for clause in ordering],
            ["tasks.updated_at DESC", "tasks.task_id DESC"],
        )

    def test_explicit_created_at_ordering_remains_supported(self):
        ordering = parse_ordering("-created_at")

        self.assertEqual(
            [str(clause) for clause in ordering],
            ["tasks.created_at DESC", "tasks.task_id DESC"],
        )


if __name__ == "__main__":
    unittest.main()
