Refactor code for improved readability and maintainability; add utility functions for score calculation and archive processing
This commit is contained in:
+26
-15
@@ -14,6 +14,11 @@ import (
|
||||
onviftesting "github.com/0x524a/onvif-go/testing"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTokenLength = 20
|
||||
percentScale = 100
|
||||
)
|
||||
|
||||
var (
|
||||
captureArchive = flag.String("capture", "", "Path to XML capture archive (.tar.gz)")
|
||||
outputDir = flag.String("output", "./", "Output directory for generated test file")
|
||||
@@ -128,7 +133,7 @@ type GeneratedTest struct {
|
||||
Code string
|
||||
}
|
||||
|
||||
// operationInfo holds info about captured operations
|
||||
// operationInfo holds info about captured operations.
|
||||
type operationInfo struct {
|
||||
OperationName string
|
||||
ServiceType onviftesting.ServiceType
|
||||
@@ -199,7 +204,8 @@ func generateTests() string {
|
||||
metadata.CameraInfo.FirmwareVersion)
|
||||
} else {
|
||||
// Try to extract from GetDeviceInformation response
|
||||
for _, ex := range capture.Exchanges {
|
||||
for i := range capture.Exchanges {
|
||||
ex := &capture.Exchanges[i]
|
||||
if ex.OperationName == "GetDeviceInformation" && ex.Success {
|
||||
manufacturer := extractXMLValue(ex.ResponseBody, "Manufacturer")
|
||||
model := extractXMLValue(ex.ResponseBody, "Model")
|
||||
@@ -207,6 +213,7 @@ func generateTests() string {
|
||||
if manufacturer != "" && model != "" {
|
||||
cameraDesc = fmt.Sprintf("%s %s (Firmware: %s)", manufacturer, model, firmware)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -241,12 +248,9 @@ func generateTests() string {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create output file: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
defer f.Close()
|
||||
|
||||
if err := tmpl.Execute(f, testData); err != nil {
|
||||
_ = f.Close()
|
||||
log.Fatalf("Failed to execute template: %v", err)
|
||||
}
|
||||
|
||||
@@ -264,10 +268,11 @@ func generateTests() string {
|
||||
}
|
||||
|
||||
func analyzeOperations(capture *onviftesting.CameraCaptureV2) []operationInfo {
|
||||
var ops []operationInfo
|
||||
ops := make([]operationInfo, 0, len(capture.Exchanges))
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, ex := range capture.Exchanges {
|
||||
for i := range capture.Exchanges {
|
||||
ex := &capture.Exchanges[i]
|
||||
// Create unique key for deduplication
|
||||
key := ex.OperationName
|
||||
if token := ex.GetProfileToken(); token != "" {
|
||||
@@ -297,10 +302,13 @@ func analyzeOperations(capture *onviftesting.CameraCaptureV2) []operationInfo {
|
||||
func hasNonDeviceOperations(ops []operationInfo) bool {
|
||||
for _, op := range ops {
|
||||
switch op.ServiceType {
|
||||
case onviftesting.ServiceMedia, onviftesting.ServicePTZ, onviftesting.ServiceImaging:
|
||||
case onviftesting.ServiceMedia, onviftesting.ServicePTZ, onviftesting.ServiceImaging, onviftesting.ServiceEvent, onviftesting.ServiceDeviceIO:
|
||||
return true
|
||||
case onviftesting.ServiceDevice, onviftesting.ServiceUnknown:
|
||||
// continue checking
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -574,6 +582,7 @@ func generatePTZTests(ops []operationInfo) []GeneratedTest {
|
||||
Code: code,
|
||||
})
|
||||
delete(ptzOps, op.OperationName)
|
||||
|
||||
continue
|
||||
}
|
||||
if token, ok := op.Parameters["ProfileToken"].(string); ok && token != "" {
|
||||
@@ -699,9 +708,10 @@ func sanitizeToken(token string) string {
|
||||
token = strings.ReplaceAll(token, ".", "_")
|
||||
token = strings.ReplaceAll(token, " ", "_")
|
||||
// Truncate if too long
|
||||
if len(token) > 20 {
|
||||
token = token[:20]
|
||||
if len(token) > maxTokenLength {
|
||||
token = token[:maxTokenLength]
|
||||
}
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
@@ -774,7 +784,7 @@ func updateCameraRegistry(regPath, archivePath, testFile string) {
|
||||
}
|
||||
|
||||
// Add or update the camera entry
|
||||
registry.AddCamera(*entry)
|
||||
registry.AddCamera(entry)
|
||||
|
||||
// Update coverage statistics
|
||||
updateRegistryCoverage(registry, archivePath)
|
||||
@@ -874,7 +884,7 @@ func generateCoverageMarkdown(registry *onviftesting.Registry) string {
|
||||
total, captured := registry.GetTotalCoverage()
|
||||
if total > 0 {
|
||||
sb.WriteString(fmt.Sprintf("- **Overall Coverage**: %.1f%% (%d/%d operations)\n\n",
|
||||
float64(captured)/float64(total)*100, captured, total))
|
||||
float64(captured)/float64(total)*percentScale, captured, total))
|
||||
}
|
||||
|
||||
// Cameras
|
||||
@@ -883,7 +893,8 @@ func generateCoverageMarkdown(registry *onviftesting.Registry) string {
|
||||
sb.WriteString("| Manufacturer | Model | Firmware | Operations | Capabilities |\n")
|
||||
sb.WriteString("|--------------|-------|----------|------------|---------------|\n")
|
||||
|
||||
for _, cam := range registry.Cameras {
|
||||
for i := range registry.Cameras {
|
||||
cam := ®istry.Cameras[i]
|
||||
caps := strings.Join(cam.Capabilities, ", ")
|
||||
sb.WriteString(fmt.Sprintf("| %s | %s | %s | %d | %s |\n",
|
||||
cam.Manufacturer, cam.Model, cam.Firmware, cam.OperationsCaptured, caps))
|
||||
@@ -902,7 +913,7 @@ func generateCoverageMarkdown(registry *onviftesting.Registry) string {
|
||||
if cov, ok := registry.Coverage[service]; ok {
|
||||
pct := 0.0
|
||||
if cov.Total > 0 {
|
||||
pct = float64(cov.Captured) / float64(cov.Total) * 100
|
||||
pct = float64(cov.Captured) / float64(cov.Total) * percentScale
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("| %s | %d | %d | %.1f%% |\n",
|
||||
service, cov.Total, cov.Captured, pct))
|
||||
|
||||
Reference in New Issue
Block a user