def test_many_to_many_field(self): """lists!""" instance = fields.ManyToManyField("User") Serializable = namedtuple("Serializable", ("to_activity", "remote_id")) Queryset = namedtuple("Queryset", ("all", "instance")) item = Serializable(lambda: {"a": "b"}, "https://e.b/c") another_item = Serializable(lambda: {}, "example.com") items = Queryset(lambda: [item], another_item) self.assertEqual(instance.field_to_activity(items), ["https://e.b/c"]) instance = fields.ManyToManyField("User", link_only=True) instance.name = "snake_case" self.assertEqual(instance.field_to_activity(items), "example.com/snake_case")
def test_many_to_many_field(self): ''' lists! ''' instance = fields.ManyToManyField('User') Serializable = namedtuple('Serializable', ('to_activity', 'remote_id')) Queryset = namedtuple('Queryset', ('all', 'instance')) item = Serializable(lambda: {'a': 'b'}, 'https://e.b/c') another_item = Serializable(lambda: {}, 'example.com') items = Queryset(lambda: [item], another_item) self.assertEqual(instance.field_to_activity(items), ['https://e.b/c']) instance = fields.ManyToManyField('User', link_only=True) instance.name = 'snake_case' self.assertEqual(instance.field_to_activity(items), 'example.com/snake_case')
def test_many_to_many_field_from_activity(self): ''' resolve related fields for a list, takes a list of remote ids ''' instance = fields.ManyToManyField(User) datafile = pathlib.Path(__file__).parent.joinpath( '../data/ap_user.json') userdata = json.loads(datafile.read_bytes()) # don't try to load the user icon del userdata['icon'] # test receiving an unknown remote id and loading data responses.add(responses.GET, 'https://example.com/user/mouse', json=userdata, status=200) with patch('bookwyrm.models.user.set_remote_server.delay'): value = instance.field_from_activity( ['https://example.com/user/mouse', 'bleh']) self.assertIsInstance(value, list) self.assertEqual(len(value), 1) self.assertIsInstance(value[0], User)
def test_many_to_many_field_from_activity(self): """resolve related fields for a list, takes a list of remote ids""" instance = fields.ManyToManyField(User) datafile = pathlib.Path(__file__).parent.joinpath( "../data/ap_user.json") userdata = json.loads(datafile.read_bytes()) # don't try to load the user icon del userdata["icon"] # test receiving an unknown remote id and loading data responses.add(responses.GET, "https://example.com/user/mouse", json=userdata, status=200) with patch( "bookwyrm.preview_images.generate_user_preview_image_task.delay" ): with patch("bookwyrm.models.user.set_remote_server.delay"): value = instance.field_from_activity( ["https://example.com/user/mouse", "bleh"]) self.assertIsInstance(value, list) self.assertEqual(len(value), 1) self.assertIsInstance(value[0], User)