def test_queries(self): """Test the list_queries context manager""" u = amcattest.create_test_user() with list_queries() as l: amcattest.create_test_project(owner=u) #query_list_to_table(l, output=print) self.assertEquals(len(l), 2) # create project, create role for owner
def profile_store_triples(): transaction.enter_transaction_management() transaction.managed(True) try: aa = amcattest.create_test_analysis_article() log.info("Created test article %i" % aa.id) tokens = [] for s in range(5): log.info("Creating test sentence %i" % s) s = amcattest.create_test_analysis_sentence(aa) log.info("Created test sentence %i" % s.id) tokens += [ amcattest.create_tokenvalue(analysis_sentence=s.id, word=w, lemma=w) for w in "123456789" * 3 ] log.info("Storing %i tokens" % len(tokens)) with djangotoolkit.list_queries() as queries: aa.store_analysis(tokens=tokens) djangotoolkit.query_list_to_table(queries, maxqlen=150, output=print, encoding="utf-8") finally: transaction.rollback() transaction.leave_transaction_management()
def checkMaxQueries(self, n=0, action="Query", **outputargs): """Check that the action took at most n queries (which should be collected in seq)""" # lazy import to prevent cycles from amcat.tools.djangotoolkit import list_queries with list_queries(**outputargs) as l: yield m = len(l) if m > n: msg = """{} should take at most {} queries, but used {}""".format(action, n, m) for i, q in enumerate(l): msg += "\n({}) {}".format(i+1, q["sql"]) self.fail(msg)
def test_nqueries_table_sentence_codings(self): """Check for efficient retrieval of codings""" from amcat.models.coding.coding import CodingValue from amcat.tools.djangotoolkit import list_queries, query_list_to_table ca = CodedArticle(self.an1) # create 1000 sentence annotations for i in range(1): sent = amcattest.create_test_sentence() sa = Coding.objects.create(codingjob=self.jobs[0], article=self.articles[0], sentence=sent) CodingValue.objects.create(coding=sa, field=self.intfield, intval=i) language = ca.codingjob.coder.userprofile.language with list_queries() as l: t = get_table_sentence_codings_article(ca, language) t.output() # force getting all values #query_list_to_table(l, output=print, maxqlen=190) self.assertTrue(len(l) <= 20, "Retrieving table used %i queries" % len(l))
def profile_store_triples(): transaction.enter_transaction_management() transaction.managed(True) try: aa = amcattest.create_test_analysis_article() log.info("Created test article %i" % aa.id) tokens = [] for s in range(5): log.info("Creating test sentence %i" % s) s = amcattest.create_test_analysis_sentence(aa) log.info("Created test sentence %i" % s.id) tokens += [amcattest.create_tokenvalue(analysis_sentence=s.id, word=w, lemma=w) for w in "123456789"*3] log.info("Storing %i tokens" % len(tokens)) with djangotoolkit.list_queries() as queries: aa.store_analysis(tokens=tokens) djangotoolkit.query_list_to_table(queries, maxqlen=150, output=print, encoding="utf-8") finally: transaction.rollback() transaction.leave_transaction_management()
ccode_b = cb.add_code(code_b, ordernr=2) ccode_a = cb.add_code(code_a, ordernr=1) cb.invalidate_cache() # These tests will automatically be run with caching enabled by # the method test_caching_correctness() self.assertEquals(tuple(cb.codebookcodes), (ccode_a, ccode_b, ccode_c)) roots = [ti.code_id for ti in cb.get_tree()] self.assertEquals(roots, [code_a.id, code_b.id, code_c.id]) # Test again with differnt order ccode_b.ordernr = 256 ccode_b.save() cb.invalidate_cache() self.assertEquals(tuple(cb.codebookcodes), (ccode_a, ccode_c, ccode_b)) roots = [ti.code_id for ti in cb.get_tree()] self.assertEquals(roots, [code_a.id, code_c.id, code_b.id]) if __name__ == '__main__': cb = get_codebook(-5001) from amcat.tools.djangotoolkit import list_queries with list_queries(output=print, printtime=True): set(cb.codes) with list_queries(output=print, printtime=True): set(cb.codes)
def test_queries(self): """Test the list_queries context manager""" u = amcattest.create_test_user() with list_queries() as l: amcattest.create_test_project(owner=u) self.assertEquals(len(l), 3) # create project, create role for owner, create plugin settings
ccode_c = cb.add_code(code_c, ordernr=3) ccode_b = cb.add_code(code_b, ordernr=2) ccode_a = cb.add_code(code_a, ordernr=1) cb.invalidate_cache() # These tests will automatically be run with caching enabled by # the method test_caching_correctness() self.assertEquals(tuple(cb.codebookcodes), (ccode_a, ccode_b, ccode_c)) roots = [ti.code_id for ti in cb.get_tree()] self.assertEquals(roots, [code_a.id, code_b.id, code_c.id]) # Test again with differnt order ccode_b.ordernr = 256 ccode_b.save() cb.invalidate_cache() self.assertEquals(tuple(cb.codebookcodes), (ccode_a, ccode_c, ccode_b)) roots = [ti.code_id for ti in cb.get_tree()] self.assertEquals(roots, [code_a.id, code_c.id, code_b.id]) if __name__ == '__main__': cb = get_codebook(-5001) from amcat.tools.djangotoolkit import list_queries with list_queries(output=print, printtime=True): set(cb.codes) with list_queries(output=print, printtime=True): set(cb.codes)