示例#1
0
class DictionaryNamesTestCase(unittest.TestCase):

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

    def tearDown(self):
        db.drop_collection(self.obj.code)

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

    def test_add(self):
        data = dict(code='test', name=u'Тестовый справочник')
        _id = self.obj.add(data)
        self.assertIsNotNone(_id)
        result = self.obj.get_by_id(_id)
        data.update(dict(_id=_id))
        self.assertDictEqual(data, result)

    def test_update(self):
        data = dict(code='test2', name=u'Тестовый справочник2')
        _id = self.obj.add(data)
        self.assertIsNotNone(_id)
        update_data = dict(code='test2', name=u'Тестовый справочник2 (updated)', updated=datetime.now())
        self.obj.update(_id, update_data)
        result = self.obj.get_by_id(_id)
        update_data.update(dict(_id=_id))
        result.pop('updated')
        update_data.pop('updated')
        self.assertDictEqual(result, update_data)

    def test_delete(self):
        code = 'test3'
        data = dict(code=code, name=u'Тестовый справочник3')
        _id = self.obj.add(data)
        self.assertIsNotNone(_id)
        self.obj.delete(code)
        result = self.obj.get_by_code(code)
        self.assertIsNone(result)

    def test_update_by_code(self):
        code = 'test4'
        data = dict(code=code, name=u'Тестовый справочник4')
        _id = self.obj.add(data)
        self.assertIsNotNone(_id)
        update_data = dict(code=code, name=u'Тестовый справочник4 (updated)', updated=datetime.now())
        self.obj.update_by_code(code, update_data)
        result = self.obj.get_by_code(code)
        update_data.update(dict(_id=_id))
        result.pop('updated')
        update_data.pop('updated')
        self.assertDictEqual(result, update_data)
示例#2
0
def dict_edit(_id):
    obj = DictionaryNames()
    info = obj.get_by_id(_id)
    collections = Collections()
    if not info or 'code' not in info:# or info['code'] not in collections.get_list():
        flash(u'Коллекция не существует или пуста')
        abort(404)

    if request.method == 'POST':
        worker = Worker(info['code'])
        try:
            worker.link_collection(request.form.get('linked_collection'),
                                   request.form.get('origin_field'),
                                   request.form.get('linked_field'))
            info = obj.get_by_id(_id)
        except Exception, e:
            flash(e, 'error')
        else:
            flash(u'Справочник успешно обновлён', 'info')
            return redirect(url_for('.dict_edit', _id=_id))
示例#3
0
def dict_view(_id):
    obj = DictionaryNames()
    info = obj.get_by_id(_id)
    collections = Collections()
    if not info or 'code' not in info or info['code'] not in collections.get_list():
        flash(u'Коллекция не существует или пуста')
        abort(404)
    collection = Dictionary(info['code'])
    data = obj.get_list({'version': {'$exists': True}})
    fields = _get_fields(info['code'])
    try:
        return render_template('{0}/dict_view.html'.format(module.name),
                               info=info,
                               data=data,
                               fields=fields,
                               documents=collection.get_list(sort='id', limit=1000))
    except TemplateNotFound:
        abort(404)