oerppy-hackers team mailing list archive
-
oerppy-hackers team
-
Mailing list archive
-
Message #00017
[Branch ~oerppy-hackers/oerppy/trunk] Rev 37: * Added a model for User and UsersSet, complete with initial unit tests.
------------------------------------------------------------
revno: 37
committer: duncan@xxxxxxxxxx
branch nick: trunk
timestamp: Fri 2011-06-03 14:29:11 -0600
message:
* Added a model for User and UsersSet, complete with initial unit tests.
* Updaed oerppy.query.Query.get_users to use the new UsersSet model.
* Added an export class in oerppy.scripting.base for providing a list of users.
* Added a new script that calls this export class.
* Changed the result set to be based on a list rather than a set object.
* Updated the README with instructions on running the unit test suite.
* Re-added the missing unit tests for the hr and project addons (when those
addons were created, the unit tests didn't get added to bzr).
added:
bin/export_users
oerppy/addons/hr/tests/
oerppy/addons/hr/tests/__init__.py
oerppy/addons/hr/tests/test_model.py
oerppy/addons/project/tests/
oerppy/addons/project/tests/__init__.py
oerppy/addons/project/tests/test_model.py
modified:
README
oerppy/const.py
oerppy/model.py
oerppy/query.py
oerppy/scripting/base.py
oerppy/tests/test_model.py
--
lp:oerppy
https://code.launchpad.net/~oerppy-hackers/oerppy/trunk
Your team oerppy Hackers is subscribed to branch lp:oerppy.
To unsubscribe from this branch go to https://code.launchpad.net/~oerppy-hackers/oerppy/trunk/+edit-subscription
=== modified file 'README'
--- README 2011-06-02 17:30:17 +0000
+++ README 2011-06-03 20:29:11 +0000
@@ -12,3 +12,17 @@
every script) are used to do a look-up in your configuration file, where the
rest of the information is extracted and used. As usual, any script's full
options may be seen by simply calling it with the '--help' option.
+
+
+Running the Test Suite
+======================
+
+If you have Twisted Python installed, all you need to do is this:
+
+ $ cd <source code dir>
+ $ trial ./oerppy
+
+That will display the success or failure of all the unit tests.
+
+If you *don't* have Twisted Python installed, do it :-) Otherwise, I'll have to
+add a test runner :-/
=== added file 'bin/export_users'
--- bin/export_users 1970-01-01 00:00:00 +0000
+++ bin/export_users 2011-06-03 20:29:11 +0000
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+"""
+This is an example of a script that uses the default addon, i.e., standard
+OpenERP data.
+"""
+from oerppy import const, query
+from oerppy.config import get_config_data
+from oerppy.reg import registry
+from oerppy.scripting import base
+
+
+# get configuration; note that option parsing is handled by the script
+# class(es)
+config_data = get_config_data()
+# setup any addons that we want
+registry.add(const.DEFAULT_ADDON, query.Query)
+# run the exporter
+all_weeks_export = base.OpenERPUsersExport(config_data)
+all_weeks_export.run()
=== added directory 'oerppy/addons/hr/tests'
=== added file 'oerppy/addons/hr/tests/__init__.py'
=== added file 'oerppy/addons/hr/tests/test_model.py'
--- oerppy/addons/hr/tests/test_model.py 1970-01-01 00:00:00 +0000
+++ oerppy/addons/hr/tests/test_model.py 2011-06-03 20:29:11 +0000
@@ -0,0 +1,40 @@
+import unittest
+
+from oerppy.addons.hr import model
+
+
+employee_results = [
+ {"name": "Bob",
+ "user_id": [314, "Bob"],
+ "department_id": [1, "Crypto"],
+ "resource_id": [34, "Bob"]},
+ {"name": "Alice",
+ "user_id": [315, "Alice"],
+ "department_id": [2, "Maths"],
+ "resource_id": [37, "Alice"]},
+ {"name": "Carol",
+ "user_id": [316, False],
+ "department_id": False,
+ "resource_id": [42, "Carol"]},
+ ]
+
+
+class HRModelTestCase(unittest.TestCase):
+
+ def test_hr_employee(self):
+ employee = model.Employee(employee_results[1])
+ self.assertEqual(employee.user.key, 315)
+ self.assertEqual(employee.user.value, "Alice")
+ self.assertEqual(employee.name, "Alice")
+ self.assertEqual(employee.department.key, 2)
+ self.assertEqual(employee.department.value, "Maths")
+ self.assertEqual(employee.resource.key, 37)
+
+ def test_hr_employee_set(self):
+ results = model.EmployeeSet(employee_results)
+ self.assertEqual(len(results), 3)
+ user = results.get_user(314)
+ self.assertEqual(user.name, "Bob")
+ table = results.build_department_lookup_table()
+ self.assertEqual(table.get(315), "Maths")
+ self.assertEqual(table.get(316), None)
=== added directory 'oerppy/addons/project/tests'
=== added file 'oerppy/addons/project/tests/__init__.py'
=== added file 'oerppy/addons/project/tests/test_model.py'
--- oerppy/addons/project/tests/test_model.py 1970-01-01 00:00:00 +0000
+++ oerppy/addons/project/tests/test_model.py 2011-06-03 20:29:11 +0000
@@ -0,0 +1,48 @@
+import unittest
+
+from oerppy.addons import hr
+from oerppy.addons.hr.tests.test_model import employee_results
+from oerppy.addons.project import model
+
+
+class ProjectTaskWorkTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.work_results = [
+ {"project_id": [1, "Manhattan"],
+ "task_id": [1001, "Figure out Atoms"],
+ "user_id": [314, "Alice"],
+ "hours": 273,
+ "department_id": [1, "Crypto"],
+ "date": "2011-05-01",
+ "week_start": 172},
+ {"project_id": [2, "Turing"],
+ "task_id": [1002, "Build a von Nuemann machine"],
+ "user_id": [315, "Bob"],
+ "hours": 735,
+ "department_id": [2, "Maths"],
+ "date": "2011-05-01",
+ "week_start": 172},
+ ]
+ self.results = hr.model.EmployeeSet(employee_results)
+ self.table = self.results.build_department_lookup_table()
+
+ def test_project_task_work(self):
+
+ work = model.TaskWork(self.work_results[1], self.table)
+ self.assertEqual(work.project.key, 2)
+ self.assertEqual(work.project.value, "Turing")
+ self.assertEqual(work.task.key, 1002)
+ self.assertEqual(work.user.key, 315)
+ self.assertEqual(work.department_name, "Maths")
+ self.assertEqual(work.hours, 735)
+ self.assertEqual(work.date, "2011-05-01")
+ self.assertEqual(work.year, 2011)
+ self.assertEqual(work.month, 5)
+ self.assertEqual(work.week_start, 172)
+
+ def test_project_task_work_set(self):
+ results = model.TaskWorkSet(self.work_results, self.table)
+ self.assertEqual(len(results), 2)
+ work = results.get_work(1)
+ self.assertEqual(work.hours, 273)
=== modified file 'oerppy/const.py'
--- oerppy/const.py 2011-06-02 16:51:35 +0000
+++ oerppy/const.py 2011-06-03 20:29:11 +0000
@@ -1,3 +1,4 @@
+DEFAULT_ADDON = "default"
STDOUT_FILENAME = "stdout"
EXPORT_RAW = "raw"
EXPORT_CSV = "csv"
=== modified file 'oerppy/model.py'
--- oerppy/model.py 2011-06-03 18:46:30 +0000
+++ oerppy/model.py 2011-06-03 20:29:11 +0000
@@ -24,9 +24,11 @@
"""
Base class for representing result sets and result data.
"""
-
-
-class ResultSet(set):
+ def __init__(self, row):
+ raise NotImplementedError
+
+
+class ResultSet(list):
"""
Base class for collections of models, all together representing result sets
in queries to the OpenERP server.
@@ -36,6 +38,51 @@
def __init__(self, results, *args, **kwds):
for row in results:
try:
- self.add(self.model(row, *args, **kwds))
+ self.append(self.model(row, *args, **kwds))
except Exception, error:
import pdb;pdb.set_trace()
+
+
+class User(Model):
+
+ def __init__(self, row):
+ self.id = row.get("id")
+ self.active = row.get("active")
+ self.category_ids = row.get("category_ids")
+ self.company_ids = row.get("company_ids")
+ self.menu_tips = row.get("menu_tips")
+ self.date = row.get("date")
+ self.context_lang = row.get("context_lang")
+ self.password = row.get("password")
+ self.context_tz = row.get("context_tz")
+ self.name = row.get("name")
+ self.signature = row.get("signature")
+ self.login = row.get("login")
+ self.user_email = row.get("user_email")
+ self.view = row.get("view")
+ self.groups_id = row.get("groups_id")
+ # The following are key/value pairs, each with .key and .value
+ # attributes
+ self.company = KeyValuePairField(row.get("company_id"))
+ self.address = KeyValuePairField(row.get("address_id"))
+ self.action = KeyValuePairField(row.get("action_id"))
+ self.menu = KeyValuePairField(row.get("menu_id"))
+ self.context_project = KeyValuePairField(
+ row.get("context_project_id"))
+ self.context_department = KeyValuePairField(
+ row.get("context_department_id"))
+ # XXX I haven't used these; they need to be checked by someone who has
+ # and then given the appropriate field class.
+ # XXX I'm assuming these would just be simple values:
+ self.new_password = None
+ self.email = None
+
+
+class UsersSet(ResultSet):
+
+ model = User
+
+ def get_user(self, user_id):
+ for user in self:
+ if user.id == user_id:
+ return user
=== modified file 'oerppy/query.py'
--- oerppy/query.py 2011-06-03 18:46:30 +0000
+++ oerppy/query.py 2011-06-03 20:29:11 +0000
@@ -1,3 +1,6 @@
+from oerppy import model
+
+
class Query(object):
"""
A wrapper class for RPC requests (queries) that are more
@@ -15,11 +18,12 @@
raise NotImplementedError
def get_users(self):
- users = self.client.searchfields(
+ raw_results = self.client.searchfields(
entity='res.users',
query=[],
fields=[])
- return users
+ results = model.UsersSet(raw_results)
+ return results
def get_user_names(self, user_ids):
"""
=== modified file 'oerppy/scripting/base.py'
--- oerppy/scripting/base.py 2011-06-03 14:19:21 +0000
+++ oerppy/scripting/base.py 2011-06-03 20:29:11 +0000
@@ -93,3 +93,11 @@
export.export_csv(data, fd)
#elif options.export_format == const.EXPORT_GOOGLE_DOCS:
# export.export_google(user, pass, data, ..., ?)
+
+
+class OpenERPUsersExport(ExportScript):
+
+ def run(self):
+ addon = self.service.get_addon(const.DEFAULT_ADDON)
+ data = addon.get_users()
+ self.export(data)
=== modified file 'oerppy/tests/test_model.py'
--- oerppy/tests/test_model.py 2011-06-03 18:46:30 +0000
+++ oerppy/tests/test_model.py 2011-06-03 20:29:11 +0000
@@ -1,3 +1,96 @@
import unittest
from oerppy import model
+
+
+user_results = [
+ {'action_id': [418, 'Weekly Timesheet'],
+ 'active': True,
+ 'address_id': [185, 'Alice'],
+ 'category_ids': [1],
+ 'company_id': [1, 'The Company, Inc.'],
+ 'company_ids': [1],
+ 'context_department_id': False,
+ 'context_lang': 'en_US',
+ 'context_project_id': False,
+ 'context_tz': False,
+ 'date': '2011-04-25 15:03:51.386437',
+ 'email': False,
+ 'groups_id': [22, 27, 3],
+ 'id': 84,
+ 'login': 'alice@xxxxxxxxxxx',
+ 'menu_id': [1, 'Menu'],
+ 'menu_tips': True,
+ 'name': 'Alice',
+ 'new_password': [],
+ 'password': 's3cr3t',
+ 'signature': False,
+ 'user_email': 'alice@xxxxxxxxxxx',
+ 'view': 'simple'},
+ {'action_id': [380, 'Administration Dashboard'],
+ 'active': True,
+ 'address_id': [1,
+ '101 Main St.'],
+ 'category_ids': [],
+ 'company_id': [1, 'The Company, Inc.'],
+ 'company_ids': [1],
+ 'context_department_id': False,
+ 'context_lang': 'en_US',
+ 'context_project_id': False,
+ 'context_tz': 'Europe/London',
+ 'date': '2011-06-03 08:37:00.67816',
+ 'email': False,
+ 'groups_id': [17,
+ 16,
+ 18,
+ 2,
+ 1,
+ 28,
+ 29,
+ 30,
+ 24,
+ 25,
+ 26,
+ 23,
+ 14,
+ 15,
+ 3,
+ 20,
+ 21,
+ 7,
+ 8,
+ 13,
+ 27,
+ 22,
+ 9,
+ 11,
+ 32,
+ 33,
+ 5],
+ 'id': 1,
+ 'login': 'admin',
+ 'menu_id': [1, 'Menu'],
+ 'menu_tips': False,
+ 'name': 'Administrator',
+ 'new_password': [],
+ 'password': 'th3p455',
+ 'signature': 'Administrator',
+ 'user_email': 'bob@xxxxxxxxxxx',
+ 'view': 'extended'}]
+
+
+class UsersModelTestCase(unittest.TestCase):
+
+ def test_user(self):
+ user = model.User(user_results[0])
+ self.assertEqual(user.id, 84)
+ self.assertEqual(user.name, "Alice")
+ self.assertEqual(user.company.key, 1)
+ self.assertEqual(user.company.value, "The Company, Inc.")
+ self.assertEqual(user.user_email, "alice@xxxxxxxxxxx")
+
+ def test_users_set(self):
+ results = model.UsersSet(user_results)
+ self.assertEqual(len(results), 2)
+ user = results.get_user(84)
+ self.assertEqual(user.name, "Alice")