def test_it_creates_and_runs_query(sdk: mtds.Looker31SDK, queries_system_activity: TQueries): """create_query() creates a query and run_query() returns its result.""" for q in queries_system_activity: limit = cast(str, q["limit"]) or "10" request = create_query_request(q, limit) query = sdk.create_query(request) assert isinstance(query, ml.Query) assert query.id assert isinstance(query.id, int) assert query.id > 0 sql = sdk.run_query(query.id, "sql") assert "SELECT" in sql json_ = sdk.run_query(query.id, "json") assert isinstance(json_, str) json_ = json.loads(json_) assert isinstance(json_, list) assert len(json_) == int(limit) row = json_[0] if q.get("fields"): for field in q["fields"]: assert field in row.keys() csv = sdk.run_query(query.id, "csv") assert isinstance(csv, str) assert len(re.findall(r"\n", csv)) == int(limit) + 1
def test_enum(sdk: mtds.Looker31SDK): # TODO: there is currently no example in the Looker API of a "bare" # ForwardRef property on a model that is returned by the API. We # have unittests deserializing into "bare" ForwardRef properties, # that will have to do for now. query = ml.WriteQuery( model="system__activity", view="dashboard", fields=["dashboard.id", "dashboard.title", "dashboard.count"], ) query_id = sdk.create_query(query).id assert query_id task = ml.WriteCreateQueryTask( query_id=query_id, source="test", result_format=ml.ResultFormat.csv ) created = sdk.create_query_task(task) # created.result_format is type str, not ResultFormat.csv assert ml.ResultFormat.csv.value == created.result_format
def test_crud_look(sdk: mtds.Looker31SDK, looks): """Test creating, retrieving, updating and deleting a look.""" for l in looks: request = create_query_request(l["query"][0], "10") query = sdk.create_query(request) look = sdk.create_look( ml.WriteLookWithQuery( title=l.get("title"), description=l.get("description"), deleted=l.get("deleted"), is_run_on_load=l.get("is_run_on_load"), public=l.get("public"), query_id=query.id, space_id=l.get("space_id") or str(sdk.me().personal_space_id), )) assert isinstance(look, ml.LookWithQuery) assert look.title == l.get("title") assert look.description == l.get("description") assert look.deleted == l.get("deleted") assert look.is_run_on_load == l.get("is_run_on_load") # TODO this is broken for local dev but works for CI... # assert look.public == l.get("public") assert look.query_id == query.id assert look.space_id == l.get("space_id") or sdk.me().home_space_id assert look.user_id == l.get("user_id") or sdk.me().id # Update assert isinstance(look.id, int) updated_look = sdk.update_look(look.id, ml.WriteLookWithQuery(deleted=True)) assert updated_look.deleted assert updated_look.title == look.title look = sdk.update_look(look.id, ml.WriteLookWithQuery(deleted=False)) assert not look.deleted
def test_crud_dashboard(sdk: mtds.Looker31SDK, queries_system_activity, dashboards): """Test creating, retrieving, updating and deleting a dashboard. """ qhash: Dict[Union[str, int], ml.Query] = {} for idx, q in enumerate(queries_system_activity): limit = "10" request = create_query_request(q, limit) key = q.get("id") or str(idx) qhash[key] = sdk.create_query(request) for d in dashboards: dashboard = sdk.create_dashboard( ml.WriteDashboard( description=d.get("description"), hidden=d.get("hidden"), query_timezone=d.get("query_timezone"), refresh_interval=d.get("refresh_interval"), title=d.get("title"), background_color=d.get("background_color"), load_configuration=d.get("load_configuration"), lookml_link_id=d.get("lookml_link_id"), show_filters_bar=d.get("show_filters_bar"), show_title=d.get("show_title"), slug=d.get("slug"), space_id=d.get("space_id") or sdk.me().home_space_id, text_tile_text_color=d.get("text_tile_text_color"), tile_background_color=d.get("tile_background_color"), tile_text_color=d.get("tile_text_color"), title_color=d.get("title_color"), )) assert isinstance(dashboard, ml.Dashboard) if d.get("background_color"): assert d["background_color"] == dashboard.background_color if d.get("text_tile_text_color"): assert d["text_tile_text_color"] == dashboard.text_tile_text_color if d.get("tile_background_color"): assert d[ "tile_background_color"] == dashboard.tile_background_color if d.get("tile_text_color"): assert d["tile_text_color"] == dashboard.tile_text_color if d.get("title_color"): assert d["title_color"] == dashboard.title_color # Update dashboard assert isinstance(dashboard.id, str) update_response = sdk.update_dashboard(dashboard.id, ml.WriteDashboard(deleted=True)) assert update_response.deleted assert update_response.title == dashboard.title dashboard = sdk.update_dashboard(dashboard.id, ml.WriteDashboard(deleted=False)) assert isinstance(dashboard.id, str) assert not dashboard.deleted if d.get("filters"): for f in d["filters"]: filter = sdk.create_dashboard_filter( ml.WriteCreateDashboardFilter( dashboard_id=dashboard.id, name=f.get("name"), title=f.get("title"), type=f.get("type"), default_value=f.get("default_value"), model=f.get("model"), explore=f.get("explore"), dimension=f.get("dimension"), row=f.get("row"), listens_to_filters=f.get("listens_to_filters"), allow_multiple_values=f.get("allow_multiple_values"), required=f.get("required"), )) assert isinstance(filter, ml.DashboardFilter) assert filter.name == f.get("name") assert filter.title == f.get("title") assert filter.type == f.get("type") assert filter.default_value == f.get("default_value") assert filter.model == f.get("model") assert filter.explore == f.get("explore") assert filter.dimension == f.get("dimension") assert filter.row == f.get("row") assert filter.allow_multiple_values == f.get( "allow_multiple_values", False) assert filter.required == f.get("required", False) if d.get("tiles"): for t in d["tiles"]: tile = sdk.create_dashboard_element( ml.WriteDashboardElement( body_text=t.get("body_text"), dashboard_id=dashboard.id, look=t.get("look"), look_id=t.get("look_id"), merge_result_id=t.get("merge_result_id"), note_display=t.get("note_display"), note_state=t.get("note_state"), note_text=t.get("note_text"), query=t.get("query"), query_id=get_query_id(qhash, t.get("query_id")), refresh_interval=t.get("refresh_interval"), subtitle_text=t.get("subtitle_text"), title=t.get("title"), title_hidden=t.get("title_hidden"), type=t.get("type"), )) assert isinstance(tile, ml.DashboardElement) assert tile.dashboard_id == dashboard.id assert tile.title == t.get("title") assert tile.type == t.get("type")