def test_basic_successful_tx(self): with db.tx() as tx: tx.do("INSERT INTO foo (foo_id, value) VALUES (2, 'bar')") tx.do("INSERT INTO foo (foo_id, value) VALUES (3, 'baz')") rows = db.items("SELECT * FROM foo ORDER BY foo_id") assert len(rows) == 3 for index, row in enumerate(rows): assert row.foo_id == index + 1 assert rows[2].value == "baz"
def test_operations(): test_db = db.from_environ("DB_PSYCOPG2_TEST_URL") test_db.do("DROP TABLE IF EXISTS conn_test;") test_db.do("CREATE TABLE conn_test (name TEXT)") assert test_db.count("conn_test") == 0 test_db.do("INSERT INTO conn_test (name) VALUES ('foo');") test_db.do("INSERT INTO conn_test (name) VALUES ('bar');") assert test_db.count("conn_test") == 2 assert set([x.name for x in db.items("SELECT * FROM conn_test")]) == \ set(["foo", "bar"])
def drawPlanets(self, dc, rect): flt = self.rectFilter(rect) if rect else self.visibleAreaFilter() cond = ['owner_id is not null'] if int(config.options['filter']['inhabited_planets'])==1 else [] #print 'request planets with turn %s'%(self.turn,) for p in db.planets(self.turn, self.planet_filter + flt + cond): self.drawPlanet(dc, p) if self.draw_geo: self.drawPlanetGeo(dc, p) for p in db.items('planet_size', flt, ('x', 'y', 's')): self.drawPlanet(dc, p)
def saveTable(table_name, keys, filters, turn_n = None): pt = config.options['data']['path'] pt = os.path.join(pt, str(db.getTurn())) util.assureDirExist(pt) path = os.path.join(pt, '%s.csv'%(table_name,)) try: f = open(path, 'wt') writer = csv.DictWriter(f, keys) writer.writeheader() for p in db.items(table_name, filters, keys, turn_n): try: for s in unicode_strings: if s in p and p[s]: p[s] = p[s].encode('utf-8') writer.writerow(p) except UnicodeEncodeError, e: log.error('failed convert data %s - %s'%(p, e)) except IOError, e: log.error('failed writing data to csv file %s: %s'%(path, e))
p[8] = 1 - p[8] p[3] = p[7] = 0 return tuple(p) db = dict([(dec(k), (v & 1, v >> 1)) for k, v in mdb.items()]) won_1st = 0 won_2nd = 0 winning_1st = 0 winning_2nd = 0 fork_1st = 0 fork_2nd = 0 misleading_fork_1st = {} misleading_fork_2nd = {} for k, v in db.items(): if v[1] == 0: if v[0] == 0: won_1st += 1 continue if v[0] == 1: won_2nd += 1 continue if v[0] == 0: winning_1st += 1 else: winning_2nd += 1 next_pos = [move(k, i) for i in range(3) if k[(k[8] << 2) + i]] result = [db[p][0] for p in next_pos] if (0 in result and 1 in result): if k[8] == 0:
def _load_items(self): if self._items: return self._items = db.items()
def test_items(self): self.sql = "SELECT name FROM tests" null_items = self.Null.items(self.sql) db_items = db.items(self.sql) nn = lambda xs: map(lambda x: x.name, xs) assert nn(null_items) == nn(db_items) == ["foo"]