Skip to content

Commit

Permalink
Change string date to int date parsing
Browse files Browse the repository at this point in the history
Also update tests and add a warning just in case the pbf plugin changes the output format again
  • Loading branch information
Famlam committed Aug 18, 2024
1 parent c27f70b commit 24b6324
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions plugins/Construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ def node(self, data, tags):
date = self.convert2date(tagDate)

end_date = False
if date:
end_date = date
elif "timestamp" in data:
end_date = datetime.datetime.strptime(data["timestamp"][0:10], "%Y-%m-%d") + self.date_limit
else:
try:
if date:
end_date = date
elif data["timestamp"] > 0:
end_date = datetime.datetime.fromtimestamp(data["timestamp"]) + self.date_limit
else:
return
except:
# This should only trigger in case the pbf reader is updated (and the plugin not), causing the timestamp format to change (or be absent)
print("Error: Unexpected format of timestamp or no timestamp provided, expected numerical timestamp")
return

delta = int(self.total_seconds(self.today - end_date))
Expand Down Expand Up @@ -122,6 +127,9 @@ def setUp(self):
self.p.init(None)

def test(self):
# Use today, so it won't trigger due to the timestamp in this test
ts = datetime.datetime.timestamp(datetime.datetime.today())

constr_tags = [{"construction": "yes"},
{"highway": "construction"},
{"landuse": "construction"},
Expand Down Expand Up @@ -155,31 +163,34 @@ def test(self):
for val_d in correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
self.check_err(self.p.node({}, t), t)
self.check_err(self.p.node({"timestamp": ts}, t), t)
for val_d in not_correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
assert not self.p.way({}, t, None), t
assert not self.p.way({"timestamp": ts}, t, None), t

for tags in other_tags:
for tag_d in self.p.tag_date:
for val_d in correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
assert not self.p.relation({}, t, None), t
assert not self.p.relation({"timestamp": ts}, t, None), t

def test_timestamp(self):
tags = {"construction": "yes"}
for ts in ["2003-01-04", "1989-03-10"]:
self.check_err(self.p.node({"timestamp": ts}, tags), ts)
for ts in ["2078-01-04"]:
assert not self.p.node({"timestamp": ts}, tags), ts
ts_int = datetime.datetime.timestamp(datetime.datetime.strptime(ts,"%Y-%m-%d"))
self.check_err(self.p.node({"timestamp": ts_int}, tags), ts)
for ts in ["2078-01-04", str(datetime.datetime.today())[0:10]]:
ts_int = datetime.datetime.timestamp(datetime.datetime.strptime(ts,"%Y-%m-%d"))
assert not self.p.node({"timestamp": ts_int}, tags), ts
assert not self.p.node({"timestamp": 0}, tags)

def test_recall(self):
tags = {"construction": "yes"}
today = datetime.datetime.today()
td = datetime.timedelta(days=6 * 30)
for i in range(5, 10, 1):
e = self.p.node({"timestamp": (today - i*td).strftime("%Y-%m-%d")}, tags)
e = self.p.node({"timestamp": datetime.datetime.timestamp(today - i*td)}, tags)
self.check_err(e, i)
assert e["subclass"] == i - 5

0 comments on commit 24b6324

Please sign in to comment.