p = 'backend/tests/test_citations.py'
s = open(p).read()
s = s.replace('''class CiteTests(unittest.TestCase):
    def test_reads_as_topic_book_page(self):
        got = citations.cite(
            {"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024",
             "page": 1069},
            "Periventricular leukomalacia")
        self.assertEqual(
            got,
            "Periventricular leukomalacia — Kliegman R, ed. Nelson Textbook of "
            "Pediatrics, 22nd ed., 2024, p. 1069.")

    def test_no_page_leaves_the_page_off(self):
        got = citations.cite({"title": "Berkowitz's Pediatrics"}, "Colic")
        self.assertTrue(got.endswith("6th ed., 2020."))
        self.assertNotIn("p. ", got)

    def test_no_topic_leaves_the_dash_off(self):
        got = citations.cite({"title": "Berkowitz's Pediatrics", "page": 12})
        self.assertFalse(got.startswith(" —"))
        self.assertTrue(got.startswith("Berkowitz CD"))

    def test_two_names_for_one_book_give_one_citation(self):
        a = citations.cite({"title": "Kliegman R. Nelson Textbook of Pediatrics"}, "x")
        b = citations.cite({"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024"}, "x")
        self.assertEqual(a, b)''',
'''class CiteTests(unittest.TestCase):
    def test_reads_as_a_standard_reference(self):
        got = citations.cite(
            {"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024",
             "page": 1069})
        self.assertEqual(
            got, "Kliegman R, ed. Nelson Textbook of Pediatrics. 22nd ed. Elsevier; 2024.")

    def test_the_page_is_not_part_of_the_line(self):
        # It is an offset into one ebook build, kept in the entry and not shown.
        self.assertNotIn("p. ", citations.cite({"title": "Berkowitz's Pediatrics", "page": 12}))

    def test_a_book_with_no_edition_skips_it(self):
        self.assertEqual(citations.cite({"title": "Netters Pediatrics (Florin T., Ludwig St.)"}),
                         "Florin TA, Ludwig S, eds. Netter's Pediatrics. Elsevier; 2011.")

    def test_a_corporate_author_does_not_run_into_the_title(self):
        got = citations.cite({"title": "Pediatric Clinical Practice Guidelines & Policies, 18th Edition"})
        self.assertIn("American Academy of Pediatrics. Pediatric Clinical Practice", got)

    def test_two_names_for_one_book_give_one_citation(self):
        a = citations.cite({"title": "Kliegman R. Nelson Textbook of Pediatrics"})
        b = citations.cite({"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024"})
        self.assertEqual(a, b)''')

s = s.replace('''    def test_an_uptodate_topic_is_cited_as_one(self):
        got = citations.cite({"title": "Fever in infants and children: Pathophysiology and management - UpToDate"})
        self.assertEqual(
            got,
            "Fever in infants and children: Pathophysiology and management. "
            "UpToDate, Wolters Kluwer.")''',
'''    def test_an_uptodate_topic_is_cited_as_one(self):
        got = citations.cite({"title": "Fever in infants and children: Pathophysiology and management - UpToDate"})
        self.assertEqual(
            got,
            "Fever in infants and children: Pathophysiology and management. "
            "UpToDate, Wolters Kluwer")''')

# the topic block goes with topic_of
start = s.index('class TopicTests(unittest.TestCase):')
end = s.index('class TidyTests(unittest.TestCase):')
s = s[:start] + s[end:]

s = s.replace('''class TidyTests(unittest.TestCase):
    def test_a_page_list_becomes_one_entry_per_page(self):
        out = citations.tidy([
            {"title": "Kliegman R. Nelson Textbook of Pediatrics", "pages": [10, 12]},
        ], topic="Croup")
        self.assertEqual(len(out), 2)
        self.assertEqual([row["page"] for row in out], [10, 12])

    def test_the_same_book_at_the_same_page_is_one_line(self):
        out = citations.tidy([
            {"title": "Kliegman R. Nelson Textbook of Pediatrics", "pages": [10]},
            {"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024",
             "pages": [10]},
            {"title": "Cover", "author": "Vitalsource Download", "pages": [1]},
        ], topic="Croup")
        self.assertEqual(len(out), 1)

    def test_ordered_by_book_then_page(self):
        out = citations.tidy([
            {"title": "Zitelli and Davis' Atlas of Pediatric Physical Diagnosis", "pages": [5]},
            {"title": "Berkowitz's Pediatrics", "pages": [9, 2]},
        ], topic="Croup")
        self.assertEqual([row["page"] for row in out], [2, 9, 5])
        self.assertTrue(out[0]["text"].endswith("p. 2."))''',
'''class TidyTests(unittest.TestCase):
    def test_one_book_is_one_entry_however_many_pages(self):
        out = citations.tidy([
            {"title": "Kliegman R. Nelson Textbook of Pediatrics", "pages": [10]},
            {"title": "Kliegman R. Nelson Textbook of Pediatrics 2-Volume Set 22ed 2024",
             "pages": [12, 10]},
            {"title": "Cover", "author": "Vitalsource Download", "pages": [1]},
        ])
        self.assertEqual(len(out), 1)
        self.assertEqual(out[0]["pages"], [10, 12])

    def test_a_single_page_field_is_kept_too(self):
        out = citations.tidy([{"title": "Berkowitz's Pediatrics", "page": 7}])
        self.assertEqual(out[0]["pages"], [7])

    def test_alphabetical(self):
        out = citations.tidy([
            {"title": "Zitelli and Davis' Atlas of Pediatric Physical Diagnosis"},
            {"title": "Berkowitz's Pediatrics"},
        ])
        self.assertEqual([row["text"][0] for row in out], ["B", "Z"])''')
open(p, 'w').write(s)
print('written')
