|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package recon |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/datacommonsorg/mixer/internal/maps" |
| 22 | + "github.com/datacommonsorg/mixer/internal/store" |
| 23 | + "github.com/datacommonsorg/mixer/internal/store/bigtable" |
| 24 | + "github.com/datacommonsorg/mixer/internal/store/files" |
| 25 | + "github.com/datacommonsorg/mixer/internal/util" |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | + "google.golang.org/protobuf/proto" |
| 28 | + "google.golang.org/protobuf/testing/protocmp" |
| 29 | + |
| 30 | + pb "github.com/datacommonsorg/mixer/internal/proto" |
| 31 | +) |
| 32 | + |
| 33 | +func TestFindEntities(t *testing.T) { |
| 34 | + for _, tc := range []struct { |
| 35 | + name string |
| 36 | + req *pb.FindEntitiesRequest |
| 37 | + want *pb.FindEntitiesResponse |
| 38 | + }{ |
| 39 | + { |
| 40 | + "FromStore", |
| 41 | + &pb.FindEntitiesRequest{Description: "California"}, |
| 42 | + &pb.FindEntitiesResponse{Dcids: []string{"geoId/06"}}, |
| 43 | + }, |
| 44 | + // Ensure graceful handling of empty results from both store and Maps API. |
| 45 | + { |
| 46 | + "EmptyResponse", |
| 47 | + &pb.FindEntitiesRequest{Description: "UnresolvableQuery"}, |
| 48 | + &pb.FindEntitiesResponse{}, |
| 49 | + }, |
| 50 | + } { |
| 51 | + t.Run(tc.name, func(t *testing.T) { |
| 52 | + ctx := context.Background() |
| 53 | + s, err := newTestStore() |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("newTestStore() = %v", err) |
| 56 | + } |
| 57 | + mc := &maps.FakeMapsClient{} |
| 58 | + |
| 59 | + resp, err := FindEntities(ctx, tc.req, s, mc) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("FindEntities returned error: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + if diff := cmp.Diff(resp, tc.want, protocmp.Transform()); diff != "" { |
| 65 | + t.Errorf("FindEntities() got diff: %s", diff) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestFindEntitiesFromMapsApi(t *testing.T) { |
| 72 | + ctx := context.Background() |
| 73 | + s := newTestStoreWithBt(t) |
| 74 | + |
| 75 | + req := &pb.FindEntitiesRequest{Description: "Hawaii"} |
| 76 | + mc := &maps.FakeMapsClient{} |
| 77 | + resp, err := FindEntities(ctx, req, s, mc) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("FindEntities returned error: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + want := &pb.FindEntitiesResponse{Dcids: []string{"geoId/15"}} |
| 83 | + if diff := cmp.Diff(resp, want, protocmp.Transform()); diff != "" { |
| 84 | + t.Errorf("FindEntities() got diff: %s", diff) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Set up a RecogPlaceStore that has an entry for "California" |
| 89 | +func newTestStore() (*store.Store, error) { |
| 90 | + return &store.Store{ |
| 91 | + RecogPlaceStore: &files.RecogPlaceStore{ |
| 92 | + DcidToNames: map[string][]string{ |
| 93 | + "geoId/06": {"California"}, |
| 94 | + }, |
| 95 | + RecogPlaceMap: map[string]*pb.RecogPlaces{ |
| 96 | + "california": { |
| 97 | + Places: []*pb.RecogPlace{ |
| 98 | + { |
| 99 | + Dcid: "geoId/06", |
| 100 | + Names: []*pb.RecogPlace_Name{ |
| 101 | + {Parts: []string{"california"}}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, nil |
| 109 | +} |
| 110 | + |
| 111 | +// Set up an empty RecogPlaceStore and a Bigtable ReconIDMap with an entry |
| 112 | +// for "hawaii_place_id", which is the value returned by the fake Maps client for "Hawaii". |
| 113 | +func newTestStoreWithBt(t *testing.T) *store.Store { |
| 114 | + reconEntities := &pb.ReconEntities{ |
| 115 | + Entities: []*pb.ReconEntities_Entity{ |
| 116 | + { |
| 117 | + Ids: []*pb.ReconEntities_Entity_ID{ |
| 118 | + { |
| 119 | + Prop: "dcid", |
| 120 | + Val: "geoId/15", |
| 121 | + }, |
| 122 | + }, |
| 123 | + Types: []string{"State"}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + raw, err := proto.Marshal(reconEntities) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("proto.Marshal failed: %v", err) |
| 130 | + } |
| 131 | + tableValue, err := util.ZipAndEncode(raw) |
| 132 | + if err != nil { |
| 133 | + t.Errorf("util.ZipAndEncode failed: %v", err) |
| 134 | + } |
| 135 | + btData := map[string]string{ |
| 136 | + "d/5/placeId^hawaii_place_id^dcid": tableValue, |
| 137 | + } |
| 138 | + ctx := context.Background() |
| 139 | + tables, err := bigtable.SetupBigtable(ctx, btData) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("SetupBigtable got err: %v", err) |
| 142 | + } |
| 143 | + btGroup := bigtable.NewGroup( |
| 144 | + []*bigtable.Table{bigtable.NewTable("test-table", tables, false)}, "", nil) |
| 145 | + return &store.Store{ |
| 146 | + BtGroup: btGroup, |
| 147 | + RecogPlaceStore: &files.RecogPlaceStore{ |
| 148 | + DcidToNames: map[string][]string{}, |
| 149 | + RecogPlaceMap: map[string]*pb.RecogPlaces{}, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
0 commit comments