Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anlambert/swh-model
  • lunar/swh-model
  • franckbret/swh-model
  • douardda/swh-model
  • olasd/swh-model
  • swh/devel/swh-model
  • Alphare/swh-model
  • samplet/swh-model
  • marmoute/swh-model
  • rboyer/swh-model
10 results
Show changes
Showing
with 7630 additions and 1719 deletions
# Copyright (C) 2019 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from datetime import datetime
from random import choice, randint, random, shuffle
from typing import Dict, List
from pytz import all_timezones, timezone
from swh.model.hashutil import MultiHash
PROTOCOLS = ["git", "http", "https", "deb", "svn", "mock"]
DOMAINS = ["example.com", "some.long.host.name", "xn--n28h.tld"]
PATHS = [
"",
"/",
"/stuff",
"/stuff/",
"/path/to/resource",
"/path/with/anchor#id=42",
"/path/with/qargs?q=1&b",
]
CONTENT_STATUS = ["visible", "hidden", "absent"]
MAX_DATE = 3e9 # around 2065
def gen_all_origins():
for protocol in PROTOCOLS:
for domain in DOMAINS:
for urlpath in PATHS:
yield {"url": "%s://%s%s" % (protocol, domain, urlpath)}
ORIGINS = list(gen_all_origins())
def gen_origins(n: int = 100) -> List:
"""Returns a list of n randomly generated origins suitable for using as
Storage.add_origin() argument.
"""
origins = ORIGINS[:]
shuffle(origins)
return origins[:n]
def gen_content():
size = randint(1, 10 * 1024)
data = bytes(randint(0, 255) for i in range(size))
status = choice(CONTENT_STATUS)
h = MultiHash.from_data(data)
ctime = datetime.fromtimestamp(random() * MAX_DATE, timezone(choice(all_timezones)))
content = {
"data": data,
"status": status,
"length": size,
"ctime": ctime,
**h.digest(),
}
if status == "absent":
content["reason"] = "why not"
content["data"] = None
return content
def gen_contents(n=20) -> List[Dict]:
"""Returns a list of n randomly generated content objects (as dict) suitable
for using as Storage.content_add() argument.
"""
return [gen_content() for i in range(n)]
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
# Copyright (C) 2020 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import pytest
from swh.model.collections import ImmutableDict
def test_immutabledict_empty():
d = ImmutableDict()
assert d == {}
assert d != {"foo": "bar"}
assert list(d) == []
assert list(d.items()) == []
def test_immutabledict_one_item():
d = ImmutableDict({"foo": "bar"})
assert d == {"foo": "bar"}
assert d != {}
assert d["foo"] == "bar"
with pytest.raises(KeyError, match="bar"):
d["bar"]
assert list(d) == ["foo"]
assert list(d.items()) == [("foo", "bar")]
def test_immutabledict_from_iterable():
d1 = ImmutableDict()
d2 = ImmutableDict({"foo": "bar"})
assert ImmutableDict([]) == d1
assert ImmutableDict([("foo", "bar")]) == d2
def test_immutabledict_from_immutabledict():
d1 = ImmutableDict()
d2 = ImmutableDict({"foo": "bar"})
assert ImmutableDict(d1) == d1
assert ImmutableDict(d2) == d2
def test_immutabledict_immutable():
d = ImmutableDict({"foo": "bar"})
with pytest.raises(TypeError, match="item assignment"):
d["bar"] = "baz"
with pytest.raises(TypeError, match="item deletion"):
del d["foo"]
def test_immutabledict_copy_pop():
d = ImmutableDict({"foo": "bar", "baz": "qux"})
assert d.copy_pop("foo") == ("bar", ImmutableDict({"baz": "qux"}))
assert d.copy_pop("not a key") == (None, d)
def test_hash():
assert hash(ImmutableDict()) == hash(ImmutableDict({}))
assert hash(ImmutableDict({"foo": "bar"})) == hash(ImmutableDict({"foo": "bar"}))
assert hash(ImmutableDict({"foo": "bar", "baz": "qux"})) == hash(
ImmutableDict({"foo": "bar", "baz": "qux"})
)
assert hash(ImmutableDict({"foo": "bar", "baz": "qux"})) == hash(
ImmutableDict({"baz": "qux", "foo": "bar"})
)
def test_equality_order():
assert ImmutableDict({"foo": "bar", "baz": "qux"}) == ImmutableDict(
{"foo": "bar", "baz": "qux"}
)
assert ImmutableDict({"foo": "bar", "baz": "qux"}) == ImmutableDict(
{"baz": "qux", "foo": "bar"}
)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.