Beispiel #1
0
class DictionaryTestCase(unittest.TestCase):

    def setUp(self):
        app.config['TESTING'] = True
        app.config['MONGODB_DB'] = TEST_DB
        self.app = app.test_client()
        self.collection = 'test_dictionary'
        self.obj = Dictionary(self.collection)

    def drop_collection(self):
        db.drop_collection(self.collection)

    def tearDown(self):
        db.drop_collection(self.collection)

    def test_get_list_empty(self):
        self.drop_collection()
        with self.assertRaises(ValueError):
            result = self.obj.get_list()
        #self.assertEqual(result.count(), 0)

    def test_add_document(self):
        data = dict(code='test', name=u'Тестовый документ')
        _id = self.obj.add_document(data)
        self.assertIsNotNone(_id)
        result = self.obj.get_document(dict(_id=_id))
        data.update(dict(_id=_id))
        self.assertDictEqual(data, result)

    def test_add_documents(self):
        self.drop_collection()
        data = [dict(name=u'Документ1', code='document1'),
                dict(name=u'Документ2'),
                dict(name=u'Документ3', alias='document2'),
                dict(name=u'Документ4', code='document4', created=datetime.now())]
        _ids = self.obj.add_documents(data)
        self.assertIsNotNone(_ids)
        self.assertIsInstance(_ids, list)
        result = list(self.obj.get_list())
        self.assertEqual(len(result), len(data))
        self.assertEqual(result[0]['code'], data[0]['code'])
        data_2 = data[2]
        data_2.update(dict(_id=_ids[2]))
        self.assertEqual(result[2], data_2)

    def test_get_document(self):
        data = dict(code='test5', name=u'Тестовый документ5')
        _id = self.obj.add_document(data)
        self.assertIsNotNone(_id)
        result = self.obj.get_document(dict(_id=_id))
        data.update(dict(_id=_id))
        self.assertDictEqual(data, result)

    def test_count_documents(self):
        self.drop_collection()
        data = [dict(name=u'Документ1', code='document1'),
                dict(name=u'Документ2'),
                dict(name=u'Документ3', alias='document2'),
                dict(name=u'Документ4', code='document4', created=datetime.now())]
        _ids = self.obj.add_documents(data)
        self.assertIsNotNone(_ids)
        self.assertIsInstance(_ids, list)
        result = self.obj.count()
        self.assertEqual(result, len(data))
        result = self.obj.count(dict(_id=_ids[0]))
        self.assertEqual(result, 1)
        result = self.obj.count(dict(_id='not_exists_id'))
        self.assertEqual(result, 0)

    def test_delete(self):
        data = dict(code='test_delete', name=u'Тестовый документ')
        _id = self.obj.add_document(data)
        self.assertIsNotNone(_id)
        self.obj.delete(_id)
        result = self.obj.get_document(dict(_id=_id))
        self.assertIsNone(result)

    def test_exists(self):
        self.drop_collection()
        data = [dict(name=u'Документ1', code='document1'),
                dict(name=u'Документ2'),
                dict(name=u'Документ3', alias='document2'),
                dict(name=u'Документ4', code='document4', created=datetime.now())]
        _ids = self.obj.add_documents(data)
        self.assertIsNotNone(_ids)
        self.assertIsInstance(_ids, list)
        result = self.obj.exists()
        self.assertTrue(result)
        result = self.obj.count(dict(_id=_ids[0]))
        self.assertTrue(result)
        result = self.obj.count(dict(_id='not_exists_id'))
        self.assertFalse(result)
Beispiel #2
0
 def delete(self, code, document_id):
     obj = Dictionary(code)
     try:
         obj.delete(_id=document_id)
     except TypeError, e:
         raise InvalidAPIUsage(e.message, status_code=400)