Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit 6f32201

Browse files
author
Mandeep Bahal
committed
test(codegen): add codegen util testing
jira: CORP-5480
1 parent 8e77443 commit 6f32201

File tree

7 files changed

+153
-47
lines changed

7 files changed

+153
-47
lines changed

.github/workflows/Linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Linux(ubuntu)
55

66
on:
77
push:
8-
branches: [ "main", "CORP-5480-update-dag-template" ]
8+
branches: [ "main" ]
99
pull_request:
1010
branches: [ "main" ]
1111

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
![Actions Status](https://github.com/DeloitteOptimalReality/airflow-wrapper-go/actions/workflows/Linux.yml/badge.svg?branch=CORP-5480-update-dag-template)
2+
[![Actions Status](https://github.com/DeloitteOptimalReality/airflow-wrapper-go/actions/workflows/Linux.yml/badge.svg?branch=main)](https://github.com/DeloitteOptimalReality/airflow-wrapper-go/actions)
33

44
# Introduction
55
This is a wrapper library of the airflow-client-go library. The library is written in Go and allows your to create clients with interface methods to interact with the Apache Airflow REST API.

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ require (
88
)
99

1010
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
1112
github.com/golang/protobuf v1.4.2 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/stretchr/testify v1.9.0 // indirect
1215
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99 // indirect
1316
google.golang.org/appengine v1.6.6 // indirect
1417
google.golang.org/protobuf v1.25.0 // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
1519
)

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
4242
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
4343
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
4444
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
46+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4547
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
4648
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
4749
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -105,11 +107,14 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
105107
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
106108
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
107109
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
110+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
109112
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
110113
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
111114
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
112115
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
116+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
117+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
113118
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
114119
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
115120
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -352,6 +357,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
352357
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
353358
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
354359
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
360+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
361+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
355362
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
356363
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
357364
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/codegen/codegen.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ type Args struct {
2222
}
2323

2424
// Function to convert an interface to a JSON string
25-
func toJSONString(v interface{}) string {
25+
func ToJSONString(v interface{}) (string, error) {
2626
jsonData, err := json.Marshal(v)
2727
if err != nil {
28-
return ""
28+
return "", err
2929
}
30-
return string(jsonData)
30+
return string(jsonData), nil
3131
}
3232

3333
type StartDate struct {
@@ -66,7 +66,7 @@ type GenData struct {
6666
Tasks []HttpOperator
6767
}
6868

69-
func checkDeps(deps []string) bool {
69+
func CheckDeps(deps []string) bool {
7070
return len(deps) > 0
7171
}
7272

@@ -79,17 +79,18 @@ func writeValueToBuffer(v interface{}, buf *bytes.Buffer) bool {
7979
buf.WriteString("False")
8080
}
8181
case string:
82-
buf.WriteString("'")
82+
buf.WriteString("\"")
8383
buf.WriteString(v)
84-
buf.WriteString("'")
84+
buf.WriteString("\"")
8585
default:
8686
buf.WriteString(fmt.Sprintf("%v", v))
8787
}
8888
return true
8989
}
9090

91-
func toMap(v interface{}) (map[string]interface{}, error) {
91+
func ToMap(v interface{}) (map[string]interface{}, error) {
9292
var res map[string]interface{}
93+
fmt.Println("MARSHAlling")
9394
a, err := json.Marshal(v)
9495
if err != nil {
9596
return res, err
@@ -99,7 +100,7 @@ func toMap(v interface{}) (map[string]interface{}, error) {
99100
}
100101

101102
// Function to convert a Go map to a Python dictionary string
102-
func mapToPythonDict(m map[string]interface{}) (string, error) {
103+
func MapToPythonDict(m map[string]interface{}) (string, error) {
103104
var buf bytes.Buffer
104105
buf.WriteString("{")
105106
first := true
@@ -108,9 +109,9 @@ func mapToPythonDict(m map[string]interface{}) (string, error) {
108109
buf.WriteString(", ")
109110
}
110111
first = false
111-
buf.WriteString("'")
112+
buf.WriteString("\"")
112113
buf.WriteString(k)
113-
buf.WriteString("': ")
114+
buf.WriteString("\": ")
114115
switch v := v.(type) {
115116
case []interface{}:
116117
buf.WriteString("[")
@@ -119,7 +120,7 @@ func mapToPythonDict(m map[string]interface{}) (string, error) {
119120
}
120121
buf.WriteString("]")
121122
case map[string]interface{}:
122-
nested, err := mapToPythonDict(v)
123+
nested, err := MapToPythonDict(v)
123124
if err != nil {
124125
return "", err
125126
}
@@ -140,7 +141,7 @@ func BoolTitle(b bool) string {
140141
}
141142

142143
// Function to transform the task ID
143-
func transformTaskID(taskID string) string {
144+
func TransformTaskID(taskID string) string {
144145
return "wt_" + strings.ReplaceAll(taskID, "-", "_")
145146
}
146147

@@ -153,17 +154,17 @@ func CreateDagGen(g GenData, directory string) (string, error) {
153154
// Prepare a map of original task IDs to transformed task IDs
154155
taskIDMap := make(map[string]string)
155156
for _, task := range data.Tasks {
156-
taskIDMap[task.TaskID] = transformTaskID(task.TaskID)
157+
taskIDMap[task.TaskID] = TransformTaskID(task.TaskID)
157158
}
158159

159160
t := template.New("dag").Funcs(
160161
template.FuncMap{
161-
"toJSONString": toJSONString,
162-
"toMap": toMap,
162+
"toJSONString": ToJSONString,
163+
"toMap": ToMap,
163164
"BoolTitle": BoolTitle,
164-
"mapToPythonDict": mapToPythonDict,
165-
"checkDeps": checkDeps,
166-
"transformTaskID": transformTaskID,
165+
"mapToPythonDict": MapToPythonDict,
166+
"checkDeps": CheckDeps,
167+
"transformTaskID": TransformTaskID,
167168
"originalTaskIDMap": func() map[string]string { return taskIDMap },
168169
})
169170
tp, err := t.Parse(tmpl)

pkg/codegen/codegen_test.go

-27
This file was deleted.

test/codegen/codegen_test.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package codegen
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/DeloitteOptimalReality/airflow-wrapper-go/pkg/codegen"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCreateDagObject(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
want string
15+
}{
16+
{"test1", ""},
17+
}
18+
for _, tt := range tests {
19+
t.Run(tt.name, func(t *testing.T) {
20+
data := codegen.GenData{DagDef: codegen.Dag{ID: ""}}
21+
if got, err := codegen.CreateDagGen(data, ""); false {
22+
if err != nil {
23+
t.Error()
24+
}
25+
fmt.Println(got)
26+
t.Errorf("CreateDagObject() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}
31+
32+
func TestToMapValid(t *testing.T) {
33+
testStruct := codegen.HttpOperator{
34+
TaskID: "test_task_id",
35+
ConnectionId: "test_connection_id",
36+
Name: "test_name",
37+
Endpoint: "test_endpoint",
38+
Data: "test_data",
39+
Downstream: []string{"downstream_task_1", "downstream_task_2"},
40+
}
41+
res, err := codegen.ToMap(testStruct)
42+
if err != nil {
43+
t.Error(err)
44+
}
45+
assert.Equal(t, res["Name"], "test_name")
46+
assert.Equal(t, res["ConnectionId"], "test_connection_id")
47+
assert.Equal(t, res["Name"], "test_name")
48+
assert.Equal(t, res["Endpoint"], "test_endpoint")
49+
assert.Equal(t, res["Data"], "test_data")
50+
assert.ElementsMatch(t, res["Downstream"], []string{"downstream_task_1", "downstream_task_2"})
51+
}
52+
53+
// TODO: Test the invalid case for ToMap function. I'm not sure how to hit the edge case yet.
54+
55+
func TestMapToPythonDict(t *testing.T) {
56+
expectedPythonDict := `
57+
{
58+
"key1": {
59+
"ID": "dag_id",
60+
"Description": "dag_description",
61+
"StartDate": {
62+
"Day": 1,
63+
"Month": 1,
64+
"Year": 2024
65+
},
66+
"Tags": [],
67+
"DefaultArgs": {
68+
"Email": "test_email",
69+
"Retries": 1,
70+
"RetryDelay": 999
71+
}
72+
}
73+
}
74+
`
75+
76+
testMap := map[string]interface{}{
77+
"key1": map[string]interface{}{
78+
"ID": "dag_id",
79+
"Description": "dag_description",
80+
"StartDate": map[string]interface{}{
81+
"Day": 1,
82+
"Month": 1,
83+
"Year": 2024,
84+
},
85+
"Tags": []string{},
86+
"DefaultArgs": map[string]interface{}{
87+
"Email": "test_email",
88+
"Retries": 1,
89+
"RetryDelay": 999,
90+
},
91+
},
92+
}
93+
res, err := codegen.MapToPythonDict(testMap)
94+
if err != nil {
95+
t.Error(err)
96+
}
97+
assert.JSONEq(t, res, expectedPythonDict)
98+
}
99+
100+
func TestToJSONString(t *testing.T) {
101+
expectedJSONString := `
102+
{
103+
"key1": "value1"
104+
}
105+
`
106+
107+
testMap := map[string]interface{}{
108+
"key1": "value1",
109+
}
110+
res, err := codegen.ToJSONString(testMap)
111+
if err != nil {
112+
t.Error(err)
113+
}
114+
assert.JSONEq(t, res, expectedJSONString)
115+
}
116+
117+
func TestCheckDeps(t *testing.T) {
118+
assert.True(t, codegen.CheckDeps([]string{"dep_1"}))
119+
assert.True(t, codegen.CheckDeps([]string{"dep_1", "dep_2"}))
120+
assert.False(t, codegen.CheckDeps([]string{}))
121+
}

0 commit comments

Comments
 (0)