예제 #1
0
    def test_query_v2_multiple_conditions(self):
        repository = DynamoPlusRepository(self.collection)
        for i in range(1, 10):
            document = {
                "id": str(i),
                "attribute1": str(i % 2),
                "attribute2": "value_" + str(i)
            }
            self.table.put_item(
                Item={
                    "pk": "example#" + str(i),
                    "sk": "example",
                    "data": str(i),
                    "document": json.dumps(document)
                })
            self.table.put_item(
                Item={
                    "pk": "example#" + str(i),
                    "sk": "example#attribute1#attribute2",
                    "data": str(i % 2) + "#value_" + str(i),
                    "document": json.dumps(document)
                })

        query_model = Query(
            And([Eq("attribute1", "1"),
                 Eq("attribute2", "value_3")]), self.collection)
        result = repository.query_v2(query_model)
        self.assertIsNotNone(result)
        self.assertEqual(len(result.data), 1)
        self.assertEqual("1", result.data[0].document["attribute1"])
        self.assertEqual("value_3", result.data[0].document["attribute2"])
예제 #2
0
 def get_index(name: str, collection_name: str):
     query: QueryRepository = QueryRepository(
         And([Eq("collection.name", collection_name),
              Eq("name", name)]), index_metadata, 1)
     repository = DynamoPlusRepository(index_metadata, True)
     result = repository.query_v2(query)
     indexes = list(
         map(lambda m: from_dict_to_index(m.document), result.data))
     if len(indexes) == 0:
         return None
     else:
         return indexes[0]
예제 #3
0
    def test_query_v2(self):
        repository = DynamoPlusRepository(self.collection)
        for i in range(1, 10):
            document = {
                "id": str(i),
                "attribute1": str(i % 2),
                "attribute2": "value_" + str(i)
            }
            self.table.put_item(
                Item={
                    "pk": "example#" + str(i),
                    "sk": "example",
                    "data": str(i),
                    "document": json.dumps(document)
                })
            self.table.put_item(
                Item={
                    "pk": "example#" + str(i),
                    "sk": "example#attribute1",
                    "data": str(i % 2),
                    "document": json.dumps(document)
                })

        query_model = Query(Eq("attribute1", "1"), self.collection)
        result = repository.query_v2(query_model)
        self.assertIsNotNone(result)
        self.assertEqual(len(result.data), 5)
        for r in result.data:
            self.assertEqual(r.document["attribute1"], '1')
예제 #4
0
 def find_collections_by_example(example: Collection):
     repository = DynamoPlusRepository(collectionMetadata, True)
     query: QueryRepository = QueryRepository(Eq("name", example.name),
                                              index_metadata)
     result = repository.query_v2(query)
     return list(
         map(lambda m: from_dict_to_collection(m.document), result.data))
예제 #5
0
def from_dict_to_predicate(d: dict):
    if "eq" in d:
        return Eq(d["eq"]["field_name"], d["eq"]["value"])
    elif "range" in d:
        return Range(d["range"]["field_name"], d["range"]["from"],
                     d["range"]["to"])
    elif "and" in d:
        conditions = list(map(lambda cd: from_dict_to_predicate(cd), d["and"]))
        return And(conditions)
예제 #6
0
 def find_indexes_from_collection_name(collection_name: str,
                                       limit: int = None,
                                       start_from: str = None):
     repository = DynamoPlusRepository(index_metadata, True)
     last_evaluated_key = None
     if start_from:
         last_evaluated_key = repository.get(start_from)
     query: QueryRepository = QueryRepository(
         Eq("collection.name", collection_name), index_metadata, limit,
         last_evaluated_key)
     result = repository.query_v2(query)
     return list(map(lambda m: from_dict_to_index(m.document),
                     result.data)), result.lastEvaluatedKey
예제 #7
0
    def test_query_v2_multiple_conditions_range_limit_start_from(self):
        repository = DynamoPlusRepository(self.collection)
        for i in range(1, 50):
            document = {
                "id": f"{i:08}",
                "attribute1": str(i % 2),
                "attribute2": "value_" + f"{i:08}",
                "attribute3": f"{i:08}"
            }
            if i == 27:
                starting_after_document = document
            self.table.put_item(
                Item={
                    "pk": "example#" + document["id"],
                    "sk": "example",
                    "data": document["id"],
                    "document": json.dumps(document)
                })
            self.table.put_item(
                Item={
                    "pk": "example#" + document["id"],
                    "sk": "example#attribute1#attribute3",
                    "data": str(i % 2) + "#" + f"{i:08}",
                    "document": json.dumps(document)
                })

        # starting_after_document = {"id": "00000029", "attribute1": "1", "attribute2": "value_00000029", "attribute3": "00000029"}
        index = Index("1", self.collection.name, ["attribute1", "attribute3"])
        starting_after = IndexModel(self.collection, starting_after_document,
                                    index)
        query_model = Query(
            And([
                Eq("attribute1", "1"),
                Range("attribute3", "00000020", "00000040")
            ]), self.collection, 3, starting_after)
        result = repository.query_v2(query_model)
        self.assertIsNotNone(result)
        self.assertEqual(len(result.data), 3)
        self.assertEqual("1", result.data[1].document["attribute1"])
        self.assertEqual("value_00000029",
                         result.data[0].document["attribute2"])
        self.assertEqual("00000029", result.data[0].document["attribute3"])
        self.assertEqual("1", result.data[1].document["attribute1"])
        self.assertEqual("value_00000031",
                         result.data[1].document["attribute2"])
        self.assertEqual("00000031", result.data[1].document["attribute3"])
        self.assertEqual("1", result.data[2].document["attribute1"])
        self.assertEqual("value_00000033",
                         result.data[2].document["attribute2"])
        self.assertEqual("00000033", result.data[2].document["attribute3"])
