Skip to content

Commit

Permalink
Update mtgxlsx to work with in memory models.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Feb 27, 2016
1 parent 767aa7e commit 04e09e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 85 deletions.
28 changes: 13 additions & 15 deletions mtg_ssm/serialization/mtgxlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@

import openpyxl

from mtg_ssm.db import models
from mtg_ssm.mtg import models
from mtg_ssm.serialization import mtgdict


def dump_workbook(session):
"""Return xlsx workbook from the database."""
def dump_workbook(collection):
"""Return xlsx workbook from a Collection."""
workbook = openpyxl.Workbook()

name_to_prints = collections.defaultdict(list)
printings = session.query(models.CardPrinting)
for printing in printings:
name_to_prints[printing.card_name].append(printing)
card_sets = sorted(
collection.code_to_card_set.values(),
key=lambda cset: cset.release_date)

card_sets = session.query(models.CardSet)
sets_sheet = workbook['Sheet']
create_sets_sheet(sets_sheet, card_sets)
for card_set in card_sets:
cards_sheet = workbook.create_sheet()
create_cards_sheet(cards_sheet, card_set, name_to_prints)
create_cards_sheet(
cards_sheet, card_set, collection.card_name_to_printings)
return workbook


Expand Down Expand Up @@ -53,7 +52,7 @@ def create_sets_sheet(sheet, card_sets):
card_set.name,
card_set.release_date,
card_set.block,
card_set.type,
card_set.type_,
len(card_set.printings),
'=COUNTIF(\'{}\'!A:A,">0")'.format(card_set.code),
'=COUNTIF(\'{}\'!A:A,">=4")'.format(card_set.code),
Expand Down Expand Up @@ -186,12 +185,11 @@ def create_cards_sheet(sheet, card_set, name_to_prints):
cdim.hidden = hidden


def read_workbook_counts(session, workbook):
"""Read mtgxlsx workbook and load counts into the database."""
card_sets = session.query(models.CardSet)
set_codes = {s.code for s in card_sets}
def read_workbook_counts(collection, workbook):
"""Read mtgxlsx workbook and load counts into a Collection."""
set_codes = collection.code_to_card_set.keys()
card_dicts = workbook_row_reader(workbook, set_codes)
mtgdict.load_counts(session, card_dicts)
mtgdict.load_counts(collection, card_dicts)


def workbook_row_reader(workbook, known_sets):
Expand Down
109 changes: 39 additions & 70 deletions tests/serialization/mtgxlsx_test.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
"""Tests for mtg_ssm.mtgxlsx"""

import collections
import datetime
from unittest import mock

import openpyxl

from mtg_ssm.db import models
from mtg_ssm.mtgjson import mtgjson
from mtg_ssm.mtg import collection
from mtg_ssm.mtg import models
from mtg_ssm.serialization import mtgxlsx

from tests.mtgjson import mtgjson_testcase
from tests.db import sqlite_testcase


class MtgXlsxTest(
sqlite_testcase.SqliteTestCase, mtgjson_testcase.MtgJsonTestCase):
class MtgXlsxTest(mtgjson_testcase.MtgJsonTestCase):

def setUp(self):
super().setUp()
models.Base.metadata.create_all(self.connection)
self.collection = collection.Collection(
self.mtg_data, include_online_only=True)

def test_create_sets_sheet(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
card_sets = self.session.query(models.CardSet) \
.order_by(models.CardSet.release_date).all()
card_sets = self.collection.code_to_card_set.values()
card_sets = sorted(card_sets, key=lambda cset: cset.release_date)
book = openpyxl.Workbook()
sheet = book.create_sheet()

Expand All @@ -52,7 +48,7 @@ def test_create_sets_sheet(self):
['ISD', 'Innistrad', datetime.datetime(2011, 9, 30), 'Innistrad', 'expansion', 6, '=COUNTIF(\'ISD\'!A:A,">0")', '=COUNTIF(\'ISD\'!A:A,">=4")', "=SUM('ISD'!A:A)"],
['PC2', 'Planechase 2012 Edition', datetime.datetime(2012, 6, 1), None, 'planechase', 4, '=COUNTIF(\'PC2\'!A:A,">0")', '=COUNTIF(\'PC2\'!A:A,">=4")', "=SUM('PC2'!A:A)"],
['MMA', 'Modern Masters', datetime.datetime(2013, 6, 7, 0, 0), None, 'reprint', 1, '=COUNTIF(\'MMA\'!A:A,">0")', '=COUNTIF(\'MMA\'!A:A,">=4")', "=SUM('MMA'!A:A)"],
['VMA', 'Vintage Masters', datetime.datetime(2014, 6, 16), None, 'masters', 1, '=COUNTIF(\'VMA\'!A:A,">0")', '=COUNTIF(\'VMA\'!A:A,">=4")', "=SUM('VMA'!A:A)"],
['VMA', 'Vintage Masters', datetime.datetime(2014, 6, 16, 0, 0), None, 'masters', 1, '=COUNTIF(\'VMA\'!A:A,">0")', '=COUNTIF(\'VMA\'!A:A,">=4")', "=SUM('VMA'!A:A)"],
]
# pylint: enable=line-too-long
self.assertEqual(expected, rows)
Expand Down Expand Up @@ -95,17 +91,12 @@ def test_create_haveref_sum(self):

