Beispiel #1
0
 def setUp(self) -> None:
     collection = Connection.get_collection(SimplePerson)
     collection.delete_many({})
     collection = Connection.get_collection(SimpleSinger)
     collection.delete_many({})
     collection = Connection.get_collection(SimpleAlbum)
     collection.delete_many({})
Beispiel #2
0
 def test_1l_1f_allows_deletion(self):
     user = LinkedDUser(name='crazy')
     user.save()
     collection = Connection.get_collection(LinkedDUser)
     self.assertEqual(collection.count_documents({}), 1)
     user.delete()
     collection = Connection.get_collection(LinkedDUser)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #3
0
 def test1f_1l_allows_deletion(self):
     profile = LinkedDProfile(name='crazy')
     profile.save()
     collection = Connection.get_collection(LinkedDProfile)
     self.assertEqual(collection.count_documents({}), 1)
     profile.delete()
     collection = Connection.get_collection(LinkedDProfile)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #4
0
 def test_1_many_allows_deletion(self):
     notebook = LinkedNotebook(name='N', notes=[])
     notebook.save()
     collection = Connection.get_collection(LinkedNotebook)
     self.assertEqual(collection.count_documents({}), 1)
     notebook.delete()
     collection = Connection.get_collection(LinkedNotebook)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #5
0
 def test_many_1_allows_deletion(self):
     note = LinkedRNote(name='N')
     note.save()
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 1)
     note.delete()
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #6
0
 def test_many_many_allows_deletion(self):
     owner = LinkedOwner(name='OK')
     owner.save()
     collection = Connection.get_collection(LinkedOwner)
     self.assertEqual(collection.count_documents({}), 1)
     owner.delete()
     collection = Connection.get_collection(LinkedOwner)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #7
0
 def setUp(self) -> None:
     collection = Connection.get_collection(SimpleRecord)
     collection.delete_many({})
     collection = Connection.get_collection(SimpleORecord)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedRecord)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedContent)
     collection.delete_many({})
Beispiel #8
0
 def test_1l_1f_list_can_be_nullified(self):
     author = LinkedAuthor(name='A', posts=[
         LinkedPost(title='P1', content='C1'),
         LinkedPost(title='P2', content='C2')])
     author.save()
     collection = Connection.get_collection(LinkedAuthor)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedPost)
     self.assertEqual(collection.count_documents({}), 2)
     author.delete()
     for obj in collection.find():
         self.assertNotIn('authorId', obj)
Beispiel #9
0
 def test_1l_1f_cascade_delete(self):
     balance = LinkedBalance(name='A', account=LinkedAccount(name='B'))
     balance.save()
     collection = Connection.get_collection(LinkedAccount)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedBalance)
     self.assertEqual(collection.count_documents({}), 1)
     balance.delete()
     collection = Connection.get_collection(LinkedAccount)
     self.assertEqual(collection.count_documents({}), 0)
     collection = Connection.get_collection(LinkedBalance)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #10
0
 def test_1l_1f_instance_can_be_nullified(self):
     user = LinkedUser(name='crazy')
     profile = LinkedProfile(name='six')
     user.profile = profile
     user.save()
     collection = Connection.get_collection(LinkedUser)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedProfile)
     self.assertEqual(collection.count_documents({}), 1)
     user.delete()
     for obj in collection.find():
         self.assertNotIn('userId', obj)
Beispiel #11
0
 def test_many_1_cascade_delete(self):
     buyer = LinkedCBuyer(name='B')
     order1 = LinkedCOrder(name='O1')
     order2 = LinkedCOrder(name='O2')
     buyer.orders = [order1, order2]
     buyer.save()
     collection = Connection.get_collection(LinkedCOrder)
     self.assertEqual(collection.count_documents({}), 2)
     order1.delete()
     self.assertEqual(collection.count_documents({}), 0)
     collection = Connection.get_collection(LinkedCBuyer)
     self.assertEqual(collection.count_documents({}), 0)
