class ReadXMLTests(unittest.TestCase): readxml = ReadXML() attr1 = ['id', 'date', 'quantity', 'length', 'incomplete', 'nowinstats', 'location', 'game_name', 'gameid'] attr2 = ['username', 'userid', 'name', 'startposition', 'colour', 'score', 'new', 'rating', 'win'] xml_data = [PlaysXMLDataset(id=38023491, date="2019-09-29", quantity=1, length=0, incomplete=0, nowinstats=0, location="", game_name="Asking for Trobils", gameid=156442), PlaysXMLDataset(id=38021178, date="2019-09-29", quantity=1, length=0, incomplete=0, nowinstats=0, location="", game_name="Coup", gameid=131357)] xml_data[0].players = [ PlayerXMLDataset(username="******", userid=508171, name="Richard Allen", startposition=1, colour="", score=32, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Jeff", startposition=2, colour="", score=30, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Tanya", startposition=3, colour="", score=41.1, new=False, rating=0, win=True), PlayerXMLDataset(username="", userid=0, name="Arden", startposition=4, colour="", score=35, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Shelly", startposition=5, colour="", score=29, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Alex", startposition=6, colour="", score=32, new=False, rating=0, win=False)] xml_data[1].players = [ PlayerXMLDataset(username="******", userid=508171, name="Richard Allen", startposition=1, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Jeff", startposition=2, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Tanya", startposition=3, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Damon", startposition=4, colour="", score=0, new=False, rating=0, win=False), PlayerXMLDataset(username="", userid=0, name="Arden", startposition=5, colour="", score=0, new=False, rating=0, win=True), PlayerXMLDataset(username="", userid=0, name="Shelly", startposition=6, colour="", score=0, new=False, rating=0, win=False)] def test_read_xml_file(self): self.readxml.read_xml_file('test.xml') for x in self.attr1: play_fun1 = operator.attrgetter(x) for play_one, play_two in zip(self.readxml.plays, self.xml_data): self.assertEqual(play_fun1(play_one), play_fun1(play_two)) for y in self.attr2: play_fun2 = operator.attrgetter(y) for player_one, player_two in zip(play_one.players, play_two.players): self.assertEqual(play_fun2(player_one), play_fun2(player_two))
def xml(self): plays = PlaysXMLDataset(id=self.id, date=self.date, quantity=self.quantity, length=self.length, incomplete=self.incomplete, nowinstats=self.nowinstats, location=self.location, game_name=self.gamedataset.name, gameid=self.gamedataset.id) for x in self.playersplaydataset: plays.players.append(x.xml) return plays
def xml(self): return PlaysXMLDataset(id=self.id, game_name=self.name)
return PlayerXMLDataset(username=self.playerdataset.username, userid=self.playerdataset.userid, name=self.playerdataset.name, startposition=self.startposition, colour=self.colour, score=float(self.score), new=self.new, rating=self.rating, win=self.win) @xml.setter def xml(self, val): self.startposition = val.startposition self.colour = val.colour self.score = float(val.score) self.new = val.new self.rating = val.rating self.win = val.win self.playerdataset = PlayerDataset.query.filter_by( name=val.name).first() if self.playerdataset is None: self.playerdataset = PlayerDataset(xml=val) if __name__ == "__main__": tmp = PlaysXMLDataset() tmp.gameid = 99 tmp.game_name = "Hello World" data = GameDataset(xml=tmp)
def _read_xml_plays(dom): rtn = PlaysXMLDataset() rtn.id = int(dom.attributes['id'].value) rtn.date = dom.attributes['date'].value rtn.quantity = int(dom.attributes['quantity'].value) rtn.length = int(dom.attributes['length'].value) rtn.incomplete = bool(int(dom.attributes['incomplete'].value)) rtn.nowinstats = int(dom.attributes['nowinstats'].value) rtn.location = dom.attributes['location'].value items = dom.getElementsByTagName("item") for item in items: rtn.game_name = item.attributes['name'].value rtn.gameid = int(item.attributes['objectid'].value) return rtn