package cache import ( "context" "errors" "testing" "chis_osi/osi" ) func TestDictionarySnapshotLooksUpMasterDataByName(t *testing.T) { snapshot := NewDictionarySnapshot( []osi.GridAddress{{RegionCode: "441625000000", RegionName: "测试网格"}}, []osi.Doctor{{PersonID: "doc-001", PersonName: "测试医生"}}, []osi.Org{{OrganizCode: "123456789", OrganizName: "测试机构"}}, ) if got, ok := snapshot.RegionCodeByName("测试网格"); !ok || got != "441625000000" { t.Fatalf("RegionCodeByName = %q, %v", got, ok) } if got, ok := snapshot.DoctorIDByName("测试医生"); !ok || got != "doc-001" { t.Fatalf("DoctorIDByName = %q, %v", got, ok) } if got, ok := snapshot.ManaUnitIDByName("测试机构"); !ok || got != "123456789" { t.Fatalf("ManaUnitIDByName = %q, %v", got, ok) } } func TestDictionaryServiceRefreshKeepsMemoryWhenPersistentStoreFails(t *testing.T) { client := fakePublicClient{ grids: []osi.GridAddress{{RegionCode: "441625000000", RegionName: "测试网格"}}, doctors: []osi.Doctor{{PersonID: "doc-001", PersonName: "测试医生"}}, orgs: []osi.Org{{OrganizCode: "123456789", OrganizName: "测试机构"}}, } service := NewDictionaryService(client, failingStore{}) snapshot, err := service.Refresh(context.Background(), RefreshParams{ Grid: osi.GridAddressQuery{ParentCode: "441625", PageNo: 1, OperateUser: "712041"}, Doctors: osi.DoctorQuery{ManaUnitID: "123456789", OperateUser: "712041"}, Orgs: osi.OrgQuery{OrganizCode: "123456789"}, }) if err != nil { t.Fatalf("Refresh: %v", err) } if got, ok := snapshot.DoctorIDByName("测试医生"); !ok || got != "doc-001" { t.Fatalf("snapshot doctor = %q, %v", got, ok) } cached := service.Snapshot() if got, ok := cached.RegionCodeByName("测试网格"); !ok || got != "441625000000" { t.Fatalf("memory snapshot region = %q, %v", got, ok) } } type fakePublicClient struct { grids []osi.GridAddress doctors []osi.Doctor orgs []osi.Org } func (c fakePublicClient) QueryGridAddress(context.Context, osi.GridAddressQuery) ([]osi.GridAddress, osi.Result, error) { return c.grids, osi.Result{Code: "01", Success: true}, nil } func (c fakePublicClient) QueryDoctors(context.Context, osi.DoctorQuery) ([]osi.Doctor, osi.Result, error) { return c.doctors, osi.Result{Code: "01", Success: true}, nil } func (c fakePublicClient) QueryOrgs(context.Context, osi.OrgQuery) ([]osi.Org, osi.Result, error) { return c.orgs, osi.Result{Code: "01", Success: true}, nil } type failingStore struct{} func (failingStore) SaveDictionarySnapshot(context.Context, DictionarySnapshot) error { return errors.New("redis unavailable") }