Beispiel #12
0
 def test_many_many_can_be_nullified(self):
     course1 = LinkedCourse(name='C1')
     course2 = LinkedCourse(name='C2')
     student1 = LinkedStudent(name='S1')
     student2 = LinkedStudent(name='S2')
     course1.students = [student1, student2]
     course2.students = [student1, student2]
     course1.save() # this triggers save also for course2
     collname = 'linkedcoursesstudentslinkedstudentscourses'
     collection = Connection('linked').collection(collname)
     self.assertEqual(collection.count_documents({}), 4)
     course1.delete()
     self.assertEqual(collection.count_documents({}), 2)
Beispiel #13
0
 def test_many_many_unlink_is_saved(self):
     course1 = LinkedCourse(name='C1')
     course2 = LinkedCourse(name='C2')
     student1 = LinkedStudent(name='S1')
     student2 = LinkedStudent(name='S2')
     course1.students = [student1, student2]
     course2.students = [student1, student2]
     course1.save()
     course1.students.remove(student1)
     course1.save()
     collname = 'linkedcoursesstudentslinkedstudentscourses'
     collection = Connection('linked').collection(collname)
     self.assertEqual(collection.count_documents({}), 3)
Beispiel #14
0
 def test_many_1_denies_deletion(self):
     notebook = LinkedRNotebook(name='N',
                               notes=[{'name': 'A'}, {'name': 'B'}])
     notebook.save()
     collection = Connection.get_collection(LinkedRNotebook)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 2)
     self.assertRaises(DeletionDeniedException, notebook.notes[0].delete)
     collection = Connection.get_collection(LinkedRNotebook)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 2)
Beispiel #15
0
 def test_1f_1l_denies_deletion(self):
     user = LinkedCUser(name='crazy')
     profile = LinkedDProfile(name='six')
     user.profile = profile
     user.save()
     collection = Connection.get_collection(LinkedCUser)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedDProfile)
     self.assertEqual(collection.count_documents({}), 1)
     self.assertRaises(DeletionDeniedException, profile.delete)
     collection = Connection.get_collection(LinkedCUser)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedDProfile)
     self.assertEqual(collection.count_documents({}), 1)
Beispiel #16
0
 def test_many_1_reverse_deny_has_nullify_effect(self):
     notebook = LinkedRNotebook(name='N',
                                notes=[{'name': 'A'}, {'name': 'B'}])
     notebook.save()
     collection = Connection.get_collection(LinkedRNotebook)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 2)
     notebook.delete()
     collection = Connection.get_collection(LinkedRNotebook)
     self.assertEqual(collection.count_documents({}), 0)
     collection = Connection.get_collection(LinkedRNote)
     self.assertEqual(collection.count_documents({}), 2)
     for item in collection.find({}):
         self.assertNotIn('notebookId', item)
    def test_encode_embedded_instance_list(self):
        @pymongo
        @jsonclass
        class MediumEncodeEmbeddedInstanceAddress:
            id: str = types.readonly.str.primary.mongoid.required
            line1: str

        @pymongo
        @jsonclass
        class MediumEncodeEmbeddedInstance:
            id: str = types.readonly.str.primary.mongoid.required
            addresses: List[MediumEncodeEmbeddedInstanceAddress]

        medium_object = MediumEncodeEmbeddedInstance(
            addresses=[{
                'line1': 'Flam Road'
            }, {
                'line1': 'Plam Road'
            }])
        batch_command = Encoder().encode_root(medium_object)
        commands = batch_command.commands
        self.assertEqual(len(commands), 1)
        command = commands[0]
        self.assertIs(command.collection,
                      Connection.get_collection(MediumEncodeEmbeddedInstance))
        data = command.object
        addresses = data['addresses']
        self.assertEqual(len(addresses), 2)
        self.assertEqual(addresses[0]['line1'], 'Flam Road')
        self.assertEqual(addresses[1]['line1'], 'Plam Road')
    def test_encode_embedded_instance(self):
        @pymongo
        @jsonclass
        class SimpleEncodeEmbeddedInstanceAddress:
            id: str = types.readonly.str.primary.mongoid.required
            line1: str

        @pymongo
        @jsonclass
        class SimpleEncodeEmbeddedInstance:
            id: str = types.readonly.str.primary.mongoid.required
            address: SimpleEncodeEmbeddedInstanceAddress

        simple_object = SimpleEncodeEmbeddedInstance(
            address={'line1': 'Flam Road'})
        batch_command = Encoder().encode_root(simple_object)
        commands = batch_command.commands
        self.assertEqual(len(commands), 1)
        command = commands[0]
        self.assertIs(command.collection,
                      Connection.get_collection(SimpleEncodeEmbeddedInstance))
        data = command.object
        address = data['address']
        self.assertIsInstance(address['_id'], ObjectId)
        self.assertEqual(address['line1'], 'Flam Road')
 def setUp(self) -> None:
     collection = Connection.get_collection(LinkedAccount)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedBalance)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedSong)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedSinger)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedAlbum)
     collection.delete_many({})
     collection = Connection.get_collection(LinkedArtist)
     collection.delete_many({})
     collection = Connection('linked').collection('linkedalbumsartists'
                                                  'linkedartistsalbums')
     collection.delete_many({})
