def test_where(self): collection = Collection([ { "id": 1, "name": "Joe" }, { "id": 2, "name": "Joe" }, { "id": 3, "name": "Bob" }, ]) self.assertEqual(len(collection.where("name", "Joe")), 2) self.assertEqual(len(collection.where("id", "!=", 1)), 2) self.assertEqual(len(collection.where("id", ">", 1)), 2) self.assertEqual(len(collection.where("id", ">=", 1)), 3) self.assertEqual(len(collection.where("id", "<=", 1)), 1) self.assertEqual(len(collection.where("id", "<", 3)), 2)
def test_where_in(self): collection = Collection([ { "id": 1, "name": "Joe" }, { "id": 2, "name": "Joe" }, { "id": 3, "name": "Bob" }, ]) self.assertEqual(len(collection.where_in("id", [1, 2])), 2) self.assertEqual(len(collection.where_in("id", [3])), 1) self.assertEqual(len(collection.where_in("id", [4])), 0) self.assertEqual(len(collection.where_in("id", ["1", "2"])), 2) self.assertEqual(len(collection.where_in("id", ["3"])), 1) self.assertEqual(len(collection.where_in("id", ["4"])), 0)
def test_json(self): collection = Collection([ { "name": "Corentin", "age": 10 }, { "name": "Joe", "age": 20 }, { "name": "Marlysson", "age": 15 }, ]) json_data = collection.to_json() self.assertEqual( json_data, '[{"name": "Corentin", "age": 10}, ' '{"name": "Joe", "age": 20}, {"name": "Marlysson", "age": 15}]', )
def test_every(self): collection = Collection([1, 2, 3, 4]) self.assertFalse(collection.every(lambda x: x > 2)) collection = Collection([1, 2, 3, 4]) self.assertTrue(collection.every(lambda x: x >= 1))
def test_each(self): collection = Collection([1, 2, 3, 4]) collection.each(lambda x: x + 2) self.assertEqual(collection.all(), [x + 2 for x in range(1, 5)])
def test_pop(self): collection = Collection([1, 2, 3]) self.assertEqual(collection.pop(), 3) self.assertEqual(collection.all(), [1, 2])
def test_zip(self): collection = Collection(["Chair", "Desk"]) zipped = collection.zip([100, 200]) self.assertEqual(zipped.all(), [["Chair", 100], ["Desk", 200]])
def test_diff(self): collection = Collection(["Chair", "Desk"]) diff = collection.diff([100, 200]) self.assertEqual(diff.all(), ["Chair", "Desk"])
def test_last(self): collection = Collection([1, 2, 3, 4]) self.assertEqual(collection.last(), 4) self.assertEqual(collection.last(lambda x: x < 3), 2)
def test_sort(self): collection = Collection([4, 1, 2, 3]) collection.sort() self.assertEqual(collection.all(), [1, 2, 3, 4])
def test_flatten(self): collection = Collection([1, 2, [3, 4, 5, {"foo": "bar"}]]) flattened = collection.flatten() self.assertEqual(flattened.all(), [1, 2, 3, 4, 5, "bar"])
def test_make_comparison(self): collection = Collection([]) self.assertTrue(collection._make_comparison(1, 1, "==")) self.assertTrue(collection._make_comparison(1, "1", "=="))
def test_pull(self): collection = Collection([1, 2, 3, 4]) value = collection.pull(0) self.assertEqual(value, 1) self.assertEqual(collection.all(), [2, 3, 4])
def test_push(self): collection = Collection([1, 2, 3, 4]) collection.push(5) self.assertEqual(collection.get(4), 5) self.assertEqual(collection.all(), [1, 2, 3, 4, 5])
def test_prepend(self): collection = Collection([1, 2, 3, 4]) collection.prepend(0) self.assertEqual(collection.get(0), 0) self.assertEqual(collection.all(), [0, 1, 2, 3, 4])
def test_merge(self): collection = Collection([[1, 1], [2, 4]]) collection.merge([[2, 1]]) self.assertEqual(collection.all(), [[1, 1], [2, 4], [2, 1]])
def test_collapse(self): collection = Collection([[1, 1], [2, 4]]) collapsed = collection.collapse() self.assertEqual(collapsed, Collection([1, 1, 2, 4]))
def test_filter(self): collection = Collection([1, 2, 3, 4]) filtered = collection.filter(lambda x: x > 2) self.assertEqual(filtered.all(), Collection([3, 4]))
def test_make_comparison(self): collection = Collection([]) self.assertTrue(collection._make_comparison(1, 1, '==')) self.assertTrue(collection._make_comparison(1, '1', '=='))
def test_put(self): collection = Collection([1, 2, 3, 4]) collection.put(2, 5) self.assertEqual(collection.get(2), 5) self.assertEqual(collection.all(), [1, 2, 5, 4])
def test_is_empty(self): collection = Collection([]) self.assertEqual(collection.is_empty(), True) collection = Collection([1, 2, 3]) self.assertEqual(collection.is_empty(), False)
def test_take(self): collection = Collection([1, 2, 3, 4]) self.assertEqual(collection.take(2), [1, 2])
def test_for_page(self): collection = Collection([1, 2, 3, 4]) chunked = collection.for_page(0, 3) self.assertEqual(chunked.all(), [1, 2, 3])
def test_eq(self): collection = Collection([1, 2, 3, 4]) other = Collection([1, 2, 3, 4]) self.assertTrue(collection == other) different = Collection([1, 2, 3]) self.assertFalse(collection == different)
def test_transform(self): collection = Collection([1, 1, 2, 3, 4]) collection.transform(lambda x: x * 2) self.assertEqual(collection.all(), [2, 2, 4, 6, 8])