Beispiel #1
0
    def test_init(self):
        c = m.Card(name="Test Card")
        s = m.Set(name="Test Set", code="TE")
        a = m.SetAppearance(card=c, set=s, rarity="R")

        self.assertEqual(a.card, c)
        self.assertEqual(a.set, s)
        self.assertEqual(a.rarity, "R")
Beispiel #2
0
 def test_repr(self):
     c = m.Card(name="Test Card")
     s = m.Set(name="Test Set", code="TE")
     a = m.SetAppearance(card=c, set=s, rarity="C")
     self.assertEqual(repr(a), "<Test Card (TE-C)>")
Beispiel #3
0
 def test_cards(self):
     s = m.Set()
     c = m.Card()
     s.cards[c] = u"R"
     self.assertEqual(s.cards, {c: u"R"})
Beispiel #4
0
 def test_init(self):
     s = m.Set(name="Test", code="TE")
     self.assertEqual(s.name, "Test")
     self.assertEqual(s.code, "TE")
Beispiel #5
0
 def test_repr(self):
     s = m.Set(name="Test", code="TE")
     self.assertEqual(repr(s), "<Set Model: Test>")
Beispiel #6
0
 def test_sets(self):
     c = m.Card()
     s = m.Set()
     c.sets[s] = u"R"
     self.assertEqual(c.sets, {s: u"R"})
Beispiel #7
0
def populate(cards_info, sets_file=None, session=Session):
    """
    Populate the database using a collection of cards.

    cards_info: an iterable of dict-like objects in the format returned by
                `parse` each containing the information about a given card

    sets_file: a unicode yielding file-like object containing comma-separated
               information about the sets that are used in `cards_info`.
               If unspecified, `DEFAULT_SETS_FILE` is used. The file will be
               closed after iteration.

    """

    s = session()

    with sets_file or codecs.open(DEFAULT_SETS_FILE, encoding="utf-8") as file:
        reader = unicode_csv_reader(file, reader=csv.DictReader)
        sets = {}
        for row in reader:
            row["released"] = datetime.datetime.strptime(
                row["released"], u"%Y/%m/%d"
            )
            sets[row["code"]] = m.Set(**row)

    sts = itertools.chain.from_iterable(types.subtypes.itervalues())

    types_ = {type : m.Type(name=type) for type in types.all}
    supertypes = {st : m.Supertype(name=st) for st in types.supertypes}
    subtypes = {st : m.Subtype(name=st) for st in sts}

    s.add_all(
        itertools.chain.from_iterable(
            i.itervalues() for i in (sets, types_, supertypes, subtypes)
        )
    )

    for card in cards_info:
        # XXX: Split cards / Stupid multiple ability
        if " // " in card[u"name"] or card[u"name"] == u"Seeds of Strength":
            continue

        t, u, v = (card.pop(k) for k in [u"supertypes", u"types", u"subtypes"])

        card[u"ability_objects"] = [
            s.query(m.Ability).filter_by(description=d).first() or
            m.Ability(description=d) for d in card.pop(u"abilities")
        ]

        card[u"supertype_objects"] = {supertypes[st] for st in t}
        card[u"type_objects"] = {types_[type] for type in u}
        card[u"subtype_objects"] = {subtypes[st] for st in v}

        appearances = {
            m.SetAppearance(set=sets[set], rarity=rarity)
            for set, rarity in card.pop(u"appearances")
        }

        card = m.Card(**card)
        card.set_appearances.update(appearances)

        s.add(card)

    s.commit()