Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions wp1/logic/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def insert(wp10db, move):
VALUES
(%(m_timestamp)s, %(m_old_namespace)s, %(m_old_article)s,
%(m_new_namespace)s, %(m_new_article)s)
ON DUPLICATE KEY UPDATE
m_new_namespace = %(m_new_namespace)s,
m_new_article = %(m_new_article)s
""",
attr.asdict(move),
)
51 changes: 51 additions & 0 deletions wp1/logic/move_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest

from wp1.base_db_test import BaseWpOneDbTest
from wp1.logic import move as logic_move
from wp1.models.wp10.move import Move


class LogicMoveTest(BaseWpOneDbTest):

def test_insert_duplicate_key_update(self):
# Insert initial data using raw SQL
with self.wp10db.cursor() as cursor:
cursor.execute(
"""INSERT INTO moves
(m_timestamp, m_old_namespace, m_old_article,
m_new_namespace, m_new_article)
VALUES (%s, %s, %s, %s, %s)""",
(
b"20200101000000\x00\x00\x00\x00\x00\x00",
0,
b"Old_Article_1",
0,
b"New_Article_1",
),
)
self.wp10db.commit()

# Insert duplicate row using the code under test
move2 = Move(
m_timestamp=b"20200101000000\x00\x00\x00\x00\x00\x00",
m_old_namespace=0,
m_old_article=b"Old_Article_1",
m_new_namespace=1,
m_new_article=b"Different_Article",
)
logic_move.insert(self.wp10db, move2)
self.wp10db.commit()

# Verify the record was updated, not duplicated
with self.wp10db.cursor() as cursor:
cursor.execute(
"""SELECT * FROM moves
WHERE m_timestamp = %s
AND m_old_namespace = %s
AND m_old_article = %s""",
(b"20200101000000\x00\x00\x00\x00\x00\x00", 0, b"Old_Article_1"),
)
updated_move = cursor.fetchone()
self.assertIsNotNone(updated_move)
self.assertEqual(updated_move["m_new_namespace"], 1)
self.assertEqual(updated_move["m_new_article"], b"Different_Article")
Loading