11package resource
22
33import (
4+ "context"
45 "testing"
56
7+ "github.com/hashicorp/terraform-plugin-framework/types"
8+ "github.com/posthog/terraform-provider/internal/httpclient"
69 "github.com/stretchr/testify/assert"
710 "github.com/stretchr/testify/require"
811)
@@ -192,11 +195,26 @@ func TestNormalizeQueryForState(t *testing.T) {
192195 },
193196 "falls back on invalid user JSON" : {
194197 apiQuery : map [string ]interface {}{
195- "kind" : "test" ,
198+ "kind" : "test" ,
199+ "version" : float64 (2 ),
196200 },
197201 userQueryJSON : `invalid json` ,
198202 expected : `{"kind":"test"}` ,
199203 },
204+ "strips server fields when user query is unavailable" : {
205+ apiQuery : map [string ]interface {}{
206+ "kind" : "DataVisualizationNode" ,
207+ "result" : []interface {}{1 , 2 , 3 },
208+ "hogql" : "SELECT 1" ,
209+ "is_cached" : true ,
210+ "source" : map [string ]interface {}{
211+ "kind" : "TrendsQuery" ,
212+ "version" : float64 (2 ),
213+ },
214+ },
215+ userQueryJSON : "" ,
216+ expected : `{"kind":"DataVisualizationNode","source":{"kind":"TrendsQuery"}}` ,
217+ },
200218 }
201219
202220 for name , tt := range tests {
@@ -207,3 +225,28 @@ func TestNormalizeQueryForState(t *testing.T) {
207225 })
208226 }
209227}
228+
229+ func TestInsightMapResponseToModel_StripsServerQueryFieldsWithoutUserConfig (t * testing.T ) {
230+ ops := InsightOps {}
231+ model := InsightResourceTFModel {
232+ QueryJSON : types .StringNull (),
233+ }
234+
235+ resp := httpclient.Insight {
236+ ID : 123 ,
237+ Query : map [string ]interface {}{
238+ "kind" : "DataVisualizationNode" ,
239+ "result" : []interface {}{1 , 2 , 3 },
240+ "hogql" : "SELECT 1" ,
241+ "is_cached" : true ,
242+ "source" : map [string ]interface {}{
243+ "kind" : "TrendsQuery" ,
244+ "version" : float64 (2 ),
245+ },
246+ },
247+ }
248+
249+ diags := ops .MapResponseToModel (context .Background (), resp , & model )
250+ require .False (t , diags .HasError (), diags .Errors ())
251+ assert .Equal (t , `{"kind":"DataVisualizationNode","source":{"kind":"TrendsQuery"}}` , model .QueryJSON .ValueString ())
252+ }
0 commit comments