Skip to content
Snippets Groups Projects
Commit 75645964 authored by David Douard's avatar David Douard
Browse files

model: make model entities frozen

we do not really need them to be mutable, plus we gain their instances now
being hashable, so we can add them in set() for example.
parent 4b79a2b0
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# License: GNU General Public License version 3, or any later version # License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
import attr
import datetime import datetime
from hypothesis.strategies import ( from hypothesis.strategies import (
...@@ -94,9 +95,10 @@ def releases(draw): ...@@ -94,9 +95,10 @@ def releases(draw):
author=none(), author=none(),
date=none(), date=none(),
target=sha1_git())) target=sha1_git()))
rel.date = date return attr.evolve(
rel.author = author rel,
return rel date=date,
author=author)
def revision_metadata(): def revision_metadata():
......
...@@ -51,7 +51,7 @@ class BaseModel: ...@@ -51,7 +51,7 @@ class BaseModel:
return cls(**d) return cls(**d)
@attr.s @attr.s(frozen=True)
class Person(BaseModel): class Person(BaseModel):
"""Represents the author/committer of a revision or release.""" """Represents the author/committer of a revision or release."""
name = attr.ib(type=bytes) name = attr.ib(type=bytes)
...@@ -59,7 +59,7 @@ class Person(BaseModel): ...@@ -59,7 +59,7 @@ class Person(BaseModel):
fullname = attr.ib(type=bytes) fullname = attr.ib(type=bytes)
@attr.s @attr.s(frozen=True)
class Timestamp(BaseModel): class Timestamp(BaseModel):
"""Represents a naive timestamp from a VCS.""" """Represents a naive timestamp from a VCS."""
seconds = attr.ib(type=int) seconds = attr.ib(type=int)
...@@ -78,7 +78,7 @@ class Timestamp(BaseModel): ...@@ -78,7 +78,7 @@ class Timestamp(BaseModel):
raise ValueError('Microseconds must be in [0, 1000000[.') raise ValueError('Microseconds must be in [0, 1000000[.')
@attr.s @attr.s(frozen=True)
class TimestampWithTimezone(BaseModel): class TimestampWithTimezone(BaseModel):
"""Represents a TZ-aware timestamp from a VCS.""" """Represents a TZ-aware timestamp from a VCS."""
timestamp = attr.ib(type=Timestamp) timestamp = attr.ib(type=Timestamp)
...@@ -105,7 +105,7 @@ class TimestampWithTimezone(BaseModel): ...@@ -105,7 +105,7 @@ class TimestampWithTimezone(BaseModel):
negative_utc=d['negative_utc']) negative_utc=d['negative_utc'])
@attr.s @attr.s(frozen=True)
class Origin(BaseModel): class Origin(BaseModel):
"""Represents a software source: a VCS and an URL.""" """Represents a software source: a VCS and an URL."""
url = attr.ib(type=str) url = attr.ib(type=str)
...@@ -117,7 +117,7 @@ class Origin(BaseModel): ...@@ -117,7 +117,7 @@ class Origin(BaseModel):
return r return r
@attr.s @attr.s(frozen=True)
class OriginVisit(BaseModel): class OriginVisit(BaseModel):
"""Represents a visit of an origin at a given point in time, by a """Represents a visit of an origin at a given point in time, by a
SWH loader.""" SWH loader."""
...@@ -176,7 +176,7 @@ class ObjectType(Enum): ...@@ -176,7 +176,7 @@ class ObjectType(Enum):
SNAPSHOT = 'snapshot' SNAPSHOT = 'snapshot'
@attr.s @attr.s(frozen=True)
class SnapshotBranch(BaseModel): class SnapshotBranch(BaseModel):
"""Represents one of the branches of a snapshot.""" """Represents one of the branches of a snapshot."""
target = attr.ib(type=bytes) target = attr.ib(type=bytes)
...@@ -198,7 +198,7 @@ class SnapshotBranch(BaseModel): ...@@ -198,7 +198,7 @@ class SnapshotBranch(BaseModel):
target_type=TargetType(d['target_type'])) target_type=TargetType(d['target_type']))
@attr.s @attr.s(frozen=True)
class Snapshot(BaseModel): class Snapshot(BaseModel):
"""Represents the full state of an origin at a given point in time.""" """Represents the full state of an origin at a given point in time."""
id = attr.ib(type=Sha1Git) id = attr.ib(type=Sha1Git)
...@@ -214,7 +214,7 @@ class Snapshot(BaseModel): ...@@ -214,7 +214,7 @@ class Snapshot(BaseModel):
}) })
@attr.s @attr.s(frozen=True)
class Release(BaseModel): class Release(BaseModel):
id = attr.ib(type=Sha1Git) id = attr.ib(type=Sha1Git)
name = attr.ib(type=bytes) name = attr.ib(type=bytes)
...@@ -261,7 +261,7 @@ class RevisionType(Enum): ...@@ -261,7 +261,7 @@ class RevisionType(Enum):
MERCURIAL = 'hg' MERCURIAL = 'hg'
@attr.s @attr.s(frozen=True)
class Revision(BaseModel): class Revision(BaseModel):
id = attr.ib(type=Sha1Git) id = attr.ib(type=Sha1Git)
message = attr.ib(type=bytes) message = attr.ib(type=bytes)
...@@ -291,7 +291,7 @@ class Revision(BaseModel): ...@@ -291,7 +291,7 @@ class Revision(BaseModel):
**d) **d)
@attr.s @attr.s(frozen=True)
class DirectoryEntry(BaseModel): class DirectoryEntry(BaseModel):
name = attr.ib(type=bytes) name = attr.ib(type=bytes)
type = attr.ib(type=str, type = attr.ib(type=str,
...@@ -301,7 +301,7 @@ class DirectoryEntry(BaseModel): ...@@ -301,7 +301,7 @@ class DirectoryEntry(BaseModel):
"""Usually one of the values of `swh.model.from_disk.DentryPerms`.""" """Usually one of the values of `swh.model.from_disk.DentryPerms`."""
@attr.s @attr.s(frozen=True)
class Directory(BaseModel): class Directory(BaseModel):
id = attr.ib(type=Sha1Git) id = attr.ib(type=Sha1Git)
entries = attr.ib(type=List[DirectoryEntry]) entries = attr.ib(type=List[DirectoryEntry])
...@@ -314,7 +314,7 @@ class Directory(BaseModel): ...@@ -314,7 +314,7 @@ class Directory(BaseModel):
for entry in d['entries']]) for entry in d['entries']])
@attr.s @attr.s(frozen=True)
class Content(BaseModel): class Content(BaseModel):
sha1 = attr.ib(type=bytes) sha1 = attr.ib(type=bytes)
sha1_git = attr.ib(type=Sha1Git) sha1_git = attr.ib(type=Sha1Git)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# License: GNU General Public License version 3, or any later version # License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
import attr
import copy import copy
from hypothesis import given from hypothesis import given
...@@ -44,8 +45,9 @@ def test_todict_origin_visits(origin_visit): ...@@ -44,8 +45,9 @@ def test_todict_origin_visits(origin_visit):
obj = origin_visit.to_dict() obj = origin_visit.to_dict()
assert 'type' not in obj['origin'] assert 'type' not in obj['origin']
origin_visit.origin.type = None origin2 = attr.evolve(origin_visit.origin, type=None)
assert origin_visit == type(origin_visit).from_dict(obj) origin_visit2 = attr.evolve(origin_visit, origin=origin2)
assert origin_visit2 == type(origin_visit).from_dict(obj)
def test_content_get_hash(): def test_content_get_hash():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment