Skip to content

Commit f72088b

Browse files
committed
set the logic of update and create instances with parameters
1 parent d9d8c8b commit f72088b

3 files changed

Lines changed: 115 additions & 39 deletions

File tree

src/practitest_firecracker/api.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@
103103
:attributes attributes
104104
:instances {:test-ids test-ids}}}}))))
105105

106-
(defn make-instances [testset-tests testid-params]
106+
(defn make-instances [testset-tests testname-to-params test-id-testname]
107107
(for [[testset-id test-ids-num] testset-tests
108108
test-id-num test-ids-num
109109
index (range (last test-id-num))]
110-
{:type "instances"
111-
:attributes {:set-id testset-id
112-
:test-id (first test-id-num)
113-
:parameters (get testid-params (first test-id-num))}}))
110+
{:type "instances"
111+
:attributes {:set-id testset-id
112+
:test-id (first test-id-num)
113+
:parameters (get (get testname-to-params (get test-id-testname (first test-id-num))) index)}}))
114114

115115
(defn has-duplicates? [key runs]
116116
(let [grouped (group-by key (into [] runs))]

src/practitest_firecracker/practitest.clj

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
(ns practitest-firecracker.practitest
22
(:require
3+
[clojure.set :refer [difference]]
34
[clojure.string :as string]
4-
[clojure.pprint :as pprint]
55
[clojure.tools.logging :as log]
66
[practitest-firecracker.utils :refer [print-run-time test-need-update? pformat]]
77
[practitest-firecracker.api :as api]
88
[practitest-firecracker.const :refer [max-test-ids-bucket-size]]
9-
[practitest-firecracker.query-dsl :refer [query? eval-query]]
10-
[practitest-firecracker.query-dsl :refer [read-query]]
9+
[practitest-firecracker.query-dsl :as query-dsl]
1110
[practitest-firecracker.eval :as eval]))
1211

1312
(defn find-sf-testset [client [project-id display-action-logs] options testset-name]
@@ -47,10 +46,10 @@
4746
{k (set (map
4847
(fn [test] (eval/sf-test-suite->pt-test-name options test)) v))})
4948
(into {} testsets))
50-
tests (flatten (map val (into {} testsets)))
51-
all-tests (doall (into {} (for [test tests]
49+
xml-tests (flatten (map val (into {} testsets)))
50+
all-tests (doall (into {} (for [test xml-tests]
5251
{(eval/sf-test-suite->pt-test-name options test) test})))]
53-
[all-tests testset-id-to-name ts-id-test-name-num-instances]))
52+
[all-tests xml-tests testset-id-to-name ts-id-test-name-num-instances]))
5453

5554
(defn value-get [existing-cases xml-cases]
5655
(conj (into [] existing-cases) (into [] xml-cases)))
@@ -73,7 +72,7 @@
7372
{:pt-test-step-name (:name attributes)
7473
:description (:description attributes)})
7574

76-
(defn create-or-update-tests [[all-tests testset-id-to-name ts-id-test-name-num-instances] client {:keys [project-id display-action-logs display-run-time use-test-step] :as options} start-time]
75+
(defn create-or-update-tests [[all-tests org-xml-tests testset-id-to-name ts-id-test-name-num-instances] client {:keys [project-id display-action-logs display-run-time use-test-step] :as options} start-time]
7776
(let [new-tests (into [] (eval/group-test-names (vals all-tests) options))
7877
results (doall
7978
(flatten
@@ -125,53 +124,81 @@
125124
(eval/update-sf-test client options test-suite test)))
126125
tests-with-steps))
127126
(when display-run-time (print-run-time "Time - after update tests: %d:%d:%d" start-time)))
128-
[new-all-tests testset-id-to-name ts-id-test-name-num-instances]))
127+
[new-all-tests org-xml-tests testset-id-to-name ts-id-test-name-num-instances]))
129128

