MTGProxyPrinter

Changes On Branch carddb_optimization
Login

Changes On Branch carddb_optimization

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch carddb_optimization Excluding Merge-Ins

This is equivalent to a diff from 4efb66cf41 to 98551c9879

2022-07-08
17:46
Optimized the card database size by only storing positive printing filters. Completes [4d9d5ad8f05730a5]. See [/wiki?name=branch/carddb_optimization&p] for benchmark details. check-in: c54adb5109 user: thomas tags: trunk
17:42
Added changelog entry Closed-Leaf check-in: 98551c9879 user: thomas tags: carddb_optimization
17:30
Further optimization during card data import: Instead of deleting excess entries in PrintingDisplayFilter individually, simply clear the whole table and re-populate it. check-in: 1dc70b4ec9 user: thomas tags: carddb_optimization
16:52
Optimize the handling of printing filters. The database no longer stores the full cross product in the PrintingDisplayFilter table. Instead, only positive values (a filter applies to a printing) is stored. The negative case is now implied. This reduces the total table size from ~6.6 million to ~126 thousand entries. This reduces the database file size from ~230 MiB to 160 MiB. check-in: 0b50f651ab user: thomas tags: carddb_optimization
13:12
Document.save_to_disk(): Added three logging lines. check-in: 4efb66cf41 user: thomas tags: trunk
2022-07-07
18:48
Fixed an exception when a card with position greater than the number of document pages is deleted. Issue was introduced in [e7c36b589c0596a5]. check-in: 15b35e98c6 user: thomas tags: trunk

Changes to doc/changelog.md.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


17
18
19
20
21
22
23
# Changelog


# Next version (in development)

## New features

- Proper, full support for oversized cards, like Archenemy schemes or Planechase plane cards. Regular cards and larger
  cards are always kept on separate pages to ensure that drawn cut marker lines (if enabled) are always 100% accurate.
  - Note: Some cards, like the Legacy Championship winner rewards, are tagged as being oversized, but are then served
    with regular-size images by Scryfall.
    When the image is downloaded, it will be treated as a regular card, even if the deck import wizard warns
    about it being potentially oversized.

## Fixed issues



- Fixed the “Remove selected” cards button in the deck list importer unexpectedly staying active
  when clicked while multiple cells of the same row in the card table were selected.

# Version 0.17.0 (2022-06-13)  <a name="v0_17_0"></a>

## New features



<













>
>







1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Changelog


# Next version (in development)

## New features

- Proper, full support for oversized cards, like Archenemy schemes or Planechase plane cards. Regular cards and larger
  cards are always kept on separate pages to ensure that drawn cut marker lines (if enabled) are always 100% accurate.
  - Note: Some cards, like the Legacy Championship winner rewards, are tagged as being oversized, but are then served
    with regular-size images by Scryfall.
    When the image is downloaded, it will be treated as a regular card, even if the deck import wizard warns
    about it being potentially oversized.

## Fixed issues

- Significantly optimized card database size and import speed.
  (The database now takes roughly 25% less time to update and uses about 30% less disk space)
- Fixed the “Remove selected” cards button in the deck list importer unexpectedly staying active
  when clicked while multiple cells of the same row in the card table were selected.

# Version 0.17.0 (2022-06-13)  <a name="v0_17_0"></a>

## New features

Changes to mtg_proxy_printer/card_info_downloader.py.

286
287
288
289
290
291
292




293
294
295
296
297
298
299
            for filter_name, filter_id
            in self.model.db.execute("SELECT filter_name, filter_id FROM DisplayFilters").fetchall()}
        set_wackiness_score_cache: typing.Dict[str, SetWackinessScore] = {}
        skipped_cards = 0
        index = 0
        face_ids: IntTuples = []
        db: sqlite3.Connection = self.model.db




        for index, card in enumerate(card_data, start=1):
            if not self.should_run:
                logger.info(f"Aborting card import after {index} cards due to user request.")
                self.download_finished.emit()
                return index
            if card["object"] != "card":
                logger.warning(f"Non-card found in card data during import: {card}")







>
>
>
>







