|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package graphar |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +func getRepoRoot() string { |
| 28 | + _, filename, _, _ := runtime.Caller(0) |
| 29 | + // current file: go/graphar/graphar_client_test.go |
| 30 | + return filepath.Join(filepath.Dir(filename), "..", "..") |
| 31 | +} |
| 32 | + |
| 33 | +func TestNewClient(t *testing.T) { |
| 34 | + t.Run("default", func(t *testing.T) { |
| 35 | + c := New() |
| 36 | + if c == nil { |
| 37 | + t.Fatalf("expected non-nil client") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("with options", func(t *testing.T) { |
| 42 | + called := false |
| 43 | + opt := func(c *client) { |
| 44 | + called = true |
| 45 | + } |
| 46 | + c := New(opt, nil) |
| 47 | + if c == nil { |
| 48 | + t.Fatalf("expected non-nil client") |
| 49 | + } |
| 50 | + if !called { |
| 51 | + t.Errorf("expected option to be called") |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func TestClientLoadGraphInfoErrors(t *testing.T) { |
| 57 | + c := New() |
| 58 | + _, err := c.LoadGraphInfo(context.Background(), "non-existent-path.yml") |
| 59 | + if err == nil { |
| 60 | + t.Errorf("expected error for non-existent path") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestClientLoadGraphInfo(t *testing.T) { |
| 65 | + root := getRepoRoot() |
| 66 | + graphPath := filepath.Join(root, "testing", "modern_graph", "modern_graph.graph.yml") |
| 67 | + |
| 68 | + c := New() |
| 69 | + g, err := c.LoadGraphInfo(context.Background(), graphPath) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("LoadGraphInfo failed: %v", err) |
| 72 | + } |
| 73 | + if g.Name != "modern_graph" { |
| 74 | + t.Fatalf("unexpected graph name: %s", g.Name) |
| 75 | + } |
| 76 | + if !g.HasVertex("person") { |
| 77 | + t.Fatalf("expected graph to have vertex person") |
| 78 | + } |
| 79 | + if !g.HasEdge("person", "knows", "person") { |
| 80 | + t.Fatalf("expected graph to have edge person-knows-person") |
| 81 | + } |
| 82 | +} |
0 commit comments