def get(self, request, username, format=None): mind = Mind(username) tags = mind.get_tags() serializer = TagSerializer(tags, context={'username': username}, many=True) return Response(serializer.data)
def get(self, request, username, tag_name=None, format=None): mind = Mind(username) snippets = mind.get_snippets(tag_name=tag_name, **request.GET.dict()) serializer = SnippetSerializer(snippets, context={'username': username}, many=True) return Response(serializer.data)
def get_object(self, username, pk): log.debug('get object') mind = Mind(username) try: return mind.get_tag(pk) except Tag.DoesNotExist: raise Http404
def create(self, validated_data): """ Create and return a new `Snippet` instance, given the validated data. """ log.info('creating a new tag...') username = self.context.get('username') mind = Mind(username) #TODO: log.debug('validated_data: %s', validated_data) return mind.create_tag(**validated_data)
def setUp(self): self.mind = Mind('leon') #cleaning up testing tags first self.mind.remove_testing_tags() #TODO:test if able to pass init_time and update_time (so far not supported) self.tag_name = self.mind.create_tag(name='tagfortesting' + str(random.randint(0, 10000)), desc='a random tag for testing', private=False) print('Created tag:', self.tag_name) #wait for es to be updated time.sleep(6)
def setUp(self): self.mind = Mind('leon') #TODO:test if able to pass init_time and update_time (so far not supported) s = self.mind.create_snippet(desc='a test snippet', vote=2, tags=['language'], private=False, title="", attachment=None, url="https://git-scm.com/book/en/v7", children=None, context=None) fr = self.mind.create_frame(desc='a frame', private=False, title="a testing frame", attachment=None, children=[s.id]) pfr = self.mind.create_frame(desc='a parent frame', private=False, title="a testing parent frame", attachment=None, children=[fr.id]) self.sid = s.id self.fid = fr.id self.pfid = pfr.id self.assertIsInstance(self.fid, int) #wait for es to be updated time.sleep(6)
def setUp(self): self.mind = Mind('leon') #TODO:test if able to pass init_time and update_time (so far not supported) s = self.mind.create_snippet( desc='Programming language is similar to spoken language', vote=2, tags=['language'], private=False, title="", attachment=None, url="https://git-scm.com/book/en/v7", children=None, context=None) self.sid = s.id self.assertIsInstance(self.sid, int) #wait for es to be updated time.sleep(6)
def update(self, instance, validated_data): """ Update and return an existing `Snippet` instance, given the validated data. """ instance.desc = validated_data.get('desc', instance.desc) instance.private = validated_data.get('private', instance.private) instance.init_time = validated_data.get('init_time', instance.init_time) #allow the post form having no field "name" if validated_data.get('name'): log.info('Updating the tag name...') username = self.context.get('username') mind = Mind(username) # TODO:support updating name together with other fields at the same time mind.update_tag_name(instance.name, validated_data.get('name')) else: log.info('Updating the tag...') # name is not in the post data. instance.save() return instance
class TestSnippet(unittest.TestCase): def setUp(self): self.mind = Mind('leon') #TODO:test if able to pass init_time and update_time (so far not supported) s = self.mind.create_snippet( desc='Programming language is similar to spoken language', vote=2, tags=['language'], private=False, title="", attachment=None, url="https://git-scm.com/book/en/v7", children=None, context=None) self.sid = s.id self.assertIsInstance(self.sid, int) #wait for es to be updated time.sleep(6) def test_get_snippet(self): """ Testing getting the snippet created in the setup This is dependent on whether the webhook configured for the gitlab is running correctly """ print('test_get_snippet:', self.sid) s = self.mind.get_snippet(self.sid) self.assertEqual(s.vote, 2) self.assertEqual(s.desc, 'Programming language is similar to spoken language') self.assertFalse(s.private) self.assertListEqual(s.tags, ['language']) self.assertEqual(s.title, '') self.assertIsNone(s.attachment) self.assertEqual(s.url, 'https://git-scm.com/book/en/v7') # self.assertIsNone(self.context) def test_update_snippet(self): print('test_update_snippet:', self.sid) s = self.mind.get_snippet(self.sid) s.desc = 'Programming language is similar to spoken language!' s.vote = 3 s.private = True s.tags = ['language', 'random thought'] s.title = 'about programming language' s.url = 'https://git-scm.com/book/en/v8' s.save() #wait for es update time.sleep(6) updated_s = self.mind.get_snippet(self.sid) self.assertEqual(updated_s.vote, 3) self.assertEqual( updated_s.desc, 'Programming language is similar to spoken language!') self.assertTrue(updated_s.private) self.assertListEqual(updated_s.tags, ['language', 'random thought']) self.assertEqual(updated_s.title, 'about programming language') self.assertIsNone(updated_s.attachment) self.assertEqual(updated_s.url, 'https://git-scm.com/book/en/v8') def test_get_snippets(self): sns = self.mind.get_snippets(all="y") print('all snippets:', sns) sids = [s.id for s in sns] self.assertIn(str(self.sid), sids) def test_in_frames(self): fr = self.mind.create_snippet(desc='parent of the testing snippet', vote=2, tags=['language'], private=False, title="parent", attachment=None, url="https://git-scm.com/book/en/v7", children=[self.sid], context=None) fid = fr.id print('frame id:', fid) time.sleep(6) frame = self.mind.get_snippet(fid) s = self.mind.get_snippet(self.sid) print('The snippet is in frames:', s.in_frames) self.assertIn(str(fid), s.in_frames) def tearDown(self): print('tear down:', self.sid) s = self.mind.get_snippet(self.sid) s.discard() #wait for es to get updated time.sleep(6) self.assertRaises(Snippet.DoesNotExist, self.mind.get_snippet, self.sid)
def get_object(self, username, pk): mind = Mind(username) try: return mind.get_snippet(pk) except Snippet.DoesNotExist: raise Http404
class TestTag(unittest.TestCase): def setUp(self): self.mind = Mind('leon') #cleaning up testing tags first self.mind.remove_testing_tags() #TODO:test if able to pass init_time and update_time (so far not supported) self.tag_name = self.mind.create_tag(name='tagfortesting' + str(random.randint(0, 10000)), desc='a random tag for testing', private=False) print('Created tag:', self.tag_name) #wait for es to be updated time.sleep(6) def test_get_tag(self): """ Testing getting the tag created in the setup This is dependent on whether the webhook configured for the gitlab is running correctly """ print('test_get_tag:', self.tag_name) t = self.mind.get_tag(self.tag_name) self.assertEqual(t.name, self.tag_name) self.assertEqual(t.desc, 'a random tag for testing') self.assertFalse(t.private) def test_update_tag(self): print('test_update_tag:', self.tag_name) t = self.mind.get_tag(self.tag_name) t.desc = 'Tagging is ok!' t.private = True t.save() #wait for es update time.sleep(6) updated_t = self.mind.get_tag(self.tag_name) self.assertEqual(updated_t.desc, 'Tagging is ok!') self.assertTrue(updated_t.private) def test_update_tag_with_name(self): """Update the tag name together with the content """ print('test_update_tag_with_name:', self.tag_name) t = self.mind.get_tag(self.tag_name) new_name = 'tagfortesting' + str(random.randint(0, 10000)) t.name = new_name t.desc = 'Tagging is ok!' t.private = True self.assertRaises(Tag.DoesNotExist, t.save) def test_get_tags(self): tags = self.mind.get_tags() print('Tags of %s: %s' % (self.mind.username, [tag.name for tag in tags])) def test_update_tag_name(self): new_name = 'tagfortesting' + str(random.randint(0, 10000)) old_name = self.tag_name print('Updating the tag %s to the new name %s' % (old_name, new_name)) self.tag_name = self.mind.update_tag_name(self.tag_name, new_name) time.sleep(6) self.assertEqual(self.tag_name, new_name) t = self.mind.get_tag(new_name) self.assertEqual(t.name, new_name) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, old_name) def test_merge_tag1(self): tag2_name = 'tagfortesting' + str(random.randint(0, 10000)) self.mind.create_tag(name=tag2_name, desc='a random tag for testing', private=False) print('Created tag:', tag2_name) #wait for es to be updated time.sleep(6) new_tag_name = 'tagfortesting' + str(random.randint(0, 10000)) print('Merging the tag %s and tag %s to %s' % (self.tag_name, tag2_name, new_tag_name)) self.mind.merge_tag(self.tag_name, tag2_name, new_tag_name) time.sleep(6) t = self.mind.get_tag(new_tag_name) self.assertEqual(t.name, new_tag_name) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, tag2_name) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, self.tag_name) self.tag_name = new_tag_name #TODO: test_merge_tag2 and test_merge_tag3 don't work currently. Should work on these two again after changing to one tag per yaml file # def test_merge_tag2(self): # tag2_name = 'tagfortesting'+str(random.randint(0, 10000)) # self.mind.create_tag(name=tag2_name, desc='a random tag for testing', private=False) # print('Created tag:', tag2_name) # #wait for es to be updated # time.sleep(6) # new_tag_name = tag2_name # print('Merging the tag %s and tag %s to %s' % (self.tag_name, tag2_name, new_tag_name)) # self.mind.merge_tag(self.tag_name, tag2_name, new_tag_name) # time.sleep(6) # t = self.mind.get_tag(new_tag_name) # self.assertEqual(t.name, new_tag_name) # self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, self.tag_name) # self.tag_name = new_tag_name # def test_merge_tag3(self): # tag2_name = 'tagfortesting'+str(random.randint(0, 10000)) # self.mind.create_tag(name=tag2_name, desc='a random tag for testing', private=False) # print('Created tag:', tag2_name) # #wait for es to be updated # time.sleep(6) # new_tag_name = self.tag_name # print('Merging the tag %s and tag %s to %s' % (self.tag_name, tag2_name, new_tag_name)) # self.mind.merge_tag(self.tag_name, tag2_name, new_tag_name) # time.sleep(6) # t = self.mind.get_tag(new_tag_name) # self.assertEqual(t.name, new_tag_name) # self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, tag2_name) # self.tag_name = new_tag_name def test_update_tag_with_snippets(self): """Testing if sniippets having the tag get changed after tag name changing """ old_name = 'testingsnippetchangeaftertagnamechange' new_name = 'testingsnippetchangeaftertagnamechanged' self.mind.create_tag(name=old_name, desc='a random tag for testing', private=True) time.sleep(6) t = self.mind.get_tag(old_name) self.assertEqual(t.name, old_name) s1 = self.mind.create_snippet( desc='Snippet 1 for testing snippets change with tag name change', vote=-1, tags=[old_name], private=True, title="", attachment=None, url=None, chilren=None, context=None) sid1 = s1.id time.sleep(6) s1 = self.mind.get_snippet(sid1) self.assertEqual( s1.desc, 'Snippet 1 for testing snippets change with tag name change') print('s1.tags:', s1.tags) self.assertListEqual(s1.tags, [old_name]) s2 = self.mind.create_snippet( desc='Snippet 2 for testing snippets change with tag name change', vote=-1, tags=[old_name], private=True, title="", attachment=None, url=None, chilren=None, context=None) sid2 = s2.id time.sleep(6) s2 = self.mind.get_snippet(sid2) print('s2.tags:', s2.tags) self.assertEqual( s2.desc, 'Snippet 2 for testing snippets change with tag name change') self.assertListEqual(s2.tags, [old_name]) self.mind.update_tag_name(old_name, new_name) time.sleep(6) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, old_name) t = self.mind.get_tag(new_name) self.assertEqual(t.name, new_name) s1_new = self.mind.get_snippet(sid1) print('s1_new.tags:', s1_new.tags) self.assertListEqual(s1_new.tags, [new_name]) s2_new = self.mind.get_snippet(sid2) print('s2_new.tags:', s2_new.tags) self.assertListEqual(s2_new.tags, [new_name]) def test_delete_tag_with_snippets(self): """Testing if sniippets having the tag get changed after tag deletion """ tag_name = 'testingsnippetchangeaftertagnamedelete' self.mind.create_tag(name=tag_name, desc='a random tag for testing', private=True) time.sleep(6) t = self.mind.get_tag(tag_name) self.assertEqual(t.name, tag_name) s1 = self.mind.create_snippet( desc='Snippet 1 for testing snippets change with tag deletion', vote=-1, tags=[tag_name], private=True, title="", attachment=None, url=None, chilren=None, context=None) sid1 = s1.id time.sleep(6) s1 = self.mind.get_snippet(sid1) print('s1.tags:', s1.tags) self.assertEqual( s1.desc, 'Snippet 1 for testing snippets change with tag deletion') self.assertListEqual(s1.tags, [tag_name]) s2 = self.mind.create_snippet( desc='Snippet 2 for testing snippets change with tag deletion', vote=-1, tags=[tag_name], private=True, title="", attachment=None, url=None, chilren=None, context=None) sid2 = s2.id time.sleep(6) s2 = self.mind.get_snippet(sid2) print('s2.tags:', s2.tags) self.assertEqual( s2.desc, 'Snippet 2 for testing snippets change with tag deletion') self.assertListEqual(s2.tags, [tag_name]) t.discard() time.sleep(6) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, tag_name) s1_new = self.mind.get_snippet(sid1) print('s1_new.tags:', s1_new.tags) self.assertListEqual(s1_new.tags, []) s2_new = self.mind.get_snippet(sid2) print('s2_new.tags:', s2_new.tags) self.assertListEqual(s2_new.tags, []) def tearDown(self): print('tear down:', self.tag_name) t = self.mind.get_tag(self.tag_name) t.discard() #wait for es to get updated time.sleep(6) self.assertRaises(Tag.DoesNotExist, self.mind.get_tag, self.tag_name) self.mind.remove_testing_tags()
class TestFrame(unittest.TestCase): def setUp(self): self.mind = Mind('leon') #TODO:test if able to pass init_time and update_time (so far not supported) s = self.mind.create_snippet(desc='a test snippet', vote=2, tags=['language'], private=False, title="", attachment=None, url="https://git-scm.com/book/en/v7", children=None, context=None) fr = self.mind.create_frame(desc='a frame', private=False, title="a testing frame", attachment=None, children=[s.id]) pfr = self.mind.create_frame(desc='a parent frame', private=False, title="a testing parent frame", attachment=None, children=[fr.id]) self.sid = s.id self.fid = fr.id self.pfid = pfr.id self.assertIsInstance(self.fid, int) #wait for es to be updated time.sleep(6) def test_get_frame(self): fr = self.mind.get_frame(self.fid) self.assertEqual(fr.vote, 2) self.assertEqual(fr.desc, 'a frame') self.assertFalse(fr.private) # self.assertListEqual(fr.tags, ['language']) self.assertEqual(fr.title, 'a testing frame') self.assertIsNone(fr.attachment) self.assertListEqual(fr.children, [self.sid]) def test_get_frames(self): frs = self.mind.get_frames(all="y") frids = [fr.id for fr in frs] self.assertIn(str(self.fid), frids) self.assertIn(str(self.pfid), frids) def test_update_frame(self): fr = self.mind.get_frame(self.fid) fr.title = 'testing updating the frame' fr.desc = 'testing updating the frame' fr.vote = 10 #vote shouldn't be modified. Vote should only be computed from the children fr.tags = ['play'] #tags also should only be computed from the children fr.private = True fr.save() time.sleep(6) updated_fr = self.mind.get_frame(self.fid) self.assertEqual(fr.vote, 2) self.assertEqual(fr.desc, 'testing updating the frame') self.assertTrue(fr.private) # self.assertListEqual(fr.tags, ['language']) self.assertEqual(fr.title, 'testing updating the frame') self.assertIsNone(fr.attachment) self.assertListEqual(fr.children, [self.sid]) def test_add_children(self): fr = self.mind.get_frame(self.fid) fr.add_children([1,2,3]) #the availbility of children are not verified. time.sleep(6) updated_fr = self.mind.get_frame(self.fid) self.assertListEqual(fr.children, [self.sid, 1, 2, 3]) def test_remove_children(self): fr = self.mind.get_frame(self.fid) fr.remove_children([self.sid]) #can remove one child or a list of children time.sleep(6) updated_fr = self.mind.get_frame(self.fid) self.assertListEqual(fr.children, []) def test_save_children_order(self): fr = self.mind.get_frame(self.fid) fr.add_children([1,2,3]) #the availbility of children are not verified. time.sleep(6) updated_fr = self.mind.get_frame(self.fid) updated_fr.save_children_order([3,2,1,self.sid]) time.sleep(6) updated_fr = self.mind.get_frame(self.fid) self.assertListEqual(updated_fr.children, [3,2,1,self.sid]) def test_get_related_frames(self): fr2 = self.mind.create_frame(desc='a frame', private=False, title="a testing frame", attachment=None, children=[self.sid]) time.sleep(6) fr1 = self.mind.get_frame(self.fid) print('sid:%s, fr1.id: %s, fr2.id: %s' % (self.sid, fr1.id, fr2.id)) print('fr1.children:', fr1.children) print('fr2.children:', fr2.children) self.assertIn(str(fr2.id), self.mind.get_related_frames(fr1.id)) def test_in_frames(self): fr = self.mind.get_frame(self.fid) print('The frame is in frames:', fr.in_frames) self.assertIn(str(self.pfid), fr.in_frames) def tearDown(self): print('tear down:', self.fid) fr = self.mind.get_frame(self.fid) fr.discard() pfr = self.mind.get_frame(self.pfid) pfr.discard() s = self.mind.get_snippet(self.sid) s.discard() #wait for es to get updated time.sleep(6) self.assertRaises(Frame.DoesNotExist, self.mind.get_frame, self.fid) self.assertRaises(Frame.DoesNotExist, self.mind.get_frame, self.pfid) self.assertRaises(Snippet.DoesNotExist, self.mind.get_snippet, self.sid)