286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
            for filter_name, filter_id
            in self.model.db.execute("SELECT filter_name, filter_id FROM DisplayFilters").fetchall()}
        set_wackiness_score_cache: typing.Dict[str, SetWackinessScore] = {}
        skipped_cards = 0
        index = 0
        face_ids: IntTuples = []
        db: sqlite3.Connection = self.model.db
        # Will be re-populated while iterating over the card data. Axing the previous data is far cheaper than trying
        # to update it in-place by removing up to number-of-available-filters entries per each individual card,
        # just to make sure that rare un-banned cards are updated properly.
        db.execute("DELETE FROM PrintingDisplayFilter\n")
        for index, card in enumerate(card_data, start=1):
            if not self.should_run:
                logger.info(f"Aborting card import after {index} cards due to user request.")
                self.download_finished.emit()
                return index
            if card["object"] != "card":
                logger.warning(f"Non-card found in card data during import: {card}")
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
    return result


def _insert_card_filters(
        model: CardDatabase, printing_id: int, filter_data: typing.Dict[str, bool],
        printing_filter_ids: typing.Dict[str, int]):
    model.db.executemany(
        cached_dedent("""\
            INSERT OR REPLACE INTO PrintingDisplayFilter (printing_id, filter_id, filter_applies)
              VALUES (?, ?, ?)
        """),
        ((printing_id, printing_filter_ids[filter_name], filter_applies)
         for filter_name, filter_applies in filter_data.items())
    )


def _should_skip_card(card: JSONType) -> bool:
    # Cards without images. These have no "image_uris" item can’t be printed at all. Unconditionally skip these
    return card["image_status"] == "missing"








<
|
<
<
|
|







576
577
578
579
580
581
582

583


584
585
586
587
588
589
590
591
592
    return result


def _insert_card_filters(
        model: CardDatabase, printing_id: int, filter_data: typing.Dict[str, bool],
        printing_filter_ids: typing.Dict[str, int]):
    model.db.executemany(

        "INSERT INTO PrintingDisplayFilter (printing_id, filter_id) VALUES (?, ?)\n",


        ((printing_id, printing_filter_ids[filter_name])
         for filter_name, filter_applies in filter_data.items() if filter_applies)
    )


def _should_skip_card(card: JSONType) -> bool:
    # Cards without images. These have no "image_uris" item can’t be printed at all. Unconditionally skip these
    return card["image_status"] == "missing"

Changes to mtg_proxy_printer/model/carddb.py.

747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

776
777

