Skip to content
Snippets Groups Projects
Commit d98b06ac authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

cleaner tests

parent defdee6d
No related branches found
No related tags found
No related merge requests found
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import os
import unittest import unittest
from satella.coding import merge_dicts from satella.coding import merge_dicts
class TestMergeDicts(unittest.TestCase): class TestMergeDicts(unittest.TestCase):
def test_lolwut(self): def test_lolwut(self):
with open('lolwut', 'wb') as fout: try:
fout.write(b'{"a":2}') with open('lolwut', 'wb') as fout:
fout.write(b'{"a":2}')
finally:
os.unlink('lolwut')
def test_advanced_merge_dicts(self): def test_advanced_merge_dicts(self):
DA = { DA = {
"google-fcm": { "google-fcm": {
"http-auth": { "http-auth": {
"digicort": "key=digi", "digicort": "key=digi",
}, },
"endpoint": "https://fcm.googleapis.com/fcm/send" "endpoint": "https://fcm.googleapis.com/fcm/send"
} }
} }
DB = { DB = {
"google-fcm": { "google-fcm": {
"http-auth": { "http-auth": {
"springblade": "key=spri" "springblade": "key=spri"
}, },
"endpoint": "https://fcm.googleapis.com/fcm/send" "endpoint": "https://fcm.googleapis.com/fcm/send"
} }
} }
DC = merge_dicts(DA, DB) DC = merge_dicts(DA, DB)
...@@ -48,7 +50,7 @@ class TestMergeDicts(unittest.TestCase): ...@@ -48,7 +50,7 @@ class TestMergeDicts(unittest.TestCase):
self.assertEquals(nie['kupujemy'], 'tak') self.assertEquals(nie['kupujemy'], 'tak')
def test_merge_lists(self): def test_merge_lists(self):
tak = merge_dicts({'kupujemy': ['tak']}, {'kupujemy': ['nie']}) tak = merge_dicts({'kupujemy': ['tak']}, {'kupujemy': ['nie']})
self.assertEqual(set(tak['kupujemy']), set(['tak', 'nie'])) self.assertEqual(set(tak['kupujemy']), set(['tak', 'nie']))
......
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import unittest import unittest
import six
from satella.coding import typed, CallSignature, Number from satella.coding import typed, CallSignature, Number
...@@ -17,7 +20,6 @@ class TestTypecheck(unittest.TestCase): ...@@ -17,7 +20,6 @@ class TestTypecheck(unittest.TestCase):
self.assertRaises(TypeError, lambda: Lol().zomg('a')) self.assertRaises(TypeError, lambda: Lol().zomg('a'))
def test_cls_test(self): def test_cls_test(self):
class Lol(object): class Lol(object):
# this should fail, since the first argument the decorator gets is "self", because decorators always get FUNCTION objects! # this should fail, since the first argument the decorator gets is "self", because decorators always get FUNCTION objects!
@typed(int, returns=int) @typed(int, returns=int)
...@@ -27,20 +29,18 @@ class TestTypecheck(unittest.TestCase): ...@@ -27,20 +29,18 @@ class TestTypecheck(unittest.TestCase):
self.assertRaises(TypeError, lambda: Lol().zomg(2)) self.assertRaises(TypeError, lambda: Lol().zomg(2))
def test_ta(self): def test_ta(self):
@typed(int, int, returns=int) @typed(int, int, returns=int)
def sum(a, b): def sum(a, b):
return a+b return a + b
@typed(int, int, returns=str) @typed(int, int, returns=str)
def sum2(a, b): def sum2(a, b):
return a+b return a + b
sum(1, 2) sum(1, 2)
self.assertRaises(TypeError, lambda: sum2(2, 3)) self.assertRaises(TypeError, lambda: sum2(2, 3))
def test_tma(self): def test_tma(self):
def test(a, b, c, **d): def test(a, b, c, **d):
pass pass
...@@ -52,7 +52,6 @@ class TestTypecheck(unittest.TestCase): ...@@ -52,7 +52,6 @@ class TestTypecheck(unittest.TestCase):
self.assertFalse(cs.is_match_amount(1, 2, 3, 4)) self.assertFalse(cs.is_match_amount(1, 2, 3, 4))
def test_t1(self): def test_t1(self):
@typed(int, float, six.text_type) @typed(int, float, six.text_type)
def testf(a_int, a_float, a_string): def testf(a_int, a_float, a_string):
pass pass
...@@ -78,7 +77,7 @@ class TestTypecheck(unittest.TestCase): ...@@ -78,7 +77,7 @@ class TestTypecheck(unittest.TestCase):
testb(u'hello', 1) testb(u'hello', 1)
testb(object, 2) testb(object, 2)
@typed((None, )) @typed((None,))
def testc(p): def testc(p):
pass pass
...@@ -86,7 +85,6 @@ class TestTypecheck(unittest.TestCase): ...@@ -86,7 +85,6 @@ class TestTypecheck(unittest.TestCase):
testc(None) testc(None)
def test_t2(self): def test_t2(self):
@typed((int, None)) @typed((int, None))
def testa(a=5): def testa(a=5):
pass pass
...@@ -98,16 +96,14 @@ class TestTypecheck(unittest.TestCase): ...@@ -98,16 +96,14 @@ class TestTypecheck(unittest.TestCase):
testa(a=6) testa(a=6)
def test_self(self): def test_self(self):
class Wtf(object): class Wtf(object):
@typed('self', Number, Number, returns=Number) @typed('self', Number, Number, returns=Number)
def add(self, a, b): def add(self, a, b):
return a+b return a + b
Wtf().add(1,2.5) Wtf().add(1, 2.5)
def test_T2(self): def test_T2(self):
@typed((int, None)) @typed((int, None))
def testa(a=5): def testa(a=5):
pass pass
...@@ -118,8 +114,6 @@ class TestTypecheck(unittest.TestCase): ...@@ -118,8 +114,6 @@ class TestTypecheck(unittest.TestCase):
testa(a=None) testa(a=None)
testa(a=6) testa(a=6)
def test_t3(self): def test_t3(self):
def a(b, c): def a(b, c):
pass pass
...@@ -132,4 +126,3 @@ class TestTypecheck(unittest.TestCase): ...@@ -132,4 +126,3 @@ class TestTypecheck(unittest.TestCase):
self.assertEquals(CallSignature(a), CallSignature(b)) self.assertEquals(CallSignature(a), CallSignature(b))
self.assertNotEquals(CallSignature(a), CallSignature(c)) self.assertNotEquals(CallSignature(a), CallSignature(c))
import unittest
from threading import Thread from threading import Thread
from satella.coding import Monitor
from time import sleep from time import sleep
from six.moves.queue import Queue
import unittest from six.moves.queue import Queue
from satella.coding import Monitor
class MonitorTest(unittest.TestCase): class MonitorTest(unittest.TestCase):
def test_release_contextmanager(self): def test_release_contextmanager(self):
class TestedClass(Monitor): class TestedClass(Monitor):
def __init__(self, cqueue): def __init__(self, cqueue):
...@@ -36,9 +35,9 @@ class MonitorTest(unittest.TestCase): ...@@ -36,9 +35,9 @@ class MonitorTest(unittest.TestCase):
with Monitor.acquire(tc): with Monitor.acquire(tc):
with Monitor.release(tc): with Monitor.release(tc):
tt.start() tt.start()
sleep(0.4) sleep(0.4)
self.assertEqual(cq.qsize(), 2) self.assertEqual(cq.qsize(), 2)
def test_acquire_contextmanager(self): def test_acquire_contextmanager(self):
class TestedClass(Monitor): class TestedClass(Monitor):
...@@ -71,7 +70,6 @@ class MonitorTest(unittest.TestCase): ...@@ -71,7 +70,6 @@ class MonitorTest(unittest.TestCase):
self.assertEqual(cq.qsize(), 1) self.assertEqual(cq.qsize(), 1)
def test_monitoring(self): def test_monitoring(self):
class TestedClass(Monitor): class TestedClass(Monitor):
def __init__(self, cqueue): def __init__(self, cqueue):
self.cqueue = cqueue self.cqueue = cqueue
...@@ -98,4 +96,4 @@ class MonitorTest(unittest.TestCase): ...@@ -98,4 +96,4 @@ class MonitorTest(unittest.TestCase):
while a.is_alive() or b.is_alive(): while a.is_alive() or b.is_alive():
sleep(0.1) sleep(0.1)
self.assertNotEqual(q.qsize(), 2) self.assertNotEqual(q.qsize(), 2)
\ No newline at end of file
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import unittest import unittest
from satella.coding import TimeBasedHeap, Heap from satella.coding import TimeBasedHeap, Heap
class TestTimeBasedHeap(unittest.TestCase): class TestTimeBasedHeap(unittest.TestCase):
def test_tbh(self): def test_tbh(self):
tbh = TimeBasedHeap() tbh = TimeBasedHeap()
tbh.put(10, 'ala') tbh.put(10, 'ala')
...@@ -20,15 +20,14 @@ class TestTimeBasedHeap(unittest.TestCase): ...@@ -20,15 +20,14 @@ class TestTimeBasedHeap(unittest.TestCase):
self.assertIn((20, 'ma'), q) self.assertIn((20, 'ma'), q)
self.assertNotIn((30, 'kota'), q) self.assertNotIn((30, 'kota'), q)
def test_imprv(self): def test_imprv(self):
tbh = TimeBasedHeap() tbh = TimeBasedHeap()
tbh.put(10, 'ala') tbh.put(10, 'ala')
self.assertIn('ala', list(tbh.items())) self.assertIn('ala', list(tbh.items()))
class TestHeap(unittest.TestCase):
class TestHeap(unittest.TestCase):
def test_push(self): def test_push(self):
tbh = Heap() tbh = Heap()
tbh.push(10, 'A') tbh.push(10, 'A')
...@@ -46,19 +45,18 @@ class TestHeap(unittest.TestCase): ...@@ -46,19 +45,18 @@ class TestHeap(unittest.TestCase):
self.assertEqual(sorted(tb, reverse=True), list(tbh.iter_descending())) self.assertEqual(sorted(tb, reverse=True), list(tbh.iter_descending()))
def test_tbh(self): def test_tbh(self):
tbh = Heap() tbh = Heap()
tbh.push_many([ tbh.push_many([
(10, 'ala'), (10, 'ala'),
(20, 'ma') (20, 'ma')
]) ])
self.assertIn((10, 'ala'), tbh) self.assertIn((10, 'ala'), tbh)
self.assertIn((20, 'ma'), tbh) self.assertIn((20, 'ma'), tbh)
tbh.filtermap(filter_fun=lambda x: x[0] != 20, tbh.filtermap(filter_fun=lambda x: x[0] != 20,
map_fun=lambda x: (x[0]+10, 'azomg')) map_fun=lambda x: (x[0] + 10, 'azomg'))
self.assertIn((20, 'azomg'), tbh) self.assertIn((20, 'azomg'), tbh)
self.assertNotIn((10, 'ala'), tbh) self.assertNotIn((10, 'ala'), tbh)
......
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import monotonic
import unittest import unittest
from satella.instrumentation import instrument, DEBUG, RUNTIME, DISABLED, manager
import monotonic
import six
from satella.instrumentation import DEBUG, RUNTIME, DISABLED, manager
class TestInstrumentsAndMetrics(unittest.TestCase):
class TestInstrumentsAndMetrics(unittest.TestCase):
def test_m1(self): def test_m1(self):
root = manager.getInstrument(u'zomg') root = manager.getInstrument(u'zomg')
...@@ -22,14 +24,14 @@ class TestInstrumentsAndMetrics(unittest.TestCase): ...@@ -22,14 +24,14 @@ class TestInstrumentsAndMetrics(unittest.TestCase):
st = monotonic.monotonic() st = monotonic.monotonic()
for x in six.moves.xrange(0, 100): for x in six.moves.xrange(0, 100):
txt.log(u'Dupa'+str(x)) txt.log(u'Dupa' + str(x))
self.assertEqual(len(txt.view()), 30) self.assertEqual(len(txt.view()), 30)
for i, q in enumerate(txt.view()): for i, q in enumerate(txt.view()):
ts, m, txt = q ts, m, txt = q
self.assertGreaterEqual(m, st) self.assertGreaterEqual(m, st)
self.assertEquals(u'Dupa'+str(70 + i), txt) self.assertEquals(u'Dupa' + str(70 + i), txt)
def test_kids(self): def test_kids(self):
root = manager.getInstrument(u'root') root = manager.getInstrument(u'root')
...@@ -62,4 +64,4 @@ class TestInstrumentsAndMetrics(unittest.TestCase): ...@@ -62,4 +64,4 @@ class TestInstrumentsAndMetrics(unittest.TestCase):
self.assertEquals(cnt.view()[:2], (0, 0)) self.assertEquals(cnt.view()[:2], (0, 0))
import math import math
self.assertTrue(math.isnan(cnt.view()[2])) self.assertTrue(math.isnan(cnt.view()[2]))
\ No newline at end of file
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import six
import pickle import pickle
import unittest import unittest
from satella.instrumentation import Traceback from satella.instrumentation import Traceback
class TestTraceback(unittest.TestCase): class TestTraceback(unittest.TestCase):
def test_no_exc(self): def test_no_exc(self):
tb = Traceback() tb = Traceback()
...@@ -29,9 +28,9 @@ class TestTraceback(unittest.TestCase): ...@@ -29,9 +28,9 @@ class TestTraceback(unittest.TestCase):
def test_compression_happens(self): def test_compression_happens(self):
try: try:
loc = ' ' * (10*1024*1024) loc = ' ' * (10 * 1024 * 1024)
raise ValueError('hello') raise ValueError('hello')
except ValueError: except ValueError:
tb = Traceback() tb = Traceback()
self.assertLess(len(pickle.dumps(tb, -1)), 9*1024*1024) self.assertLess(len(pickle.dumps(tb, -1)), 9 * 1024 * 1024)
# coding=UTF-8 # coding=UTF-8
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import os
import sys import sys
import six
import unittest import unittest
import time
import threading
from mock import patch, Mock from mock import patch, Mock
import os
class TestPidlock(unittest.TestCase): class TestPidlock(unittest.TestCase):
def test_pidlock(self): def test_pidlock(self):
from satella.posix.pidlock import AcquirePIDLock, FailedToAcquire from satella.posix.pidlock import AcquirePIDLock
with AcquirePIDLock('lock', '.', delete_on_dead=True): with AcquirePIDLock('lock', '.', delete_on_dead=True):
self.assertTrue(os.path.exists('./lock')) self.assertTrue(os.path.exists('./lock'))
...@@ -22,7 +18,7 @@ class TestPidlock(unittest.TestCase): ...@@ -22,7 +18,7 @@ class TestPidlock(unittest.TestCase):
try: try:
r = int(r) r = int(r)
except ValueError: except ValueError:
return # lol wut return # lol wut
self.assertEquals(int(r), os.getpid()) self.assertEquals(int(r), os.getpid())
self.assertTrue(not os.path.exists('./lock')) self.assertTrue(not os.path.exists('./lock'))
...@@ -32,9 +28,8 @@ class TestDaemon(unittest.TestCase): ...@@ -32,9 +28,8 @@ class TestDaemon(unittest.TestCase):
@unittest.skipIf('win' in sys.platform, 'Running on Windows') @unittest.skipIf('win' in sys.platform, 'Running on Windows')
def test_daemonize(self): def test_daemonize(self):
with patch('sys.stdin') as stdin, patch('sys.stdout') as stdout, patch('sys.stderr') as stderr, \ with patch('sys.stdin') as stdin, patch('sys.stdout') as stdout, patch('sys.stderr') as stderr, \
patch('os.fork', return_value=0) as fork, patch('os.umask') as umask, patch('os.setsid') as setsid, \ patch('os.fork', return_value=0) as fork, patch('os.umask') as umask, patch('os.setsid') as setsid, \
patch('os.chdir') as chdir, patch('sys.exit', new=lambda: 0) as exit: patch('os.chdir') as chdir, patch('sys.exit', new=lambda: 0) as exit:
from satella.posix import daemonize from satella.posix import daemonize
stdin.close, stdout.close, stderr.close = Mock(), Mock(), Mock() stdin.close, stdout.close, stderr.close = Mock(), Mock(), Mock()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment