def test_deserialise_tsv(): tsv = """foo x y first 1 1 second 2 1 third 3 2 """ sch = Schema( "foo", [schema.string("foo"), schema.integer("x"), schema.integer("y")]) expected = [ Blob({ "foo": "first", "x": "1", "y": "1" }), Blob({ "foo": "second", "x": "2", "y": "1" }), Blob({ "foo": "third", "x": "3", "y": "2" }), ] actual = xsv.deserialise(StringIO(tsv), sch) assert actual == expected
def test_digest(): """Should generate a digest following the canonical V1 rules.""" blob = Blob({"register-name": "Country"}) expected = Hash( "sha-256", "9f21f032105bb320d1f0c4f9c74a84a69e2d0a41932eb4543c331ce73e0bb1fb") assert blob.digest() == expected
def test_utf8_digest(): blob = Blob({ "citizen-names": "Citizen of the Ivory Coast", "country": "CI", "name": "Ivory Coast", "official-name": "The Republic of Côte D’Ivoire" }) expected = Hash( "sha-256", "b3ca21b3b3a795ab9cd1d10f3d447947328406984f8a461b43d9b74b58cccfe8") assert blob.digest() == expected
def test_record_json(): entry = Entry( "GB", Scope.User, "2016-04-05T13:23:05Z", Hash( "sha-256", "6b18693874513ba13da54d61aafa7cad0c8f5573f3431d6f1c04b07ddb27d6bb" ), # NOQA 6) blob = Blob({ "country": "GB", "official-name": "The United Kingdom of Great Britain and Northern Ireland", # NOQA "name": "United Kingdom", "citizen-names": "Briton;British citizen" }) record = Record(entry, blob) expected = ('{"GB":{"index-entry-number":"6","entry-number":"6",' '"entry-timestamp":"2016-04-05T13:23:05Z","key":"GB",' '"item":[{"citizen-names":"Briton;British citizen",' '"country":"GB","name":"United Kingdom",' '"official-name":"The United Kingdom of Great Britain ' 'and Northern Ireland"}]}}') stream = StringIO() utils.serialise_json(record, stream, compact=True) stream.seek(0) actual = stream.read() assert actual == expected
def test_parse_add_item_command(): expected = Blob({"register-name": "Country"}) command = 'add-item {"register-name":"Country"}' actual = parse_command(command) assert actual.action == Action.AddItem assert actual.value == expected
def test_record(country_register): entry = Entry( "GB", Scope.User, "2016-04-05T13:23:05Z", Hash( "sha-256", "6b18693874513ba13da54d61aafa7cad0c8f5573f3431d6f1c04b07ddb27d6bb" ), # NOQA 6) blob = Blob({ "citizen-names": "Briton;British citizen", "country": "GB", "name": "United Kingdom", "official-name": "The United Kingdom of Great Britain and Northern Ireland" # NOQA }) expected = Record(entry, blob) actual = country_register.record("GB") assert actual == expected
def test_patch_country(country_register): blobs = [ Blob({ "country": "AX", "name": "Atlantis", "end-date": "1000-01-01", }) ] timestamp = "2019-03-21T07:21:00Z" patch = Patch(country_register.schema(), blobs, timestamp) country_register.apply(patch) records = len(country_register.records()) assert records == 200 assert country_register.stats() == { "data": { "total-entries": 210, "total-blobs": 210 }, "metadata": { "total-entries": 18, "total-blobs": 16 } }
def test_patch(): sch = Schema( "foo", [schema.string("foo"), schema.integer("x"), schema.integer("y")]) blobs = [ Blob({ "foo": "first", "x": "1", "y": "1" }), Blob({ "foo": "second", "x": "2", "y": "1" }), Blob({ "foo": "third", "x": "3", "y": "2" }), ] timestamp = "2019-01-01T10:11:12Z" patch = Patch(sch, blobs, timestamp) expected = [ rsf.Command(rsf.Action.AddItem, Blob({ "foo": "first", "x": "1", "y": "1" })), rsf.Command( rsf.Action.AppendEntry, Entry( "first", Scope.User, timestamp, Hash( "sha-256", "d8c619d437b4bc234ee39967b1578f4124e0cb672a214ef437bb3b5a3dde5d6a" ))), # NOQA rsf.Command(rsf.Action.AddItem, Blob({ "foo": "second", "x": "2", "y": "1" })), rsf.Command( rsf.Action.AppendEntry, Entry( "second", Scope.User, timestamp, Hash( "sha-256", "5f99ce37f11cccc259fdf119137953363fb82f90af4520c6d66dc8ca4207d7ea" ))), # NOQA rsf.Command(rsf.Action.AddItem, Blob({ "foo": "third", "x": "3", "y": "2" })), rsf.Command( rsf.Action.AppendEntry, Entry( "third", Scope.User, timestamp, Hash( "sha-256", "020d6a2e8888ad3d41b642ac511c4cc6411a299f3720c7dad8796d18853300aa" ))) # NOQA ] assert patch.commands == expected
def test_serialize_add_item_command(): expected = 'add-item {"register-name":"Country"}' value = Blob({"register-name": "Country"}) command = Command(Action.AddItem, value) assert str(command) == expected