def test_stream_iadd(self): s1 = stream([1, 2]) s1 += [3, 4] s1 += stream(xrange(5, 6)) # use xrange to cover the iterator case self.assertEquals(s1.toList(), [1, 2, 3, 4, 5]) self.assertEquals(s1.toList(), [1, 2, 3, 4, 5]) # second time to exclude one time iterator bug self.assertEquals(s1.toList(), [1, 2, 3, 4, 5])
def test_filterFromGeneratorReinstantiatesProperly(self): s = stream(ItrFromFunc(lambda: (i for i in xrange(5)))) s = s.filter(lambda e: e % 2 == 0) self.assertEquals(s.toList(), [0, 2, 4]) self.assertEquals(s.toList(), [0, 2, 4]) s = stream(xrange(5)).filter(lambda e: e % 2 == 0) self.assertEquals(s.toList(), [0, 2, 4]) self.assertEquals(s.toList(), [0, 2, 4])
def testStream(self): s = self.s self.assertEquals(list(ifilter(lambda i: i % 2 == 0, s())), [2]) self.assertEquals(list(s().filter(lambda i: i % 2 == 0)), [2]) self.assertEquals(s().filter(lambda i: i % 2 == 0).toList(), [2]) self.assertEquals(s()[1], 2) self.assertEquals(s()[1:].toList(), [2, 3]) self.assertEqual(s().take(2).toList(), [1, 2]) self.assertAlmostEqual(stream((0, 1, 2, 3)).filter(lambda x: x > 0).entropy(), 1.4591479) self.assertEquals(stream([(1, 2), (3, 4)]).zip().toList(), [(1, 3), (2, 4)])
def test_stream_add(self): s1 = stream([1, 2]) s2 = stream([3, 4]) s3 = s1 + s2 ll = s3.toList() self.assertEquals(s3.toList(), [1, 2, 3, 4]) self.assertEquals(s3.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug s1 = s1 + s2 self.assertEquals(s1.toList(), [1, 2, 3, 4]) self.assertEquals(s1.toList(), [1, 2, 3, 4]) # second time to exclude one time iterator bug
def test_mkString(self): streamToTest = stream(('a', 'b', 'c')) mock = MagicMock() joiner = "," streamToTest.join = mock streamToTest.mkString(joiner) mock.assert_called_once_with(joiner)
def testSdictToJson(self): from pyxtension.Json import Json j = stream((("a", 2), (3, 4))).toMap().toJson() self.assertIsInstance(j, Json) self.assertEqual(j.a, 2) self.assertDictEqual(j, {'a': 2, 3: 4})
def test_joinWithFunction(self): class F: def __init__(self): self.counter = 0 def __call__(self, *args, **kwargs): self.counter += 1 return str(self.counter) strings = ('a', 'b', 'c') f = F() self.assertEqual(stream(iter(strings)).join(f), "a1b2c")
def test_fastmap_time(self): def sleepFunc(el): time.sleep(0.3) return el * el s = stream(xrange(100)) t1 = time.time() res = s.fastmap(sleepFunc, poolSize=50).toSet() dt = time.time() - t1 expected = set(i * i for i in xrange(100)) self.assertSetEqual(res, expected) self.assertLessEqual(dt, 1.5)
def testStreamsFromGenerator(self): sg = stream(ItrFromFunc(lambda: (i for i in range(4)))) self.assertEqual(sg.size(), 4) self.assertEqual(sg.size(), 4) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3]) self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True]) self.assertEqual(sg.head(), 0) self.assertEqual(sg.head(), 0) self.assertEqual(sg.map(lambda i: i ** 2).enumerate().toList(), [(0, 0), (1, 1), (2, 4), (3, 9)]) self.assertEqual(sg.reduce(lambda x, y: x + y, 5), 11)
def search(self, search_rq: SearchRequest) -> [Video]: validate_request(search_rq) st = search_rq.SearchType sf = search_rq.SearchField if st is SearchType.isFavourite: return self.search_favourite() search_words = [word.strip(" ").lower() for word in sf.split(" ") if word.strip(" ") != ""] videos = stream([self.search_by_word(st, word) for word in search_words]).flatMap() return self.rank_distinct(videos)
def test_tokenize_text(self): tokenizer = SpacyTokenizer() sentences = tokenizer.tokenizeText( "I am happy? All U.K. is happy after Brexit?") self.assertEqual(2, len(sentences)) tagged_text = sentences[0].getTaggedText() jsoned = stream(tagged_text).map(lambda _: _.toJson()).toList() self.assertListEqual([{ 'word': 'I', 'tag': 'PRP', 'stemmed': '-PRON-' }, { 'word': 'am', 'tag': 'VBP', 'stemmed': 'be' }, { 'word': 'happy', 'tag': 'JJ', 'stemmed': 'happy' }, { 'tag': 'SYM' }], jsoned)
def test_reversedException(self): s = stream(xrange(1, 2, 3)) with self.assertRaises(TypeError): s.reversed()
def test_unique_empty_stream(self): s = stream([]) self.assertListEqual(s.unique().toList(), [])
def test_pstddev_nominal(self): s = stream([1, 2, 3, 4]) self.assertAlmostEqual(s.pstddev(), 1.118033988749895)
def test_unique_generator_stream(self): s = stream(ItrFromFunc(lambda: xrange(4))) u = s.unique() self.assertListEqual(u.toList(), [0, 1, 2, 3]) self.assertListEqual(u.toList(), [0, 1, 2, 3])
def test_unique_nominal(self): s = stream([1, 2, 3, 1, 2]) self.assertListEqual(s.unique().toList(), [1, 2, 3])
def test_mean_exception(self): with self.assertRaises(ValueError): stream([]).mean()
def test_values_nominal(self): self.assertListEqual(stream([(1, 'a'), (2, 'bb'), (0, '')]).values().toList(), ['a', 'bb', ''])
def testStreamToJson(self): from pyxtension.Json import JsonList j = stream((("a", 2), (3, 4))).toJson() self.assertIsInstance(j, JsonList) self.assertListEqual(j, [["a", 2], [3, 4]])
def test_pstddev_exception(self): with self.assertRaises(ValueError): stream([]).pstddev()
def test_keys_nominal(self): self.assertListEqual(stream([(1, 'a'), (2, 'bb'), (0, '')]).keystream().toList(), [1, 2, 0])
def test_toSumCounter_onStrings(self): s = stream([('a', 'b'), ('a', 'c')]) self.assertDictEqual(s.toSumCounter(), {'a': 'bc'})
def test_toSumCounter_onEmptyStream(self): s = stream([]) self.assertDictEqual(s.toSumCounter(), {})
def test_toSumCounter_nominal(self): s = stream([('a', 2), ('a', 4), ('b', 2.1), ('b', 3), ('c', 2)]) self.assertDictEqual(s.toSumCounter(), {'a': 6, 'b': 5.1, 'c': 2})
def test_streamExists(self): s = stream([0, 1]) self.assertEqual(s.exists(lambda e: e == 0), True) self.assertEqual(s.exists(lambda e: e == 2), False)
def test_toMap(self): self.assertDictEqual(stream(((1, 2), (3, 4))).toMap(), {1: 2, 3: 4})
def test_stream_repr_doesntChangeStream(self): s = stream(iter((1, 2, 3, 4))) repr(s) self.assertListEqual(s.toList(), [1, 2, 3, 4])
def test_joinWithString(self): s = "|" strings = ('a', 'b', 'c') self.assertEqual(stream(iter(strings)).join(s), s.join(strings))
def test_unique_mapping(self): s = stream(['abc', 'def', 'a', 'b', 'ab']) self.assertListEqual(s.unique(len).toList(), ['abc', 'a', 'ab'])
def test_joinWithNone(self): s = "" strings = ('a', 'b', 'c') self.assertEqual(stream(iter(strings)).join(), s.join(strings))
def test_keyBy_nominal(self): self.assertListEqual(stream(['a', 'bb', '']).keyBy(len).toList(), [(1, 'a'), (2, 'bb'), (0, '')])
def test_mean(self): self.assertAlmostEqual(stream([1, 2, 3, 4]).mean(), 2.5)