def test_insert(self): # Disable Foreign Keys checks for this test TestSource.db.disableForeignKeys() with TestSource.db.transaction() as t: TestSource.db.upsert( Source(100, 'source name TD UPD', 'source fixtures_url TD UPD', 'source url TD UPD')) objs = TestSource.db.select(Source()) self.assertEqual(len(objs), 3) d = eval("{'id': 100}") for k, v in d.items(): self.assertEqual(objs[2].__getattribute__('get' + k.title())(), v) d = eval( "{'name': 'source name TD UPD', 'fixtures_url': 'source fixtures_url TD UPD', 'url': 'source url TD UPD'}" ) for k, v in d.items(): self.assertEqual(objs[2].__getattribute__('get' + k.title())(), v) # force a rollback t.fail()
def test_select(self): objs = TestSource.db.select(Source()) self.assertEqual(len(objs), 2) self.assertEqual(objs[0].getId(), 98) self.assertEqual(objs[0].getName(), 'source name TD') self.assertEqual(objs[0].getFixtures_Url(), 'source fixtures_url TD') self.assertEqual(objs[0].getUrl(), 'source url TD') self.assertEqual(objs[1].getId(), 99) self.assertEqual(objs[1].getName(), 'source name TD2') self.assertEqual(objs[1].getFixtures_Url(), 'source fixtures_url TD2') self.assertEqual(objs[1].getUrl(), 'source url TD2') objs = TestSource.db.select(Source(98)) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getId(), 98) self.assertEqual(objs[0].getName(), 'source name TD') self.assertEqual(objs[0].getFixtures_Url(), 'source fixtures_url TD') self.assertEqual(objs[0].getUrl(), 'source url TD') objs = TestSource.db.select( Source.createAdhoc({ 'name': 'source name TD', 'fixtures_url': 'source fixtures_url TD', 'url': 'source url TD' })) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getId(), 98) self.assertEqual(objs[0].getName(), 'source name TD') self.assertEqual(objs[0].getFixtures_Url(), 'source fixtures_url TD') self.assertEqual(objs[0].getUrl(), 'source url TD')
def test_delete(self): # Disable Foreign Keys checks for this test TestSource.db.disableForeignKeys() with TestSource.db.transaction() as t: TestSource.db.delete(Source(98)) objs = TestSource.db.select(Source()) self.assertEqual(len(objs), 1) # force a rollback t.fail()
def test_repr(self): obj = Source(98, 'source name TD', 'source fixtures_url TD', 'source url TD') self.assertEqual( str(obj), "source : Keys {'id': 98} : Values {'name': 'source name TD', 'fixtures_url': 'source fixtures_url TD', 'url': 'source url TD'}" )
def test_create(self): obj = Source.create( (98, 'source name TD', 'source fixtures_url TD', 'source url TD')) self.assertEqual(obj.getId(), 98) self.assertEqual(obj.getName(), 'source name TD') self.assertEqual(obj.getFixtures_Url(), 'source fixtures_url TD') self.assertEqual(obj.getUrl(), 'source url TD')
def test_update(self): # Disable Foreign Keys checks for this test TestSource.db.disableForeignKeys() with TestSource.db.transaction() as t: TestSource.db.upsert( Source(98, 'source name TD UPD', 'source fixtures_url TD UPD', 'source url TD UPD')) objs = TestSource.db.select(Source(98)) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getId(), 98) d = eval( "{'name': 'source name TD UPD', 'fixtures_url': 'source fixtures_url TD UPD', 'url': 'source url TD UPD'}" ) for k, v in d.items(): self.assertEqual(objs[0].__getattribute__('get' + k.title())(), v) # force a rollback t.fail() with TestSource.db.transaction() as t: source = TestSource.db.select(Source(98))[0] for k, v in d.items(): source.__getattribute__('set' + k.title())(v) TestSource.db.upsert(source) objs = TestSource.db.select(Source(98)) self.assertEqual(len(objs), 1) self.assertEqual(objs[0].getId(), 98) for k, v in d.items(): self.assertEqual(objs[0].__getattribute__('get' + k.title())(), v) # force a rollback t.fail()
def sourceData(log: Logger, target: str, currentSeason: bool): ''' Obtain historical match data :param log: a logging object :param target: the name of match data source :param currentSeason: True if only interested in the current season's data ''' log.info('Downloading data from source: {}'.format(target)) config = getSweeperConfig() dbName = config['dbName'] log.debug('Opening database: {}'.format(dbName)) with Database(dbName, SQLite3Impl()) as db, db.transaction() as t: keys = {'name': target} source = db.select(Source.createAdhoc(keys)) if source: source = source[0] else: sys.exit('{} source not in database'.format(target)) log.debug('{}'.format(source)) sourceId = source.getId() keys = {'source_id': sourceId, 'active': 1} if currentSeason: seasonMap = db.select( Source_Season_Map.createAdhoc(keys, ('>season', )))[0:1] else: seasonMap = db.select( Source_Season_Map.createAdhoc(keys, ('>season', ))) log.debug('{}'.format(seasonMap)) keys = {'source_id': sourceId} leagueMap = db.select(Source_League_Map.createAdhoc(keys)) log.debug('{}'.format(leagueMap)) teams = db.select(Team()) log.debug('{}'.format(teams)) # Process the historical data... for l in leagueMap: for s in seasonMap: url = s.getData_Url().format(l.getLeague()) try: processMatchData(log, db, url, sourceId, s.getSeason()) except Exception as e: log.info('Cannot process %s' % url) # Get the upcoming fixtures too... processMatchData(log, db, source.getUrl(), sourceId, seasonMap[0].getSeason(), leagueMap)
def test_keys_adhoc(self): l = Source.createAdhoc(None) self.assertEqual(l.getTable(), 'source') self.assertTrue(l._keys.getFields() is None)
def test_isNullable(self): obj = Source() self.assertTrue(True and obj.isNullable('url'))