示例#1
0
 def test_create_data_appends_to_existing_data(self):
     """Tests that existing data is incremented instead of replaced."""
     # Create 1 sample with the value 1.
     option = gendata.DataOptions(datetime.datetime(2018, 1, 1),
                                  datetime.datetime(2018, 1, 1, 0, 0, 1), 1,
                                  'New Topic', 1, 1, 0, 0, 1, 0)
     expected = {option.start: model.Datum(option.start, 1, '2.0')}
     actual = {option.start: model.Datum(option.start, 1, '1.0')}
     gendata.create_data(actual, option)
     self.assertEqual(len(expected), len(actual))
     self.assertEqual(expected[option.start].value_string,
                      actual[option.start].value_string)
示例#2
0
 def test_encode_datum(self):
   """Encodes a Datum object and ensures it has the correct representation."""
   ts = datetime.datetime(2018, 1, 1)
   datum = model.Datum(ts, 18, 'value')
   actual = json.loads(json.dumps(datum, cls=codec.DatumEncoder))
   expected = [ts.isoformat(), 18, 'value']
   self.assertEqual(expected, actual)
示例#3
0
    def test_write_to_db(self):
        """Test that topics and data can be written to the DB."""
        # Create one topic and one datum to be created.
        ts = datetime.datetime.now()
        self.write_to_db([], [model.Topic(1, 'Topic')],
                         [model.Datum(ts, 1, '1.0')])

        # Verify the items were written.
        session = sqlalchemy.orm.Session(bind=self.engine,
                                         expire_on_commit=False)
        actual_topics = session.query(model.Topic).all()
        actual_data = session.query(model.Datum).all()

        self.assertEqual(1, len(actual_topics))
        self.assertEqual(1, len(actual_data))

        topic = actual_topics[0]
        self.assertEqual(1, topic.topic_id)
        self.assertEqual('Topic', topic.topic_name)

        datum = actual_data[0]
        self.assertEqual(ts, datum.ts)
        self.assertEqual(1, datum.topic_id)
        self.assertEqual('1.0', datum.value_string)
        session.close()
示例#4
0
  def test_latest_data_timestamp(self):
    """Tests that the most recent known timestamp in the DB is returned."""
    # Insert two data objects.
    first = model.Datum(
        datetime.datetime(2018, 1, 1), 18, 'first value string')
    second = model.Datum(
        datetime.datetime(2018, 1, 2), 18, 'second value string')

    session = sqlalchemy.orm.Session(bind=self.engine)
    session.add(first)
    session.add(second)
    session.commit()

    # Query database and validate results.
    dt = json.loads(self.server.get_latest_data_timestamp())
    self.assertNotEqual(first.ts, second.ts)
    self.assertNotEqual(dt, first.ts.isoformat())
    self.assertEqual(dt, second.ts.isoformat())

    session.close()
示例#5
0
def create_datum(options, ts):
    """Creates a datum object for the given timestamp and topic.

  Args:
    options: The configuration options for the current data generation run.
    ts: The datum timestamp.

  Returns:
    A populated datum object, ready for insertion.
  """
    # value = offset + A_cos * cos(omega * t) + A_sin * sin(omega * t) + fuzz factor
    omega = 2 * math.pi / options.period
    seconds = (ts - options.start).total_seconds()
    fuzz = random.uniform(-options.spread, options.spread)
    x = omega * seconds
    value = (options.amplitude_offset + options.amplitude_cos * math.cos(x) +
             options.amplitude_sin * math.sin(x) + fuzz)

    datum = model.Datum(ts, options.topic_id, str(value))
    return datum