def setUp(self): welcomeNote = Note() welcomeNote.name = "Welcome to Note Space!" welcomeNote.desc = "Introduction and Quick guide" welcomeNote.owner = "Note Space Team" welcomeNote.save() econNote = Note() econNote.name = "Basic Economics" econNote.subject = "Economics" econNote.desc = "This note is about demands, suplies and how market works." econNote.owner = "Susan" econNote.save() img1 = Image() img1.index = 1 img1.note = econNote img1.image = SimpleUploadedFile( name='IMG_0809.JPG', content=open( "C:/Users/B/Desktop/NoteSpace/static/TestNotes/Econ/IMG_0809.JPG", 'rb').read(), content_type='image/jpeg') img1.save() img2 = Image() img2.index = 2 img2.note = econNote img2.image = SimpleUploadedFile( name='IMG_0810.JPG', content=open( "C:/Users/B/Desktop/NoteSpace/static/TestNotes/Econ/IMG_0810.JPG", 'rb').read(), content_type='image/jpeg') img2.save() img3 = Image() img3.index = 3 img3.note = econNote img3.image = SimpleUploadedFile( name='IMG_0811.JPG', content=open( "C:/Users/B/Desktop/NoteSpace/static/TestNotes/Econ/IMG_0811.JPG", 'rb').read(), content_type='image/jpeg') img3.save() img4 = Image() img4.index = 4 img4.note = econNote img4.image = SimpleUploadedFile( name='IMG_0812.JPG', content=open( "C:/Users/B/Desktop/NoteSpace/static/TestNotes/Econ/IMG_0812.JPG", 'rb').read(), content_type='image/jpeg') img4.save() User.objects.create_user("smith", "*****@*****.**", "1234") self.browser = webdriver.Firefox()
def test_database_can_save_and_get_one_Note_multiple_images(self): n = Note() n.name = "test note" n.desc = "this is note for ..." n.save() n_id = n.id img1 = Image() img1.index = 1 img1.note = n img1.image = SimpleUploadedFile( name='1.jpg', content=open( "C:/Users/B/OneDrive/Documents/61FC8C1A-D1FE-4D09-ABE4-BE1689D03C8E.jpg", 'rb').read(), content_type='image/jpeg') img1.save() img2 = Image() img2.index = 2 img2.note = n img2.image = SimpleUploadedFile( name='1.jpg', content=open( "C:/Users/B/OneDrive/Documents/61FC8C1A-D1FE-4D09-ABE4-BE1689D03C8E.jpg", 'rb').read(), content_type='image/jpeg') img2.save() n = Note.objects.get(pk=n_id) images = Image.objects.filter(note=n) self.assertEqual(images.count(), 2)
def test_database_can_search_by_similar(self): from django.db.models import Q note1_namesearch = Note() note1_namesearch.name = "django" note1_namesearch.save() note2_descsearch = Note() note2_descsearch.name = "writing unit test" note2_descsearch.desc = "for django" note2_descsearch.save() Tag_django = Tag() Tag_django.title = "django" Tag_django.save() note3_tagsearch = Note() note3_tagsearch.name = "Html Template Tags" note3_tagsearch.save() note3_tagsearch.tags.add(Tag_django) note3_tagsearch.save() note4_ownersearch = Note() note4_ownersearch.name = "FAQ" note4_ownersearch.owner = "Django official" note4_ownersearch.save() search_result = list( Note.objects.filter( Q(name__icontains="django") | Q(desc__icontains="django") | Q(tags__title__icontains="django") | Q(owner__icontains="django"))) print(search_result) self.assertGreaterEqual(len(search_result), 4) self.assertIn(note1_namesearch, search_result) self.assertIn(note2_descsearch, search_result) self.assertIn(note3_tagsearch, search_result) self.assertIn(note4_ownersearch, search_result) search_result = Note.objects.filter( name__trigram_similar="django").filter( desc__trigram_similar="django")
import pymongo myclient = pymongo.MongoClient("mongodb://192.168.0.104:27017/") mydb = myclient["xiaohongshu"] mycol = mydb["notes2"] myquery = {} mydoc = mycol.find(myquery) for x in mydoc: if x['NoteView']['noteInfo']['type'] == 'normal': note = Note() note.id = x['NoteView']['noteInfo']['id'] note.title = x['NoteView']['noteInfo']['title'] note.type = x['NoteView']['noteInfo']['type'] note.desc = x['NoteView']['noteInfo']['desc'] note.likes = x['NoteView']['noteInfo']['likes'] note.cover = x['NoteView']['noteInfo']['cover']['original'].replace('http://ci.xiaohongshu.com/','') note.collects = x['NoteView']['noteInfo']['collects'] imageStr = '' for image in x['NoteView']['noteInfo']['images']: imageStr += (image['original'].replace('http://ci.xiaohongshu.com/','') + ',') note.images = imageStr note.user_id = x['NoteView']['noteInfo']['user']['id'] # 检查msyql数据库是否含有改笔记 checkresults = Note.objects.filter(id=note.id) if len(checkresults) == 0: note.save() else: print('已存在!')