Beispiel #20
0
 def test_many_many_cascade_deny_triggers_partial_deletion(self):
     c1 = LinkedCompany(name='C1')
     c2 = LinkedCompany(name='C2')
     o1 = LinkedOwner(name='O1')
     o2 = LinkedOwner(name='O2')
     c1.owners = [o1, o2]
     c2.owners = [o1, o2]
     c1.save()
     c1.delete()
     collection = Connection.get_collection(LinkedCompany)
     self.assertEqual(collection.count_documents({}), 1)
     collection = Connection.get_collection(LinkedOwner)
     self.assertEqual(collection.count_documents({}), 2)
     collection = Connection('linked').collection('linkedcompaniesowners'
                                                  'linkedownerscompanies')
     self.assertEqual(collection.count_documents({}), 2)
Beispiel #21
0
 def test_object_can_be_removed_from_database(self):
     song = SimpleSong(name='Long', year=2020, artist='Thao')
     song.save()
     collection = Connection.get_collection(SimpleSong)
     self.assertEqual(collection.count_documents({}), 1)
     song.delete()
     self.assertEqual(collection.count_documents({}), 0)
     self.assertEqual(song.is_deleted, True)
Beispiel #22
0
 def test_local_key_from_param_is_saved(self):
     a1 = LinkedAuthor(name='A1')
     a1.save()
     post = LinkedPost(title='P1', content='P2', authorId=a1.id)
     post.save()
     collection = Connection.get_collection(LinkedPost)
     for item in collection.find({}):
         self.assertEqual(item['authorId'], ObjectId(a1.id))
Beispiel #23
0
 def test_set_local_key_to_id_is_saved(self):
     post = LinkedPost(title='P1', content='P2')
     author = LinkedAuthor(name='A1')
     post.save()
     author.save()
     post.author_id = author.id
     post.save()
     collection = Connection.get_collection(LinkedPost)
     for item in collection.find({}):
         self.assertEqual(item['authorId'], ObjectId(author.id))
Beispiel #24
0
 def test_set_local_key_to_unset_is_saved(self):
     post = LinkedPost(title='P1', content='P2')
     author = LinkedAuthor(name='A1')
     post.author = author
     post.save()
     post.author_id = None
     post.save()
     collection = Connection.get_collection(LinkedPost)
     for item in collection.find({}):
         self.assertNotIn('authorId', item)
Beispiel #25
0
 def test_fl_many_many_is_saved(self):
     song1 = LinkedSong(name='song1')
     song2 = LinkedSong(name='song2')
     singer1 = LinkedSinger(name='singer1')
     singer2 = LinkedSinger(name='singer2')
     song1.singers = [singer1, singer2]
     song2.singers = [singer1, singer2]
     song1.save()
     collection = Connection.get_collection(LinkedSong)
     for item in collection.find({}):
         self.assertEqual(item['singerIds'],
                          [ObjectId(singer1.id),
                           ObjectId(singer2.id)])
