def test_foreignKeySerializer(self): u = core.ForeignKeySerializer( 'foo', other_serializer=core.StringSerializer('name') ) with Mock() as fakeobj2: fakeobj2.__getattr__('name') >> 'bar' with Mock() as fakeobj: fakeobj.__getattr__('foo') >> fakeobj2 self.assertEquals(u.serialize(fakeobj), 'bar')
def test_dateWidget(self): flt = widgets.DateWidget('foo') requestDict = {'foo': '2011-01-11'} with Mock() as querySet: querySet.filter(foo=date(2011, 1, 11)) flt.filterQuerySet(querySet, requestDict)
def test_enabled(self): f = core.SimpleFilter('foo', 'contains') with Mock() as querySet: querySet.filter(foo__contains='bar') f.enabled(querySet, 'bar') querySet.validate()
def fakeQuerySet(): with Mock() as mock: for a in [1,2,3]: mock.__getattr__('foo') >> 123 mock.__getattr__('pk') >> a return [mock, mock, mock]
def test_stringWidget_filterQuerySet(self): w = widgets.StringWidget('foo') requestDict = {'foo': 'omg'} with Mock() as querySet: querySet.filter(foo__contains='omg') w.filterQuerySet(querySet, requestDict)
def mock_context(response): with Mock() as context: context.__len__() >> 1 socket = context.socket(zmq.REQ) socket.connect('inproc://zrpc') socket.send(instance_of(str)) socket.recv() >> BSON.encode(response) return context
def test_filterAndSort(self): with Mock() as querySet: querySet.filter(foo__contains='5') querySet.order_by('-foo') self.s.querySet = querySet self.s.filterAndSort(self.valueDict, (self.c, True)) querySet.validate()
def test_filterAndSort(self): with Mock() as storage: storage.getColumns() >> [] storage.filterAndSort(self.requestDict, None) self.t.storage = storage self.t.filterAndSort(self.fakeRequest) storage.validate()
def test_stringColumn(self): s = columns.StringColumn('foo') with Mock() as querySet: querySet.order_by('foo') querySet.order_by('-foo') s.sortQuerySet(querySet, False) s.sortQuerySet(querySet, True)
def test_urlSerializer(self): u = core.URLSerializer(field='foo', urlName='bar') with Mock() as resolver: resolver('bar', args=(1, )) >> True u.resolver = resolver self.assertEquals(u.serialize_value(None), None) self.assertEquals(u.serialize_value(1), True)
def test_parse_title_from_url(self): with Mock() as Http: from httplib2 import Http connection = Http() connection.request(self.url) >> ({'headers': ''}, self.html) html2data_instance = HTML2Data(url=self.url) title = html2data_instance.xpath('//head/title/text()') self.assertEqual([ 'Example Page', ], title)
def test_filterQuerySet_clean(self): f = core.SimpleFilter('foo') f.clean = lambda x: 5 with Mock() as querySet: querySet.filter(foo=5) f.filterQuerySet(querySet, 'not 5') querySet.validate()
def test_serializeRow(self): class FakeModel: foo = '123' fm = FakeModel() with Mock() as column: column.name >> "foo" column.serialize(fm, output=0) >> 'bar' d = Table("name", 'querySet', [column]) self.assertEquals(d.serializeRow(fm, output=0), {'foo': 'bar'})
def test_serializeData(self): class FakeModel: foo = '1' queryset = [FakeModel(), FakeModel()] d = Table("name", 'querySet', [('foo', 'label')]) with Mock() as d.serializeRow: d.serializeRow(1, None) d.serializeRow(1, None) d.serializeData(queryset, output=None)
def test_serializeToXLS(self): class Foo: foo = 'bar' with Mock() as querySet: querySet.filter(foo__contains='5') querySet.order_by('-foo') querySet.__getitem__(0) >> Foo() querySet.__getitem__(1) >> StopIteration() self.s.querySet = querySet res = self.s.serializeToXLS(self.valueDict, (self.c, True)) querySet.validate()
def test_request_good(self): requestDict = {'foo': '5'} self.assertEquals(self.w.existsIn(requestDict), True) self.assertEquals(self.w.exportDescription(requestDict), ['Foo', '5']) with Mock() as querySet: querySet.filter(foo='5') self.w.filterQuerySet(querySet, requestDict) querySet.validate()
def test_sortQuerySet(self): with Mock() as querySet: querySet.order_by('foo') querySet.order_by('bar') s = columns.StringColumn('foo') s.sortQuerySet(querySet, False) s = columns.StringColumn('foo', sortColumnName='bar') s.sortQuerySet(querySet, False) querySet.validate()
def test_hrefSerializer(self): u = core.HrefSerializer( format='%(foo)s', urlSerializer=core.URLSerializer( field='bar', urlName='quuz') ) with Mock() as model: model.__getattr__('foo') >> 'baz' model.__getattr__('foo') >> 'baz' model.__getattr__('bar') >> 'quux' with Mock() as resolver: resolver('quuz', args=('quux', )) >> 'http://lol/' u.urlSerializer.resolver = resolver ret = u.serialize(model, output_format=formats.XLS) self.assertEquals(ret, 'baz') ret = u.serialize(model, output_format=formats.JSON) self.assertEquals(ret, 'http://lol/\nbaz')
def test_wholeDayFilter(self): f = extra_filters.WholeDayFilter('foo') with Mock() as querySet: querySet.filter( foo__gte=date(2011, 12, 31), foo__lt=date(2012, 1, 1)) querySet.filter( foo__gte=date(2011, 12, 31), foo__lt=date(2012, 1, 1)) f.filterQuerySet(querySet, date(2011, 12, 31)) f.filterQuerySet(querySet, datetime(2011, 12, 31, 12, 32))
def test_handleRequest_filter(self): f = self.fakeRequest f.GET['table'] = "filter,xxx" self.assertRaises(Http404, self.t.handleRequest, f) with Mock() as other_storage: other_storage.__len__() >> True other_storage.serializeToJSON(dict(foo='5'), None) >> {'a': 'b'} self.w.storage = other_storage f.GET['table'] = "widget,foo" resp = self.t.handleRequest(f, 'GET') self.assertEquals( str(resp).split('\n')[-1], '{"a": "b", "success": true}')
def test_fieldSerializer(self): f = core.serializers.FieldSerializer('foo') with Mock() as mock: mock.__getattr__('foo') >> True mock.__getattr__('foo') >> True self.assertEquals( f.extract_value(mock), True) self.assertRaises( NotImplementedError, f.serialize, mock) self.assertRaises( NotImplementedError, f.serialize_value, 'foo')
def test_serializeToJSON(self): class Foo: pk = 1 foo = 'bar' with Mock() as querySet: querySet.filter(foo__contains='5') querySet.order_by('-foo') querySet.count() >> 1 querySet.__getitem__(slice(0, None, None)) >> [Foo()] self.s.querySet = querySet res = self.s.serializeToJSON(self.valueDict, (self.c, True)) self.assertIn("'pk': 1", str(res)) querySet.validate()
def runTest(self): from ludibrio import Mock with Mock() as smtplibMock: message = """\ From: %s To: %s Subject: %s %s """ % (_FROMADDR, _TOADDR, "hi", 'hello!') server = smtplibMock.SMTP(_SERVER) server.starttls() server.login(_USER, _PASSWORD) server.sendmail(_FROMADDR, _TOADDR, message) notif = Notifier(smtplibMock) notif.NotifyRaw('hi', 'hello!') smtplibMock.validate()
def test_dateWidget_lte(self): flt = widgets.DateWidget('foo', filterKwargs={'operation': 'lte'}) requestDict = {'foo': '2011-01-11'} with Mock() as querySet: querySet.filter(foo__lte=date(2011, 1, 11)) flt.filterQuerySet(querySet, requestDict)
def test_biggerThan(self): f = extra_filters.BiggerThan('foo', 5) with Mock() as querySet: querySet.filter(foo__gt=5) f.enabled(querySet, True)
def setUp(self): with Mock() as mock: mock.__getattr__('foo') >> self.firstValue mock.__getattr__('foo') >> None self.mock = mock