diff options
-rwxr-xr-x | cardbase.py | 10 | ||||
-rwxr-xr-x | fulltest.sh | 2 | ||||
-rwxr-xr-x | test_coecon.py | 74 | ||||
-rwxr-xr-x | test_hydra.py | 74 | ||||
-rwxr-xr-x | test_nightmare.py | 77 | ||||
-rwxr-xr-x | test_ugin.py | 77 |
6 files changed, 309 insertions, 5 deletions
diff --git a/cardbase.py b/cardbase.py index b6942ee..be5a3b8 100755 --- a/cardbase.py +++ b/cardbase.py @@ -39,7 +39,7 @@ def extractSubTitle(page): def getCost(page): cost = extractSubTitle(page) - cost = re.search(" ([0-9X]*[WGRBU]*) ", cost) + cost = re.search(" ([0-9X]*[WGRBU\{\}/]*) ", cost) if cost: return cost.group(1) @@ -48,7 +48,7 @@ def getCost(page): def getColour(page): colours = extractSubTitle(page) - colours = re.search(" [0-9X]*([WGRBU]*) ", colours) + colours = re.search(" [0-9X]*([WGRBU\{\}/]*) ", colours) if colours: colours = colours.group(1) @@ -57,6 +57,7 @@ def getColour(page): colours = re.sub("R+", "R", colours) colours = re.sub("B+", "B", colours) colours = re.sub("G+", "G", colours) + colours = re.sub("[\{\}/]*", "", colours) return colours @@ -65,7 +66,7 @@ def getColour(page): def getType(page): types = extractSubTitle(page) - types = re.search("([A-Za-z ]*) —", types).group(1) + types = re.search("([A-Za-z ]*)( —)?", types).group(1).strip() return types @@ -90,7 +91,8 @@ def getText(page): def getFlavour(page): flavour = page.xpath("/html/body/table[3]/tr/td[2]/p[3]/i/text()") if flavour: - return flavour[0] + flavour = re.sub("\n", "", " ".join(flavour)) + return flavour else: return "" diff --git a/fulltest.sh b/fulltest.sh index 43ce539..67a3dc6 100755 --- a/fulltest.sh +++ b/fulltest.sh @@ -1,3 +1,3 @@ #!/usr/bin/env sh -python -m unittest test_shivandragon test_callow test_cardclass test_sorin test_island test_shoal test_coecon +python -m unittest test_shivandragon test_callow test_cardclass test_sorin test_island test_shoal test_coecon test_hydra test_nightmare test_ugin diff --git a/test_coecon.py b/test_coecon.py index e69de29..2d22be5 100755 --- a/test_coecon.py +++ b/test_coecon.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys +import unittest +import cardbase +from lxml import html + + +class Test_cardInformationParsing(unittest.TestCase): + + @classmethod + def setUpClass(cls): + # but actually, use the pre-fetched file to avoid querying the server too much + with open("testcards/coecon", "r") as file: + cls.page = html.fromstring(file.read()) + + # Tests + def test_correctTitleIsParsed(self): + self.assertEqual(cardbase.getTitle(self.page), "Coerced Confession") + + def test_correctCostIsParsed(self): + self.assertEqual(cardbase.getCost(self.page), "4{U/B}") + + def test_correctColourIsParsed(self): + self.assertEqual(cardbase.getColour(self.page), "UB") + + def test_correctTypeIsParsed(self): + self.assertEqual(cardbase.getType(self.page), "Sorcery") + + def test_correctSubTypeIsParsed(self): + self.assertEqual(cardbase.getSubType(self.page), "") + + def test_correctArtistIsParsed(self): + self.assertEqual(cardbase.getArtist(self.page), "Mathias Kollros") + + def test_correctTextIsParsed(self): + self.assertEqual(cardbase.getText(self.page), ["Target player puts the top four cards of his or her library into his or her graveyard. You draw a card for each creature card put into that graveyard this way."]) + + def test_correctFlavourIsParsed(self): + self.assertEqual(cardbase.getFlavour(self.page), "\"Ask the right questions in the right way and truth is inevitable.\" —Lazav") + + def test_correctRarityIsParsed(self): + self.assertEqual(cardbase.getRarity(self.page), "Uncommon") + + def test_correctPowerIsParsed(self): + self.assertEqual(cardbase.getPower(self.page), "") + + def test_correctToughnessIsParsed(self): + self.assertEqual(cardbase.getToughness(self.page), "") + + def test_correctLoyaltyIsParsed(self): + self.assertEqual(cardbase.getLoyalty(self.page), "") + +class Test_additionalCardData(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.card = cardbase.fetchCard("gtc", "217") + + def test_cardHasCorrectEdition(self): + self.assertEqual(self.card.edition, "gtc") + + def test_cardHasCorrectScan(self): + self.assertEqual(self.card.scan, "http://magiccards.info/scans/en/gtc/217.jpg") + + def test_cardHasCorrectNumber(self): + self.assertEqual(self.card.number, "217") + +def test(): + unittest.main(exit=False) + +# The entry point +if __name__ == "__main__": + test() diff --git a/test_hydra.py b/test_hydra.py new file mode 100755 index 0000000..1fbaa9f --- /dev/null +++ b/test_hydra.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys +import unittest +import cardbase +from lxml import html + + +class Test_cardInformationParsing(unittest.TestCase): + + @classmethod + def setUpClass(cls): + # but actually, use the pre-fetched file to avoid querying the server too much + with open("testcards/hydra", "r") as file: + cls.page = html.fromstring(file.read()) + + # Tests + def test_correctTitleIsParsed(self): + self.assertEqual(cardbase.getTitle(self.page), "Khalni Hydra") + + def test_correctCostIsParsed(self): + self.assertEqual(cardbase.getCost(self.page), "GGGGGGGG") + + def test_correctColourIsParsed(self): + self.assertEqual(cardbase.getColour(self.page), "G") + + def test_correctTypeIsParsed(self): + self.assertEqual(cardbase.getType(self.page), "Creature") + + def test_correctSubTypeIsParsed(self): + self.assertEqual(cardbase.getSubType(self.page), "Hydra") + + def test_correctArtistIsParsed(self): + self.assertEqual(cardbase.getArtist(self.page), "Todd Lockwood") + + def test_correctTextIsParsed(self): + self.assertEqual(cardbase.getText(self.page), ["Khalni Hydra costs {G} less to cast for each green creature you control.", "Trample"]) + + def test_correctFlavourIsParsed(self): + self.assertEqual(cardbase.getFlavour(self.page), "\"In ages past, bargains were struck and promises were made. Now we must collect on our debt. Begin the hymns.\" —Moruul, Khalni druid") + + def test_correctRarityIsParsed(self): + self.assertEqual(cardbase.getRarity(self.page), "Mythic Rare") + + def test_correctPowerIsParsed(self): + self.assertEqual(cardbase.getPower(self.page), "8") + + def test_correctToughnessIsParsed(self): + self.assertEqual(cardbase.getToughness(self.page), "8") + + def test_correctLoyaltyIsParsed(self): + self.assertEqual(cardbase.getLoyalty(self.page), "") + +class Test_additionalCardData(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.card = cardbase.fetchCard("roe", "192") + + def test_cardHasCorrectEdition(self): + self.assertEqual(self.card.edition, "roe") + + def test_cardHasCorrectScan(self): + self.assertEqual(self.card.scan, "http://magiccards.info/scans/en/roe/192.jpg") + + def test_cardHasCorrectNumber(self): + self.assertEqual(self.card.number, "192") + +def test(): + unittest.main(exit=False) + +# The entry point +if __name__ == "__main__": + test() diff --git a/test_nightmare.py b/test_nightmare.py new file mode 100755 index 0000000..24a6581 --- /dev/null +++ b/test_nightmare.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import sys +import unittest +import cardbase +from lxml import html + + +class Test_cardInformationParsing(unittest.TestCase): + + @classmethod + def setUpClass(cls): + # fetch shivan dragon info by the card's collector number (281 in M15) + # cls.page = html.fromstring(requests.get("http://magiccards.info/m15/en/281.html").text) + + # but actually, use the pre-fetched file to avoid querying the server too much + with open("testcards/nightmare", "r") as file: + cls.page = html.fromstring(file.read()) + + # Tests + def test_correctTitleIsParsed(self): + self.assertEqual(cardbase.getTitle(self.page), "Nightmare") + + def test_correctCostIsParsed(self): + self.assertEqual(cardbase.getCost(self.page), "5B") + + def test_correctColourIsParsed(self): + self.assertEqual(cardbase.getColour(self.page), "B") + + def test_correctTypeIsParsed(self): + self.assertEqual(cardbase.getType(self.page), "Creature") + + def test_correctSubTypeIsParsed(self): + self.assertEqual(cardbase.getSubType(self.page), "Nightmare Horse") + + def test_correctArtistIsParsed(self): + self.assertEqual(cardbase.getArtist(self.page), "Vance Kovacs") + + def test_correctTextIsParsed(self): + self.assertEqual(cardbase.getText(self.page), ["Flying (This creature can't be blocked except by creatures with flying or reach.)", "Nightmare's power and toughness are each equal to the number of Swamps you control."]) + + def test_correctFlavourIsParsed(self): + self.assertEqual(cardbase.getFlavour(self.page), "The thunder of its hooves beats dreams into despair.") + + def test_correctRarityIsParsed(self): + self.assertEqual(cardbase.getRarity(self.page), "Rare") + + def test_correctPowerIsParsed(self): + self.assertEqual(cardbase.getPower(self.page), "*") + + def test_correctToughnessIsParsed(self): + self.assertEqual(cardbase.getToughness(self.page), "*") + + def test_correctLoyaltyIsParsed(self): + self.assertEqual(cardbase.getLoyalty(self.page), "") + +class Test_additionalCardData(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.card = cardbase.fetchCard("m15", "276") + + def test_cardHasCorrectEdition(self): + self.assertEqual(self.card.edition, "m15") + + def test_cardHasCorrectScan(self): + self.assertEqual(self.card.scan, "http://magiccards.info/scans/en/m15/276.jpg") + + def test_cardHasCorrectNumber(self): + self.assertEqual(self.card.number, "276") + +def test(): + unittest.main(exit=False) + +# The entry point +if __name__ == "__main__": + test() diff --git a/test_ugin.py b/test_ugin.py new file mode 100755 index 0000000..948d341 --- /dev/null +++ b/test_ugin.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import sys +import unittest +import cardbase +from lxml import html + + +class Test_cardInformationParsing(unittest.TestCase): + + @classmethod + def setUpClass(cls): + # fetch shivan dragon info by the card's collector number (281 in M15) + # cls.page = html.fromstring(requests.get("http://magiccards.info/m15/en/281.html").text) + + # but actually, use the pre-fetched file to avoid querying the server too much + with open("testcards/ugincons", "r") as file: + cls.page = html.fromstring(file.read()) + + # Tests + def test_correctTitleIsParsed(self): + self.assertEqual(cardbase.getTitle(self.page), "Ugin's Construct") + + def test_correctCostIsParsed(self): + self.assertEqual(cardbase.getCost(self.page), "4") + + def test_correctColourIsParsed(self): + self.assertEqual(cardbase.getColour(self.page), "") + + def test_correctTypeIsParsed(self): + self.assertEqual(cardbase.getType(self.page), "Artifact Creature") + + def test_correctSubTypeIsParsed(self): + self.assertEqual(cardbase.getSubType(self.page), "Construct") + + def test_correctArtistIsParsed(self): + self.assertEqual(cardbase.getArtist(self.page), "Peter Mohrbacher") + + def test_correctTextIsParsed(self): + self.assertEqual(cardbase.getText(self.page), ["When Ugin's Construct enters the battlefield, sacrifice a permanent that's one or more colors."]) + + def test_correctFlavourIsParsed(self): + self.assertEqual(cardbase.getFlavour(self.page), "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.") + + def test_correctRarityIsParsed(self): + self.assertEqual(cardbase.getRarity(self.page), "Uncommon") + + def test_correctPowerIsParsed(self): + self.assertEqual(cardbase.getPower(self.page), "4") + + def test_correctToughnessIsParsed(self): + self.assertEqual(cardbase.getToughness(self.page), "5") + + def test_correctLoyaltyIsParsed(self): + self.assertEqual(cardbase.getLoyalty(self.page), "") + +class Test_additionalCardData(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.card = cardbase.fetchCard("frf", "164") + + def test_cardHasCorrectEdition(self): + self.assertEqual(self.card.edition, "frf") + + def test_cardHasCorrectScan(self): + self.assertEqual(self.card.scan, "http://magiccards.info/scans/en/frf/164.jpg") + + def test_cardHasCorrectNumber(self): + self.assertEqual(self.card.number, "164") + +def test(): + unittest.main(exit=False) + +# The entry point +if __name__ == "__main__": + test() |