Beispiel #26
0
 def test_object_with_enum_is_saved_into_database(self):
     artist = SimpleArtist(name='Kaosai', gender='MALE')
     artist.save()
     collection = Connection.get_collection(SimpleArtist)
     self.assertEqual(collection.count_documents({}), 1)
     for item in collection.find():
         self.assertEqual(item['name'], 'Kaosai')
         self.assertEqual(item['gender'], 1)
         self.assertIsInstance(item['_id'], ObjectId)
         self.assertIsInstance(item['updatedAt'], datetime)
         self.assertIsInstance(item['createdAt'], datetime)
         self.assertEqual(
             set(item.keys()),
             {'_id', 'name', 'gender', 'updatedAt', 'createdAt'})
 def setUp(self) -> None:
     collection = Connection.get_collection(PLUser)
     collection.delete_many({})
     collection = Connection.get_collection(PLArticle)
     collection.delete_many({})
     # collection = Connection.get_collection(LLPLUser)
     # collection.delete_many({})
     # collection = Connection.get_collection(LLPLArticle)
     # collection.delete_many({})
     collection = Connection.get_collection(LJPLUser)
     collection.delete_many({})
     collection = Connection.get_collection(LJPLArticle)
     collection.delete_many({})
     collection = Connection('preload').collection(
         'ljplarticlesauthorsljplusersarticles')
     collection.delete_many({})
Beispiel #28
0
 def test_linked_objects_are_saved_at_the_same_time(self):
     input = {
         'name':
         'Ti',
         'posts': [{
             'title': 'Bo Lo Iong',
             'content': 'Pieng Iu'
         }, {
             'title': 'Bo Lo Iong',
             'content': 'Pieng Iu'
         }]
     }
     author = LinkedAuthor(**input)
     author.save()
     collection = Connection.get_collection(LinkedAuthor)
     self.assertEqual(collection.count_documents({}), 1)
     oid = ObjectId()
     for item in collection.find():
         self.assertEqual(item['name'], 'Ti')
         self.assertIsInstance(item['_id'], ObjectId)
         oid = item['_id']
         self.assertIsInstance(item['updatedAt'], datetime)
         self.assertIsInstance(item['createdAt'], datetime)
         self.assertEqual(set(item.keys()),
                          {'_id', 'name', 'updatedAt', 'createdAt'})
     collection = Connection.get_collection(LinkedPost)
     self.assertEqual(collection.count_documents({}), 2)
     for item in collection.find():
         self.assertEqual(item['title'], 'Bo Lo Iong')
         self.assertEqual(item['content'], 'Pieng Iu')
         self.assertEqual(item['authorId'], oid)
         self.assertIsInstance(item['_id'], ObjectId)
         self.assertIsInstance(item['updatedAt'], datetime)
         self.assertIsInstance(item['createdAt'], datetime)
         self.assertEqual(set(item.keys()), {
             '_id', 'title', 'content', 'authorId', 'updatedAt', 'createdAt'
         })
Beispiel #29
0
 def test_object_is_saved_into_database(self):
     song = SimpleSong(name='Long', year=2020, artist='Thao')
     song.save()
     collection = Connection.get_collection(SimpleSong)
     self.assertEqual(collection.count_documents({}), 1)
     for item in collection.find():
         self.assertEqual(item['name'], 'Long')
         self.assertEqual(item['year'], 2020)
         self.assertEqual(item['artist'], 'Thao')
         self.assertIsInstance(item['_id'], ObjectId)
         self.assertIsInstance(item['updatedAt'], datetime)
         self.assertIsInstance(item['createdAt'], datetime)
         self.assertEqual(
             set(item.keys()),
             {'_id', 'name', 'year', 'artist', 'updatedAt', 'createdAt'})
Beispiel #30
0
 def test_many_1_unlink_unset_is_saved(self):
     author = LinkedAuthor(name='A')
     post = LinkedPost(title='T', content='C')
     post.author = author
     post.save()
     post.author_id = None
     post.save()
     self.assertEqual(author.is_new, False)
     self.assertEqual(author.is_modified, False)
     self.assertEqual(author.modified_fields, ())
     self.assertEqual(post.is_new, False)
     self.assertEqual(post.is_modified, False)
     self.assertEqual(post.modified_fields, ())
     collection = Connection.get_collection(LinkedPost)
     for item in collection.find({}):
         self.assertNotIn('authorId', item)