Skip to content

Commit

Permalink
#177 fix tests for running from the specfile:
Browse files Browse the repository at this point in the history
* tweak PYTHONPATH so it can find the cython extensions
* don't fail some tests if the extension normally tested was not built, as python3 builds doesn't have all of them, just skip the test

git-svn-id: https://xpra.org/svn/Xpra/trunk@7854 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Oct 1, 2014
1 parent 9806be1 commit a19fe34
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
5 changes: 3 additions & 2 deletions rpmbuild/xpra.spec
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,13 @@ rm -rf $RPM_BUILD_ROOT
/usr/bin/desktop-file-validate %{buildroot}%{_datadir}/applications/xpra.desktop

pushd xpra-%{version}-python2/tests
%{__python2} unit/run.py
PYTHONPATH=%{buildroot}%{python2_sitearch}:. %{__python2} unit/run.py
popd

%if 0%{?with_python3}
export
pushd xpra-%{version}-python3/tests
%{__python3} unit/run.py
PYTHONPATH=%{buildroot}%{python3_sitearch}:. %{__python3} unit/run.py
popd
%endif

Expand Down
23 changes: 14 additions & 9 deletions src/tests/unit/server/cystats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import unittest
import random
import time
from xpra.server.cystats import logp, calculate_time_weighted_average, calculate_timesize_weighted_average #@UnresolvedImport

try:
from xpra.server import cystats
except ImportError:
cystats = None


class TestCystats(unittest.TestCase):
Expand All @@ -23,18 +27,18 @@ def test_calculate_timesize_weighted_average(self):
v = random.random()
data.append((t, s, v))
t += 1
a, ra = calculate_timesize_weighted_average(data)
a, ra = cystats.calculate_timesize_weighted_average(data)
assert 0<a and 0<ra
#the calculations use the ratio of the size divided by the elapsed time,
#so check that a predictable ratio gives the expected value:
for x in (5, 1000):
v = [(now, i*x, i) for i in range(1, 1000)]
a, ra = calculate_timesize_weighted_average(v)
a, ra = cystats.calculate_timesize_weighted_average(v)
#but we need to round to an int to compare
self.assertEquals(x, int(round(a)))
self.assertEquals(x, int(round(ra)))
def t(v, ea, era):
a, ra = calculate_timesize_weighted_average(v)
a, ra = cystats.calculate_timesize_weighted_average(v)
self.assertEquals(int(round(a)), ea)
self.assertEquals(int(round(ra)), era)
#an old record won't make any difference
Expand All @@ -57,7 +61,7 @@ def t(v, ea, era):
t([(now-100, 1*1000, 1000), (now, 50*1000, 1000)], 50, 50)
#if using the same time, then size matters more:
v = [(now, 100*1000, 1000), (now, 50*1000, 1000)]
a, ra = calculate_timesize_weighted_average(v)
a, ra = cystats.calculate_timesize_weighted_average(v)
#recent is the same as "normal" average:
self.assertEquals(int(round(a)), int(round(ra)))
self.assertGreater(a, 75)
Expand All @@ -79,7 +83,7 @@ def t(v, ea, era):
raw_v = [size/elapsed for _,size,elapsed in v]
min_v = min(raw_v)
max_v = max(raw_v)
a, ra = calculate_timesize_weighted_average(v)
a, ra = cystats.calculate_timesize_weighted_average(v)
self.assertLess(a, max_v)
self.assertLess(ra, max_v)
self.assertGreater(a, min_v)
Expand All @@ -94,18 +98,19 @@ def test_calculate_time_weighted_average(self):
v = random.random()
data.append((t, v))
t += 1
a, ra = calculate_time_weighted_average(data)
a, ra = cystats.calculate_time_weighted_average(data)
assert 0<a<1 and 0<ra<1

def test_logp(self):
for _ in range(10000):
x = random.random()
v = logp(x)
v = cystats.logp(x)
assert v>0 and v<1


def main():
unittest.main()
if cystats:
unittest.main()

if __name__ == '__main__':
main()
20 changes: 13 additions & 7 deletions src/tests/unit/server/region_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

import unittest

from xpra.server.region import rectangle
try:
from xpra.server.region import rectangle

R1 = rectangle(0, 0, 20, 20)
R2 = rectangle(0, 0, 20, 20)
R3 = rectangle(0, 0, 40, 40)
R4 = rectangle(10, 10, 50, 50)
R5 = rectangle(100, 100, 100, 100)
except:
rectangle, R1, R2, R3, R4, R5 = None, None, None, None, None, None

R1 = rectangle(0, 0, 20, 20)
R2 = rectangle(0, 0, 20, 20)
R3 = rectangle(0, 0, 40, 40)
R4 = rectangle(10, 10, 50, 50)
R5 = rectangle(100, 100, 100, 100)

class TestVersionUtilModule(unittest.TestCase):

Expand Down Expand Up @@ -98,7 +102,9 @@ def test_substract(self):


def main():
unittest.main()
#skip test if import failed (ie: not a server build)
if rectangle is not None:
unittest.main()

if __name__ == '__main__':
main()
26 changes: 15 additions & 11 deletions src/tests/unit/server/video_subregion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@
import gobject
gobject.threads_init()

import unittest
from collections import deque
from xpra.server.video_subregion import VideoSubregion, MIN_EVENTS
from xpra.server.video_subregion import sslog as log
from xpra.server.region import rectangle, merge_all

try:
from xpra.server import video_subregion, region
except ImportError:
video_subregion = None
region = None

import unittest

class TestVersionUtilModule(unittest.TestCase):

def test_eq(self):
log = video_subregion.sslog

def refresh_cb(window, regions):
log("refresh_cb(%s, %s)", window, regions)
r = VideoSubregion(gobject.timeout_add, gobject.source_remove, refresh_cb, 150)
r = video_subregion.VideoSubregion(gobject.timeout_add, gobject.source_remove, refresh_cb, 150)

ww = 1024
wh = 768

log("* checking that we need some events")
last_damage_events = []
for x in range(MIN_EVENTS):
for x in range(video_subregion.MIN_EVENTS):
last_damage_events.append((0, 0, 0, 1, 1))
r.identify_video_subregion(ww, wh, MIN_EVENTS, last_damage_events)
r.identify_video_subregion(ww, wh, video_subregion.MIN_EVENTS, last_damage_events)
assert r.rectangle is None

vr = (time.time(), 100, 100, 320, 240)
Expand All @@ -40,7 +43,7 @@ def refresh_cb(window, regions):
last_damage_events.append(vr)
r.identify_video_subregion(ww, wh, 50, last_damage_events)
assert r.rectangle
assert r.rectangle==rectangle(*vr[1:])
assert r.rectangle==region.rectangle(*vr[1:])

log("* checking that empty damage events does not cause errors")
r.reset()
Expand Down Expand Up @@ -84,7 +87,7 @@ def refresh_cb(window, regions):
last_damage_events.append(v1)
last_damage_events.append(v2)
r.identify_video_subregion(ww, wh, 100, last_damage_events)
m = merge_all([rectangle(*v1[1:]), rectangle(*v2[1:])])
m = region.merge_all([region.rectangle(*v1[1:]), region.rectangle(*v2[1:])])
assert r.rectangle==m, "expected %s but got %s" % (m, r.rectangle)

log("* but not if they are too far apart")
Expand All @@ -100,7 +103,8 @@ def refresh_cb(window, regions):


def main():
unittest.main()
if video_subregion and region:
unittest.main()

if __name__ == '__main__':
main()

0 comments on commit a19fe34

Please sign in to comment.