From 398303235a49e7ff5316dce461cc3b9bfa9c77d7 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 5 Feb 2015 01:34:02 +0300 Subject: [PATCH] Custom JSON marshalling for PackageDiff, updated test for snapshot diff API. #168 --- deb/reflist.go | 23 +++++++++++++++++++++++ system/t12_api/snapshots.py | 20 ++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/deb/reflist.go b/deb/reflist.go index db5721db..30845e47 100644 --- a/deb/reflist.go +++ b/deb/reflist.go @@ -2,6 +2,8 @@ package deb import ( "bytes" + "encoding/json" + "github.com/AlekSi/pointer" "github.com/ugorji/go/codec" "sort" ) @@ -154,6 +156,27 @@ type PackageDiff struct { Left, Right *Package } +// Check interface +var ( + _ json.Marshaler = PackageDiff{} +) + +// MarshalJSON implements json.Marshaler interface +func (d PackageDiff) MarshalJSON() ([]byte, error) { + serialized := struct { + Left, Right *string + }{} + + if d.Left != nil { + serialized.Left = pointer.ToString(string(d.Left.Key(""))) + } + if d.Right != nil { + serialized.Right = pointer.ToString(string(d.Right.Key(""))) + } + + return json.Marshal(serialized) +} + // PackageDiffs is a list of PackageDiff records type PackageDiffs []PackageDiff diff --git a/system/t12_api/snapshots.py b/system/t12_api/snapshots.py index f1952371..79762924 100644 --- a/system/t12_api/snapshots.py +++ b/system/t12_api/snapshots.py @@ -158,7 +158,7 @@ class SnapshotsAPITestSearch(APITest): class SnapshotsAPITestDiff(APITest): """ - POST /api/snapshot/:name/diff/:name2 + GET /api/snapshot/:name/diff/:name2 """ def check(self): repos = [self.random_name() for x in xrange(2)] @@ -180,7 +180,19 @@ class SnapshotsAPITestDiff(APITest): self.check_equal(resp.status_code, 201) resp = self.get("/api/snapshots/" + snapshots[0] + "/diff/" + snapshots[1]) - self.check_equal(resp.status_code, 200) - self.check_subset({"Right": None}, resp.json()[0]) - self.check_subset({"Name": "libboost-program-options-dev"}, resp.json()[0]["Left"]) + self.check_equal(resp.json(), [{'Left': 'Pi386 libboost-program-options-dev 1.49.0.1 918d2f433384e378', + 'Right': None}]) + + resp = self.get("/api/snapshots/" + snapshots[1] + "/diff/" + snapshots[0]) + self.check_equal(resp.status_code, 200) + self.check_equal(resp.json(), [{'Right': 'Pi386 libboost-program-options-dev 1.49.0.1 918d2f433384e378', + 'Left': None}]) + + resp = self.get("/api/snapshots/" + snapshots[0] + "/diff/" + snapshots[0]) + self.check_equal(resp.status_code, 200) + self.check_equal(resp.json(), []) + + resp = self.get("/api/snapshots/" + snapshots[1] + "/diff/" + snapshots[1]) + self.check_equal(resp.status_code, 200) + self.check_equal(resp.json(), [])