def test_get_refs_multiple_sets(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
dark_rits = self.session.query(models.CardPrinting).filter(
models.CardPrinting.card.has(name='Dark Ritual')).all()
name_to_prints = {'Dark Ritual': dark_rits}
lea_dark_rit = self.session.query(
models.CardPrinting).filter_by(multiverseid=54).first()
lea_dark_rit = self.collection.id_to_printing[
'fff0b8e8fea06ee1ac5c35f048a0a459b1222673']

# Execute
print_refs = mtgxlsx.get_other_print_references(
lea_dark_rit, name_to_prints)
lea_dark_rit, self.collection.card_name_to_printings)

# Verify
expected = (
Expand All @@ -115,17 +106,12 @@ def test_get_refs_multiple_sets(self):

def test_get_refs_multiple_variants(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
thallids = self.session.query(models.CardPrinting).filter(
models.CardPrinting.card.has(name='Thallid')).all()
name_to_prints = {'Thallid': thallids}
mma_thallid = self.session.query(
models.CardPrinting).filter_by(multiverseid=370352).first()
mma_thallid = self.collection.id_to_printing[
'fc46a4b72d216117a352f59217a84d0baeaaacb7']

# Execute
print_refs = mtgxlsx.get_other_print_references(
mma_thallid, name_to_prints)
mma_thallid, self.collection.card_name_to_printings)

# Verify
expected = (
Expand All @@ -134,46 +120,36 @@ def test_get_refs_multiple_variants(self):

def test_get_refs_basic_land(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
forests = self.session.query(models.CardPrinting).filter(
models.CardPrinting.card.has(name='Forest')).all()
name_to_prints = {'Forest': forests}
lea_forest = self.session.query(
models.CardPrinting).filter_by(multiverseid=288).first()
lea_forest = self.collection.id_to_printing[
'5ede9781b0c5d157c28a15c3153a455d7d6180fa']

# Execute
print_refs = mtgxlsx.get_other_print_references(
lea_forest, name_to_prints)
lea_forest, self.collection.card_name_to_printings)

# Verify
self.assertIsNone(print_refs)

def test_create_cards_sheet(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
forest1 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2746).first()
forest2 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2747).first()
forest3 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2748).first()
forest1 = self.collection.id_to_printing[
'676a1f5b64dc03bbb3876840c3ff2ba2c16f99cb']
forest2 = self.collection.id_to_printing[
'd0a4414893bc2f9bd3beea2f8f2693635ef926a4']
forest3 = self.collection.id_to_printing[
'c78d2da78c68c558b1adc734b3f164e885407ffc']
forest1.counts[models.CountTypes.copies] = 1
forest2.counts[models.CountTypes.foils] = 2
forest3.counts[models.CountTypes.copies] = 3
forest3.counts[models.CountTypes.foils] = 4
self.session.commit()
printings = self.session.query(models.CardPrinting)
name_to_prints = collections.defaultdict(list)
for printing in printings:
name_to_prints[printing.card.name].append(printing)
ice_age = self.session.query(models.CardSet).filter_by(code='ICE').one()

ice_age = self.collection.code_to_card_set['ICE']
book = openpyxl.Workbook()
sheet = book.create_sheet()

# Execute
mtgxlsx.create_cards_sheet(sheet, ice_age, name_to_prints)
mtgxlsx.create_cards_sheet(
sheet, ice_age, self.collection.card_name_to_printings)

# Verify
rows = [[cell.value for cell in row] for row in sheet.rows]
Expand All @@ -190,12 +166,8 @@ def test_create_cards_sheet(self):
self.assertEqual(expected, rows)

def test_dump_workbook(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()

# Execute
book = mtgxlsx.dump_workbook(self.session)
book = mtgxlsx.dump_workbook(self.collection)

# Verify
expected = [
Expand Down Expand Up @@ -292,19 +264,17 @@ def test_row_reader_invalid_set(self):

def test_read_workbook_counts(self):
# Setup
mtgjson.update_models(self.session, self.mtg_data, True)
self.session.commit()
forest1 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2746).first()
forest2 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2747).first()
forest3 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2748).first()
forest4 = self.session.query(
models.CardPrinting).filter_by(multiverseid=2749).first()
forest1 = self.collection.id_to_printing[
'676a1f5b64dc03bbb3876840c3ff2ba2c16f99cb']
forest2 = self.collection.id_to_printing[
'd0a4414893bc2f9bd3beea2f8f2693635ef926a4']
forest3 = self.collection.id_to_printing[
'c78d2da78c68c558b1adc734b3f164e885407ffc']
forest4 = self.collection.id_to_printing[
'5e9f08498a9343b1954103e493da2586be0fe394']
forest4.counts[models.CountTypes.copies] = 2
forest4.counts[models.CountTypes.foils] = 3
self.session.commit()

book = openpyxl.Workbook()
sets_sheet = book.create_sheet()
sets_sheet.title = 'Sets'
Expand All @@ -323,11 +293,10 @@ def test_read_workbook_counts(self):
cards_sheet.append(row)

# Execute
mtgxlsx.read_workbook_counts(self.session, book)
self.session.commit()
mtgxlsx.read_workbook_counts(self.collection, book)

# Verify
self.assertEqual({models.CountTypes.copies: 1}, forest1.counts)
self.assertEqual({models.CountTypes.foils: 2}, forest2.counts)
self.assertEqual({models.CountTypes.copies: 3, models.CountTypes.foils: 4}, forest3.counts)
self.assertFalse(forest4.counts)
self.assertEqual({models.CountTypes.copies: 2, models.CountTypes.foils: 3}, forest4.counts)

0 comments on commit 04e09e6

Please sign in to comment.