|
| 1 | +package docs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cloudquery/plugin-sdk/plugins" |
| 11 | + "github.com/cloudquery/plugin-sdk/schema" |
| 12 | + "github.com/cloudquery/plugin-sdk/specs" |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | + "github.com/rs/zerolog" |
| 15 | +) |
| 16 | + |
| 17 | +type testExecutionClient struct { |
| 18 | + logger zerolog.Logger |
| 19 | +} |
| 20 | + |
| 21 | +var testTables = []*schema.Table{ |
| 22 | + { |
| 23 | + Name: "test_table", |
| 24 | + Description: "Description for test table", |
| 25 | + Columns: []schema.Column{ |
| 26 | + { |
| 27 | + Name: "int_col", |
| 28 | + Type: schema.TypeInt, |
| 29 | + Description: "Int column", |
| 30 | + }, |
| 31 | + }, |
| 32 | + Relations: []*schema.Table{ |
| 33 | + { |
| 34 | + Name: "relation_table", |
| 35 | + Description: "Description for relational table", |
| 36 | + Columns: []schema.Column{ |
| 37 | + { |
| 38 | + Name: "string_col", |
| 39 | + Type: schema.TypeString, |
| 40 | + Description: "String column", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +var expectFiles = []struct { |
| 49 | + Name string |
| 50 | + Content string |
| 51 | +}{ |
| 52 | + { |
| 53 | + Name: "test_table.md", |
| 54 | + Content: ` |
| 55 | +# Table: test_table |
| 56 | +Description for test table |
| 57 | +## Columns |
| 58 | +| Name | Type | Description | |
| 59 | +| ------------- | ------------- | ----- | |
| 60 | +|int_col|Int|Int column| |
| 61 | +|_cq_id|UUID|Internal CQ ID of the row| |
| 62 | +|_cq_fetch_time|Timestamp|Internal CQ row of when fetch was started (this will be the same for all rows in a single fetch)| |
| 63 | +`, |
| 64 | + }, |
| 65 | + { |
| 66 | + Name: "relation_table.md", |
| 67 | + Content: ` |
| 68 | +# Table: relation_table |
| 69 | +Description for relational table |
| 70 | +## Columns |
| 71 | +| Name | Type | Description | |
| 72 | +| ------------- | ------------- | ----- | |
| 73 | +|string_col|String|String column| |
| 74 | +|_cq_id|UUID|Internal CQ ID of the row| |
| 75 | +|_cq_fetch_time|Timestamp|Internal CQ row of when fetch was started (this will be the same for all rows in a single fetch)| |
| 76 | +`, |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +func (c *testExecutionClient) Logger() *zerolog.Logger { |
| 81 | + return &c.logger |
| 82 | +} |
| 83 | + |
| 84 | +func newTestExecutionClient(context.Context, zerolog.Logger, specs.Source) (schema.ClientMeta, error) { |
| 85 | + return &testExecutionClient{}, nil |
| 86 | +} |
| 87 | + |
| 88 | +func TestGenerateSourcePluginDocs(t *testing.T) { |
| 89 | + tmpdir, tmpErr := os.MkdirTemp("", "docs_test_*") |
| 90 | + if tmpErr != nil { |
| 91 | + t.Fatalf("failed to create temporary directory: %v", tmpErr) |
| 92 | + } |
| 93 | + defer os.RemoveAll(tmpdir) |
| 94 | + |
| 95 | + p := plugins.NewSourcePlugin("test", "v1.0.0", testTables, newTestExecutionClient) |
| 96 | + err := GenerateSourcePluginDocs(p, tmpdir) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("unexpected error calling GenerateSourcePluginDocs: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + for _, exp := range expectFiles { |
| 102 | + output := path.Join(tmpdir, exp.Name) |
| 103 | + got, err := ioutil.ReadFile(output) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("error reading %q: %v ", exp.Name, err) |
| 106 | + } |
| 107 | + |
| 108 | + if diff := cmp.Diff(string(got), exp.Content); diff != "" { |
| 109 | + t.Errorf("Generate docs for %q not as expected (+got, -want): %v", exp.Name, diff) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments