|
| 1 | +import unittest |
| 2 | + |
| 3 | +from wp1.base_db_test import BaseWpOneDbTest |
| 4 | +from wp1.logic import move as logic_move |
| 5 | +from wp1.models.wp10.move import Move |
| 6 | + |
| 7 | + |
| 8 | +class LogicMoveTest(BaseWpOneDbTest): |
| 9 | + |
| 10 | + def test_insert_duplicate_key_update(self): |
| 11 | + # Insert initial data using raw SQL |
| 12 | + with self.wp10db.cursor() as cursor: |
| 13 | + cursor.execute( |
| 14 | + """INSERT INTO moves |
| 15 | + (m_timestamp, m_old_namespace, m_old_article, |
| 16 | + m_new_namespace, m_new_article) |
| 17 | + VALUES (%s, %s, %s, %s, %s)""", |
| 18 | + ( |
| 19 | + b"20200101000000\x00\x00\x00\x00\x00\x00", |
| 20 | + 0, |
| 21 | + b"Old_Article_1", |
| 22 | + 0, |
| 23 | + b"New_Article_1", |
| 24 | + ), |
| 25 | + ) |
| 26 | + self.wp10db.commit() |
| 27 | + |
| 28 | + # Insert duplicate row using the code under test |
| 29 | + move2 = Move( |
| 30 | + m_timestamp=b"20200101000000\x00\x00\x00\x00\x00\x00", |
| 31 | + m_old_namespace=0, |
| 32 | + m_old_article=b"Old_Article_1", |
| 33 | + m_new_namespace=1, |
| 34 | + m_new_article=b"Different_Article", |
| 35 | + ) |
| 36 | + logic_move.insert(self.wp10db, move2) |
| 37 | + self.wp10db.commit() |
| 38 | + |
| 39 | + # Verify the record was updated, not duplicated |
| 40 | + with self.wp10db.cursor() as cursor: |
| 41 | + cursor.execute( |
| 42 | + """SELECT * FROM moves |
| 43 | + WHERE m_timestamp = %s |
| 44 | + AND m_old_namespace = %s |
| 45 | + AND m_old_article = %s""", |
| 46 | + (b"20200101000000\x00\x00\x00\x00\x00\x00", 0, b"Old_Article_1"), |
| 47 | + ) |
| 48 | + updated_move = cursor.fetchone() |
| 49 | + self.assertIsNotNone(updated_move) |
| 50 | + self.assertEqual(updated_move["m_new_namespace"], 1) |
| 51 | + self.assertEqual(updated_move["m_new_article"], b"Different_Article") |
0 commit comments