Пример #1
0
 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)
Пример #2
0
    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)
Пример #3
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}]',
        )
Пример #4
0
    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))
Пример #5
0
 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)])
Пример #6
0
 def test_pop(self):
     collection = Collection([1, 2, 3])
     self.assertEqual(collection.pop(), 3)
     self.assertEqual(collection.all(), [1, 2])
Пример #7
0
    def test_zip(self):
        collection = Collection(["Chair", "Desk"])
        zipped = collection.zip([100, 200])

        self.assertEqual(zipped.all(), [["Chair", 100], ["Desk", 200]])
Пример #8
0
    def test_diff(self):
        collection = Collection(["Chair", "Desk"])
        diff = collection.diff([100, 200])

        self.assertEqual(diff.all(), ["Chair", "Desk"])
Пример #9
0
 def test_last(self):
     collection = Collection([1, 2, 3, 4])
     self.assertEqual(collection.last(), 4)
     self.assertEqual(collection.last(lambda x: x < 3), 2)
Пример #10
0
    def test_sort(self):
        collection = Collection([4, 1, 2, 3])
        collection.sort()

        self.assertEqual(collection.all(), [1, 2, 3, 4])
Пример #11
0
    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"])
Пример #12
0
 def test_make_comparison(self):
     collection = Collection([])
     self.assertTrue(collection._make_comparison(1, 1, "=="))
     self.assertTrue(collection._make_comparison(1, "1", "=="))
Пример #13
0
    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])
Пример #14
0
    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])
Пример #15
0
    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])
Пример #16
0
 def test_merge(self):
     collection = Collection([[1, 1], [2, 4]])
     collection.merge([[2, 1]])
     self.assertEqual(collection.all(), [[1, 1], [2, 4], [2, 1]])
Пример #17
0
    def test_collapse(self):
        collection = Collection([[1, 1], [2, 4]])

        collapsed = collection.collapse()

        self.assertEqual(collapsed, Collection([1, 1, 2, 4]))
Пример #18
0
 def test_filter(self):
     collection = Collection([1, 2, 3, 4])
     filtered = collection.filter(lambda x: x > 2)
     self.assertEqual(filtered.all(), Collection([3, 4]))
Пример #19
0
 def test_make_comparison(self):
     collection = Collection([])
     self.assertTrue(collection._make_comparison(1, 1, '=='))
     self.assertTrue(collection._make_comparison(1, '1', '=='))
Пример #20
0
    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])
Пример #21
0
 def test_is_empty(self):
     collection = Collection([])
     self.assertEqual(collection.is_empty(), True)
     collection = Collection([1, 2, 3])
     self.assertEqual(collection.is_empty(), False)
Пример #22
0
 def test_take(self):
     collection = Collection([1, 2, 3, 4])
     self.assertEqual(collection.take(2), [1, 2])
Пример #23
0
    def test_for_page(self):
        collection = Collection([1, 2, 3, 4])
        chunked = collection.for_page(0, 3)

        self.assertEqual(chunked.all(), [1, 2, 3])
Пример #24
0
 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)
Пример #25
0
    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])