forked from organicmaps/organicmaps
[local ads] py bindings for searelization in any version
This commit is contained in:
parent
80c4ce875a
commit
466dbb955d
2 changed files with 43 additions and 7 deletions
|
@ -11,16 +11,17 @@
|
|||
#include "pyhelpers/vector_list_conversion.hpp"
|
||||
|
||||
#include <boost/python.hpp>
|
||||
#include <boost/python/enum.hpp>
|
||||
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
|
||||
|
||||
using namespace local_ads;
|
||||
|
||||
namespace
|
||||
{
|
||||
std::vector<uint8_t> PySerialize(boost::python::list const & cs)
|
||||
std::vector<uint8_t> PySerialize(boost::python::list const & cs, Version const version)
|
||||
{
|
||||
auto const campaigns = python_list_to_std_vector<Campaign>(cs);
|
||||
return Serialize(campaigns);
|
||||
return Serialize(campaigns, version);
|
||||
}
|
||||
|
||||
boost::python::list PyDeserialize(std::vector<uint8_t> const & blob)
|
||||
|
@ -39,6 +40,7 @@ BOOST_PYTHON_MODULE(pylocal_ads)
|
|||
vector_uint8t_from_python_str();
|
||||
|
||||
class_<Campaign>("Campaign", init<uint32_t, uint16_t, uint8_t, uint8_t, uint8_t>())
|
||||
.def(init<uint32_t, uint16_t, uint8_t>())
|
||||
.def_readonly("m_featureId", &Campaign::m_featureId)
|
||||
.def_readonly("m_iconId", &Campaign::m_iconId)
|
||||
.def_readonly("m_daysBeforeExpired", &Campaign::m_daysBeforeExpired)
|
||||
|
@ -48,6 +50,13 @@ BOOST_PYTHON_MODULE(pylocal_ads)
|
|||
class_<std::vector<Campaign>>("CampaignList")
|
||||
.def(vector_indexing_suite<std::vector<Campaign>>());
|
||||
|
||||
enum_<Version>("Version")
|
||||
.value("UNKNOWN", Version::Unknown)
|
||||
.value("V1", Version::V1)
|
||||
.value("V2", Version::V2)
|
||||
.value("LATEST", Version::Latest)
|
||||
.export_values();
|
||||
|
||||
def("serialize", PySerialize);
|
||||
def("deserialize", PyDeserialize);
|
||||
}
|
||||
|
|
|
@ -1,21 +1,48 @@
|
|||
import unittest
|
||||
|
||||
from pylocal_ads import (Campaign, serialize, deserialize)
|
||||
from pylocal_ads import (Campaign, Version, serialize, deserialize)
|
||||
|
||||
|
||||
class PyLocalAdsTest(unittest.TestCase):
|
||||
|
||||
def assert_equal_campaigns(self, lhs, rhs):
|
||||
self.assertEqual(len(lhs), len(rhs))
|
||||
|
||||
for i in range(0, len(lhs)):
|
||||
self.assertEqual(lhs[i].m_featureId, rhs[i].m_featureId)
|
||||
self.assertEqual(lhs[i].m_iconId, rhs[i].m_iconId)
|
||||
self.assertEqual(lhs[i].m_daysBeforeExpired, rhs[i].m_daysBeforeExpired)
|
||||
self.assertEqual(lhs[i].m_minZoomLevel, rhs[i].m_minZoomLevel)
|
||||
self.assertEqual(lhs[i].m_priority, rhs[i].m_priority)
|
||||
|
||||
def test_smoke(self):
|
||||
campaigns = [
|
||||
campaigns_v1 = [
|
||||
Campaign(10, 10, 10),
|
||||
Campaign(1000, 100, 20),
|
||||
Campaign(120003, 456, 15)
|
||||
]
|
||||
|
||||
campaigns_v2 = [
|
||||
Campaign(10, 10, 10, 10, 0),
|
||||
Campaign(1000, 100, 20, 17, 7),
|
||||
Campaign(120003, 456, 15, 13, 6)
|
||||
]
|
||||
|
||||
serialized = serialize(campaigns)
|
||||
result = deserialize(serialized)
|
||||
serialized = serialize(campaigns_v1, Version.V1)
|
||||
result_v1 = deserialize(serialized)
|
||||
|
||||
self.assertEqual(campaigns.sort(), result.sort())
|
||||
self.assert_equal_campaigns(campaigns_v1, result_v1)
|
||||
|
||||
serialized = serialize(campaigns_v2, Version.V2)
|
||||
result_v2 = deserialize(serialized)
|
||||
|
||||
self.assert_equal_campaigns(campaigns_v2, result_v2)
|
||||
|
||||
serialized = serialize(campaigns_v2, Version.LATEST)
|
||||
result_latest = deserialize(serialized)
|
||||
|
||||
self.assert_equal_campaigns(campaigns_v2, result_latest)
|
||||
self.assert_equal_campaigns(result_v2, result_latest)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Reference in a new issue