Permalink
Browse files

Merge pull request #12 from wmatyskiewicz/plugins

plugin - sync data form assets
  • Loading branch information...
deshipu committed May 9, 2013
2 parents 6b2f387 + eb398d8 commit cb7ce73f3f4c48e609e9b80b44a8ec42c66afae1
Showing with 87 additions and 16 deletions.
  1. +39 −0 src/ralph_pricing/plugins/assets.py
  2. +0 −16 src/ralph_pricing/tests/__init__.py
  3. +48 −0 src/ralph_pricing/tests/test_assets.py
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+from ralph.util import plugin
+from ralph_assets.api_pricing import get_assets
+from ralph_pricing.models import Device, DailyDevice
+
+
+def update_assets(data, date):
+ device, created = Device.objects.get_or_create(
+ device_id=data['ralph_id'],
+ )
+ if not created:
+ device.device_id = data['ralph_id']
+ device.asset_id = data['asset_id']
+ device.slots = data['slots']
+ device.save()
+ daily, daily_created = DailyDevice.objects.get_or_create(
+ date=date,
+ pricing_device=device,
+ )
+ daily.price = data['price']
+ daily.is_deprecated = data['is_deprecated']
+ daily.save()
+ return created
+
+
+@plugin.register(chain='pricing', requires=['sync_devices'])
+def sync_assets(**kwargs):
+ """Updates the devices from Ralph Assets."""
+
+ date = kwargs['today']
+ count = sum(update_assets(data, date) for data in get_assets())
+ return True, '%d new devices' % count, kwargs
+
@@ -1,16 +0,0 @@
-"""
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
-
-Replace this with more appropriate tests for your application.
-"""
-
-from django.test import TestCase
-
-
-class SimpleTest(TestCase):
- def test_basic_addition(self):
- """
- Tests that 1 + 1 always equals 2.
- """
- self.assertEqual(1 + 1, 2)
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import datetime
+
+from django.test import TestCase
+
+from ralph_pricing.models import Device, DailyDevice
+from ralph_pricing.plugins.assets import update_assets
+
+
+class TestAssetPlugin(TestCase):
+ def setUp(self):
+ self.today = datetime.date.today()
+
+ def get_asset(self):
+ """Simulated api result"""
+ yield {
+ 'asset_id': 1123,
+ 'ralph_id': 13342,
+ 'slots': 10.0,
+ 'price': 100,
+ 'is_deprecated': True,
+ }
+
+ def test_sync_asset_device(self):
+ count = sum(
+ update_assets(data, self.today) for data in self.get_asset()
+ )
+ device = Device.objects.get(device_id=13342)
+ self.assertEqual(device.device_id, 13342)
+ self.assertEqual(device.asset_id, 1123)
+ self.assertEqual(device.slots, 10.0)
+
+
+ def test_sync_asset_daily(self):
+ count = sum(
+ update_assets(data, self.today) for data in self.get_asset()
+ )
+ daily = DailyDevice.objects.get(date=self.today)
+ self.assertEqual(daily.is_deprecated, True)
+ self.assertEqual(daily.price, 100)
+ self.assertEqual(daily.pricing_device_id, 1)
+ self.assertEqual(daily.date, self.today)

0 comments on commit cb7ce73

Please sign in to comment.