Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
working with runcaseversion_environments. changed runcaseversion_suit…
Browse files Browse the repository at this point in the history
…es to just use a template tag on run
  • Loading branch information
Cameron Dawson committed Nov 18, 2012
1 parent d004776 commit 256a9e5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 49 deletions.
97 changes: 53 additions & 44 deletions moztrap/model/execution/models.py
Expand Up @@ -105,16 +105,21 @@ def _remove_rcv_dupes(self):


def _lock_case_versions(self):
"""Select caseversions from suites, create runcaseversions."""
"""
Select caseversions from suites, create runcaseversions.
WARNING: Testing this code in the PyCharm debugger will give an incorrect
number of queries, because for the debugger to show all the information
it wants, it must do queries itself. When testing with
assertnumqueries, don't use the PyCharm debugger.
"""

self._remove_rcv_dupes()

# make a list of cvs in order

# @@@ I shouldn't need the name here, but for some reason it's required
# and will do a query if I don't fetch it first.
cvs = CaseVersion.objects.raw("""
SELECT cv.id as id, cv.name as name
SELECT cv.id as id
FROM execution_runsuite as rs
INNER JOIN library_suitecase as sc ON rs.suite_id = sc.suite_id
INNER JOIN library_caseversion as cv ON cv.case_id = sc.case_id
Expand Down Expand Up @@ -144,7 +149,7 @@ def _lock_case_versions(self):

order = 1
for cv in cv_set:
kwargs = {"run": self, "caseversion_id": cv, "order": order}
kwargs = {"run_id": self.id, "caseversion_id": cv, "order": order}
try:
kwargs["id"] = existing_rcv_map[cv]
except KeyError:
Expand All @@ -156,9 +161,23 @@ def _lock_case_versions(self):
# insert these rcvs in bulk
RunCaseVersion.objects.bulk_insert_or_update(rcv_proxies)

# build a list of RunCaseVersion_environment objects
# and use bulk_create.
#
self._bulk_update_runcaseversion_environments_for_lock()
# self._bulk_update_runcaseversion_suites_for_lock()


def _bulk_update_runcaseversion_environments_for_lock(self):
"""
update runcaseversion_environment records with latest state.
Approach:
do another raw sql query to get all existing_rcv_envs for this run
existing_rcv_envs - needed_rcv_envs = list to delete (no longer needed)
needed_rcv_envs - existing_rcv_envs = list to create
build a list of RunCaseVersion_environment objects
and use bulk_create.
"""

# re-query all the rcvs (including newly created) for this run
final_rcvs = RunCaseVersion.objects.filter(run=self).select_related(
"caseversion").prefetch_related("caseversion__environments")
Expand All @@ -172,61 +191,51 @@ def _lock_case_versions(self):
"runcaseversion_id", "environment_id"))

# runcaseversion_environment objects we will use to bulk create
needed_rcv_envs = []
# loop through all cvs and fetch the env intersection with this run
needed_rcv_envs_tuples = []
# runcaseversion_environment ids that we need to delete
delete_rcv_envs = []

#loop through all cvs and fetch the env intersection with this run
run_env_ids = set(
self.environments.values_list("id", flat=True))
for rcv in final_rcvs:
case_env_ids = set([x.id for x in rcv.caseversion.environments.all()])
for env in run_env_ids.intersection(case_env_ids):
needed_rcv_envs_tuples.append((rcv.id, env))
needed_rcv_envs_set = set(needed_rcv_envs_tuples)

# get the set of rcv_envs we need to delete because they don't belong
# to the needed set.
delete_rcv_envs = prev_rcv_envs_set - set(needed_rcv_envs_tuples)
delquery = Q()
for combo in delete_rcv_envs:
delquery = delquery | Q(
**{"runcaseversion_id": combo[0],
"environment_id": combo[1]})

RunCaseVersion.environments.through.objects.filter(delquery).delete()
delete_rcv_envs = prev_rcv_envs_set - needed_rcv_envs_set
if len(delete_rcv_envs):
delquery = Q()
for combo in delete_rcv_envs:
delquery = delquery | Q(
**{"runcaseversion_id": combo[0],
"environment_id": combo[1]})
RunCaseVersion.environments.through.objects.filter(delquery).delete()

# get the set of rcv_envs we need to create that don't already exist
needed_rcv_envs_tuples = set(needed_rcv_envs_tuples) - prev_rcv_envs_set
needed_rcv_envs_set = needed_rcv_envs_set - prev_rcv_envs_set

# build all the objects to pass to bulk_create
needed_rcv_envs = [RunCaseVersion.environments.through(
runcaseversion=needed[0],
environment_id=needed[1]) for needed in needed_rcv_envs_tuples]
runcaseversion_id=needed[0],
environment_id=needed[1]) for needed in needed_rcv_envs_set]

RunCaseVersion.environments.through.objects.bulk_create(needed_rcv_envs)
# """
# Approach:
# do another raw sql query to get all existing_rcv_envs for this run
# existing_rcv_envs - needed_rcv_envs = list to delete (no longer needed)
# needed_rcv_envs - existing_rcv_envs = list to create
# """
# rcv_env_proxies = []
# for cv_envs in needed_rcv_envs:
# rcv_env_proxies.append(RunCaseVersion.environments.through(
# caseversion=needed_env[0]))
# rcv_env_model = RunCaseVersion.environments.through
# rcv_env_model.bulk_create(rcv_env_proxies)




# @TODO: still need runcaseversion_suites




def _bulk_update_runcaseversion_suites_for_lock(self):
"""
update runcaseversion_suite records with latest state.
Approach:
get list of suites from run
loop through that where
"""
pass
# @TODO: I don't think I need this. I can do this with a
# template tag to get the intersection of suites to caseversion
# to display it.

"""
run - runsuite - suite - suitecase - case - caseversion - m2m - env
Expand Down
30 changes: 30 additions & 0 deletions moztrap/view/runtests/templatetags/execution.py
Expand Up @@ -136,3 +136,33 @@ def render_tag(self, context, result, casestep, varname):


register.tag(StepResultFor)



class SuitesFor(Tag):
"""Return suite intersection of case and run."""

name = "suites_for"
options = Options(
Argument("run"),
Argument("runcaseversion"),
"as",
Argument("varname", resolve=False),
)


def render_tag(self, context, run, runcaseversion, varname):
"""Get/construct StepResult and place it in context under ``varname``"""
stepresult_kwargs = dict(
run=run,
rcv=runcaseversion,
)
casesuites = set(runcaseversion.caseversion.case.suites.values_list("id", flat=True))
runsuites = set(run.suites.values_list("id", flat=True))
result = model.Suites.objects.filter(pk__in=casesuites.intersection(runsuites))

context[varname] = result
return u""


register.tag(SuitesFor)
2 changes: 1 addition & 1 deletion templates/runtests/list/_runtest_list_item.html
Expand Up @@ -3,6 +3,7 @@
{% with runcaseversion.caseversion as caseversion %}
{% result_for runcaseversion user environment as result %}
{% other_result_for runcaseversion user environment as other_result %}
{% suites_for run runcaseversion as suites %}

<article id="test-id-{{ runcaseversion.id }}" class="listitem {{ result.status }}" data-title="{{ caseversion.name }}">

Expand Down Expand Up @@ -66,7 +67,6 @@ <h3 class="title" title="{{ caseversion.name }}">{{ caseversion.name }}</h3>
</div>

<div class="suites">
{% with runcaseversion.suites.all as suites %}
{% if suites %}
<ul>
{% for suite in suites %}
Expand Down
18 changes: 14 additions & 4 deletions tests/model/execution/models/test_run.py
Expand Up @@ -607,6 +607,7 @@ def test_query_count_on_activate(self):
old_rcv = self.F.RunCaseVersionFactory(run=r, caseversion=old_cv)
old_rcv_id = old_rcv.id


print(r.runcaseversions.all().values_list("id", "caseversion__name", "caseversion_id"))

# test suite add to run
Expand All @@ -625,11 +626,19 @@ def test_query_count_on_activate(self):
cv_needed.append(cv)

# existing one that we should keep
self.F.RunCaseVersionFactory(run=r,
existing_rcv = self.F.RunCaseVersionFactory(run=r,
caseversion=cv_needed[3],
order=0,
)

# existing env that should be removed in removal phase
old_env = self.F.EnvironmentFactory.create_set(
["OS", "Browser"],
["Atari", "RS-232"],
)[0]
self.F.model.RunCaseVersion.environments.through(
runcaseversion=existing_rcv,
environment=old_env,
).save()

from django.conf import settings
from django.db import connection
Expand Down Expand Up @@ -659,9 +668,10 @@ def test_query_count_on_activate(self):
self.refresh(r)
print(r.runcaseversions.all().values_list("id", "caseversion__name", "caseversion_id"))

self.assertEqual(r.runcaseversions.count(), 6)
for rcv_env in self.F.model.RunCaseVersion.environments.through.objects.all().values():
print(json.dumps(rcv_env, indent=4))
print("removed_rcv_id={0}".format(old_rcv_id))

print(old_rcv_id)
self.assertEqual(r.runcaseversions.count(), 6)
self.assertEqual(self.F.model.RunCaseVersion.environments.through.objects.count(), 24)
self.assertEqual(r.runcaseversions.all(), "foo")

0 comments on commit 256a9e5

Please sign in to comment.