def test_save(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} status = s.save([a], timestamp=timestamp, ip="1.2.3.4", author=None, comment="Testing create.", action="save") assert status['changes'][0]['revision'] == 1 assert self.get_json('/a') == update_doc(a, 1, timestamp, timestamp) a['title'] = 'b' timestamp2 = datetime.datetime(2010, 02, 02, 02, 02, 02) status = s.save([a], timestamp=timestamp2, ip="1.2.3.4", author=None, comment="Testing update.", action="save") assert status['changes'][0]['revision'] == 2 assert self.get_json('/a') == update_doc(a, 2, timestamp, timestamp2)
def test_type_change(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} status = s.save([a], timestamp=timestamp, ip="1.2.3.4", author=None, comment="Testing create.", action="save") # insert new type type_delete_id = db.insert("thing", key='/type/delete') a['type']['key'] = '/type/delete' timestamp2 = datetime.datetime(2010, 2, 2, 2, 2, 2) status = s.save([a], timestamp=timestamp2, ip="1.2.3.4", author=None, comment="Testing type change.", action="save") assert status['changes'][0]['revision'] == 2 assert self.get_json('/a') == update_doc(a, 2, timestamp, timestamp2) thing = db.select("thing", where="key='/a'")[0] assert thing.type == type_delete_id
def test_transaction(self, wildcard): docs = [{ "key": "/foo", "type": { "key": "/type/object" }, }] s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) changeset = s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save") changeset.pop("docs") changeset.pop("old_docs") assert changeset == { "id": wildcard, "kind": "save", "timestamp": timestamp.isoformat(), "bot": False, "comment": "foo", "ip": "1.2.3.4", "author": None, "changes": [{ "key": "/foo", "revision": 1 }], "data": {} }
def test_save_with_new_type(self): docs = [{ "key": "/type/foo", "type": { "key": "/type/type" } }, { "key": "/foo", "type": { "key": "/type/foo" } }] s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save") type = db.query("SELECT * FROM thing where key='/type/foo'")[0] thing = db.query("SELECT * FROM thing where key='/foo'")[0] assert thing.type == type.id
def test_existing(self): def insert(doc, revision, created, last_modified): id = db.insert( 'thing', key=doc['key'], latest_revision=revision, created=created, last_modified=last_modified, ) db.insert( 'data', seqname=False, thing_id=id, revision=revision, data=json.dumps(doc), ) created = datetime.datetime(2010, 1, 1, 1, 1, 1) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} insert(a, 1, created, created) s = SaveImpl(db) timestamp = datetime.datetime(2010, 2, 2, 2, 2, 2) records = s._get_records_for_save([a], timestamp) assert_record(records[0], a, 2, created, timestamp)
def test_type_change(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} status = s.save([a], timestamp=timestamp, ip="1.2.3.4", author=None, comment="Testing create.", action="save") # insert new type type_delete_id = db.insert("thing", key='/type/delete') a['type']['key'] = '/type/delete' timestamp2 = datetime.datetime(2010, 02, 02, 02, 02, 02) status = s.save([a], timestamp=timestamp2, ip="1.2.3.4", author=None, comment="Testing type change.", action="save") assert status['changes'][0]['revision'] == 2 assert self.get_json('/a') == update_doc(a, 2, timestamp, timestamp2) thing = db.select("thing", where="key='/a'")[0] assert thing.type == type_delete_id
def test_save(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} status = s.save([a], timestamp=timestamp, ip="1.2.3.4", author=None, comment="Testing create.", action="save") assert status['changes'][0]['revision'] == 1 assert self.get_json('/a') == update_doc(a, 1, timestamp, timestamp) a['title'] = 'b' timestamp2 = datetime.datetime(2010, 2, 2, 2, 2, 2) status = s.save([a], timestamp=timestamp2, ip="1.2.3.4", author=None, comment="Testing update.", action="save") assert status['changes'][0]['revision'] == 2 assert self.get_json('/a') == update_doc(a, 2, timestamp, timestamp2)
def test_save_with_long_string(self): docs = [{ "key": "/type/foo", "type": {"key": "/type/type"}, "title": "a" * 4000 }] s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save")
def _save(self, docs): s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) return s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save")
def _save(self, docs, author=None, ip="1.2.3.4", comment="testing", kind="test_save", timestamp=None, data=None): timestamp = timestamp=timestamp or datetime.datetime(2010, 01, 02, 03, 04, 05) s = SaveImpl(db) s.save(docs, timestamp=timestamp, comment=comment, ip=ip, author=author, action=kind, data=data )
def test_new(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} b = {"key": "/b", "type": {"key": "/type/object"}, "title": "b"} docs = [a, b] records = s._get_records_for_save(docs, timestamp) assert len(records) == 2 assert_record(records[0], docs[0], 1, timestamp, timestamp) assert_record(records[1], docs[1], 1, timestamp, timestamp)
def test_new(self): s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} b = {"key": "/b", "type": {"key": "/type/object"}, "title": "b"} docs = [a, b] records = s._get_records_for_save(docs, timestamp) assert len(records) == 2 assert_record(records[0], docs[0], 1, timestamp, timestamp) assert_record(records[1], docs[1], 1, timestamp, timestamp)
def test_existing(self): def insert(doc, revision, created, last_modified): id = db.insert('thing', key=doc['key'], latest_revision=revision, created=created, last_modified=last_modified) db.insert('data', seqname=False, thing_id=id, revision=revision, data=simplejson.dumps(doc)) created = datetime.datetime(2010, 01, 01, 01, 01, 01) a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} insert(a, 1, created, created) s = SaveImpl(db) timestamp = datetime.datetime(2010, 02, 02, 02, 02, 02) records = s._get_records_for_save([a], timestamp) assert_record(records[0], a, 2, created, timestamp)
def test_save_with_long_string(self): docs = [ {"key": "/type/foo", "type": {"key": "/type/type"}, "title": "a" * 4000} ] s = SaveImpl(db) timestamp = datetime.datetime(2010, 1, 1, 1, 1, 1) s.save( docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save", )
def reindex_transaction(id): logging.info("reindexing %s", id) db = get_db() impl = SaveImpl(db) with db.transaction(): tx = db.query("SELECT id, data FROM transaction where id=$id", vars=locals())[0] db.delete("transaction_index", where="tx_id=$id", vars=locals()) logging.info("tx.data %s", tx.data) if tx.data: data = simplejson.loads(tx.data) impl._index_transaction_data(id, data)
def test_reindex(self): a = {"key": "/a", "type": {"key": "/type/object"}, "title": "a"} self._save([a]) thing = db.query("SELECT * FROM thing WHERE key='/a'")[0] key_id = db.query( "SELECT * FROM property WHERE type=$thing.type AND name='title'", vars=locals())[0].id # there should be only one entry in the index d = db.query( "SELECT * FROM datum_str WHERE thing_id=$thing.id AND key_id=$key_id", vars=locals()) assert len(d) == 1 # corrupt the index table by adding bad entries for i in range(10): db.insert("datum_str", thing_id=thing.id, key_id=key_id, value="foo %d" % i) # verify that the bad entries are added d = db.query( "SELECT * FROM datum_str WHERE thing_id=$thing.id AND key_id=$key_id", vars=locals()) assert len(d) == 11 # reindex now and verify again that there is only one entry SaveImpl(db).reindex(["/a"]) d = db.query( "SELECT * FROM datum_str WHERE thing_id=$thing.id AND key_id=$key_id", vars=locals()) assert len(d) == 1
def test_save_with_new_type(self): docs = [{ "key": "/type/foo", "type": {"key": "/type/type"} }, { "key": "/foo", "type": {"key": "/type/foo"} }] s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save") type = db.query("SELECT * FROM thing where key='/type/foo'")[0] thing = db.query("SELECT * FROM thing where key='/foo'")[0] assert thing.type == type.id
def save_many(self, docs, timestamp, comment, data, ip, author, action=None): docs = list(docs) action = action or "bulk_update" logger.debug( "saving %d docs - %s", len(docs), dict( timestamp=timestamp, comment=comment, data=data, ip=ip, author=author, action=action, ), ) s = SaveImpl(self.db, self.schema, self.indexer, self.property_manager) # Hack to allow processing of json data before using. Required for OL legacy. s.process_json = process_json docs = common.format_data(docs) changeset = s.save( docs, timestamp=timestamp, comment=comment, ip=ip, author=author, action=action, data=data, ) # update cache. # Use the docs from result as they contain the updated revision and last_modified fields. for doc in changeset.get('docs', []): web.ctx.new_objects[doc['key']] = json.dumps(doc) return changeset
def _save(self, docs, author=None, ip="1.2.3.4", comment="testing", kind="test_save", timestamp=None, data=None): timestamp = timestamp = timestamp or datetime.datetime( 2010, 01, 02, 03, 04, 05) s = SaveImpl(db) s.save(docs, timestamp=timestamp, comment=comment, ip=ip, author=author, action=kind, data=data)
def test_transaction(self, wildcard): docs = [{ "key": "/foo", "type": {"key": "/type/object"}, }] s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) changeset = s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save") changeset.pop("docs") changeset.pop("old_docs") assert changeset == { "id": wildcard, "kind": "save", "timestamp": timestamp.isoformat(), "bot": False, "comment": "foo", "ip": "1.2.3.4", "author": None, "changes": [{"key": "/foo", "revision": 1}], "data": {} }
def verify_index(tx_id): logging.info("verifying %s", tx_id) db = get_db() impl = SaveImpl(db) tx = db.query("SELECT id, action, created, data FROM transaction where id=$tx_id", vars=locals())[0] if tx.data: d = db.query("SELECT * FROM transaction_index WHERE tx_id=$tx_id", vars=locals()) index = sorted((row.key, row.value) for row in d) tx_data = simplejson.loads(tx.data) index2 = compute_index(tx_data) if index != index2: print "\t".join([str(x) for x in [tx.id, tx.action, tx.created.isoformat(), index, index2]])
def _save(self, docs): s = SaveImpl(db) timestamp = datetime.datetime(2010, 01, 01, 01, 01, 01) return s.save(docs, timestamp=timestamp, comment="foo", ip="1.2.3.4", author=None, action="save")
def reindex(self, keys): s = SaveImpl(self.db, self.schema, self.indexer, self.property_manager) # Hack to allow processing of json before using. Required for OL legacy. s.process_json = process_json return s.reindex(keys)