예제 #8
0
 def test_queryCollectionByName(self, mock_index_dynamoplus_repository,
                                mock_find):
     collection_metadata = Collection("example", "name")
     expected_query = Query(Eq("name", "example"), collection_metadata)
     mock_index_dynamoplus_repository.return_value = None
     mock_find.return_value = QueryResult([
         Model(Collection("example", "id"), {
             "name": "example",
             "id_key": "id"
         })
     ])
     collections = self.systemService.find_collections_by_example(
         collection_metadata)
     self.assertTrue(len(collections) == 1)
     self.assertEqual(collections[0].name, "example")
     self.assertEqual(call(expected_query), mock_find.call_args_list[0])
예제 #9
0
 def test_queryIndex_by_CollectionByName_generator(
         self, mock_index_dynamoplus_repository, mock_find):
     expected_query = Query(Eq("collection.name", "example"),
                            Collection("index", "uid"), 2)
     mock_index_dynamoplus_repository.return_value = None
     mock_find.side_effect = [
         self.fake_query_result("1", "2"),
         self.fake_query_result("2", "3"),
         self.fake_query_result("3", "4"),
         self.fake_query_result("4", "5"),
         self.fake_query_result("5"),
     ]
     indexes = self.systemService.get_indexes_from_collection_name_generator(
         "example", 2)
     uids = list(map(lambda i: i.uid, indexes))
     self.assertEqual(5, len(uids))
     self.assertEqual(["1", "2", "3", "4", "5"], uids)
     self.assertEqual(call(expected_query), mock_find.call_args_list[0])
예제 #10
0
    def test_query_v2_multiple_conditions_range_limit(self):
        repository = DynamoPlusRepository(self.collection)
        for i in range(1, 50):
            document = {
                "id": f"{i:08}",
                "attribute1": str(i % 2),
                "attribute2": "value_" + f"{i:08}",
                "attribute3": f"{i:08}"
            }
            self.table.put_item(
                Item={
                    "pk": "example#" + document["id"],
                    "sk": "example",
                    "data": document["id"],
                    "document": json.dumps(document)
                })
            self.table.put_item(
                Item={
                    "pk": "example#" + document["id"],
                    "sk": "example#attribute1#attribute3",
                    "data": str(i % 2) + "#" + f"{i:08}",
                    "document": json.dumps(document)
                })

        query_model = Query(
            And([
                Eq("attribute1", "1"),
                Range("attribute3", "00000020", "00000030")
            ]), self.collection, 3)
        result = repository.query_v2(query_model)
        self.assertIsNotNone(result)
        self.assertEqual(len(result.data), 3)
        self.assertEqual("1", result.data[1].document["attribute1"])
        self.assertEqual("value_00000021",
                         result.data[0].document["attribute2"])
        self.assertEqual("00000021", result.data[0].document["attribute3"])
        self.assertEqual("1", result.data[1].document["attribute1"])
        self.assertEqual("value_00000023",
                         result.data[1].document["attribute2"])
        self.assertEqual("00000023", result.data[1].document["attribute3"])
        self.assertEqual("1", result.data[2].document["attribute1"])
        self.assertEqual("value_00000025",
                         result.data[2].document["attribute2"])
        self.assertEqual("00000025", result.data[2].document["attribute3"])
예제 #11
0
 def test_get_documents_by_index(self, mock_system_service, mock_get_collection_by_name, mock_get_index,
                                 mock_domain_service, mock_find_by_index):
     mock_system_service.return_value = None
     mock_get_collection_by_name.return_value = Collection("example", "id", "ordering")
     mock_domain_service.return_value = None
     expected_index = Index("1","example", ["attribute1"])
     expected_predicate = Eq("attribute1","1")
     mock_get_index.return_value=expected_index
     expected_document_example = {"attribute1": "1"}
     expected_documents = [
         {"id": "1", "attribute1": "1"},
         {"id": "2", "attribute1": "1"}
     ]
     mock_find_by_index.return_value = expected_documents, None
     documents = query("example", {"matches": {"eq":{"field_name":"attribute1", "value":"1"}}})
     self.assertEqual(len(documents), len(expected_documents))
     self.assertTrue(mock_get_collection_by_name.called_with("example"))
     self.assertTrue(mock_get_index.called_with("attribute1"))
     self.assertEqual(call(expected_predicate, None, None), mock_find_by_index.call_args_list[0])
예제 #12
0
 def test_queryIndex_by_CollectionByName(self,
                                         mock_index_dynamoplus_repository,
                                         mock_find):
     expected_query = Query(Eq("collection.name", "example"),
                            Collection("index", "uid"))
     mock_index_dynamoplus_repository.return_value = None
     mock_find.return_value = QueryResult([
         Model(
             Collection("index", "name"), {
                 "uid": "1",
                 "name": "collection.name",
                 "collection": {
                     "name": "example"
                 },
                 "conditions": ["collection.name"]
             })
     ])
     indexes, last_key = self.systemService.find_indexes_from_collection_name(
         "example")
     self.assertEqual(1, len(indexes))
     self.assertEqual(indexes[0].uid, "1")
     self.assertEqual(indexes[0].index_name, "collection.name")
     self.assertEqual(call(expected_query), mock_find.call_args_list[0])