130129
(defn split-n-filter-instance-params [test pt-instance-params]
131130
(into []
132131
(filter not-empty
133132
(string/split
134-
(eval-query
135-
(second test)
136-
(read-query pt-instance-params))
133+
(query-dsl/eval-query
134+
test
135+
(query-dsl/read-query pt-instance-params))
137136
#"\|"))))
138137

139-
(defn create-instances [[all-tests testset-id-to-name ts-id-test-name-num-instances] client {:keys [project-id display-action-logs display-run-time pt-instance-params] :as options} start-time]
138+
(defn create-instances [[all-tests org-xml-tests testset-id-to-name ts-id-test-name-num-instances] client {:keys [project-id display-action-logs display-run-time pt-instance-params] :as options} start-time]
140139
(when display-action-logs (log/infof "pt-instance-params: %s" pt-instance-params))
141140
(let [all-test-ids (map (fn [test] (:id (last test))) all-tests)
142141
testname-test (into {} (map (fn [test] {(first test) test}) all-tests))
142+
143+
test-id-testname (into {} (map (fn [test] {(query-dsl/parse-int (:id (last test))) (first test)}) all-tests))
143144
testid-params (when (not-empty pt-instance-params)
144145
(into {}
145146
(map
146147
(fn [test]
147-
(let [split-params (split-n-filter-instance-params test pt-instance-params)]
148+
(let [split-params (split-n-filter-instance-params (second test) pt-instance-params)]
148149
{(Integer/parseInt (:id (last test)))
149-
(when (even? (count split-params))
150-
(apply array-map
151-
(split-n-filter-instance-params test pt-instance-params)))}))
150+
(zipmap (iterate inc 1) split-params)}))
152151
all-tests)))
152+
153153
log (when display-action-logs (log/infof "testid-params: %s" testid-params))
154+
154155
testset-ids (map (fn [testset] (first (first testset))) testset-id-to-name)
156+
155157
ts-ids (string/join "," testset-ids)
158+
156159
instances (mapcat (fn [test-ids-bucket]
157160
(api/ll-testset-instances client
158161
[project-id display-action-logs]
159162
ts-ids
160163
(string/join "," test-ids-bucket)))
161164
(partition-all max-test-ids-bucket-size all-test-ids))
165+
166+
testname-to-params (when (not-empty pt-instance-params)
167+
(into {}
168+
(map
169+
(fn [[key tests]]
170+
{key
171+
(into []
172+
(map
173+
(fn [test]
174+
(zipmap
175+
(iterate inc 1)
176+
(split-n-filter-instance-params test pt-instance-params))) tests))})
177+
(group-by
178+
#(eval/sf-test-suite->pt-test-name options %)
179+
org-xml-tests))))
180+
181+
testname-test2 (when
182+
(not-empty pt-instance-params)
183+
(into
184+
#{}
185+
(map
186+
(fn [test]
187+
(str
188+
(eval/sf-test-suite->pt-test-name options test) ":"
189+
(split-n-filter-instance-params test pt-instance-params)))
190+
org-xml-tests)))
191+
162192
filter-instances (if (not-empty pt-instance-params)
163193
(filter
164194
(fn [instance]
165195
(let
166196
[{:keys [name bdd-parameters parameters]} (:attributes instance)
167-
[_ xml-test test] (get testname-test name)
168-
split-params (split-n-filter-instance-params test pt-instance-params)
169-
xml-params (into {} (map (fn [[key value]] {(keyword key) value})
170-
(when (even? (count split-params))
171-
(apply array-map split-params))))
172-
test-type (:test-type (:attributes test))
173-
params (if (= test-type "BDDTest") bdd-parameters parameters)]
174-
(= xml-params params)))
197+
obj-test (get testname-test name)
198+
test-type (:test-type (:attributes (last obj-test)))
199+
params (if (= test-type "BDDTest") bdd-parameters parameters)
200+
vals (into [] (if (map? params) (vals params) params))]
201+
(contains? testname-test2 (str name ":" vals))))
175202
instances)
176203
instances)
177204
log (when display-action-logs (do (log/infof "filter-instances: ") (pprint/pprint filter-instances)))
@@ -186,17 +213,66 @@
186213
(last (last testset-id-name))))})
187214
testset-id-to-name))
188215
ts-id-instances (group-by (fn [inst] (get-in inst [:attributes :set-id])) filter-instances)
216+
189217
missing-instances (into {}
190218
(doall
191219
(for [ts-id (into () testset-ids)]
192-
{ts-id (merge-with -
193-
(get ts-id-instance-num ts-id)
194-
(frequencies (vec (map #(get-in % [:attributes :test-id]) (get ts-id-instances (read-string ts-id))))))})))
195-
make-instances (flatten (api/make-instances missing-instances testid-params))
220+
{ts-id
221+
(merge-with -
222+
(get ts-id-instance-num ts-id)
223+
(frequencies (vec (map #(get-in % [:attributes :test-id])
224+
(get ts-id-instances (read-string ts-id))))))})))
225+
226+
existing-instance (when (not-empty pt-instance-params)
227+
(into {}
228+
(map
229+
(fn [[key tests]]
230+
{key
231+
(into []
232+
(map
233+
(fn [test]
234+
(get-in test [:attributes :parameters])) tests))})
235+
(group-by
236+
(fn [inst]
237+
(get-in inst [:attributes :name]))
238+
filter-instances))))
239+
240+
new-map (into {}
241+
(map
242+
(fn [test-name]
243+
(reduce (fn [a b]
244+
(if (= (keys a) (keys b))
245+
{(first (keys a))
246+
(into [] (conj
247+
(if (vector? (first (vals a)))
248+
(first (vals a))
249+
(into [] (vals a)))
250+
(first (vals b))))}
251+
(conj a b)))
252+
{}
253+
(map (fn [x]
254+
{test-name
255+
(into {}
256+
(map (fn [y]
257+
{(keyword (str (first y)))
258+
(last y)})
259+
x))})
260+
(get testname-to-params test-name))))
261+
(keys testname-to-params)))
262+
263+
new-testname-to-params (into {}
264+
(map
265+
(fn [test-name]
266+
{test-name
267+
[(into {} (difference (set (get new-map test-name)) (set (get existing-instance test-name))))]})
268+
(keys new-map)))
269+
270+
make-instances (flatten (api/make-instances missing-instances new-testname-to-params test-id-testname))
196271
test-by-id (group-by (fn [test] (read-string (:id (last test)))) all-tests)
197272
new-intstances (flatten (for [instances-part (partition-all 100 (shuffle make-instances))]
198273
(api/ll-create-instances client [project-id display-action-logs] instances-part)))
199-
all-intstances (into [] (concat new-intstances instances))
274+
all-intstances (into [] (concat new-intstances filter-instances))
275+
200276
instance-to-ts-test (group-by (fn [inst] [(:set-id (:attributes inst)) (:test-id (:attributes inst))]) all-intstances)]
201277
(when display-run-time (print-run-time "Time - after create instances: %d:%d:%d" start-time))
202278
[test-by-id instance-to-ts-test]))

src/practitest_firecracker/query_dsl.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@
7171
(return-error "Syntax error: 'join' argument has to be ISeqable (String, Array etc.)" query))
7272
(return-error "Syntax error: 'join' must have one argument" query))
7373
'split (if (= 2 (count args))
74-
(let [quoted #?(:clj (string/escape (first args) char-escape-string)
75-
:cljs (first args))
76-
complied #?(:clj (java.util.regex.Pattern/compile quoted)
77-
:cljs (js/RegExp. quoted))]
78-
(string/split (second args) complied))
74+
(if (empty? (last args))
75+
(last args)
76+
(let [complied #?(:clj (java.util.regex.Pattern/compile (first args))
77+
:cljs (js/RegExp. (first args)))]
78+
(string/split (second args) complied)))
7979
(return-error "Syntax error: 'split' must have two arguments" query))
8080
'get (if (= 2 (count args))
8181
(first (take 1 (drop (- (parse-int (first args)) 1) (last args))))

0 commit comments

Comments
 (0)