Update Discord trade listings

This commit is contained in:
2026-11-19 23:51:10 +01:00
parent 51459d6ae5
commit f37559ad6a
7 changed files with 1002 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
import csv
from io import StringIO
from pathlib import Path
import sqlite3
import unittest
from unittest.mock import patch
from discord_trade_parser import Message, clean_offer_items, export_csv, parse_listing
class OpenStringIO(StringIO):
def close(self):
# Let the test inspect the CSV after export_csv closes its context.
pass
class TradeItemCleanupTests(unittest.TestCase):
def test_keeps_stats_broad_options_and_owned_best_offer(self):
offer = ("SSS DMG 23.1% | All Gamepasses | ALL SKINS | "
"BEST OFFER:Aizen Evo FULL SSS | HIGH OFFER Shiny Homura Evo")
self.assertEqual(clean_offer_items(offer), offer)
def test_removes_chat_and_ui_fragments_without_damaging_stats(self):
offer = ("[ ] | Shiny Elf Mage Evo {SS | A- | S+} | "
"Dark Mage Evo S-/S-/S- | no response = nty | 1 • 19:36 | "
"Shiny Homura Evo S+ | S | SSS | COME WITH OFFER | "
"Please Use My And Yours | Total Value:885 | -1x Gilgamesh Evo")
self.assertEqual(
clean_offer_items(offer),
"Shiny Elf Mage Evo {SS / A- / S+} | Dark Mage Evo S-/S-/S- | "
"Shiny Homura Evo S+ / S / SSS | 1x Gilgamesh Evo",
)
def test_parse_listing_rejects_channel_only_message(self):
self.assertIsNone(parse_listing(Message("a", "Trading: channel for Server Boosters")))
listing = parse_listing(Message("b", "Trading: Aizen Evo | no response = nty\nLF: Homura Evo"))
self.assertEqual(listing.offer, "Aizen Evo")
def test_export_filters_old_database_rows(self):
conn = sqlite3.connect(":memory:")
conn.execute("""CREATE TABLE trades (
id INTEGER PRIMARY KEY, offer TEXT, looking_for TEXT, lf TEXT,
mlf TEXT, nlf TEXT, author_hash TEXT, seen_count INTEGER,
update_count INTEGER)""")
conn.executemany(
"INSERT INTO trades VALUES (?,?,?,?,?,?,?,?,?)",
[(1, "channel for Server Boosters", "", "", "", "", "a", 1, 0),
(2, "Aizen Evo | no response = nty", "Homura Evo", "Homura Evo", "", "", "b", 1, 0),
(3, "ILLUSIONST (TRANSCENDED) FOR BUNDLE 2", "", "", "", "", "c", 1, 0)],
)
stream = OpenStringIO(newline="")
with patch.object(Path, "open", return_value=stream):
count = export_csv(conn, Path("trades.csv"), detailed=True)
stream.seek(0)
rows = list(csv.DictReader(stream))
self.assertEqual(count, 2)
self.assertEqual(rows[0]["offer"], "Aizen Evo")
self.assertEqual(rows[0]["lf"], "Homura Evo")
self.assertEqual(rows[1]["offer"], "Aizen Evo")
self.assertEqual(rows[1]["lf"], "BUNDLE 2")
if __name__ == "__main__":
unittest.main()