From 18f05fbd4fc5c9131646191fb2fe80f16ee840d3 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 28 Sep 2016 16:30:37 +0200 Subject: [PATCH] User class with tests --- .../cartodb_services/refactor/__init__.py | 0 .../refactor/core/__init__.py | 0 .../cartodb_services/refactor/core/user.py | 24 +++++++++++ .../test/refactor/__init__.py | 0 .../test/refactor/core/__init__.py | 0 .../test/refactor/core/test_user.py | 43 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/__init__.py create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/core/__init__.py create mode 100644 server/lib/python/cartodb_services/cartodb_services/refactor/core/user.py create mode 100644 server/lib/python/cartodb_services/test/refactor/__init__.py create mode 100644 server/lib/python/cartodb_services/test/refactor/core/__init__.py create mode 100644 server/lib/python/cartodb_services/test/refactor/core/test_user.py diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/__init__.py b/server/lib/python/cartodb_services/cartodb_services/refactor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/core/__init__.py b/server/lib/python/cartodb_services/cartodb_services/refactor/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/cartodb_services/refactor/core/user.py b/server/lib/python/cartodb_services/cartodb_services/refactor/core/user.py new file mode 100644 index 0000000..a36a350 --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/refactor/core/user.py @@ -0,0 +1,24 @@ +class User(object): + + def __init__(self, username, orgname = None): + self.__username = username + self.__orgname = orgname + + @property + def username(self): + return self.__username + + @property + def orgname(self): + return self.__orgname + + @property + def is_org_user(self): + return self.__orgname is not None + + def __eq__(self, other): + # NOTE: usernames are unique in the system + eq = (self.__username == other.__username) + if eq: + assert self.__orgname == other.__orgname, 'Found two users with same name and different orgs!' + return eq diff --git a/server/lib/python/cartodb_services/test/refactor/__init__.py b/server/lib/python/cartodb_services/test/refactor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/test/refactor/core/__init__.py b/server/lib/python/cartodb_services/test/refactor/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/lib/python/cartodb_services/test/refactor/core/test_user.py b/server/lib/python/cartodb_services/test/refactor/core/test_user.py new file mode 100644 index 0000000..70d4359 --- /dev/null +++ b/server/lib/python/cartodb_services/test/refactor/core/test_user.py @@ -0,0 +1,43 @@ +from unittest import TestCase +from cartodb_services.refactor.core.user import User +from nose.tools import assert_raises, raises + +class TestUser(TestCase): + + def test_can_retrieve_properties(self): + u = User('pepito', 'acme') + assert u.username == 'pepito' + assert u.orgname == 'acme' + + @raises(AttributeError) + def test_cannot_modify_properties(self): + u = User('pepito') + u.username = 'juanito' + + def test_is_org_user_returns_true_for_org_users(self): + u = User('pepito', 'acme') + assert u.is_org_user == True + + def test_is_org_user_returns_false_for_non_org_users(self): + u = User('pepito') + assert u.is_org_user == False + + def test_different_users_are_not_equal(self): + u1 = User('pepito') + u2 = User('juanito') + assert u1 != u2 + + def test_same_user_are_equal(self): + u1 = User('pepito') + u2 = User('pepito') + assert u1 == u2 + + o1 = User('juanito', 'acme') + o2 = User('juanito', 'acme') + assert o1 == o2 + + @raises(AssertionError) + def test_raises_exception_if_same_user_is_in_two_orgs(self): + u1 = User('juanito', 'acme') + u2 = User('juanito', 'some_other_org') + u1 == u2