Repository: sgaynetdinov/py-vkontakte

Issue:
Add field `User.relation`

Discussion:

Patch:
diff --git a/vk/users.py b/vk/users.py
index ac1ece6..9a50ca6 100644
--- a/vk/users.py
+++ b/vk/users.py
@@ -23,6 +23,7 @@ class User(VKBase):
         'status',
         'trending',
         'site',
+        'relation',
     )
 
     @classmethod
@@ -37,6 +38,8 @@ class User(VKBase):
         user.sex = cls._sex(json_obj.get('sex'))
         user.status = cls._status(json_obj)
         user.site = json_obj.get('site')
+        user.relation = cls._relation(json_obj)
+        user.relation_partner = cls._relation_partner(json_obj)
 
         user.facebook = json_obj.get('facebook')
         user.skype = json_obj.get('skype')
@@ -126,6 +129,34 @@ class User(VKBase):
         }
         return platform.get(platform_id)
 
+    @classmethod
+    def _relation(cls, user_json):
+        if 'relation' not in user_json:
+            return None
+
+        index = user_json['relation']
+
+        relation = {
+            0: None,
+            1: 'single',
+            2: 'in a relationship',
+            3: 'engaged',
+            4: 'married',
+            5: 'it\'s complicated',
+            6: 'actively searching',
+            7: 'in love',
+            8: 'in a civil union'
+        }
+
+        return relation.get(index)
+
+    @classmethod
+    def _relation_partner(cls, user_json):
+        if 'relation_partner' not in user_json:
+            return None
+
+        return user_json['relation_partner']['id']
+
     def get_games(self):
         response = self._fetch("games")[0]
         return response.get('games')


Test patch:
diff --git a/tests/factory/user.json b/tests/factory/user.json
index 18438a5..eb5874f 100644
--- a/tests/factory/user.json
+++ b/tests/factory/user.json
@@ -14,5 +14,6 @@
     "last_seen": {"time": 1535548834, "platform": 7},
     "verified": 1,
     "trending": 0,
-    "site": "http://t.me/durov"
+    "site": "http://t.me/durov",
+    "relation": 0
 }
\ No newline at end of file
diff --git a/tests/test_user.py b/tests/test_user.py
index 43061d5..b39a87e 100644
--- a/tests/test_user.py
+++ b/tests/test_user.py
@@ -32,6 +32,8 @@ def test_user(factory):
     assert user.livejournal is None
     assert user.instagram == 'durov'
     assert user.site == 'http://t.me/durov'
+    assert user.relation is None
+    assert user.relation_partner is None
 
 
 def test_user_is_deleted(factory):
@@ -158,3 +160,42 @@ def test_get(mock, method, field, expected):
     mock.return_value = [{field: expected}]
 
     assert getattr(user, method)() == expected 
+
+
+def test_not_relation(factory):
+    user_json = factory('user.json')
+    del user_json['relation']
+
+    user = User.from_json(None, user_json)
+
+    assert user.relation is None
+
+
+@pytest.mark.parametrize('index, expected', [
+    (0, None),
+    (1, 'single'),
+    (2, 'in a relationship'),
+    (3, 'engaged'),
+    (4, 'married'),
+    (5, 'it\'s complicated'),
+    (6, 'actively searching'),
+    (7, 'in love'),
+    (8, 'in a civil union'),
+    (9, None),
+])
+def test_relation(factory, index, expected):
+    user_json = factory('user.json')
+    user_json['relation'] = index
+
+    user = User.from_json(None, user_json)
+
+    assert user.relation == expected
+
+
+def test_relation_partner(factory):
+    user_json = factory('user.json')
+    user_json['relation_partner'] = {'id': 100500, 'first_name': 'Abc', 'last_name': 'Def'}
+
+    user = User.from_json(None, user_json)
+
+    assert user.relation_partner == 100500