def setUp(self): # establish application context and test client TestConfig.SOLUTIONS_TO_SHOW = 5 self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.test_client = self.app.test_client() # create in-memory database structure db.create_all() # add a User object self.user1 = User(username='******') self.user1.set_password('pass1') db.session.add(self.user1) # add a Language object self.language1 = Language(language_id=1, language='Python', extension='py') self.language2 = Language(language_id=2, language='C++', extension='cpp') self.language3 = Language(language_id=3, language='F#', extension='fs') self.language4 = Language(language_id=4, language='F#', extension='fsx') db.session.add_all([self.language1, self.language2, self.language3, self.language4]) # add Problem objects for _ in range(10): p1 = Problem(contents="contents", language_id=self.language1.language_id, title="title") p2 = Problem(contents="contents", language_id=self.language2.language_id, title="title") db.session.add_all([p1, p2]) f1 = Problem(contents='contents', language_id=self.language3.language_id, title='title') f2 = Problem(contents='contents', language_id=self.language4.language_id, title='title') db.session.add_all([f1, f2]) db.session.commit()
def test_resource(): resource = Resource(name='name', url='https://resource.url', category=Category(name='Category'), languages=[Language(name='language')], free=False, notes='Some notes') assert (resource.key() == 'https://resource.url') assert (resource.__hash__() == hash('https://resource.url')) assert (resource.__repr__() == "<Resource \n" "\tName: name\n" "\tLanguages: [<Language language>]\n" "\tCategory: <Category Category>\n" "\tURL: https://resource.url\n>") assert (resource.serialize() == { 'id': None, 'name': 'name', 'url': 'https://resource.url', 'category': 'Category', 'languages': ['language'], 'free': False, 'notes': 'Some notes', 'upvotes': None, 'downvotes': None, 'times_clicked': None, 'created_at': '', 'last_updated': '', 'user_vote_direction': None }) # Test equality resource_copy = resource assert (resource == resource_copy) not_copy = Resource(name='different name') assert (resource != not_copy) not_copy.name = 'name' not_copy.url = 'https://notresource.url' assert (resource != not_copy) not_copy.url = 'https://resource.url' assert (resource != not_copy) not_copy.free = True assert (resource != not_copy) not_copy.free = False not_copy.notes = 'Some other notes' assert (resource != not_copy) not_copy.notes = 'Some notes' not_copy.category = 'Different Category' assert (resource != not_copy) not_copy.category = Category(name='Category') not_copy.languages = ["different language"] assert (resource != not_copy) not_copy.languages = [Language(name='language')] assert (resource == not_copy) assert (resource != 1)
def upgrade(): for language in languages: if not Language.query.filter_by(code=language[0]).first(): lang = Language(code=language[0], name=language[1]) db.session.add(lang) db.session.commit()
def index(): form = SurveyForm() if form.validate_on_submit(): voter = Voter(name=form.name.data, email=form.email.data) db.session.add(voter) db.session.commit() answer = Answer(age=form.age.data, gender=form.gender.data, path=form.path.data, voter=voter) db.session.add(answer) db.session.commit() for i in range(0, len(form.language.data)): language = Language(language=form.language.data[i], voter=voter) db.session.add(language) db.session.commit() comment = Comment(comment=form.text_area.data, voter=voter) db.session.add(comment) db.session.commit() flash(f"Thanks, the survey has been submitted!") return redirect(url_for('index')) return render_template('survey.html', form=form)
def setUp(self): # establish application context and test client self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.test_client = self.app.test_client() # create in-memory database structure db.create_all() # add a User object self.user1 = self.create_user1() db.session.add(self.user1) # add a Language object self.language = Language(language_id=1, language='Python', extension='py') db.session.add(self.language) # add a Problem object self.problem = Problem(contents="arbitrary solution to problem", language_id=self.language.language_id, title="arbitrary") db.session.add(self.problem) db.session.commit()
def test_vote_information(): test_apikey = '1234abcd' test_id = 1 test_direction = 'upvote' resource = Resource(id=test_id, name='name', url='https://resource.url', category=Category(name='Category'), languages=[Language(name='language')], paid=False, notes='Some notes') key = Key(email="*****@*****.**", apikey=test_apikey) vote_info = VoteInformation(voter_apikey=key.apikey, resource_id=resource.id, current_direction=test_direction) vote_info.voter = key resource.voters.append(vote_info) assert (vote_info.voter_apikey == test_apikey) assert (vote_info.resource_id == test_id) assert (vote_info.current_direction == test_direction) assert (vote_info.voter == key) assert (vote_info.resource == resource)
def test_get_text(): items = [ Language(id="sv"), Language(id="en"), TranslatedText(key="TEST_TEXT_KEY", language="sv", value="sv-text-val"), TranslatedText(key="TEST_TEXT_KEY", language="en", value="en-text-val"), TranslatedText(key="OTHER_TEXT_KEY", language="sv", value="sv-other-val"), TranslatedText(key="OTHER_TEXT_KEY", language="en", value="en-other-val"), TranslatedText(key="ONLY_SV_TEXT_KEY", language="sv", value="sv-only-val"), ] with TestEnvironment(items) as client: sv_headers_ok = headers(language="sv") res_sv = client.get("/v1/texts/key/TEST_TEXT_KEY", headers=sv_headers_ok) assert res_sv.status_code == status.HTTP_200_OK assert res_sv.get_json()["TEST_TEXT_KEY"] == "sv-text-val" en_headers_ok = headers(language="en") res_en = client.get("/v1/texts/key/TEST_TEXT_KEY", headers=en_headers_ok) assert res_en.status_code == status.HTTP_200_OK assert res_en.get_json()["TEST_TEXT_KEY"] == "en-text-val" no_lang_headers = headers(language=None) res_no_lang = client.get("/v1/texts/key/TEST_TEXT_KEY", headers=no_lang_headers) assert res_no_lang.status_code == status.HTTP_400_BAD_REQUEST en_headers = headers(language="en") res_missing = client.get("/v1/texts/key/ONLY_SV_TEXT_KEY", headers=en_headers) assert res_missing.status_code == status.HTTP_404_NOT_FOUND wrong_lang_headers = headers(language="xy") res_wrong_lang = client.get("/v1/texts/key/TEST_TEXT_KEY", headers=wrong_lang_headers) assert res_wrong_lang.status_code == status.HTTP_400_BAD_REQUEST
def add_lang(self, id, display, ext): try: lang = Language.query.get(id) lang.display_text = display lang.extension = ext db.session.add(lang) except: lang = Language(id, display, ext) db.session.add(lang)
def import_language(): click.echo("Importing Language...", nl=False) with open("data/languages.json") as data_file: languages = json.load(data_file) for language in languages: new_language = Language(name=language["name"], ietf=language["ietf"]) db.session.add(new_language) db.session.commit() click.echo(DONE)
def language(): languages = Language.query.order_by(Language.name.asc()).all() form = LanguageForm() if form.validate_on_submit(): l = Language(name=form.name.data) db.session.add(l) db.session.commit() flash('Lang added to db') return redirect(url_for('admin.language')) return render_template('admin_movie_friend.html', title='admin.movie_friend', collection=languages, collection_name='languages', header='Language', form=form)
def get_attributes(json): languages_list = Language.query.all() categories_list = Category.query.all() language_dict = {l.key(): l for l in languages_list} category_dict = {c.key(): c for c in categories_list} langs = [] for lang in json.get('languages') or []: language = language_dict.get(lang) if not language: language = Language(name=lang) langs.append(language) categ = category_dict.get(json.get('category'), Category(name=json.get('category'))) return (langs, categ)
def create_language(current_user): data = request.get_json() if not 'name' in data or not 'resume id' in data: return { 'error': 'Invalid data', 'message': 'Name and resume id must be given' }, 400 try: r = Resume.query.get_or_404(int(data['resume id'])) except Exception as e: return { 'error': 'Your request can not be processed', 'message': str(e) }, 500 l = Language(name=data['name'], resume_id=data['resume id']) db.session.add(l) r.languages.append(l) db.session.commit() return l.to_dict(), 201
def api_post_language(api_version): data = request.get_json() if "data" in data: data = data["data"] created_data = [] try: for language in data: a = Language(**language) db.session.add(a) created_data.append(a) db.session.commit() except Exception as e: db.session.rollback() return make_409(str(e)) return make_200([d.serialize() for d in created_data]) else: return make_400("no data")
def handle(self, *args, **options): """ The command's main. """ try: assert type(options['file_name']) is list assert len(options['file_name']) == 1 assert type(options['file_name'][0]) is str except AssertionError: raise CommandError("Please refer to --help") else: file_name = options['file_name'][0] with open(file_name, 'r') as f: for line in f: items = line.split() try: assert len(items) == 3 iso_code = str(items[0]) latitude = float(items[1]) longitude = float(items[2]) except (AssertionError, TypeError): self.stdout.write("Skipped incomprehensible line") continue try: language = Language.objects.get(iso_code=iso_code) except Language.DoesNotExist: language = Language() language.iso_code = iso_code language.latitude = latitude language.longitude = longitude language.save() self.stdout.write("Updated location of "+ iso_code) self.stdout.write("Harvest done")
def setUp(self): # establish application context and test client self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.test_client = self.app.test_client() # create in-memory database structure db.create_all() # add a User object self.user1 = User(username='******') self.user1.set_password('pass1') db.session.add(self.user1) # add a Language object self.language = Language(language_id=1, language='Python', extension='py') db.session.add(self.language) db.session.commit() # log in self.test_client.post('/', data = {'username': '******', 'password': '******'})
def setUp(self): # establish application context and test client self.app = create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() self.test_client = self.app.test_client() # create in-memory database structure db.create_all() # add a User object self.user1 = User(username='******') self.user1.set_password('pass1') db.session.add(self.user1) # add a Language object self.language = Language(language_id=1, language='Python', extension='py') db.session.add(self.language) db.session.commit() # log in self.test_client.post('/', data={ 'username': '******', 'password': '******' }) # add a problem self.data = { 'problem_selection': 1, 'problem_title': 'a title', 'file_upload': (BytesIO(b'file contents'), 'test.py') } self.test_client.post('/create_solution', data=self.data)
def create_fake_documents(db, nb_docs=1000, nb_correspondents=None, fake=None): from app.models import Document from app.models import Institution from app.models import User from app.models import Whitelist from app.models import Image from app.models import Note from app.models import Language from app.models import PersonRole from app.models import Person from app.models import Witness if fake is None: fake = Faker() logging.getLogger('faker.factory').setLevel(logging.ERROR) users = User.query.all() whitelists = Whitelist.query.all() # add some languages db.session.add(Language(code="FRO")) db.session.add(Language(code="ENG")) db.session.add(Language(code="OCC")) db.session.add(Language(code="CZC")) db.session.add(Language(code="ITA")) db.session.commit() languages = Language.query.all() # add fake collections for i in range(1, 10): db.session.add( Collection(title=fake.sentence(), description=fake.text())) db.session.commit() collections = Collection.query.all() # add fake correspondent roles for i in range(5, 20): db.session.add(PersonRole(label=fake.word())) db.session.flush() roles = PersonRole.query.all() # add fake persons if nb_correspondents is None: nb_correspondents = nb_docs * 2 for i in range(0, nb_correspondents): db.session.add( Person(firstname=fake.first_name(), lastname=fake.last_name(), key=fake.name(), ref=fake.uri())) db.session.flush() correspondents = Person.query.all() # add fake Institutions institutions = [] for i in range(0, 20): ins = Institution(name=fake.sentence(nb_words=3), ref=fake.uri()) db.session.add(ins) institutions.append(ins) db.session.flush() # add fake documents last_progress = -1 for n_doc in range(0, nb_docs): try: doc = Document(title=fake.sentence(), transcription=fake.text(max_nb_chars=1000), argument=fake.text()) doc.owner_id = users[0].id doc.whitelist_id = whitelists[0].id doc.languages = [languages[0], languages[1]] doc.collections = collections db.session.add(doc) db.session.flush() # add fake witnesses witnesses = [] for i in range(0, 3): wit = Witness(document_id=doc.id, content=fake.sentence(), tradition=random.choice(TRADITION_VALUES), status=random.choice(WITNESS_STATUS_VALUES), institution_id=random.choice(institutions).id, classification_mark=fake.sentence()) db.session.add(wit) witnesses.append(wit) db.session.flush() # add fake Images for w in range(0, len(witnesses)): for i in range(0, 5): img = Image(canvas_idx=random.randint(1, 100), manifest_url=fake.uri(), witness_id=witnesses[w].id) db.session.add(img) # add fake Notes nb_notes = 50 for i in range(0, nb_notes): n = Note(label=fake.sentence(), content=fake.paragraph(), document_id=doc.id) db.session.add(n) db.session.flush() # add fake correspondent to the doc from app.models import PersonHasRole correspondents_have_roles = [] nb_corr = 3 for i in range(0, nb_corr): role = roles[0] co = correspondents[i] correspondents_have_roles.append((role.id, co.id)) c_h_roles = [] for (role_id, co_id) in set(correspondents_have_roles): chr = PersonHasRole(document_id=doc.id, correspondent_id=co_id, correspondent_role_id=role_id) db.session.add(chr) c_h_roles.append(chr) db.session.flush() doc.correspondents_have_roles = c_h_roles docs = Document.query.filter(Document.id != doc.id).all() if len(docs) > 0: if len(docs) > doc.id - 1 > 0: doc.next_document = docs[doc.id - 1] db.session.add(doc) db.session.commit() except IntegrityError as e: db.session.rollback() print("Warning:", e) progress = int(n_doc / nb_docs * 100) if progress % 10 == 0 and last_progress != progress: print("%s..." % progress, end="", flush=True) last_progress = progress db.session.commit()
def test_language(): language = Language(name="Language") assert (language.__hash__() == hash('Language')) assert (language.__repr__() == "<Language Language>") assert (language != 1) assert (language == language)
def test_create_language(self): """Tests the creation of a new language entry.""" self.assertEqual(len(Language.query.all()), 0) lang = Language(code='EN') self.add_to_db(lang)
def handle(self, *args, **options): """ The command's main. """ """Some input validation.""" try: assert type(options['tsv_file']) is list assert len(options['tsv_file']) == 1 assert type(options['tsv_file'][0]) is str except AssertionError: raise CommandError("Please refer to --help") else: tsv_file = options['tsv_file'][0] try: assert type(options['iso_codes_file']) is str assert len(options['iso_codes_file']) > 0 except AssertionError: raise CommandError("Please refer to --help") else: self.iso_codes_file = options['iso_codes_file'] """Extract languages.""" extracted = set() with open(tsv_file, 'r') as f: reader = csv.reader(f, delimiter='\t') count = -1 for row in reader: count += 1 if count == 0: continue try: assert row[2].find(':') > 0 assert row[3].find(':') > 0 except AssertionError: raise CommandError("File does not conform to format.") extracted.add(row[2].split(':')[0]) extracted.add(row[3].split(':')[0]) self.stdout.write("Input file is OK") """Add them to the database.""" count = 0 for iso_code in extracted: if len(iso_code) == 2: # iso 639-1 iso_639_1 = iso_code try: iso_639_3 = self.get_iso_639_3(iso_639_1) except ValueError: self.stderr.write("Skipped " + iso_639_1 + ": ISO 639-3 code not found.") continue else: # iso 639-3 iso_639_1 = None iso_639_3 = iso_code try: Language.objects.get(iso_639_3=iso_639_3) except Language.DoesNotExist: lang = Language() lang.iso_639_1 = iso_639_1 lang.iso_639_3 = iso_639_3 lang.save() count += 1 self.stdout.write("Added " + iso_639_3 + " to database.") else: self.stdout.write("Skipped " + iso_639_3 + ": already in database.") self.stdout.write("Done. Languages added: " + str(count))
def add_language(code): lang = Language(code=code) db.session.add(lang) db.session.commit() return lang
def add_language(self, code='US'): language = Language(code=code) db.session.add(language) db.session.commit() return language
def add_language(short, full, name): if (API_KEY != request.args.get('api')): return jsonify({'error': 'Wrong api key'}), 400 lang = Language(short=short, full=full, name=name) lang.save() return '200 OK'
def test_get_text_by_group(): items = [ Language(id="sv"), Language(id="en"), TranslatedText(key="TEST_TEXT_KEY", language="sv", value="sv-text-val"), TranslatedText(key="TEST_TEXT_KEY", language="en", value="en-text-val"), TranslatedText(key="OTHER_TEXT_KEY", language="sv", value="sv-other-val"), TranslatedText(key="OTHER_TEXT_KEY", language="en", value="en-other-val"), TranslatedText(key="NOT_IN_GROUP", language="sv", value="sv-other-val"), TranslatedText(key="NOT_IN_GROUP", language="en", value="en-other-val"), TranslatedText(key="ONLY_SV_TEXT_KEY", language="sv", value="sv-only-val"), TextGroup(id="MOBILE_APP"), TextGroupMembership(text_key="TEST_TEXT_KEY", group_id="MOBILE_APP"), TextGroupMembership(text_key="OTHER_TEXT_KEY", group_id="MOBILE_APP"), TextGroupMembership(text_key="ONLY_SV_TEXT_KEY", group_id="MOBILE_APP"), ] with TestEnvironment(items) as client: sv_headers_ok = headers(language="sv") res_sv = client.get("/v1/texts/group/MOBILE_APP", headers=sv_headers_ok) assert res_sv.status_code == status.HTTP_200_OK body = res_sv.get_json() assert len(body) == 3 assert body["TEST_TEXT_KEY"] == "sv-text-val" assert body["OTHER_TEXT_KEY"] == "sv-other-val" assert body["ONLY_SV_TEXT_KEY"] == "sv-only-val" no_lang_headers = headers(language=None) res_no_lang = client.get("/v1/texts/group/MOBILE_APP", headers=no_lang_headers) assert res_no_lang.status_code == status.HTTP_400_BAD_REQUEST en_headers = headers(language="en") res_missing = client.get("/v1/texts/group/MISSING_GROUP", headers=en_headers) assert res_missing.status_code == status.HTTP_404_NOT_FOUND wrong_lang_headers = headers(language="xy") res_wrong_lang = client.get("/v1/texts/group/MOBILE_APP", headers=wrong_lang_headers) assert res_wrong_lang.status_code == status.HTTP_400_BAD_REQUEST en_headers_ok = headers(language="en") res_en = client.get("/v1/texts/group/MOBILE_APP", headers=en_headers_ok) assert res_en.status_code == status.HTTP_200_OK body = res_en.get_json() assert len(body) == 2 assert body["TEST_TEXT_KEY"] == "en-text-val" assert body["OTHER_TEXT_KEY"] == "en-other-val" assert "ONLY_SV_TEXT_KEY" not in body
def create_fake_documents(db, nb_docs=1000, nb_correspondents=None, fake=None): from app.models import Document from app.models import Institution from app.models import Witness from app.models import User from app.models import Whitelist from app.models import Image from app.models import Note from app.models import Language from app.models import PersonRole from app.models import Person if fake is None: fake = Faker() users = User.query.all() whitelists = Whitelist.query.all() # add some languages db.session.add(Language(code="FRO", label="Ancien français")) db.session.add(Language(code="ENG", label="Anglais")) db.session.add(Language(code="OCC", label="Occitant")) db.session.add(Language(code="CZC", label="Tchèque")) db.session.add(Language(code="ITA", label="Italien")) db.session.commit() languages = Language.query.all() # add fake collections for i in range(1, 10): db.session.add( Collection(title=fake.sentence(), description=fake.text())) db.session.commit() collections = Collection.query.all() # add fake correspondent roles for i in range(5, 20): db.session.add(PersonRole(label=fake.word())) db.session.commit() roles = PersonRole.query.all() # add fake persons if nb_correspondents is None: nb_correspondents = nb_docs * 2 for i in range(0, nb_correspondents): db.session.add( Person(firstname=fake.first_name(), lastname=fake.last_name(), key=fake.name(), ref=random.choice([None, fake.uri()]))) db.session.commit() correspondents = Person.query.all() # add fake Institutions institutions = [] for i in range(0, 20): ins = Institution(name=fake.sentence(nb_words=3), ref=fake.uri()) db.session.add(ins) institutions.append(ins) db.session.commit() # add fake documents last_progress = -1 for n_doc in range(0, nb_docs): try: doc = Document( title=fake.sentence(), transcription=fake.text( max_nb_chars=random.randint(100, 7500)), argument=fake.text(max_nb_chars=random.randint(250, 500)), creation=fake.sentence(nb_words=1), creation_label=fake.sentence(nb_words=1), location_date_label=fake.sentence(nb_words=1), location_date_ref=fake.sentence(nb_words=1), date_insert="2018/01/01", date_update="2018/01/02", is_published=True) doc.owner_id = random.choice(users).id doc.whitelist_id = random.choice(whitelists).id doc.languages = random.choices(languages) doc.collections = random.choices(collections, k=random.randint(0, 3)) db.session.add(doc) db.session.commit() # add fake witnesses witnesses = [] for i in range(0, random.randint(1, 5)): wit = Witness(document_id=doc.id, content=fake.sentence(), tradition=random.choice(TRADITION_VALUES), status=random.choice(WITNESS_STATUS_VALUES), institution_id=random.choice(institutions).id, classification_mark=fake.sentence()) db.session.add(wit) witnesses.append(wit) db.session.commit() # add fake Images for w in range(0, len(witnesses)): for i in range(0, random.randint(0, 5)): img = Image(canvas_idx=random.randint(1, 100), manifest_url=fake.uri(), witness_id=witnesses[w].id) db.session.add(img) # add fake Notes for i in range(0, random.randint(0, 30)): n = Note(label=fake.sentence(), content=fake.paragraph(), document_id=doc.id) db.session.add(n) # add fake correspondent to the doc from app.models import PersonHasRole correspondents_have_roles = [] for i in range(1, random.randint(1, 4)): role = random.choice(roles) co = random.choice(correspondents) correspondents_have_roles.append((role.id, co.id)) c_h_roles = [] for (role_id, co_id) in set(correspondents_have_roles): chr = PersonHasRole(document_id=doc.id, correspondent_id=co_id, correspondent_role_id=role_id) db.session.add(chr) c_h_roles.append(chr) db.session.commit() doc.correspondents_have_roles = c_h_roles if random.randint(0, 10) % 2 == 0: docs = Document.query.filter(Document.id != doc.id).all() if len(docs) > 0: doc.next_document = random.choice(docs) db.session.add(doc) except IntegrityError as e: db.session.rollback() progress = int(n_doc / nb_docs * 100) if progress % 10 == 0 and last_progress != progress: print("%s..." % progress, end="", flush=True) last_progress = progress db.session.commit()