def tearDown(self): IndexSet({"title": "Ploni"}).delete() IndexSet({"title": "Shmoni"}).delete() HistorySet({"title": "Ploni"}).delete() HistorySet({"title": "Shmoni"}).delete() HistorySet({"version": "Ploni Edition"}).delete() HistorySet({"new.refs": {"$regex": "^Ploni on Job"}}).delete() HistorySet({"new.refs": {"$regex": "^Shmoni on Job"}}).delete() VersionStateSet({"title": "Ploni on Job"}).delete() VersionStateSet({"title": "Shmoni on Job"}).delete()
def test_post_new_text(self): """ Tests: post of index & that new index is in index/titles post and get of English text post and get of Hebrew text Verify that in-text ref is caught and made a link Verify that changing of in-text ref results in old link removed and new one added counts docs of both he and en index delete and its cascading """ # Post a new Index index = { "title": "Sefer Test", "titleVariants": ["The Book of Test"], "sectionNames": ["Chapter", "Paragraph"], "categories": ["Musar"], } response = c.post("/api/index/Sefer_Test", {'json': json.dumps(index)}) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertIn("titleVariants", data) self.assertIn(u'Sefer Test', data["titleVariants"]) response = c.get("/api/index/titles") data = json.loads(response.content) self.assertIn(u'Sefer Test', data["books"]) #test the toc is updated toc = json.loads(c.get("/api/index").content) tutils.verify_title_existence_in_toc(index['title'], index['categories']) # Post Text (with English citation) text = { "text": "As it is written in Job 3:14, waste places.", "versionTitle": "The Test Edition", "versionSource": "www.sefaria.org", "language": "en", } response = c.post("/api/texts/Sefer_Test.99.99", {'json': json.dumps(text)}) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertTrue("error" not in data) # Verify one link was auto extracted response = c.get('/api/texts/Sefer_Test.99.99') self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data["commentary"])) # Verify Count doc was updated response = c.get('/api/counts/Sefer_Test') self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertNotIn("error", data) self.assertEqual([1, 1], data["_en"]["availableCounts"]) self.assertEqual(1, data["_en"]["availableTexts"][98][98]) self.assertEqual(0, data["_en"]["availableTexts"][98][55]) # Update link in the text text = { "text": "As it is written in Job 4:10, The lions may roar and growl.", "versionTitle": "The Test Edition", "versionSource": "www.sefaria.org", "language": "en", } response = c.post("/api/texts/Sefer_Test.99.99", {'json': json.dumps(text)}) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertTrue("error" not in data) # Verify one link was auto extracted response = c.get('/api/texts/Sefer_Test.99.99') self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data["commentary"])) self.assertEqual(data["commentary"][0]["ref"], 'Job 4:10') # Post Text (with Hebrew citation) text = { "text": 'כדכתיב: "לא תעשה לך פסל כל תמונה" כו (דברים ה ח)', "versionTitle": "The Hebrew Test Edition", "versionSource": "www.sefaria.org", "language": "he", } response = c.post("/api/texts/Sefer_Test.88.88", {'json': json.dumps(text)}) self.assertEqual(200, response.status_code) # Verify one link was auto extracted response = c.get('/api/texts/Sefer_Test.88.88') self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data["commentary"])) # Verify count doc was updated response = c.get('/api/counts/Sefer_Test') self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual([1, 1], data["_he"]["availableCounts"]) self.assertEqual(1, data["_he"]["availableTexts"][87][87]) self.assertEqual(0, data["_en"]["availableTexts"][87][87]) # Delete Test Index textRegex = Ref('Sefer Test').regex() IndexSet({"title": u'Sefer Test'}).delete() #Make sure that index was deleted, and that delete cascaded to: versions, counts, links, cache, #todo: notes?, reviews? self.assertEqual(0, IndexSet({"title": u'Sefer Test'}).count()) self.assertEqual(0, VersionSet({"title": u'Sefer Test'}).count()) self.assertEqual(0, VersionStateSet({"title": u'Sefer Test'}).count()) #todo: better way to do this? self.assertEqual(0, LinkSet({"refs": {"$regex": textRegex}}).count())
def test_change_index_name(self): #Set up an index and text to test index = { "title": "Name Change Test", "titleVariants": ["The Book of Name Change Test"], "sectionNames": ["Chapter", "Paragraph"], "categories": ["Musar"], } response = c.post("/api/index/Name_Change_Test", {'json': json.dumps(index)}) self.assertEqual(200, response.status_code) self.assertEqual( 1, HistorySet({ "rev_type": "add index", "title": "Name Change Test" }).count()) self.in_cache("Name Change Test") # Post some text, including one citation text = { "text": "Blah blah blah Genesis 5:12 blah", "versionTitle": "The Name Change Test Edition", "versionSource": "www.sefaria.org", "language": "en", } response = c.post("/api/texts/Name_Change_Test.1.1", {'json': json.dumps(text)}) self.assertEqual(200, response.status_code) self.assertEqual( 1, LinkSet({ "refs": { "$regex": "^Name Change Test" } }).count()) self.assertEqual( 1, HistorySet({ "version": "The Name Change Test Edition", "rev_type": "add text" }).count()) self.assertEqual( 1, HistorySet({ "new.refs": { "$regex": "Name Change Test" }, "rev_type": "add link" }).count()) # Test posting notes and links note1 = { 'title': u'test title 1', 'text': u'test body 1', 'type': u'note', 'ref': u'Name Change Test 1.1', 'public': False } note2 = { 'title': u'test title 2', 'text': u'test body 2', 'type': u'note', 'ref': u'Name Change Test 1.1', 'public': True } link1 = { 'refs': ['Name Change Test 1.1', 'Genesis 1:5'], 'type': 'reference' } link2 = { 'refs': ['Name Change Test 1.1', 'Rashi on Genesis 1:5'], 'type': 'reference' } # Post notes and refs and record ids of records for o in [note1, note2, link1, link2]: url = "/api/notes/" if o['type'] == 'note' else "/api/links/" response = c.post(url, {'json': json.dumps(o)}) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertIn("_id", data) o["id"] = data["_id"] # test history self.assertEqual(1, HistorySet({ "new.ref": { "$regex": "Name Change Test" }, "rev_type": "add note" }).count()) # only one is public self.assertEqual( 3, HistorySet({ "new.refs": { "$regex": "Name Change Test" }, "rev_type": "add link" }).count()) # Change name of index record orig = json.loads(c.get("/api/index/Name_Change_Test").content) new = deepcopy(orig) new["oldTitle"] = orig["title"] new["title"] = "Name Changed" new["titleVariants"].remove("Name Change Test") response = c.post("/api/index/Name_Changed", {'json': json.dumps(new)}) self.assertEqual(200, response.status_code) # Check for change on index record response = c.get("/api/index/Name_Changed") self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertTrue(u"Name Changed" == data["title"]) self.assertIn(u"Name Changed", data["titleVariants"]) self.assertTrue(u"Name Change Test" not in data["titleVariants"]) # In History self.assertEqual( 0, HistorySet({ "rev_type": "add index", "title": "Name Change Test" }).count()) self.assertEqual( 0, HistorySet({ "ref": { "$regex": "Name Change Test" }, "rev_type": "add text" }).count()) self.assertEqual( 0, HistorySet({ "new.ref": { "$regex": "Name Change Test" }, "rev_type": "add note" }).count()) self.assertEqual( 0, HistorySet({ "new.refs": { "$regex": "Name Change Test" }, "rev_type": "add link" }).count()) self.assertEqual( 1, HistorySet({ "rev_type": "add index", "title": "Name Changed" }).count()) self.assertEqual( 1, HistorySet({ "ref": { "$regex": "Name Changed" }, "rev_type": "add text" }).count()) self.assertEqual( 1, HistorySet({ "new.ref": { "$regex": "Name Changed" }, "rev_type": "add note" }).count()) self.assertEqual( 3, HistorySet({ "new.refs": { "$regex": "Name Changed" }, "rev_type": "add link" }).count()) # In cache self.not_in_cache("Name Change Test") self.in_cache("Name Changed") # And in the titles api response = c.get("/api/index/titles") data = json.loads(response.content) self.assertTrue(u"Name Changed" in data["books"]) self.assertTrue(u"Name Change Test" not in data["books"]) #toc changed toc = json.loads(c.get("/api/index").content) tutils.verify_title_existence_in_toc(new["title"], orig['categories']) self.assertEqual(2, NoteSet({ "ref": { "$regex": "^Name Changed" } }).count()) self.assertEqual( 3, LinkSet({ "refs": { "$regex": "^Name Changed" } }).count()) # Now delete a link and a note response = c.delete("/api/links/" + link1["id"]) self.assertEqual(200, response.status_code) response = c.delete("/api/notes/" + note2["id"]) self.assertEqual(200, response.status_code) # Make sure two are now deleted self.assertEqual(1, NoteSet({ "ref": { "$regex": "^Name Changed" } }).count()) self.assertEqual( 2, LinkSet({ "refs": { "$regex": "^Name Changed" } }).count()) # and that deletes show up in history self.assertEqual( 1, HistorySet({ "old.ref": { "$regex": "Name Changed" }, "rev_type": "delete note" }).count()) self.assertEqual( 1, HistorySet({ "old.refs": { "$regex": "Name Changed" }, "rev_type": "delete link" }).count()) # Delete Test Index IndexSet({"title": u'Name Changed'}).delete() # Make sure that index was deleted, and that delete cascaded to: versions, counts, links, cache self.not_in_cache("Name Changed") self.assertEqual(0, IndexSet({"title": u'Name Changed'}).count()) self.assertEqual(0, VersionSet({"title": u'Name Changed'}).count()) self.assertEqual(0, VersionStateSet({ "title": u'Name Changed' }).count()) self.assertEqual( 0, LinkSet({ "refs": { "$regex": "^Name Changed" } }).count()) self.assertEqual(1, NoteSet({ "ref": { "$regex": "^Name Changed" } }).count()) # Notes are note removed