def test_documents(self): aCollection = "col_test_documents" anIndex = "idx_test_documents" self._q(query.create_collection({"name": aCollection})) self._q( query.create_index({ "name": anIndex, "source": query.collection(aCollection), "active": True })) count = 56 data = [{} for x in range(count)] self._q( query.foreach( query.lambda_( "x", query.create(query.collection(aCollection), {"data": query.var("x")})), data)) self.assertEqual( self._q( query.select([0], query.count( query.paginate( query.documents( query.collection(aCollection)))))), count) self.assertEqual( self._q(query.count(query.documents( query.collection(aCollection)))), count)
def test_range(self): data = list(range(1, 20)) self._q(query.create_collection({"name": "range_test"})) self._q( query.create_index({ "name": "range_idx", "source": query.collection("range_test"), "active": True, "values": [{ "field": ["data", "value"] }] })) self._q( query.foreach( query.lambda_query(lambda x: query.create( query.collection("range_test"), {"data": { "value": x }})), data)) m = query.match(query.index("range_idx")) q1 = query.select("data", query.paginate(query.range(m, 3, 8))) q2 = query.select("data", query.paginate(query.range(m, 17, 18))) q3 = query.select("data", query.paginate(query.range(m, 19, 0))) self.assertEqual(self._q(q1), [3, 4, 5, 6, 7, 8]) self.assertEqual(self._q(q2), [17, 18]) self.assertEqual(self._q(q3), [])
def test_count_mean_sum(self): data = [1, 2, 3, 4, 5, 6, 7, 8, 9] self._q(query.create_collection({"name": "countmeansum_test"})) self._q( query.create_index({ "name": "countmeansum_idx", "source": query.collection("countmeansum_test"), "active": True, "values": [{ "field": ["data", "value"] }] })) self._q( query.foreach( query.lambda_( "x", query.create( query.collection("countmeansum_test"), {"data": { "value": query.add(query.var("x"), 2) }})), data)) m = query.match(query.index("countmeansum_idx")) expected = [9, 5.0, 45, 9, 7.0, 63] self.assertEqual( self._q([ query.count(data), query.mean(data), query.sum(data), query.count(m), query.mean(m), query.sum(m) ]), expected)
def _delete_data(): resources = [q.functions(), q.indexes(), q.collections()] delete = lambda res: q.foreach( q.lambda_("res", q.delete(q.var("res"))), q.paginate(res) ) delete_queries = [delete(res) for res in resources] _execute_with_retries(q.do(*delete_queries))
def test_foreach(self): self.assertJson( query.foreach(lambda a: a, [1, 2, 3]), '{"collection":[1,2,3],"foreach":{"expr":{"var":"a"},"lambda":"a"}}' )
def test_foreach(self): refs = [self._create()["ref"], self._create()["ref"]] self._q(query.foreach(query.delete, refs)) for ref in refs: self.assertFalse(self._q(query.exists(ref)))
def _update_information_metadata( table_name: str, collection_metadata: CollectionMetadata) -> QueryExpression: column_metadata = [{ "name_": "id", "table_name_": table_name, "type_": "Integer", "nullable": False, "default_": None, }] column_metadata.extend([ { "name_": name, "table_name_": table_name, "type_": metadata["type"], # A bit awkward, but SQL uses the 'NOT NULL' keyword, while SQLAlchemy # uses 'nullable' when returning metadata "nullable_": not metadata["not_null"], "default_": metadata["default"], } for name, metadata in collection_metadata["fields"].items() ]) index_metadata = [{ "name_": index_name, "table_name_": table_name, **typing.cast(typing.Dict[str, typing.Any], metadata), } for index_name, metadata in collection_metadata["indexes"].items()] return q.if_( # We don't want to update information schema collections with information schema info, # because that would some weird inception-type stuff. q.contains_str_regex( table_name, r"^information_schema_(?:tables|columns|indexes)_$"), None, q.do( q.create( q.collection("information_schema_tables_"), {"data": { "name_": table_name }}, ), q.foreach( q.lambda_( "column_metadata", q.create( q.collection("information_schema_columns_"), {"data": q.var("column_metadata")}, ), ), column_metadata, ), q.foreach( q.lambda_( "index_metadata", q.create( q.collection("information_schema_indexes_"), {"data": q.var("index_metadata")}, ), ), index_metadata, ), ), )