Skip to content

Commit

Permalink
Finish 0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dgk committed Nov 30, 2016
2 parents c9a9261 + 090fbb0 commit 2e68eea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions business_logic/blockly/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ def visit_constant(self, node, parent_xml):
NumberConstant: 'math_number',
StringConstant: 'text',
BooleanConstant: 'logic_boolean',
DateConstant: 'business_logic_date',
}
field_name = {
NumberConstant: 'NUM',
StringConstant: 'TEXT',
BooleanConstant: 'BOOL',
DateConstant: 'DATE',
}
content_object = node.content_object
cls = content_object.__class__
Expand Down
8 changes: 7 additions & 1 deletion business_logic/blockly/parse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
from lxml import etree, objectify

from django.utils.six import StringIO
Expand Down Expand Up @@ -213,12 +213,18 @@ def visit_field_text(self, node):
def visit_field_bool(self, node):
return self._visit_field(BooleanConstant, value=node.text.lower() == 'true')

def visit_field_date(self, node):
return self._visit_field(DateConstant, value=datetime.datetime.strptime(node.text, '%Y-%m-%d').date())

def visit_value(self, node):
return self._visit_single_child(node)

def visit_statement(self, node):
return self._visit_single_child(node)

def visit_block_business_logic_date(self, node):
return self._visit_single_child(node)

def visit_block_business_logic_reference(self, node):
data = {
'data': {
Expand Down
16 changes: 16 additions & 0 deletions tests/blockly/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ def test_block_if_sequence(self):
block = block[0]
self.assertEqual('controls_if', block.get('type'))

class BlocklyXmlDateTest(TestCase):
def test_block_date(self):
today = datetime.date.today()
root = Node.add_root(content_object=DateConstant(value=today))
xml_str = BlocklyXmlBuilder().build(root)
xml = etree.parse(StringIO(xml_str))

block = xml.xpath('/xml/block')
self.assertEqual(1, len(block))
block = block[0]
self.assertEqual('business_logic_date', block.get('type'))

field = block.find('field')
self.assertEqual('DATE', field.get('name'))
self.assertEqual(today.strftime('%Y-%m-%d'), field.text)


class BlocklyXmlBuilderBinaryOperatorTest(TestCase):
def _test_math_binary_operator(self, operator, block_type, operator_field_value):
Expand Down
10 changes: 10 additions & 0 deletions tests/blockly/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ def test_create_reference_constant(self):
self.assertIsNot(tree1, tree2)
self.assertFalse(self.tree_diff(tree1, tree2))

def test_create_date(self):
today = datetime.date.today()
tree1 = variable_assign_value(value=DateConstant(value=today))
dict1 = self.build_dict(tree1)

tree2 = NodeTreeCreator().create(dict1)

self.assertIsInstance(tree2, Node)
self.assertIsNot(tree1, tree2)
self.assertFalse(self.tree_diff(tree1, tree2))

def test_create_function(self):
function_definition = PythonCodeFunctionDefinition.objects.create(title='xxx')
Expand Down
3 changes: 3 additions & 0 deletions tests/blockly/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def test_boolean_constant(self):
for value in (True, False):
self._test_constant(BooleanConstant, value)

def test_date_constantant(self):
self._test_constant(DateConstant, datetime.date.today())


class BlocklyXmlParserReferenceConstantTest(BlocklyXmlParserTestCase):
def setUp(self):
Expand Down
1 change: 1 addition & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import unittest
import datetime

from decimal import Decimal
from pprint import pprint
Expand Down

0 comments on commit 2e68eea

Please sign in to comment.