def test_remove_model_from_list(self, db): wiki_group = WikiGroupFactory.create() retrieved_full = WikiGroup.select().execute()[0] retrieved_only_id = WikiGroup.select(WikiGroup.id).execute() retrieved_only_id = list(retrieved_only_id) retrieved_only_id.remove(retrieved_full) assert len(retrieved_only_id) == 0
def test_get_object_or_404(self, db): WikiGroup.create(name='wiki group 1', db_name='wikigroup1') WikiGroup.create(name='wiki group 2', db_name='wikigroup2') wiki_group = get_object_or_404( WikiGroup.select(WikiGroup.id, WikiGroup.db_name), WikiGroup.name == 'wiki group 2') assert wiki_group.id == 2
def test_delete_and_in(self, db): with db.atomic(): for i in range(3): WikiGroupFactory.create() WikiGroup.delete().where(WikiGroup.id.in_([1,2])).execute() assert WikiGroup.select().count() == 1 assert WikiGroup.get_by_id(3)
def delete_group(wiki_group): # remove wiki group record in _admin.db WikiGroup.delete().where(WikiGroup.db_name == wiki_group).execute() # remove the database file os.remove(os.path.join(DB_PATH, '{0}.db'.format(wiki_group))) # remove uploaded files shutil.rmtree(os.path.join(DB_PATH, wiki_group)) # remove db name from cached db names current_app.active_wiki_groups.remove(wiki_group) return redirect(url_for('.super_admin'))
def test_upsert(self, db): wiki_group_id = (WikiGroup.replace(name='wiki group 1', db_name='wikigroup1').execute()) wiki_group = (WikiGroup.select().execute())[0] assert wiki_group.active is True wiki_group_id = (WikiGroup.replace(name='wiki group 1', db_name='wikigroup1', active=False).execute()) wiki_group = (WikiGroup.select().execute())[0] assert wiki_group.active is False assert WikiGroup.select().count() == 1
def activate(wiki_group): try: wg = (WikiGroup.select().where(WikiGroup.db_name == wiki_group).get()) wg.active = not wg.active wg.save() query = WikiGroup.select().where(WikiGroup.active == True) current_app.active_wiki_groups = [ wiki_group.db_name for wiki_group in query.execute() ] except WikiGroup.DoesNotExist: pass return redirect(url_for('.super_admin'))
def test_insert_many_with_replace(self, db): data_source = [ dict(name='wiki group 1', db_name='wikigroup1', active=True), dict(name='wiki group 1', db_name='wikigroup1', active=False), dict(name='wiki group 2', db_name='wikigroup2', active=True) ] WikiGroup.insert_many(data_source).on_conflict_replace().execute() assert WikiGroup.select().count() == 2 wiki_groups = (WikiGroup.select().execute()) assert wiki_groups[0].name == 'wiki group 1' assert wiki_groups[0].active == False assert wiki_groups[1].name == 'wiki group 2' assert wiki_groups[1].active == True
def test_factory(self, db): wiki_group = WikiGroupFactory.create() retrieved = WikiGroup.get_by_id(wiki_group.id) assert retrieved.id == 1 assert retrieved.name == 'wiki group 1' assert retrieved.db_name == 'wikigroup1' assert retrieved.active == True
def super_admin(): """Manage wiki groups.""" all_wiki_groups = WikiGroup.select().execute() form = AddWikiGroupForm() # Create a new wiki group with its own database and static file directory if form.validate_on_submit(): new_wiki_group_name = form.wiki_group_name.data new_db_name = new_wiki_group_name.replace(' ', '') # Save the name of the new wiki group in database `_admin` # Remove whitespaces in the wiki group name. # Then use it to name the database which is about to be initialized. try: new_wiki_group = WikiGroup.create(name=new_wiki_group_name, db_name=new_db_name, active=True) os.mkdir(os.path.join(DB_PATH, new_wiki_group.db_name)) query = WikiGroup.select().where(WikiGroup.active == True) current_app.active_wiki_groups = [ wiki_group.db_name for wiki_group in query.execute() ] db.close() db.pick(new_wiki_group.db_filename()) db.create_tables([ WikiPage, WikiPageIndex, WikiPageVersion, WikiReference, WikiFile, WikiKeypage ]) # Create wiki group home page, and the id is 1. WikiPage.create(title='Home') flash('New wiki group added', 'info') return redirect(url_for('.super_admin')) except IntegrityError: flash('Wiki Group already exists', 'warning') except FileExistsError: flash('Upload directory already exists.', 'warning') else: flash_errors(form) return render_template('admin/super_admin.html', form=form, all_wiki_groups=all_wiki_groups)
def register_database(app): admin_db_exists = os.path.exists( os.path.join(app.config['DB_PATH'], app.config['ADMIN_DB'])) db.pick(app.config['ADMIN_DB']) if not admin_db_exists: db.create_tables([WikiGroup]) query = WikiGroup.select().where(WikiGroup.active == True) app.active_wiki_groups = [ wiki_group.db_name for wiki_group in query.execute() ] db.close()
def test_model_equal(self, db): wiki_group = WikiGroupFactory.create() retrieved_full = WikiGroup.select().execute()[0] retrieved_only_id = WikiGroup.select(WikiGroup.id).execute()[0] assert retrieved_full == retrieved_only_id
def test_unique_constraint(self, db): WikiGroup.create(name='wiki group 1', db_name='wikigroup1') with pytest.raises(IntegrityError): WikiGroup.create(name='wiki group 1', db_name='wikigroup1')
def test_db_atomic(self, db): with db.atomic(): for i in range(10): WikiGroupFactory.create() assert WikiGroup.select().count() == 10
def test_DoesNotExist(self, db): with pytest.raises(WikiGroup.DoesNotExist): WikiGroup.get_by_id(10)
def home(): """Cover page.""" query = (WikiGroup.select().where(WikiGroup.active == True)) active_wiki_groups = query.execute() return render_template('admin/cover.html', active_wiki_groups=active_wiki_groups)