778
779
780
781
782
783
784
        filters_in_db: typing.Dict[str, bool] = {
            key: bool(value) for key, value
            in self.db.execute("SELECT filter_name, filter_active FROM DisplayFilters").fetchall()
        }
        filters_in_settings: typing.Dict[str, bool] = {key: section.getboolean(key) for key in section.keys()}
        return filters_in_settings != filters_in_db

    @profile
    def _remove_old_printing_filters(self, section) -> bool:
        stored_filters = {
            filter_name for filter_name, in self.db.execute("SELECT filter_name FROM DisplayFilters").fetchall()
        }
        known_filters = set(section.keys())
        old_filters = stored_filters - known_filters
        if old_filters:
            logger.info(f"Removing old printing filters from the database: {old_filters}")
            self.db.executemany(
                "DELETE FROM DisplayFilters WHERE filter_name = ?",
                ((filter_name,) for filter_name in old_filters)
            )
        return bool(old_filters)

    @profile
    def _update_cached_data(self, progress_signal: typing.Callable[[int], None]):
        logger.debug("Update the Printing.is_hidden column")
        self.db.execute(cached_dedent("""\
        UPDATE Printing    -- _update_cached_data()
            SET is_hidden = HiddenPrintings.should_be_hidden
            FROM HiddenPrintings

            WHERE Printing.printing_id = HiddenPrintings.printing_id
              AND Printing.is_hidden <> HiddenPrintings.should_be_hidden

        ;
        """))
        progress_signal(2)
        logger.debug("Update the FaceName.is_hidden column")
        self.db.execute(cached_dedent("""\
        WITH FaceNameShouldBeHidden (face_name_id, should_be_hidden) AS (    -- _update_cached_data()
          -- A FaceName should be hidden, iff all uses by printings are hidden,







|



















|
|
>
|
|
>







747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
        filters_in_db: typing.Dict[str, bool] = {
            key: bool(value) for key, value
            in self.db.execute("SELECT filter_name, filter_active FROM DisplayFilters").fetchall()
        }
        filters_in_settings: typing.Dict[str, bool] = {key: section.getboolean(key) for key in section.keys()}
        return filters_in_settings != filters_in_db

    @profile  # TODO: This decorator is unnecessary
    def _remove_old_printing_filters(self, section) -> bool:
        stored_filters = {
            filter_name for filter_name, in self.db.execute("SELECT filter_name FROM DisplayFilters").fetchall()
        }
        known_filters = set(section.keys())
        old_filters = stored_filters - known_filters
        if old_filters:
            logger.info(f"Removing old printing filters from the database: {old_filters}")
            self.db.executemany(
                "DELETE FROM DisplayFilters WHERE filter_name = ?",
                ((filter_name,) for filter_name in old_filters)
            )
        return bool(old_filters)

    @profile
    def _update_cached_data(self, progress_signal: typing.Callable[[int], None]):
        logger.debug("Update the Printing.is_hidden column")
        self.db.execute(cached_dedent("""\
        UPDATE Printing    -- _update_cached_data()
            SET is_hidden = Printing.printing_id IN (
              SELECT HiddenPrintingIDs.printing_id FROM HiddenPrintingIDs
            )
            WHERE is_hidden <> (Printing.printing_id IN (
              SELECT HiddenPrintingIDs.printing_id FROM HiddenPrintingIDs
            ))
        ;
        """))
        progress_signal(2)
        logger.debug("Update the FaceName.is_hidden column")
        self.db.execute(cached_dedent("""\
        WITH FaceNameShouldBeHidden (face_name_id, should_be_hidden) AS (    -- _update_cached_data()
          -- A FaceName should be hidden, iff all uses by printings are hidden,

Changes to mtg_proxy_printer/model/carddb.sql.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.


PRAGMA user_version = 0000028;
PRAGMA foreign_keys = on;
BEGIN TRANSACTION;


CREATE TABLE PrintLanguage (
  language_id INTEGER PRIMARY KEY NOT NULL,
  "language" TEXT NOT NULL UNIQUE







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.


PRAGMA user_version = 0000029;
PRAGMA foreign_keys = on;
BEGIN TRANSACTION;


CREATE TABLE PrintLanguage (
  language_id INTEGER PRIMARY KEY NOT NULL,
  "language" TEXT NOT NULL UNIQUE
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

120
121
122
123
124
125
126
  filter_active INTEGER NOT NULL CHECK (filter_active IN (TRUE, FALSE))
);

CREATE TABLE PrintingDisplayFilter (
  -- Stores which filter applies to which printing.
  printing_id    INTEGER NOT NULL REFERENCES Printing (printing_id) ON DELETE CASCADE,
  filter_id      INTEGER NOT NULL REFERENCES DisplayFilters (filter_id) ON DELETE CASCADE,
  filter_applies INTEGER NOT NULL CHECK (filter_applies IN (TRUE, FALSE)),
  PRIMARY KEY (printing_id, filter_id)
) WITHOUT ROWID;

CREATE VIEW HiddenPrintings AS
  SELECT printing_id, sum(filter_applies * filter_active) > 0 AS should_be_hidden
  FROM PrintingDisplayFilter
  JOIN DisplayFilters USING (filter_id)

  GROUP BY printing_id
;

CREATE TABLE LastImageUseTimestamps (
  -- Used to store the last image use timestamp and usage count of each image.
  -- The usage count measures how often an image was part of a printed or exported document. Printing multiple copies
  -- in a document still counts as a single use. Saving/loading is not enough to count as a "use".







<



|
|


>







105
106
107
108
109
110
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  filter_active INTEGER NOT NULL CHECK (filter_active IN (TRUE, FALSE))
);

CREATE TABLE PrintingDisplayFilter (
  -- Stores which filter applies to which printing.
  printing_id    INTEGER NOT NULL REFERENCES Printing (printing_id) ON DELETE CASCADE,
  filter_id      INTEGER NOT NULL REFERENCES DisplayFilters (filter_id) ON DELETE CASCADE,

  PRIMARY KEY (printing_id, filter_id)
) WITHOUT ROWID;

CREATE VIEW HiddenPrintingIDs AS
SELECT printing_id
  FROM PrintingDisplayFilter
  JOIN DisplayFilters USING (filter_id)
  WHERE filter_active IS TRUE
  GROUP BY printing_id
;

CREATE TABLE LastImageUseTimestamps (
  -- Used to store the last image use timestamp and usage count of each image.
  -- The usage count measures how often an image was part of a printed or exported document. Printing multiple copies
  -- in a document still counts as a single use. Saving/loading is not enough to count as a "use".
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  JOIN PrintLanguage USING (language_id)
  WHERE Printing.is_hidden IS FALSE
    AND FaceName.is_hidden IS FALSE
;

CREATE VIEW AllPrintings AS
  SELECT card_name, set_code, set_name, "language", collector_number, scryfall_id, highres_image, face_number,
         is_front, is_oversized, png_image_uri, oracle_id, release_date, wackiness_score, Printing.is_hidden,
         release_date
  FROM Card
  JOIN Printing USING (card_id)
  JOIN MTGSet   USING (set_id)
  JOIN CardFace USING (printing_id)
  JOIN FaceName USING (face_name_id)
  JOIN PrintLanguage USING (language_id)
;

COMMIT;







|
<









154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169
170
  JOIN PrintLanguage USING (language_id)
  WHERE Printing.is_hidden IS FALSE
    AND FaceName.is_hidden IS FALSE
;

CREATE VIEW AllPrintings AS
  SELECT card_name, set_code, set_name, "language", collector_number, scryfall_id, highres_image, face_number,
         is_front, is_oversized, png_image_uri, oracle_id, release_date, wackiness_score, Printing.is_hidden

  FROM Card
  JOIN Printing USING (card_id)
  JOIN MTGSet   USING (set_id)
  JOIN CardFace USING (printing_id)
  JOIN FaceName USING (face_name_id)
  JOIN PrintLanguage USING (language_id)
;

COMMIT;

Changes to mtg_proxy_printer/model/carddb_migrations.py.

562
563
564
565
566
567
568










































569
570
571
572
573
574
575
          JOIN MTGSet   USING (set_id)
          JOIN CardFace USING (printing_id)
          JOIN FaceName USING (face_name_id)
          JOIN PrintLanguage USING (language_id)"""),
    ]:
        db.execute(statement)












































MIGRATION_SCRIPTS: MigrationScriptListing = (
    # First component of each tuple contains the source schema version, second contains the migration script function.
    # These MUST be ordered by source schema version, otherwise the migration logic breaks. In other words: APPEND only.
    (9, _migrate_9_to_10),
    (10, _migrate_10_to_11),
    (11, _migrate_11_to_12),







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
          JOIN MTGSet   USING (set_id)
          JOIN CardFace USING (printing_id)
          JOIN FaceName USING (face_name_id)
          JOIN PrintLanguage USING (language_id)"""),
    ]:
        db.execute(statement)


def _migrate_28_to_29(db: sqlite3.Connection):
    db.execute("DROP VIEW HiddenPrintings\n")
    db.execute(textwrap.dedent("""\
    CREATE TABLE PrintingDisplayFilter2 (
      -- Stores which filter applies to which printing.
      printing_id    INTEGER NOT NULL REFERENCES Printing (printing_id) ON DELETE CASCADE,
      filter_id      INTEGER NOT NULL REFERENCES DisplayFilters (filter_id) ON DELETE CASCADE,
      PRIMARY KEY (printing_id, filter_id)
    ) WITHOUT ROWID;
    """))
    db.execute(textwrap.dedent("""\
    INSERT INTO PrintingDisplayFilter2 (printing_id, filter_id)
      SELECT printing_id, filter_id
      FROM PrintingDisplayFilter
      WHERE filter_applies IS TRUE
    """))
    db.execute("DROP TABLE PrintingDisplayFilter\n")
    db.execute("ALTER TABLE PrintingDisplayFilter2 RENAME TO PrintingDisplayFilter\n")
    db.execute(textwrap.dedent("""\
    CREATE VIEW HiddenPrintingIDs AS
      SELECT printing_id
        FROM PrintingDisplayFilter
        JOIN DisplayFilters USING (filter_id)
        WHERE filter_active IS TRUE
        GROUP BY printing_id
    ;
    """))
    db.execute("DROP VIEW AllPrintings\n")
    db.execute(textwrap.dedent("""\
    CREATE VIEW AllPrintings AS
      SELECT card_name, set_code, set_name, "language", collector_number, scryfall_id, highres_image, face_number,
             is_front, is_oversized, png_image_uri, oracle_id, release_date, wackiness_score, Printing.is_hidden
      FROM Card
      JOIN Printing USING (card_id)
      JOIN MTGSet   USING (set_id)
      JOIN CardFace USING (printing_id)
      JOIN FaceName USING (face_name_id)
      JOIN PrintLanguage USING (language_id)
    ;
    """))


MIGRATION_SCRIPTS: MigrationScriptListing = (
    # First component of each tuple contains the source schema version, second contains the migration script function.
    # These MUST be ordered by source schema version, otherwise the migration logic breaks. In other words: APPEND only.
    (9, _migrate_9_to_10),
    (10, _migrate_10_to_11),
    (11, _migrate_11_to_12),
585
586
587
588
589
590
591

592
593
594
595
596
597
598
    (21, _migrate_21_to_22),
    (22, _migrate_22_to_23),
    (23, _migrate_23_to_24),
    (24, _migrate_24_to_25),
    (25, _migrate_25_to_26),
    (26, _migrate_26_to_27),
    (27, _migrate_27_to_28),

)


def migrate_card_database_location():
    from mtg_proxy_printer.model.carddb import DEFAULT_DATABASE_LOCATION, OLD_DATABASE_LOCATION
    if DEFAULT_DATABASE_LOCATION.exists() and OLD_DATABASE_LOCATION.exists():
        logger.warning(f"A card database at both the new location '{DEFAULT_DATABASE_LOCATION}' and the old location "







>







627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    (21, _migrate_21_to_22),
    (22, _migrate_22_to_23),
    (23, _migrate_23_to_24),
    (24, _migrate_24_to_25),
    (25, _migrate_25_to_26),
    (26, _migrate_26_to_27),
    (27, _migrate_27_to_28),
    (28, _migrate_28_to_29),
)


def migrate_card_database_location():
    from mtg_proxy_printer.model.carddb import DEFAULT_DATABASE_LOCATION, OLD_DATABASE_LOCATION
    if DEFAULT_DATABASE_LOCATION.exists() and OLD_DATABASE_LOCATION.exists():
        logger.warning(f"A card database at both the new location '{DEFAULT_DATABASE_LOCATION}' and the old location "
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636


    schema version right before it is executed. Each migration script must upgrade to the next schema version. Functions
    that combine multiple version upgrades in one SQL script are not supported.

    :param db: card database, given as a plain sqlite3 database connection object
    :param migration_scripts: List of migration script functions to run, if applicable. Defaults to a built-in list of
      migration scripts. Should only be passed explicitly for testing purposes.
    """
    current_schema_version = db.execute("PRAGMA user_version").fetchone()[0]
    if mtg_proxy_printer.sqlite_helpers.check_database_schema_version(db, "carddb") > 0:
        logger.info(f"Database schema outdated, running database migrations. {current_schema_version=}")
        if migration_scripts is not MIGRATION_SCRIPTS:
            logger.debug(f"Custom migration scripts passed: {migration_scripts}")
    else:
        logger.info("Database schema recent, not running any database migrations")
        return
    for source_version, migration_script in migration_scripts:
        if db.execute("PRAGMA user_version").fetchone()[0] == source_version:
            logger.info(f"Running migration task for schema version {source_version}")
            db.execute("BEGIN TRANSACTION")
            migration_script(db)
            db.execute(f"PRAGMA user_version = {source_version + 1}")
            db.commit()

    current_schema_version = db.execute("PRAGMA user_version").fetchone()[0]
    logger.info(f"Finished database migrations. {current_schema_version=}")









|

|






|

|

|

<
|
|
>
>
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

677
678
679
680
    schema version right before it is executed. Each migration script must upgrade to the next schema version. Functions
    that combine multiple version upgrades in one SQL script are not supported.

    :param db: card database, given as a plain sqlite3 database connection object
    :param migration_scripts: List of migration script functions to run, if applicable. Defaults to a built-in list of
      migration scripts. Should only be passed explicitly for testing purposes.
    """
    begin_schema_version = db.execute("PRAGMA user_version\n").fetchone()[0]
    if mtg_proxy_printer.sqlite_helpers.check_database_schema_version(db, "carddb") > 0:
        logger.info(f"Database schema outdated, running database migrations. {begin_schema_version=}")
        if migration_scripts is not MIGRATION_SCRIPTS:
            logger.debug(f"Custom migration scripts passed: {migration_scripts}")
    else:
        logger.info("Database schema recent, not running any database migrations")
        return
    for source_version, migration_script in migration_scripts:
        if db.execute("PRAGMA user_version\n").fetchone()[0] == source_version:
            logger.info(f"Running migration task for schema version {source_version}")
            db.execute("BEGIN TRANSACTION\n")
            migration_script(db)
            db.execute(f"PRAGMA user_version = {source_version + 1}\n")
            db.commit()

    current_schema_version = db.execute("PRAGMA user_version\n").fetchone()[0]
    logger.info(f"Finished database migrations, rebuilding database. {current_schema_version=}")
    db.execute("VACUUM\n")
    logger.info("Rebuild done.")

Changes to tests/helpers.py.

59
60
61
62
63
64
65








66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        dw.populate_database(data)


@functools.lru_cache()
def load_json(name: str) -> mtg_proxy_printer.card_info_downloader.JSONType:
    return json.loads(pkg_resources.resource_string("tests.json_samples", f"{name}.json").decode("utf-8"))










def fill_card_database_with_json_cards(
        qtbot: QtBot,
        card_db: mtg_proxy_printer.model.carddb.CardDatabase,
        json_files_or_names: typing.List[typing.Union[str, mtg_proxy_printer.card_info_downloader.JSONType]],
        filter_settings: typing.Dict[str, str] = None) -> mtg_proxy_printer.model.carddb.CardDatabase:
    section = mtg_proxy_printer.settings.settings["card-filter"]
    settings_to_use = {filter_name: "False" for filter_name in section.keys()}
    if filter_settings:
        settings_to_use.update(filter_settings)
    data = [
        load_json(json_file_or_name) if isinstance(json_file_or_name, str) else json_file_or_name
        for json_file_or_name in json_files_or_names
    ]
    with patch.dict(section, settings_to_use):
        populate_database(qtbot, card_db, data)
    return card_db


def fill_card_database_with_json_card(
        qtbot: QtBot,







>
>
>
>
>
>
>
>










|
<
<
<







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84



85
86
87
88
89
90
91
        dw.populate_database(data)


@functools.lru_cache()
def load_json(name: str) -> mtg_proxy_printer.card_info_downloader.JSONType:
    return json.loads(pkg_resources.resource_string("tests.json_samples", f"{name}.json").decode("utf-8"))


def load_multiple_json_cards(
        json_files_or_names: typing.List[typing.Union[str, mtg_proxy_printer.card_info_downloader.JSONType]]):
    return [
        load_json(json_file_or_name) if isinstance(json_file_or_name, str) else json_file_or_name
        for json_file_or_name in json_files_or_names
    ]


def fill_card_database_with_json_cards(
        qtbot: QtBot,
        card_db: mtg_proxy_printer.model.carddb.CardDatabase,
        json_files_or_names: typing.List[typing.Union[str, mtg_proxy_printer.card_info_downloader.JSONType]],
        filter_settings: typing.Dict[str, str] = None) -> mtg_proxy_printer.model.carddb.CardDatabase:
    section = mtg_proxy_printer.settings.settings["card-filter"]
    settings_to_use = {filter_name: "False" for filter_name in section.keys()}
    if filter_settings:
        settings_to_use.update(filter_settings)
    data = load_multiple_json_cards(json_files_or_names)



    with patch.dict(section, settings_to_use):
        populate_database(qtbot, card_db, data)
    return card_db


def fill_card_database_with_json_card(
        qtbot: QtBot,

Changes to tests/test_card_info_downloader.py.

10
11
12
13
14
15
16

17
18
19
20
21
22
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import dataclasses

import typing
import unittest.mock

from hamcrest import *
import pytest

from mtg_proxy_printer.card_info_downloader import SetWackinessScore







>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import dataclasses
import sqlite3
import typing
import unittest.mock

from hamcrest import *
import pytest

from mtg_proxy_printer.card_info_downloader import SetWackinessScore
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    """Checks png_image_uri, is_front, face_number"""
    assert_that(
        data := card_db.db.execute(f"SELECT png_image_uri, is_front, face_number FROM {relation_name}").fetchall(),
        contains_inanyorder(*test_case.db_card_face()),
        f"CardFace relation contains unexpected data: {data}")


def _assert_all_printings_contains(card_db: CardDatabase, test_case: TestCaseData):
    """
    Checks
      card_name, set_code, "language", collector_number, scryfall_id,
      highres_image, png_image_uri, is_front, is_oversized
    """
    assert_that(
        data := card_db.db.execute(







|







184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    """Checks png_image_uri, is_front, face_number"""
    assert_that(
        data := card_db.db.execute(f"SELECT png_image_uri, is_front, face_number FROM {relation_name}").fetchall(),
        contains_inanyorder(*test_case.db_card_face()),
        f"CardFace relation contains unexpected data: {data}")


def _assert_visible_printings_contains(card_db: CardDatabase, test_case: TestCaseData):
    """
    Checks
      card_name, set_code, "language", collector_number, scryfall_id,
      highres_image, png_image_uri, is_front, is_oversized
    """
    assert_that(
        data := card_db.db.execute(
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    """
    _assert_printing_contains(card_db, test_case, is_hidden=False)
    _assert_card_face_contains(card_db, test_case)
    _assert_face_name_contains(card_db, test_case)
    _assert_set_contains(card_db, test_case)
    _assert_card_contains(card_db, test_case)
    _assert_print_language_contains(card_db, test_case)
    _assert_all_printings_contains(card_db, test_case)


def assert_hidden_import(card_db: CardDatabase, test_case: TestCaseData):
    """
    Verifies that the printing is correctly stored, but invisible in all VIEWs that filter out unwanted printings.
    """
    _assert_print_language_contains(card_db, test_case)







|







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    """
    _assert_printing_contains(card_db, test_case, is_hidden=False)
    _assert_card_face_contains(card_db, test_case)
    _assert_face_name_contains(card_db, test_case)
    _assert_set_contains(card_db, test_case)
    _assert_card_contains(card_db, test_case)
    _assert_print_language_contains(card_db, test_case)
    _assert_visible_printings_contains(card_db, test_case)


def assert_hidden_import(card_db: CardDatabase, test_case: TestCaseData):
    """
    Verifies that the printing is correctly stored, but invisible in all VIEWs that filter out unwanted printings.
    """
    _assert_print_language_contains(card_db, test_case)
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456


































457
458
459
460
461
462
463
        "en", "28", "650722b4-d72b-4745-a1a5-00a34836282b", "7e6b9b59-cd68-4e3c-827b-38833c92d6eb", True,
    )
    filter_name = "hide-oversized-cards"
    # Pass 1: Populate the database and exclude the card. The card should not be visible
    fill_card_database_with_json_card(qtbot, card_db, test_case.json_name, {filter_name: "True"})
    assert_hidden_import(card_db, test_case)
    # Pass 2: Re-Populate the database, but include the card now.
    fill_card_database_with_json_card(qtbot, card_db, test_case.json_name, {filter_name: "False"})
    # The card should be in the database. The RemovedPrintings table should be empty
    assert_visible_import(card_db, test_case)
    assert_that(
        card_db.db.execute("SELECT scryfall_id, oracle_id FROM RemovedPrintings").fetchall(),
        is_(empty()),
        "RemovedPrintings table not properly cleaned up."
    )




































def test_updates_language(qtbot, card_db: CardDatabase):
    test_case = TestCaseData(  # English "Fury Sliver" from Time Spiral. Modified the language
        "regular_english_card", True, (
            FaceData("Fury Sliver",
                     "https://c1.scryfall.com/file/scryfall-cards/png/front/0/0/0000579f-7b35-4ed3-b44c-db2a538066fe.png?1562894979",
                     True),







|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
        "en", "28", "650722b4-d72b-4745-a1a5-00a34836282b", "7e6b9b59-cd68-4e3c-827b-38833c92d6eb", True,
    )
    filter_name = "hide-oversized-cards"
    # Pass 1: Populate the database and exclude the card. The card should not be visible
    fill_card_database_with_json_card(qtbot, card_db, test_case.json_name, {filter_name: "True"})
    assert_hidden_import(card_db, test_case)
    # Pass 2: Re-Populate the database, but include the card now.
    fill_card_database_with_json_card(qtbot, card_db, test_case.json_name)
    # The card should be in the database. The RemovedPrintings table should be empty
    assert_visible_import(card_db, test_case)
    assert_that(
        card_db.db.execute("SELECT scryfall_id, oracle_id FROM RemovedPrintings").fetchall(),
        is_(empty()),
        "RemovedPrintings table not properly cleaned up."
    )


@pytest.mark.parametrize("test_case_data", [
    TestCaseData(  # English "Fury Sliver" from Time Spiral
        "regular_english_card", True, (
            FaceData("Fury Sliver", "https://c1.scryfall.com/file/scryfall-cards/png/front/0/0/0000579f-7b35-4ed3-b44c-db2a538066fe.png?1562894979", True),
        ), DatabaseSetData("tsp", "Time Spiral", "https://scryfall.com/sets/tsp?utm_source=api", "2006-10-06"),
        "en", "157", "0000579f-7b35-4ed3-b44c-db2a538066fe", "44623693-51d6-49ad-8cd7-140505caf02f", False,
    ),
])
def test_re_import_after_card_ban_hides_it(qtbot, card_db: CardDatabase, test_case_data: TestCaseData):
    card_json = load_json(test_case_data.json_name)
    with unittest.mock.patch.dict(card_json["legalities"], {"commander": "banned"}):
        fill_card_database_with_json_card(qtbot, card_db, card_json, {"hide-banned-in-commander": "True"})
        assert_hidden_import(card_db, test_case_data)
    fill_card_database_with_json_card(qtbot, card_db, card_json, {"hide-banned-in-commander": "True"})
    assert_visible_import(card_db, test_case_data)


@pytest.mark.parametrize("test_case_data", [
    TestCaseData(  # English "Fury Sliver" from Time Spiral
        "regular_english_card", True, (
            FaceData("Fury Sliver", "https://c1.scryfall.com/file/scryfall-cards/png/front/0/0/0000579f-7b35-4ed3-b44c-db2a538066fe.png?1562894979", True),
        ), DatabaseSetData("tsp", "Time Spiral", "https://scryfall.com/sets/tsp?utm_source=api", "2006-10-06"),
        "en", "157", "0000579f-7b35-4ed3-b44c-db2a538066fe", "44623693-51d6-49ad-8cd7-140505caf02f", False,
    ),
])
def test_re_import_after_unban_makes_card_visible(qtbot, card_db: CardDatabase, test_case_data: TestCaseData):
    card_json = load_json(test_case_data.json_name)
    fill_card_database_with_json_card(qtbot, card_db, card_json, {"hide-banned-in-commander": "True"})
    assert_visible_import(card_db, test_case_data)
    with unittest.mock.patch.dict(card_json["legalities"], {"commander": "banned"}):
        fill_card_database_with_json_card(qtbot, card_db, card_json, {"hide-banned-in-commander": "True"})
        assert_hidden_import(card_db, test_case_data)


def test_updates_language(qtbot, card_db: CardDatabase):
    test_case = TestCaseData(  # English "Fury Sliver" from Time Spiral. Modified the language
        "regular_english_card", True, (
            FaceData("Fury Sliver",
                     "https://c1.scryfall.com/file/scryfall-cards/png/front/0/0/0000579f-7b35-4ed3-b44c-db2a538066fe.png?1562894979",
                     True),