forked from facebookresearch/PyTorch-BigGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_checkpoint_manager.py
70 lines (58 loc) · 2.71 KB
/
test_checkpoint_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE.txt file in the root directory of this source tree.
import json
from unittest import TestCase, main
from torchbiggraph.checkpoint_manager import (
ConfigMetadataProvider,
TwoWayMapping,
)
from torchbiggraph.config import ConfigSchema, EntitySchema, RelationSchema
class TestTwoWayMapping(TestCase):
def test_one_field(self):
m = TwoWayMapping("foo.bar.{field}", "{field}/ham/eggs", fields=["field"])
self.assertEqual(m.private_to_public.map("foo.bar.baz"), "baz/ham/eggs")
self.assertEqual(m.public_to_private.map("spam/ham/eggs"), "foo.bar.spam")
with self.assertRaises(ValueError):
m.private_to_public.map("f00.b4r.b4z")
with self.assertRaises(ValueError):
m.private_to_public.map("foo.bar")
with self.assertRaises(ValueError):
m.private_to_public.map("foo.bar.")
with self.assertRaises(ValueError):
m.private_to_public.map("foo.bar.baz.2")
with self.assertRaises(ValueError):
m.private_to_public.map("2.foo.bar.baz")
with self.assertRaises(ValueError):
m.public_to_private.map("sp4m/h4m/3gg5")
with self.assertRaises(ValueError):
m.public_to_private.map("ham/eggs")
with self.assertRaises(ValueError):
m.public_to_private.map("/ham/eggs")
with self.assertRaises(ValueError):
m.public_to_private.map("2/spam/ham/eggs")
with self.assertRaises(ValueError):
m.public_to_private.map("spam/ham/eggs/2")
def test_many_field(self):
m = TwoWayMapping("fo{field1}.{field2}ar.b{field3}z",
"sp{field3}m/{field2}am/egg{field1}",
fields=["field1", "field2", "field3"])
self.assertEqual(m.private_to_public.map("foo.bar.baz"), "spam/bam/eggo")
self.assertEqual(m.public_to_private.map("spam/ham/eggs"), "fos.har.baz")
class TestConfigMetadataProvider(TestCase):
def test_basic(self):
config = ConfigSchema(
entities={"e": EntitySchema(num_partitions=1)},
relations=[RelationSchema(name="r", lhs="e", rhs="e")],
dimension=1,
entity_path="foo", edge_paths=["bar"], checkpoint_path="baz")
metadata = ConfigMetadataProvider(config).get_checkpoint_metadata()
self.assertIsInstance(metadata, dict)
self.assertCountEqual(metadata.keys(), ["config/json"])
self.assertEqual(
config, ConfigSchema.from_dict(json.loads(metadata["config/json"])))
if __name__ == '__main__':
main()